2 * Copyright 2003 J Brown
3 * Copyright 2006 Andrey Korotaev <unC0Rr@inbox.ru>
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.
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.
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
27 #define APPNAME _T("Cylfrac")
31 #define timerdelay 1000/FPS
44 GLUquadricObj
* cylinder
;
48 float rval
, gval
, bval
;
50 BOOL fullscreen
= FALSE
;
52 float _RGB(float H
, float M1
, float M2
)
54 if(H
< 0.0) H
+= 360.0;
55 else if(H
> 360.0) H
-= 360.0;
56 if(H
< 60) return M1
+ (M2
- M1
) * H
/ 60.0;
57 if((H
>= 60 )&&(H
< 180)) return M2
;
58 if((H
>= 180)&&(H
< 240)) return M1
+ (M2
- M1
)*(240 - H
) / 60.0;
62 void HLStoRGB(float H
, float L
, float S
,
63 float* R
, float* G
, float* B
)
66 if(S
<= 0.5) M2
= S
* (1 + L
);
67 else M2
= S
* (1 - L
) + L
;
75 *R
= _RGB(H
+ 120.0, M1
, M2
);
76 *G
= _RGB(H
, M1
, M2
);
77 *B
= _RGB(H
- 120.0, M1
, M2
);
81 void DrawCylinder(int n
, float rota
, float width
)
84 glColor3f(rval
/n
, gval
/n
, bval
/n
);
85 glRotatef(rota
, 0.0, 1.0, 0.0);
86 gluCylinder(cylinder
, width
, width
* wfactor
, n
* 0.5, cylquality
, 1);
87 glTranslatef(0.0, 0.0, -n
* 0.5);
88 gluCylinder(cylinder
, width
* wfactor
, width
, n
* 0.5, cylquality
, 1);
91 float r
= rota
* rotfactor
;
92 glRotatef(90.0, 1.0, 0.0, 0.0);
93 DrawCylinder(n
- 1, r
, width
* wfactor
);
94 glTranslatef(0.0, n
, 0.0);
95 DrawCylinder(n
- 1, -r
, width
* wfactor
);
100 void DrawScene(HWND hwnd
, HDC dc
, int ticks
)
103 dc
= BeginPaint(hwnd
, &ps
);
104 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
105 glRotatef(ticks
* 0.01, 0.0, 1.0, -0.5);
106 angle
+= ticks
* 0.01;
107 colorh
+= ticks
* 0.003;
108 if (colorh
> 360.0) colorh
-= 360.0;
109 HLStoRGB(colorh
, 1.0, 0.7, &rval
, &gval
, &bval
);
110 DrawCylinder(lvls
, angle
, 0.2);
115 void CALLBACK
TimeProc(UINT uID
, UINT uMsg
, DWORD dwUser
, DWORD dw1
, DWORD dw2
)
117 InvalidateRect((HWND
)dwUser
, NULL
, 0);
120 void MyPixelFormat(HDC dc
)
123 PIXELFORMATDESCRIPTOR pfd
;
125 ZeroMemory(&pfd
, sizeof(pfd
));
126 pfd
.nSize
= sizeof(pfd
);
128 pfd
.dwFlags
= PFD_DOUBLEBUFFER
| PFD_SUPPORT_OPENGL
| PFD_DRAW_TO_WINDOW
;
130 npf
= ChoosePixelFormat(dc
, &pfd
);
132 SetPixelFormat(dc
, npf
, &pfd
);
135 void InitGL(HWND hwnd
)
137 GLfloat lightpos
[4] = {2.0, 2.0, -2.0, 0.7};
141 hrc
= wglCreateContext(dc
);
142 wglMakeCurrent(dc
, hrc
);
143 cylinder
= gluNewQuadric();
144 glEnable(GL_DEPTH_TEST
);
146 glLightfv(GL_LIGHT0
, GL_POSITION
, (GLfloat
*)&lightpos
);
147 glLightfv(GL_LIGHT0
, GL_LINEAR_ATTENUATION
, &ca
);
148 glEnable(GL_LIGHTING
);
149 glEnable(GL_COLOR_MATERIAL
);
152 LRESULT WINAPI
ScreenSaverProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
157 GetCursorPos(&initpoint
);
159 oldticks
= GetTickCount();
160 TimerID
= timeSetEvent(timerdelay
, 0, TimeProc
, (DWORD_PTR
)hwnd
, TIME_PERIODIC
);
165 DWORD ticks
= oldticks
;
167 oldticks
= GetTickCount();
168 DrawScene(hwnd
, dc
, oldticks
- ticks
);
171 GetCursorPos(&currpoint
);
172 if(abs(currpoint
.x
- initpoint
.x
) + (abs(currpoint
.y
- initpoint
.y
)) > 10)
173 PostMessage(hwnd
, WM_CLOSE
, 0, 0);
179 timeKillEvent(TimerID
);
180 gluDeleteQuadric(cylinder
);
181 wglMakeCurrent(0, 0);
182 wglDeleteContext(hrc
);
192 int width
= LOWORD(lParam
);
193 int height
= HIWORD(lParam
);
195 glViewport(0, 0, width
, height
);
196 glMatrixMode(GL_MODELVIEW
);
198 fscale
= 0.8/(float)lvls
;
199 glScalef(fscale
, fscale
, fscale
);
203 return DefScreenSaverProc(hwnd
, msg
, wParam
, lParam
);
208 BOOL WINAPI
ScreenSaverConfigureDialog(HWND hWnd
, UINT message
, WPARAM wparam
, LPARAM lparam
)
213 BOOL WINAPI
RegisterDialogClasses(HANDLE hmodule
)
231 MB_OK
| MB_ICONWARNING
);