3 ** A simple screensaver, displays the text "Haiku" at random locations.
7 ** Copyright (c) 2002, 2005 Marcus Overhagen. All Rights Reserved.
8 ** This file may be used under the terms of the MIT License.
15 #include <DefaultSettingsView.h>
17 #include <ScreenSaver.h>
18 #include <StringView.h>
22 #undef B_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_CONTEXT "Screensaver Haiku"
26 class ScreenSaver
: public BScreenSaver
29 ScreenSaver(BMessage
*archive
, image_id
);
30 void Draw(BView
*view
, int32 frame
);
31 void StartConfig(BView
*view
);
32 status_t
StartSaver(BView
*view
, bool preview
);
46 BScreenSaver
*instantiate_screen_saver(BMessage
*msg
, image_id image
)
48 return new ScreenSaver(msg
, image
);
52 ScreenSaver::ScreenSaver(BMessage
*archive
, image_id id
)
53 : BScreenSaver(archive
, id
)
62 ScreenSaver::StartConfig(BView
*view
)
64 BPrivate::BuildDefaultSettingsView(view
, "Haiku",
65 B_TRANSLATE("by Marcus Overhagen"));
70 ScreenSaver::StartSaver(BView
*view
, bool preview
)
72 // save view dimensions and preview mode
74 fSizeX
= view
->Bounds().Width();
75 fSizeY
= view
->Bounds().Height();
77 // set a new font, about 1/8th of view height, and bold
80 font
.SetSize(fSizeY
/ 8);
81 font
.SetFace(B_BOLD_FACE
);
84 // find out space needed for text display
86 escapement_delta delta
;
89 font
.GetBoundingBoxesForStrings(&fText
, 1, B_SCREEN_METRIC
, &delta
, &rect
);
90 fTextHeight
= rect
.Height();
91 fTextWith
= rect
.Width();
93 // seed the random number generator
94 srand((int)system_time());
101 ScreenSaver::Draw(BView
*view
, int32 frame
)
104 // fill with black on first frame
105 view
->SetLowColor(0, 0, 0);
106 view
->FillRect(view
->Bounds(), B_SOLID_LOW
);
108 // erase old text on all other frames
109 view
->SetHighColor(0, 0, 0);
110 view
->DrawString(fText
, BPoint(fX
, fY
));
113 // find some new text coordinates
114 fX
= rand() % int(fSizeX
- fTextWith
);
115 fY
= rand() % int(fSizeY
- fTextHeight
- (fIsPreview
? 2 : 20)) + fTextHeight
;
118 view
->SetHighColor(0, 255, 0);
119 view
->DrawString(fText
, BPoint(fX
, fY
));
121 // randomize time until next update (preview mode is faster)
122 SetTickSize(((rand() % 4) + 1) * (fIsPreview
? 300000 : 1000000));