2 * Star field screensaver
4 * Copyright 2011 Carlo Bramini
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #define WIN32_LEAN_AND_MEAN
30 #define SIZEOF(_v) (sizeof(_v) / sizeof(*_v))
32 // Options for the starfield
35 // Factory default settings.
36 static const SSSTARS FactoryDefaults
= {
47 static const DWORD RotoStrings
[] = {
53 static DWORD
QueryDWORD(HKEY hKey
, LPCTSTR pszValueName
, DWORD Default
)
55 DWORD dwData
, dwType
, cbData
;
59 cbData
= sizeof(DWORD
);
61 lRes
= RegQueryValueEx(
69 if (lRes
!= ERROR_SUCCESS
|| dwType
!= REG_DWORD
)
75 static void SaveDWORD(HKEY hKey
, LPCTSTR pszValueName
, DWORD dwValue
)
77 RegSetValueEx(hKey
, pszValueName
, 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(dwValue
));
81 void LoadSettings(void)
86 Settings
= FactoryDefaults
;
88 lRes
= RegCreateKeyEx(
90 _T("Software\\Microsoft\\ScreenSavers\\Ssstars"),
99 if (lRes
!= ERROR_SUCCESS
)
102 Settings
.uiNumStars
= QueryDWORD(hKey
, _T("NumberOfStars"), Settings
.uiNumStars
);
103 Settings
.uiSpeed
= QueryDWORD(hKey
, _T("Speed"), Settings
.uiSpeed
);
104 Settings
.uiRotation
= QueryDWORD(hKey
, _T("TypeOfRotation"), Settings
.uiRotation
);
106 Settings
.bDoBlending
= QueryDWORD(hKey
, _T("DoBlending"), Settings
.bDoBlending
);
107 Settings
.bFinePerspective
= QueryDWORD(hKey
, _T("FinePerspective"), Settings
.bFinePerspective
);
108 Settings
.bEnableFiltering
= QueryDWORD(hKey
, _T("EnableFiltering"), Settings
.bEnableFiltering
);
109 Settings
.bSmoothShading
= QueryDWORD(hKey
, _T("SmoothShading"), Settings
.bSmoothShading
);
111 // Check the number of stars to be in range
112 if (Settings
.uiNumStars
< MIN_STARS
)
113 Settings
.uiNumStars
= MIN_STARS
;
115 if (Settings
.uiNumStars
> MAX_STARS
)
116 Settings
.uiNumStars
= MAX_STARS
;
118 // Check the speed to be in range
119 if (Settings
.uiSpeed
< MIN_SPEED
)
120 Settings
.uiSpeed
= MIN_SPEED
;
122 if (Settings
.uiSpeed
> MAX_SPEED
)
123 Settings
.uiSpeed
= MAX_SPEED
;
125 // Check type of rotation to be in range
126 if (Settings
.uiRotation
!= ROTATION_NONE
&&
127 Settings
.uiRotation
!= ROTATION_LINEAR
&&
128 Settings
.uiRotation
!= ROTATION_PERIODIC
)
129 Settings
.uiRotation
= ROTATION_PERIODIC
;
134 void SaveSettings(void)
139 lRes
= RegCreateKeyEx(
141 _T("Software\\Microsoft\\ScreenSavers\\Ssstars"),
150 if (lRes
!= ERROR_SUCCESS
)
153 SaveDWORD(hKey
, _T("NumberOfStars"), Settings
.uiNumStars
);
154 SaveDWORD(hKey
, _T("Speed"), Settings
.uiSpeed
);
155 SaveDWORD(hKey
, _T("TypeOfRotation"), Settings
.uiRotation
);
157 SaveDWORD(hKey
, _T("DoBlending"), Settings
.bDoBlending
);
158 SaveDWORD(hKey
, _T("FinePerspective"), Settings
.bFinePerspective
);
159 SaveDWORD(hKey
, _T("EnableFiltering"), Settings
.bEnableFiltering
);
160 SaveDWORD(hKey
, _T("SmoothShading"), Settings
.bSmoothShading
);
165 static void SetupControls(HWND hWnd
)
174 HGDIOBJ hOldBmp
, hOldFnt
;
179 hInstance
= (HINSTANCE
)GetWindowLong(hWnd
, GWL_HINSTANCE
);
181 SendDlgItemMessage(hWnd
, IDC_SLIDER_NUM_OF_STARS
, TBM_SETRANGE
, FALSE
, MAKELPARAM(MIN_STARS
, MAX_STARS
));
183 SendDlgItemMessage(hWnd
, IDC_SLIDER_SPEED
, TBM_SETRANGE
, FALSE
, MAKELPARAM(1, 100));
185 for (x
= 0; x
< ROTATION_ITEMS
; x
++)
187 LoadString(hInstance
, RotoStrings
[x
], Strings
, sizeof(Strings
)/sizeof(TCHAR
));
188 SendDlgItemMessage(hWnd
, IDC_COMBO_ROTATION
, CB_ADDSTRING
, 0, (LPARAM
)Strings
);
191 hCosmos
= LoadImage(hInstance
, MAKEINTRESOURCE(IDB_COSMOS
), IMAGE_BITMAP
, 0, 0, LR_CREATEDIBSECTION
| LR_DEFAULTSIZE
);
194 hMemDC
= CreateCompatibleDC(hDC
);
196 // Create the font for the title
197 ZeroMemory(&lf
, sizeof(lf
));
199 lf
.lfWeight
= FW_THIN
;
200 lf
.lfCharSet
= ANSI_CHARSET
;
201 lf
.lfQuality
= PROOF_QUALITY
;
203 _tcscpy(lf
.lfFaceName
, _T("Tahoma"));
205 hFont
= CreateFontIndirect(&lf
);
207 hOldBmp
= SelectObject(hMemDC
, hCosmos
);
208 hOldFnt
= SelectObject(hMemDC
, hFont
);
210 SetBkMode(hMemDC
, TRANSPARENT
);
211 SetTextColor(hMemDC
, RGB(0xFF, 0xFF, 0xFF));
213 x
= LoadString(hInstance
, IDS_DESCRIPTION
, Strings
, sizeof(Strings
)/sizeof(TCHAR
));
215 GetTextExtentPoint32(hMemDC
, _T("ReactOS"), 7, &sizeReactOS
);
216 GetTextExtentPoint32(hMemDC
, Strings
, x
, &sizeStarfield
);
218 GetObject(hCosmos
, sizeof(BITMAP
), &bm
);
220 gap
= bm
.bmHeight
- sizeReactOS
.cy
- sizeStarfield
.cy
;
222 TextOut(hMemDC
, 16, gap
* 2 / 5, _T("ReactOS"), 7);
223 TextOut(hMemDC
, 16, gap
* 3 / 5 + sizeReactOS
.cy
, Strings
, x
);
225 SelectObject(hMemDC
, hOldBmp
);
226 SelectObject(hMemDC
, hOldFnt
);
231 ReleaseDC(hWnd
, hDC
);
233 SendDlgItemMessage(hWnd
, IDC_IMAGE_COSMOS
, STM_SETIMAGE
, IMAGE_BITMAP
, (LPARAM
)hCosmos
);
236 static void ApplySettings(HWND hWnd
)
238 SendDlgItemMessage(hWnd
, IDC_SLIDER_NUM_OF_STARS
, TBM_SETPOS
, TRUE
, Settings
.uiNumStars
);
239 SetDlgItemInt(hWnd
, IDC_TEXT_NUM_OF_STARS
, Settings
.uiNumStars
, FALSE
);
241 SendDlgItemMessage(hWnd
, IDC_SLIDER_SPEED
, TBM_SETPOS
, TRUE
, Settings
.uiSpeed
);
242 SetDlgItemInt(hWnd
, IDC_TEXT_SPEED
, Settings
.uiSpeed
, FALSE
);
244 SendDlgItemMessage(hWnd
, IDC_COMBO_ROTATION
, CB_SETCURSEL
, (WPARAM
)Settings
.uiRotation
, 0);
246 SendDlgItemMessage(hWnd
, IDC_CHECK_DOBLENDING
, BM_SETCHECK
, (WPARAM
)Settings
.bDoBlending
, 0);
247 SendDlgItemMessage(hWnd
, IDC_CHECK_PERSPECTIVE
, BM_SETCHECK
, (WPARAM
)Settings
.bFinePerspective
, 0);
248 SendDlgItemMessage(hWnd
, IDC_CHECK_FILTERING
, BM_SETCHECK
, (WPARAM
)Settings
.bEnableFiltering
, 0);
249 SendDlgItemMessage(hWnd
, IDC_CHECK_SHADING
, BM_SETCHECK
, (WPARAM
)Settings
.bSmoothShading
, 0);
252 static void ReadSettings(HWND hWnd
)
254 Settings
.uiNumStars
= SendDlgItemMessage(hWnd
, IDC_SLIDER_NUM_OF_STARS
, TBM_GETPOS
, 0, 0);
255 SetDlgItemInt(hWnd
, IDC_TEXT_NUM_OF_STARS
, Settings
.uiNumStars
, FALSE
);
257 Settings
.uiSpeed
= SendDlgItemMessage(hWnd
, IDC_SLIDER_SPEED
, TBM_GETPOS
, 0, 0);
258 SetDlgItemInt(hWnd
, IDC_TEXT_SPEED
, Settings
.uiSpeed
, FALSE
);
260 Settings
.uiRotation
= SendDlgItemMessage(hWnd
, IDC_COMBO_ROTATION
, CB_GETCURSEL
, 0, 0);
262 Settings
.bDoBlending
= SendDlgItemMessage(hWnd
, IDC_CHECK_DOBLENDING
, BM_GETCHECK
, 0, 0);
263 Settings
.bFinePerspective
= SendDlgItemMessage(hWnd
, IDC_CHECK_PERSPECTIVE
, BM_GETCHECK
, 0, 0);
264 Settings
.bEnableFiltering
= SendDlgItemMessage(hWnd
, IDC_CHECK_FILTERING
, BM_GETCHECK
, 0, 0);
265 Settings
.bSmoothShading
= SendDlgItemMessage(hWnd
, IDC_CHECK_SHADING
, BM_GETCHECK
, 0, 0);
268 static BOOL
OnCommandAbout(HWND hWnd
)
272 TCHAR szAppName
[256];
274 TCHAR szLicense
[1024];
276 hInstance
= (HINSTANCE
)GetWindowLong(hWnd
, GWL_HINSTANCE
);
278 hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_STARFIELD
));
280 LoadString(hInstance
, IDS_DESCRIPTION
, szAppName
, SIZEOF(szAppName
));
281 LoadString(hInstance
, IDS_AUTHOR
, szAuthor
, SIZEOF(szAuthor
));
282 LoadString(hInstance
, IDS_LICENSE
, szLicense
, SIZEOF(szLicense
));
284 _tcscat(szAppName
, _T("#"));
285 _tcscat(szAppName
, szAuthor
);
287 ShellAbout(hWnd
, szAppName
, szLicense
, hIcon
);
293 // Dialogbox procedure for Configuration window
295 BOOL CALLBACK
ScreenSaverConfigureDialog(HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
305 switch (LOWORD( wParam
)) {
308 // Write configuration
313 EndDialog( hDlg
, LOWORD( wParam
));
316 case IDC_BUTTON_ABOUT
:
317 return OnCommandAbout(hDlg
);
329 BOOL WINAPI
RegisterDialogClasses(HANDLE hInst
)
331 InitCommonControls();