ReactOS  0.4.13-dev-92-gf251225
cylfrac.c
Go to the documentation of this file.
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 <scrnsave.h>
25 #include "resource.h"
26 
27 #define APPNAME _T("Cylfrac")
28 #define wfactor 0.9
29 #define rotfactor 1.5
30 #define FPS 100
31 #define timerdelay 1000/FPS
32 
38 
39 DWORD lvls = 7;
40 int cylquality = 8;
41 
43 
45 
46 float angle = 0;
47 float colorh = 0.0;
48 float rval, gval, bval;
49 
51 
52 float _RGB(float H, float M1, float M2)
53 {
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;
59  return M1;
60 }
61 
62 void HLStoRGB(float H, float L, float S,
63  float* R, float* G, float* B)
64 {
65  float M1, M2;
66  if(S <= 0.5) M2 = S * (1 + L);
67  else M2 = S * (1 - L) + L;
68  M1 = 2 * S - M2;
69  if (L == 0.0)
70  {
71  *R = S;
72  *G = S;
73  *B = S;
74  } else {
75  *R = _RGB(H + 120.0, M1, M2);
76  *G = _RGB(H , M1, M2);
77  *B = _RGB(H - 120.0, M1, M2);
78  }
79 }
80 
81 void DrawCylinder(int n, float rota, float width)
82 {
83  glPushMatrix();
84  glColor3f(rval/n, gval/n, bval/n);
85  glRotatef(rota, 0.0, 1.0, 0.0);
87  glTranslatef(0.0, 0.0, -n * 0.5);
89  if(n > 1)
90  {
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);
96  }
97  glPopMatrix();
98 }
99 
100 void DrawScene(HWND hwnd, HDC dc, int ticks)
101 {
102  PAINTSTRUCT ps;
103  dc = BeginPaint(hwnd, &ps);
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);
111  SwapBuffers(dc);
112  EndPaint(hwnd, &ps);
113 }
114 
115 void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
116 {
117  InvalidateRect((HWND)dwUser, NULL, 0);
118 }
119 
121 {
122  int npf;
124 
125  ZeroMemory(&pfd, sizeof(pfd));
126  pfd.nSize = sizeof(pfd);
127  pfd.nVersion = 1;
129 
130  npf = ChoosePixelFormat(dc, &pfd);
131  if(npf != 0)
132  SetPixelFormat(dc, npf, &pfd);
133 }
134 
136 {
137  GLfloat lightpos[4] = {2.0, 2.0, -2.0, 0.7};
138  GLfloat ca = 1.0;
139  dc = GetDC(hwnd);
140  MyPixelFormat(dc);
146  glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat *)&lightpos);
150 }
151 
153 {
154  switch(msg) {
155  case WM_CREATE:
156  {
158  InitGL(hwnd);
161  }
162  break;
163  case WM_PAINT:
164  {
165  DWORD ticks = oldticks;
166  POINT currpoint;
168  DrawScene(hwnd, dc, oldticks - ticks);
169  if(fullscreen)
170  {
171  GetCursorPos(&currpoint);
172  if(abs(currpoint.x - initpoint.x) + (abs(currpoint.y - initpoint.y)) > 10)
173  PostMessage(hwnd, WM_CLOSE, 0, 0);
174  }
175  }
176  break;
177  case WM_DESTROY:
178  {
181  wglMakeCurrent(0, 0);
183  ReleaseDC(hwnd, dc);
184  DeleteDC(dc);
185  if (fullscreen)
186  ShowCursor(TRUE);
187  PostQuitMessage(0);
188  }
189  break;
190  case WM_SIZE:
191  {
192  int width = LOWORD(lParam);
193  int height = HIWORD(lParam);
194  float fscale;
195  glViewport(0, 0, width, height);
197  glLoadIdentity();
198  fscale = 0.8/(float)lvls;
199  glScalef(fscale, fscale, fscale);
200  }
201  break;
202  default:
204  }
205  return 0;
206 }
207 
209 {
210  return FALSE;
211 }
212 
214 {
215  TCHAR szTitle[256];
216  TCHAR szText[256];
217 
219  IDS_TITLE,
220  szTitle,
221  256);
222 
224  IDS_TEXT,
225  szText,
226  256);
227 
228  MessageBox(0,
229  szText,
230  szTitle,
232  return FALSE;
233 }
#define gluNewQuadric
Definition: glu_mangle.h:36
float colorh
Definition: cylfrac.c:47
#define GL_COLOR_BUFFER_BIT
Definition: gl.h:716
GLAPI void GLAPIENTRY glClear(GLbitfield mask)
static HGLRC(WINAPI *pwglCreateContextAttribsARB)(HDC hDC
#define abs(i)
Definition: fconv.c:206
BOOL WINAPI ScreenSaverConfigureDialog(HWND hWnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: cylfrac.c:208
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
GLint GLint GLsizei width
Definition: gl.h:1546
#define R(b, x)
Definition: sha2.c:134
Definition: tftpd.h:59
#define TRUE
Definition: types.h:120
BOOL WINAPI wglDeleteContext(HGLRC hglrc)
Definition: wgl.c:514
long y
Definition: polytest.cpp:48
LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: cylfrac.c:152
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
long x
Definition: polytest.cpp:48
HDC WINAPI GetDC(_In_opt_ HWND)
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define GL_DEPTH_BUFFER_BIT
Definition: gl.h:710
GLUquadricObj * cylinder
Definition: cylfrac.c:44
static HDC
Definition: imagelist.c:92
#define CALLBACK
Definition: compat.h:27
GLAPI void GLAPIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
HWND hWnd
Definition: settings.c:17
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
GLdouble n
Definition: glext.h:7729
#define timerdelay
Definition: cylfrac.c:31
#define S(x)
Definition: test.h:190
#define ZeroMemory
Definition: winbase.h:1635
UINT MMRESULT
Definition: mmsystem.h:962
HDC dc
Definition: cylfrac.c:34
void ShowCursor(void)
Definition: hardware.c:556
UINT_PTR WPARAM
Definition: windef.h:207
GLfloat angle
Definition: glext.h:10853
Definition: movable.cpp:7
BOOL WINAPI EndPaint(_In_ HWND, _In_ const PAINTSTRUCT *)
HGLRC hrc
Definition: cylfrac.c:35
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2635
MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc, DWORD_PTR dwUser, UINT wFlags)
Definition: time.c:359
WPARAM wParam
Definition: combotst.c:138
BOOL WINAPI SetPixelFormat(_In_ HDC, _In_ int, _In_ const PIXELFORMATDESCRIPTOR *)
#define MB_ICONWARNING
Definition: winuser.h:780
#define PFD_DOUBLEBUFFER
Definition: wingdi.h:300
BOOL WINAPI RegisterDialogClasses(HANDLE hmodule)
Definition: cylfrac.c:213
#define GL_LIGHT0
Definition: gl.h:311
static const WCHAR szText[]
Definition: dialog.c:139
float gval
Definition: cylfrac.c:48
unsigned int BOOL
Definition: ntddk_ex.h:94
MMRESULT TimerID
Definition: cylfrac.c:37
HDC WINAPI BeginPaint(_In_ HWND, _Out_ LPPAINTSTRUCT)
static UINT WPARAM LPARAM lparam
Definition: combo.c:716
static HMODULE hmodule
Definition: rasapi.c:29
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
BOOL fullscreen
Definition: cylfrac.c:50
DWORD lvls
Definition: cylfrac.c:39
#define wfactor
Definition: cylfrac.c:28
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
MMRESULT WINAPI timeKillEvent(UINT wID)
Definition: time.c:369
void DrawCylinder(int n, float rota, float width)
Definition: cylfrac.c:81
GLAPI void GLAPIENTRY glScalef(GLfloat x, GLfloat y, GLfloat z)
#define WM_DESTROY
Definition: winuser.h:1591
float _RGB(float H, float M1, float M2)
Definition: cylfrac.c:52
#define PostMessage
Definition: winuser.h:5698
BOOL WINAPI SwapBuffers(HDC)
Definition: wingl.c:187
char TCHAR
Definition: xmlstorage.h:189
void MyPixelFormat(HDC dc)
Definition: cylfrac.c:120
POINT initpoint
Definition: cylfrac.c:33
#define IDS_TITLE
Definition: resource.h:30
HINSTANCE hInstance
Definition: cylfrac.c:42
void HLStoRGB(float H, float L, float S, float *R, float *G, float *B)
Definition: cylfrac.c:62
#define WM_SIZE
Definition: winuser.h:1593
DWORD oldticks
Definition: cylfrac.c:36
#define WM_CLOSE
Definition: winuser.h:1603
#define WINAPI
Definition: msvc.h:8
int WINAPI ChoosePixelFormat(_In_ HDC hdc, _In_ const PIXELFORMATDESCRIPTOR *ppfd)
BOOL WINAPI InvalidateRect(_In_opt_ HWND, _In_opt_ LPCRECT, _In_ BOOL)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define GL_COLOR_MATERIAL
Definition: gl.h:340
#define GL_LIGHTING
Definition: gl.h:310
#define gluCylinder
Definition: glu_mangle.h:43
void DrawScene(HWND hwnd, HDC dc, int ticks)
Definition: cylfrac.c:100
TCHAR szTitle[MAX_LOADSTRING]
Definition: magnifier.c:36
#define WM_PAINT
Definition: winuser.h:1602
static PIXELFORMATDESCRIPTOR pfd
Definition: ssstars.c:67
static const WCHAR L[]
Definition: oid.c:1250
float rval
Definition: cylfrac.c:48
GLAPI void GLAPIENTRY glEnable(GLenum cap)
void InitGL(HWND hwnd)
Definition: cylfrac.c:135
#define MessageBox
Definition: winuser.h:5688
void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
Definition: cylfrac.c:115
GLAPI void GLAPIENTRY glLightfv(GLenum light, GLenum pname, const GLfloat *params)
uint32_t DWORD_PTR
Definition: typedefs.h:63
BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
Definition: wgl.c:650
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
GLAPI void GLAPIENTRY glPushMatrix(void)
#define G(x, y, z)
Definition: md5.c:52
#define gluDeleteQuadric
Definition: glu_mangle.h:37
#define GL_DEPTH_TEST
Definition: gl.h:301
BOOL WINAPI DeleteDC(_In_ HDC)
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
GLAPI void GLAPIENTRY glPopMatrix(void)
GLAPI void GLAPIENTRY glLoadIdentity(void)
#define GL_MODELVIEW
Definition: gl.h:245
#define B(row, col)
unsigned int UINT
Definition: ndis.h:50
#define MB_OK
Definition: winuser.h:784
#define rotfactor
Definition: cylfrac.c:29
float GLfloat
Definition: gl.h:161
#define msg(x)
Definition: auth_time.c:54
int cylquality
Definition: cylfrac.c:40
#define WM_CREATE
Definition: winuser.h:1590
#define HIWORD(l)
Definition: typedefs.h:246
LRESULT WINAPI DefScreenSaverProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:95
#define GL_POSITION
Definition: gl.h:329
#define GL_LINEAR_ATTENUATION
Definition: gl.h:322
GLAPI void GLAPIENTRY glColor3f(GLfloat red, GLfloat green, GLfloat blue)
#define IDS_TEXT
Definition: resource.h:3
float bval
Definition: cylfrac.c:48
#define PFD_SUPPORT_OPENGL
Definition: wingdi.h:305
#define TIME_PERIODIC
Definition: mmsystem.h:422
static const WCHAR ca[]
Definition: main.c:457
LONG_PTR LRESULT
Definition: windef.h:209
#define PFD_DRAW_TO_WINDOW
Definition: wingdi.h:302
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
#define LoadString
Definition: winuser.h:5685
HGLRC WINAPI wglCreateContext(HDC hdc)
Definition: wgl.c:383
#define H
static UINT WPARAM wparam
Definition: combo.c:716