03ed11485ea0814975d9a954f7838c802e52c9e5
[reactos.git] / rosapps / applications / screensavers / cylfrac / cylfrac.c
1 /*
2 * Copyright 2003 J Brown
3 * Copyright 2006 Andrey Korotaev <unC0Rr@inbox.ru>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 */
19
20 #include <windows.h>
21 #include <GL/gl.h>
22 #include <GL/glu.h>
23 #include <tchar.h>
24 #include "resource.h"
25
26 #define APPNAME _T("Cylfrac")
27 #define wfactor 0.9
28 #define rotfactor 1.5
29 #define FPS 100
30 #define timerdelay 1000/FPS
31
32 POINT initpoint;
33 HDC dc;
34 HGLRC hrc;
35 DWORD oldticks;
36 MMRESULT TimerID;
37
38 DWORD lvls = 7;
39 int cylquality = 8;
40
41 HINSTANCE hInstance;
42
43 GLUquadricObj * cylinder;
44
45 float angle = 0;
46 float colorh = 0.0;
47 float rval, gval, bval;
48
49 BOOL fullscreen = FALSE;
50
51 float _RGB(float H, float M1, float M2)
52 {
53 if(H < 0.0) H += 360.0;
54 else if(H > 360.0) H -= 360.0;
55 if(H < 60) return M1 + (M2 - M1) * H / 60.0;
56 if((H >= 60 )&&(H < 180)) return M2;
57 if((H >= 180)&&(H < 240)) return M1 + (M2 - M1)*(240 - H) / 60.0;
58 return M1;
59 }
60
61 void HLStoRGB(float H, float L, float S,
62 float* R, float* G, float* B)
63 {
64 float M1, M2;
65 if(S <= 0.5) M2 = S * (1 + L);
66 else M2 = S * (1 - L) + L;
67 M1 = 2 * S - M2;
68 if (L == 0.0)
69 {
70 *R = S;
71 *G = S;
72 *B = S;
73 } else {
74 *R = _RGB(H + 120.0, M1, M2);
75 *G = _RGB(H , M1, M2);
76 *B = _RGB(H - 120.0, M1, M2);
77 }
78 }
79
80 void DrawCylinder(int n, float rota, float width)
81 {
82 glPushMatrix();
83 glColor3f(rval/n, gval/n, bval/n);
84 glRotatef(rota, 0.0, 1.0, 0.0);
85 gluCylinder(cylinder, width, width * wfactor, n * 0.5, cylquality, 1);
86 glTranslatef(0.0, 0.0, -n * 0.5);
87 gluCylinder(cylinder, width * wfactor, width, n * 0.5, cylquality, 1);
88 if(n > 1)
89 {
90 float r = rota * rotfactor;
91 glRotatef(90.0, 1.0, 0.0, 0.0);
92 DrawCylinder(n - 1, r, width * wfactor);
93 glTranslatef(0.0, n, 0.0);
94 DrawCylinder(n - 1, -r, width * wfactor);
95 }
96 glPopMatrix();
97 }
98
99 void DrawScene(HWND hwnd, HDC dc, int ticks)
100 {
101 PAINTSTRUCT ps;
102 dc = BeginPaint(hwnd, &ps);
103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104 glRotatef(ticks * 0.01, 0.0, 1.0, -0.5);
105 angle += ticks * 0.01;
106 colorh += ticks * 0.003;
107 if (colorh > 360.0) colorh -= 360.0;
108 HLStoRGB(colorh, 1.0, 0.7, &rval, &gval, &bval);
109 DrawCylinder(lvls, angle, 0.2);
110 SwapBuffers(dc);
111 EndPaint(hwnd, &ps);
112 }
113
114 void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
115 {
116 InvalidateRect((HWND)dwUser, NULL, 0);
117 }
118
119 void MyPixelFormat(HDC dc)
120 {
121 int npf;
122 PIXELFORMATDESCRIPTOR pfd;
123
124 ZeroMemory(&pfd, sizeof(pfd));
125 pfd.nSize = sizeof(pfd);
126 pfd.nVersion = 1;
127 pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
128
129 npf = ChoosePixelFormat(dc, &pfd);
130 if(npf != 0)
131 SetPixelFormat(dc, npf, &pfd);
132 }
133
134 void InitGL(HWND hwnd)
135 {
136 GLfloat lightpos[4] = {2.0, 2.0, -2.0, 0.7};
137 GLfloat ca = 1.0;
138 dc = GetDC(hwnd);
139 MyPixelFormat(dc);
140 hrc = wglCreateContext(dc);
141 wglMakeCurrent(dc, hrc);
142 cylinder = gluNewQuadric();
143 glEnable(GL_DEPTH_TEST);
144 glEnable(GL_LIGHT0);
145 glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat *)&lightpos);
146 glLightfv(GL_LIGHT0, GL_LINEAR_ATTENUATION, &ca);
147 glEnable(GL_LIGHTING);
148 glEnable(GL_COLOR_MATERIAL);
149 }
150
151 LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
152 {
153 switch(msg) {
154 case WM_CREATE:
155 GetCursorPos(&initpoint);
156 InitGL(hwnd);
157 oldticks = GetTickCount();
158 TimerID = timeSetEvent (timerdelay, 0, TimeProc, (DWORD)hwnd, TIME_PERIODIC);
159 break;
160 case WM_PAINT:
161 {
162 DWORD ticks = oldticks;
163 POINT currpoint;
164 oldticks = GetTickCount();
165 DrawScene(hwnd, dc, oldticks - ticks);
166 if(fullscreen)
167 {
168 GetCursorPos(&currpoint);
169 if(abs(currpoint.x - initpoint.x) + (abs(currpoint.y - initpoint.y)) > 10)
170 PostMessage(hwnd, WM_CLOSE, 0, 0);
171 }
172 break;
173 }
174 case WM_DESTROY:
175 timeKillEvent(TimerID);
176 gluDeleteQuadric(cylinder);
177 wglMakeCurrent(0, 0);
178 wglDeleteContext(hrc);
179 ReleaseDC(hwnd, dc);
180 DeleteDC(dc);
181 if (fullscreen)
182 ShowCursor(TRUE);
183 PostQuitMessage(0);
184 break;
185 case WM_NOTIFY:
186 case WM_SYSKEYDOWN:
187 PostMessage(hwnd, WM_CLOSE, 0, 0);
188 break;
189 case WM_SIZE:
190 {
191 int width = LOWORD(lParam);
192 int height = HIWORD(lParam);
193 float fscale;
194 glViewport(0, 0, width, height);
195 glMatrixMode(GL_MODELVIEW);
196 glLoadIdentity();
197 fscale = 0.8/(float)lvls;
198 glScalef(fscale, fscale, fscale);
199 break;
200 }
201
202 }
203 return DefWindowProc(hwnd, msg, wParam, lParam);
204 }
205
206 void InitSaver(HWND hwndParent)
207 {
208 WNDCLASS wc;
209 ZeroMemory(&wc, sizeof(wc));
210 wc.style = CS_HREDRAW | CS_VREDRAW;
211 wc.lpfnWndProc = WndProc;
212 wc.lpszClassName = APPNAME;
213 RegisterClass(&wc);
214
215 if (hwndParent != 0)
216 {
217 RECT rect;
218 GetClientRect(hwndParent, &rect);
219 CreateWindow(APPNAME, APPNAME,
220 WS_VISIBLE | WS_CHILD,
221 0, 0,
222 rect.right,
223 rect.bottom,
224 hwndParent, 0,
225 hInstance, NULL);
226 fullscreen = FALSE;
227 } else {
228 HWND hwnd;
229 hwnd = CreateWindow(APPNAME, APPNAME,
230 WS_VISIBLE | WS_POPUP | WS_EX_TOPMOST,
231 0, 0,
232 GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
233 HWND_DESKTOP, 0,
234 hInstance, NULL);
235 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
236 ShowCursor(FALSE);
237 fullscreen = TRUE;
238 }
239 }
240
241 //
242 // Look for any options Windows has passed to us:
243 //
244 // -a <hwnd> (set password)
245 // -s (screensave)
246 // -p <hwnd> (preview)
247 // -c <hwnd> (configure)
248 //
249 VOID ParseCommandLine(LPWSTR szCmdLine, UCHAR *chOption, HWND *hwndParent)
250 {
251 UCHAR ch = *szCmdLine++;
252
253 if(ch == '-' || ch == '/')
254 ch = *szCmdLine++;
255
256 if(ch >= 'A' && ch <= 'Z')
257 ch += 'a' - 'A'; //convert to lower case
258
259 *chOption = ch;
260 ch = *szCmdLine++;
261
262 if(ch == ':')
263 ch = *szCmdLine++;
264
265 while(ch == ' ' || ch == '\t')
266 ch = *szCmdLine++;
267
268 if(isdigit(ch))
269 {
270 unsigned int i = _wtoi(szCmdLine - 1);
271 *hwndParent = (HWND)i;
272 }
273 else
274 *hwndParent = NULL;
275 }
276
277 void Configure(void)
278 {
279 TCHAR szTitle[256];
280 TCHAR szText[256];
281
282 LoadString(hInstance,
283 IDS_TITLE,
284 szTitle,
285 256);
286
287 LoadString(hInstance,
288 IDS_TEXT,
289 szText,
290 256);
291
292 MessageBox(0,
293 szText,
294 szTitle,
295 MB_OK | MB_ICONWARNING);
296 }
297
298 int CALLBACK wWinMain (HINSTANCE hInst,
299 HINSTANCE hPrev,
300 LPWSTR lpCmdLine,
301 int iCmdShow)
302 {
303 HWND hwndParent = 0;
304 UCHAR chOption;
305 MSG Message;
306
307 hInstance = hInst;
308
309 ParseCommandLine(lpCmdLine, &chOption, &hwndParent);
310
311 switch(chOption)
312 {
313 case 's':
314 InitSaver(0);
315 break;
316
317 case 'p':
318 InitSaver(hwndParent);
319 break;
320
321 case 'c':
322 default:
323 Configure();
324 return 0;
325 }
326
327 while (GetMessage(&Message, 0, 0, 0))
328 DispatchMessage(&Message);
329
330 return Message.wParam;
331 }
332
333