move from branch
[reactos.git] / reactos / base / 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
25 #define APPNAME _T("Cylfrac")
26 #define wfactor 0.9
27 #define rotfactor 1.5
28 #define FPS 100
29 #define timerdelay 1000/FPS
30
31 POINT initpoint;
32 HDC dc;
33 HGLRC hrc;
34 DWORD oldticks;
35 MMRESULT TimerID;
36
37 DWORD lvls = 7;
38 int cylquality = 8;
39
40 HINSTANCE hInstance;
41
42 GLUquadricObj * cylinder;
43
44 float angle = 0;
45 float colorh = 0.0;
46 float rval, gval, bval;
47
48 BOOL fullscreen = FALSE;
49
50 float _RGB(float H, float M1, float M2)
51 {
52 if(H < 0.0) H += 360.0;
53 else if(H > 360.0) H -= 360.0;
54 if(H < 60) return M1 + (M2 - M1) * H / 60.0;
55 if((H >= 60 )&&(H < 180)) return M2;
56 if((H >= 180)&&(H < 240)) return M1 + (M2 - M1)*(240 - H) / 60.0;
57 return M1;
58 }
59
60 void HLStoRGB(float H, float L, float S,
61 float* R, float* G, float* B)
62 {
63 float M1, M2;
64 if(S <= 0.5) M2 = S * (1 + L);
65 else M2 = S * (1 - L) + L;
66 M1 = 2 * S - M2;
67 if (L == 0.0)
68 {
69 *R = S;
70 *G = S;
71 *B = S;
72 } else {
73 *R = _RGB(H + 120.0, M1, M2);
74 *G = _RGB(H , M1, M2);
75 *B = _RGB(H - 120.0, M1, M2);
76 }
77 }
78
79 void DrawCylinder(int n, float rota, float width)
80 {
81 glPushMatrix();
82 glColor3f(rval/n, gval/n, bval/n);
83 glRotatef(rota, 0.0, 1.0, 0.0);
84 gluCylinder(cylinder, width, width * wfactor, n * 0.5, cylquality, 1);
85 glTranslatef(0.0, 0.0, -n * 0.5);
86 gluCylinder(cylinder, width * wfactor, width, n * 0.5, cylquality, 1);
87 if(n > 1)
88 {
89 float r = rota * rotfactor;
90 glRotatef(90.0, 1.0, 0.0, 0.0);
91 DrawCylinder(n - 1, r, width * wfactor);
92 glTranslatef(0.0, n, 0.0);
93 DrawCylinder(n - 1, -r, width * wfactor);
94 }
95 glPopMatrix();
96 }
97
98 void DrawScene(HWND hwnd, HDC dc, int ticks)
99 {
100 PAINTSTRUCT ps;
101 dc = BeginPaint(hwnd, &ps);
102 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103 glRotatef(ticks * 0.01, 0.0, 1.0, -0.5);
104 angle += ticks * 0.01;
105 colorh += ticks * 0.003;
106 if (colorh > 360.0) colorh -= 360.0;
107 HLStoRGB(colorh, 1.0, 0.7, &rval, &gval, &bval);
108 DrawCylinder(lvls, angle, 0.2);
109 SwapBuffers(dc);
110 EndPaint(hwnd, &ps);
111 }
112
113 void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
114 {
115 InvalidateRect((HWND)dwUser, NULL, 0);
116 }
117
118 void MyPixelFormat(HDC dc)
119 {
120 int npf;
121 PIXELFORMATDESCRIPTOR pfd;
122
123 ZeroMemory(&pfd, sizeof(pfd));
124 pfd.nSize = sizeof(pfd);
125 pfd.nVersion = 1;
126 pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
127
128 npf = ChoosePixelFormat(dc, &pfd);
129 if(npf != 0)
130 SetPixelFormat(dc, npf, &pfd);
131 }
132
133 void InitGL(HWND hwnd)
134 {
135 GLfloat lightpos[4] = {2.0, 2.0, -2.0, 0.7};
136 GLfloat ca = 1.0;
137 dc = GetDC(hwnd);
138 MyPixelFormat(dc);
139 hrc = wglCreateContext(dc);
140 wglMakeCurrent(dc, hrc);
141 cylinder = gluNewQuadric();
142 glEnable(GL_DEPTH_TEST);
143 glEnable(GL_LIGHT0);
144 glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat *)&lightpos);
145 glLightfv(GL_LIGHT0, GL_LINEAR_ATTENUATION, &ca);
146 glEnable(GL_LIGHTING);
147 glEnable(GL_COLOR_MATERIAL);
148 }
149
150 LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
151 {
152 switch(msg) {
153 case WM_CREATE:
154 GetCursorPos(&initpoint);
155 InitGL(hwnd);
156 oldticks = GetTickCount();
157 TimerID = timeSetEvent (timerdelay, 0, TimeProc, (DWORD)hwnd, TIME_PERIODIC);
158 break;
159 case WM_PAINT:
160 {
161 DWORD ticks = oldticks;
162 POINT currpoint;
163 oldticks = GetTickCount();
164 DrawScene(hwnd, dc, oldticks - ticks);
165 if(fullscreen)
166 {
167 GetCursorPos(&currpoint);
168 if(abs(currpoint.x - initpoint.x) + (abs(currpoint.y - initpoint.y)) > 10)
169 PostMessage(hwnd, WM_CLOSE, 0, 0);
170 }
171 break;
172 }
173 case WM_DESTROY:
174 timeKillEvent(TimerID);
175 gluDeleteQuadric(cylinder);
176 wglMakeCurrent(0, 0);
177 wglDeleteContext(hrc);
178 ReleaseDC(hwnd, dc);
179 DeleteDC(dc);
180 PostQuitMessage(0);
181 break;
182 case WM_NOTIFY:
183 case WM_SYSKEYDOWN:
184 PostMessage(hwnd, WM_CLOSE, 0, 0);
185 break;
186 case WM_SIZE:
187 {
188 int width = LOWORD(lParam);
189 int height = HIWORD(lParam);
190 float fscale;
191 glViewport(0, 0, width, height);
192 glMatrixMode(GL_MODELVIEW);
193 glLoadIdentity();
194 fscale = 0.8/(float)lvls;
195 glScalef(fscale, fscale, fscale);
196 break;
197 }
198
199 }
200 return DefWindowProc(hwnd, msg, wParam, lParam);
201 }
202
203 void InitSaver(HWND hwndParent)
204 {
205 WNDCLASS wc;
206 ZeroMemory(&wc, sizeof(wc));
207 wc.style = CS_HREDRAW | CS_VREDRAW;
208 wc.lpfnWndProc = WndProc;
209 wc.lpszClassName = APPNAME;
210 RegisterClass(&wc);
211
212 if (hwndParent != 0)
213 {
214 RECT rect;
215 GetClientRect(hwndParent, &rect);
216 CreateWindow(APPNAME, APPNAME,
217 WS_VISIBLE | WS_CHILD,
218 0, 0,
219 rect.right,
220 rect.bottom,
221 hwndParent, 0,
222 hInstance, NULL);
223 fullscreen = FALSE;
224 } else {
225 HWND hwnd;
226 hwnd = CreateWindow(APPNAME, APPNAME,
227 WS_VISIBLE | WS_POPUP | WS_EX_TOPMOST,
228 0, 0,
229 GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
230 HWND_DESKTOP, 0,
231 hInstance, NULL);
232 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
233 ShowCursor(FALSE);
234 fullscreen = TRUE;
235 }
236 }
237
238 void ParseCommandLine(PSTR szCmdLine, int *chOption, HWND *hwndParent)
239 {
240 int ch;
241
242 if (!strlen(szCmdLine))
243 return;
244
245 ch = *szCmdLine++;
246
247 if(ch == '-' || ch == '/')
248 ch = *szCmdLine++;
249
250 if(ch >= 'A' && ch <= 'Z')
251 ch += 'a' - 'A';
252
253 *chOption = ch;
254
255 if (ch == 's' || ch == 'c')
256 return;
257
258 ch = *szCmdLine++;
259
260 if(ch == ':')
261 ch = *szCmdLine++;
262
263 while(ch == ' ' || ch == '\t')
264 ch = *szCmdLine++;
265
266 if(isdigit(ch))
267 {
268 unsigned int i = atoi(szCmdLine - 1);
269 *hwndParent = (HWND)i;
270 }
271 else
272 *hwndParent = 0;
273 }
274
275 int WINAPI WinMain (HINSTANCE hInst,
276 HINSTANCE hPrev,
277 LPSTR lpCmdLine,
278 int iCmdShow)
279 {
280 HWND hwndParent;
281 int chOption = 0;
282 MSG Message;
283
284 hInstance = hInst;
285
286 ParseCommandLine(lpCmdLine, &chOption, &hwndParent);
287
288 switch(chOption)
289 {
290 case 's':
291 InitSaver(0);
292 break;
293
294 case 'p':
295 InitSaver(hwndParent);
296 break;
297
298 case 'c':
299 default:
300 MessageBox(0,
301 _T("Cylinders fractal by unC0Rr.\nSpecial for ReactOS.\n"),
302 _T("About"),
303 MB_OK | MB_ICONINFORMATION);
304 return 0;
305 }
306
307 while (GetMessage(&Message, 0, 0, 0))
308 DispatchMessage(&Message);
309
310 return Message.wParam;
311 }
312
313