2 * Copyright 2003 J Brown
3 * Copyright 2006 Eric Kohl
4 * Copyright 2007 Marc Piulachs (marc.piulachs@codexchange.net)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
25 #define RANDOM( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
27 #define MAX_LOADSTRING 100
28 #define MAX_STARS 1000
30 #define APPNAME _T("Starfield")
32 #define APP_TIMER_INTERVAL 20
34 #define MAX_STARS 1000
36 // Details of each individual star
39 int m_nXPos
, m_nYPos
, m_nZPos
;
44 BOOL fullscreen
= FALSE
;
49 int m_nCenterX
, m_nCenterY
;
51 void DrawStarField (HDC pDC
)
55 for (i
= 0; i
< m_nTotStars
; i
++)
57 // Clear last position of this star
64 nX
= (int)((((long)stars
[i
].m_nXPos
<< 7) / (long)stars
[i
].m_nZPos
) + m_nCenterX
);
65 nY
= (int)((((long)stars
[i
].m_nYPos
<< 7) / (long)stars
[i
].m_nZPos
) + m_nCenterY
);
74 // Remember current position for clearing later
75 stars
[i
].m_nOldX
= nX
;
76 stars
[i
].m_nOldY
= nY
;
80 BOOL
SetUpStars (int nNumStars
)
83 if (nNumStars
> MAX_STARS
)
86 _T("Too many stars! Aborting!"),
88 MB_OK
| MB_ICONWARNING
);
95 m_nTotStars
= nNumStars
;
97 stars
= (STAR
*)malloc(nNumStars
* sizeof(STAR
));
102 _T("Unable to allocate memory! Aborting!"),
104 MB_OK
| MB_ICONWARNING
);
108 for (i
= 0; i
< m_nTotStars
; i
++)
112 stars
[i
].m_nXPos
= RANDOM (-320, 320);
113 stars
[i
].m_nYPos
= RANDOM (-200, 200);
114 stars
[i
].m_nZPos
= i
+1;
115 stars
[i
].m_nOldX
= -1;
116 stars
[i
].m_nOldY
= -1;
117 } while ((stars
[i
].m_nXPos
== 0) || (stars
[i
].m_nYPos
== 0));
123 void MoveStarField (int nXofs
, int nYofs
, int nZofs
)
126 for (i
= 0; i
< m_nTotStars
; i
++)
128 stars
[i
].m_nXPos
+= nXofs
;
129 stars
[i
].m_nYPos
+= nYofs
;
130 stars
[i
].m_nZPos
+= nZofs
;
132 if (stars
[i
].m_nZPos
> m_nTotStars
)
133 stars
[i
].m_nZPos
-= m_nTotStars
;
134 if (stars
[i
].m_nZPos
< 1)
135 stars
[i
].m_nZPos
+= m_nTotStars
;
139 void SetDimensions (int nWidth
, int nHeight
)
141 m_nCenterX
= nWidth
/ 2;
142 m_nCenterY
= nHeight
/ 2;
145 LRESULT WINAPI
WndProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
148 static POINT ptCursor
;
149 static BOOL fFirstTime
= TRUE
;
166 HDC pDC
= BeginPaint (hwnd
, &PtStr
);
168 EndPaint (hwnd
, &PtStr
);
174 if (wParam
== APP_TIMER
)
176 MoveStarField (0, 0, -3);
179 ReleaseDC(hwnd
, pDC
);
185 // Change the center point of the starfield
193 KillTimer (hwnd
, APP_TIMER
);
200 // break out of screen-saver if any keyboard activity
204 PostMessage(hwnd
, WM_CLOSE
, 0, 0);
207 // break out of screen-saver if any mouse activity
216 // If we've got a parent then we must be a preview
217 if(GetParent(hwnd
) != 0)
222 GetCursorPos(&ptLast
);
226 GetCursorPos(&ptCursor
);
228 // if the mouse has moved more than 3 pixels then exit
229 if( (abs(ptCursor
.x
- ptLast
.x
) >= 3) ||
230 (abs(ptCursor
.y
- ptLast
.y
) >= 3))
232 PostMessage(hwnd
, WM_CLOSE
, 0, 0);
240 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
244 void InitSaver(HWND hwndParent
)
247 ZeroMemory(&wc
, sizeof(wc
));
248 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
249 wc
.lpfnWndProc
= WndProc
;
250 wc
.lpszClassName
= APPNAME
;
251 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
257 GetClientRect(hwndParent
, &rect
);
258 CreateWindow(APPNAME
, APPNAME
,
259 WS_VISIBLE
| WS_CHILD
,
270 hwnd
= CreateWindowEx(WS_EX_TOPMOST
,
273 WS_VISIBLE
| WS_POPUP
,
275 GetSystemMetrics(SM_CXSCREEN
), GetSystemMetrics(SM_CYSCREEN
),
281 SWP_NOZORDER
|SWP_NOACTIVATE
|SWP_NOSIZE
|SWP_SHOWWINDOW
);
291 // Look for any options Windows has passed to us:
293 // -a <hwnd> (set password)
295 // -p <hwnd> (preview)
296 // -c <hwnd> (configure)
298 VOID
ParseCommandLine(LPWSTR szCmdLine
, UCHAR
*chOption
, HWND
*hwndParent
)
300 UCHAR ch
= *szCmdLine
++;
302 if(ch
== '-' || ch
== '/')
305 if(ch
>= 'A' && ch
<= 'Z')
306 ch
+= 'a' - 'A'; //convert to lower case
314 while(ch
== ' ' || ch
== '\t')
319 unsigned int i
= _wtoi(szCmdLine
- 1);
320 *hwndParent
= (HWND
)i
;
331 LoadString(hInstance
,
336 LoadString(hInstance
,
344 MB_OK
| MB_ICONWARNING
);
347 int CALLBACK
wWinMain (HINSTANCE hInst
,
359 ParseCommandLine(lpCmdLine
, &chOption
, &hwndParent
);
361 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING
, TRUE
, &nPreviousState
, 0);
370 InitSaver(hwndParent
);
379 while (GetMessage(&Message
, 0, 0, 0))
380 DispatchMessage(&Message
);
382 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING
, FALSE
, &nPreviousState
, 0);
384 return Message
.wParam
;