ReactOS 0.4.16-dev-340-g0540c21
butterflies.c
Go to the documentation of this file.
1#include <windows.h>
2#include <scrnsave.h>
3#include <tchar.h>
4#include <math.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <GL/gl.h>
8#include <GL/glu.h>
9#include <GL/glext.h>
10#include "resource.h"
11
12HINSTANCE hInstance; // Holds The Instance Of The Application
13
14GLuint texture[3]; //stores texture objects and display list
16
17LPCTSTR registryPath = _T("Software\\Microsoft\\ScreenSavers\\Butterflies");
19
20
21struct object // Create A Structure Called Object
22{
23 int tex; // Integer Used To Select Our Texture
24 float x; // X Position
25 float y; // Y Position
26 float z; // Z Position
27 float yi; // Y Increase Speed (Fall Speed)
28 float spinz; // Z Axis Spin
29 float spinzi; // Z Axis Spin Speed
30 float flap; // Flapping Triangles :)
31 float fi; // Flap Direction (Increase Value)
32};
33
34struct object obj[50];
35//object obj[50]; // Create 50 Objects Using The Object Structure
36
38{
39 dRotate = TRUE;
40}
41
44 HKEY skey;
45 DWORD valtype, valsize, val;
46
48
51 return;
52
53 valsize=sizeof(val);
54
55 result = RegQueryValueEx(skey, _T("Rotate"), 0, &valtype, (LPBYTE)&val, &valsize);
57 dRotate = val;
58 RegCloseKey(skey);
59}
60
63 HKEY skey;
64 DWORD val, disp;
65
68 return;
69
70 val = dRotate;
71 RegSetValueEx(skey, _T("Rotate"), 0, REG_DWORD, (CONST BYTE*)&val, sizeof(val));
72
73 RegCloseKey(skey);
74}
75
76void SetObject(int loop) // Sets The Initial Value Of Each Object (Random)
77{
78 obj[loop].tex=rand()%3; // Texture Can Be One Of 3 Textures
79 obj[loop].x=rand()%34-17.0f; // Random x Value From -17.0f To 17.0f
80 obj[loop].y=18.0f; // Set y Position To 18 (Off Top Of Screen)
81 obj[loop].z=-((rand()%30000/1000.0f)+10.0f); // z Is A Random Value From -10.0f To -40.0f
82 obj[loop].spinzi=(rand()%10000)/5000.0f-1.0f; // spinzi Is A Random Value From -1.0f To 1.0f
83 obj[loop].flap=0.0f; // flap Starts Off At 0.0f;
84 obj[loop].fi=0.05f+(rand()%100)/1000.0f; // fi Is A Random Value From 0.05f To 0.15f
85 obj[loop].yi=0.001f+(rand()%1000)/10000.0f; // yi Is A Random Value From 0.001f To 0.101f
86}
87
88void LoadGLTextures() // Creates Textures From Bitmaps In The Resource File
89{
90 HBITMAP hBMP; // Handle Of The Bitmap
91 BITMAP BMP; // Bitmap Structure
92 int loop;
93
94 // The ID Of The 3 Bitmap Images We Want To Load From The Resource File
95 byte Texture[]={ IDB_BUTTERFLY1, IDB_BUTTERFLY2, IDB_BUTTERFLY3 };
96
97 glGenTextures(sizeof(Texture), &texture[0]); // Generate 3 Textures (sizeof(Texture)=3 ID's)
98 for (loop=0; loop<sizeof(Texture); loop++) // Loop Through All The ID's (Bitmap Images)
99 {
101 if (hBMP) // Does The Bitmap Exist?
102 { // If So...
103 GetObject(hBMP,sizeof(BMP), &BMP); // Get The Object
104 // hBMP: Handle To Graphics Object
105 // sizeof(BMP): Size Of Buffer For Object Information
106 // Buffer For Object Information
107 glPixelStorei(GL_UNPACK_ALIGNMENT,4); // Pixel Storage Mode (Word Alignment / 4 Bytes)
108 glBindTexture(GL_TEXTURE_2D, texture[loop]); // Bind Our Texture
111
112 // Generate Mipmapped Texture (3 Bytes, Width, Height And Data From The BMP)
113 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, BMP.bmWidth, BMP.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);
114 }
115 }
116}
117
119{
120 HDC hDC = GetDC(hWnd);
121 HGLRC hRC = 0;
123 int nFormat;
124
126
128 pfd.nVersion = 1;
130 pfd.cColorBits = 24;
131 pfd.cDepthBits = 24;
132
133 nFormat = ChoosePixelFormat(hDC, &pfd);
135 SetPixelFormat(hDC, nFormat, &pfd);
136
139
140 hdcOpenGL = hDC;
141
142 return hRC;
143}
144
146{
147 int loop;
148
149 if (height==0) // Prevent A Divide By Zero By
150 {
151 height=1; // Making Height Equal One
152 }
153
154 glViewport(0,0,width,height); // Reset The Current Viewport
155
156 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
157 glLoadIdentity(); // Reset The Projection Matrix
158
159 // Calculate The Aspect Ratio Of The Window
160 gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height),1.0f, 1000.0f);
161
162 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
164 // Start Of User Initialization
165 LoadGLTextures(); // Load The Textures From Our Resource File
166
167 glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background
168 glClearDepth (1.0f); // Depth Buffer Setup
169 glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
170 glDisable(GL_DEPTH_TEST); // Disable Depth Testing
171 glShadeModel (GL_SMOOTH); // Select Smooth Shading
172 glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate
173 glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
174 glBlendFunc(GL_ONE,GL_SRC_ALPHA); // Set Blending Mode (Cheap / Quick)
176
177
178 for (loop=0; loop<50; loop++) // Loop To Initialize 50 Objects
179 {
180 SetObject(loop); // Call SetObject To Assign New Random Values
181 }
182
183}
184
186{
187 int loop;
188 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
189
190 for (loop=0; loop<50; loop++) // Loop Of 50 (Draw 50 Objects)
191 {
192 glLoadIdentity (); // Reset The Modelview Matrix
193 glBindTexture(GL_TEXTURE_2D, texture[obj[loop].tex]); // Bind Our Texture
194 glTranslatef(obj[loop].x,obj[loop].y,obj[loop].z); // Position The Object
195 glRotatef(45.0f,1.0f,0.0f,0.0f); // Rotate On The X-Axis
196 if (dRotate)
197 {
198 glRotatef((obj[loop].spinz),0.0f,0.0f,1.0f); // Spin On The Z-Axis
199 }
200 glBegin(GL_TRIANGLES); // Begin Drawing Triangles
201 // First Triangle _____
202 glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); // (2)| / (1)
203 glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f, 1.0f, obj[loop].flap); // | /
204 glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); // (3)|/
205
206 // Second Triangle
207 glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); // /|(1)
208 glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); // / |
209 glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f,-1.0f, obj[loop].flap); // (2)/____|(3)
210
211 glEnd(); // Done Drawing Triangles
212
213 obj[loop].y-=obj[loop].yi; // Move Object Down The Screen
214 obj[loop].spinz+=obj[loop].spinzi; // Increase Z Rotation By spinzi
215 obj[loop].flap+=obj[loop].fi; // Increase flap Value By fi
216
217 if (obj[loop].y<-18.0f) // Is Object Off The Screen?
218 {
219 SetObject(loop); // If So, Reassign New Values
220 }
221
222 if ((obj[loop].flap>1.0f) || (obj[loop].flap<-1.0f)) // Time To Change Flap Direction?
223 {
224 obj[loop].fi=-obj[loop].fi; // Change Direction By Making fi = -fi
225 }
226 }
227
228 Sleep(15); // Create A Short Delay (15 Milliseconds)
229
230 glFlush ();
231
232}
233
235
236 switch(msg){
238 if(((HWND)lpm == GetDlgItem(hdlg, WEBPAGE1)) || ((HWND)lpm == GetDlgItem(hdlg, WEBPAGE2)))
239 {
240 SetTextColor((HDC)wpm, RGB(0,0,255));
243 }
244 break;
245 case WM_COMMAND:
246 switch(LOWORD(wpm)){
247 case IDOK:
248 EndDialog(hdlg, LOWORD(wpm));
249 break;
250 case WEBPAGE1:
251 ShellExecute(NULL, _T("open"), _T("http://nehe.gamedev.net"), NULL, NULL, SW_SHOWNORMAL);
252 break;
253 case WEBPAGE2:
254 ShellExecute(NULL, _T("open"), _T("http://www.thaputer.com"), NULL, NULL, SW_SHOWNORMAL);
255 break;
256 }
257 }
258 return FALSE;
259}
260
263{
264 static HGLRC hRC;
265 static DWORD timer = 1;
266 HDC hDC;
267 RECT WindowRect;
268 int width;
269 int height;
270
271 switch (message)
272 {
273 case WM_CREATE:
274 ReadRegistry();
276 GetClientRect (hWnd, &WindowRect);
277 width = WindowRect.right - WindowRect.left;
278 height = WindowRect.bottom - WindowRect.top;
280 SetTimer(hWnd, timer, 5, NULL);
281 break;
282 case WM_TIMER:
283 hDC = GetDC(hWnd);
284 Display();
287 break;
288 case WM_DESTROY:
292 break;
293 }
294
296}
297
300{
301 switch (message)
302 {
303 case WM_INITDIALOG:
304 ReadRegistry();
306 return TRUE;
307 case WM_COMMAND:
308 switch (LOWORD(wParam))
309 {
310 case IDOK:
313 EndDialog(hDlg, TRUE);
314 return TRUE;
315 case IDCANCEL:
316 EndDialog(hDlg, TRUE);
317 break;
318 case IDABOUT:
320 break;
321 }
322 }
323
324 return FALSE;
325}
326
328{
329 return TRUE;
330}
331
static HDC hDC
Definition: 3dtext.c:33
static HGLRC hRC
Definition: 3dtext.c:32
#define msg(x)
Definition: auth_time.c:54
HWND hWnd
Definition: settings.c:17
#define RegCloseKey(hKey)
Definition: registry.h:49
void LoadGLTextures()
Definition: butterflies.c:88
LPCTSTR registryPath
Definition: butterflies.c:17
void SetObject(int loop)
Definition: butterflies.c:76
LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition: butterflies.c:261
HGLRC InitOGLWindow(HWND hWnd)
Definition: butterflies.c:118
void Display()
Definition: butterflies.c:185
HINSTANCE hInstance
Definition: butterflies.c:12
void SetDefaults()
Definition: butterflies.c:37
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
Definition: butterflies.c:298
void InitOpenGL(GLsizei width, GLsizei height)
Definition: butterflies.c:145
INT_PTR CALLBACK AboutProc(HWND hdlg, UINT msg, WPARAM wpm, LPARAM lpm)
Definition: butterflies.c:234
HDC hdcOpenGL
Definition: butterflies.c:15
void ReadRegistry()
Definition: butterflies.c:42
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
Definition: butterflies.c:327
void WriteRegistry()
Definition: butterflies.c:61
BOOL dRotate
Definition: butterflies.c:18
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CALLBACK
Definition: compat.h:35
@ BMP
Definition: image.c:4272
#define RGB(r, g, b)
Definition: precomp.h:71
HINSTANCE hInst
Definition: dxdiag.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLAPI void GLAPIENTRY glClearDepth(GLclampd depth)
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glTexCoord2f(GLfloat s, GLfloat t)
#define GL_NICEST
Definition: gl.h:585
#define GL_TEXTURE_MIN_FILTER
Definition: gl.h:649
GLAPI void GLAPIENTRY glDepthFunc(GLenum func)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
float GLfloat
Definition: gl.h:161
GLAPI void GLAPIENTRY glHint(GLenum target, GLenum mode)
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
#define GL_DEPTH_TEST
Definition: gl.h:301
#define GL_LINEAR
Definition: gl.h:421
#define GL_SRC_ALPHA
Definition: gl.h:378
#define GL_SMOOTH
Definition: gl.h:339
#define GL_TRIANGLES
Definition: gl.h:194
GLAPI void GLAPIENTRY glLoadIdentity(void)
unsigned int GLuint
Definition: gl.h:159
#define GL_LINEAR_MIPMAP_LINEAR
Definition: gl.h:670
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
#define GL_UNSIGNED_BYTE
Definition: gl.h:178
#define GL_PROJECTION
Definition: gl.h:246
#define GL_LEQUAL
Definition: gl.h:296
#define GL_MODELVIEW
Definition: gl.h:245
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
#define GL_UNPACK_ALIGNMENT
Definition: gl.h:632
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
GLAPI void GLAPIENTRY glBindTexture(GLenum target, GLuint texture)
#define GL_COLOR_BUFFER_BIT
Definition: gl.h:716
GLAPI void GLAPIENTRY glBegin(GLenum mode)
GLAPI void GLAPIENTRY glGenTextures(GLsizei n, GLuint *textures)
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define GL_BLEND
Definition: gl.h:371
#define GL_ONE
Definition: gl.h:375
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
GLAPI void GLAPIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glEnd(void)
int GLsizei
Definition: gl.h:160
GLAPI void GLAPIENTRY glTexParameteri(GLenum target, GLenum pname, GLint param)
#define GL_TEXTURE_MAG_FILTER
Definition: gl.h:648
GLAPI void GLAPIENTRY glDisable(GLenum cap)
GLint GLint GLsizei width
Definition: gl.h:1546
#define GL_TEXTURE_2D
Definition: gl.h:645
GLAPI void GLAPIENTRY glPixelStorei(GLenum pname, GLint param)
#define GL_PERSPECTIVE_CORRECTION_HINT
Definition: gl.h:578
#define GL_DEPTH_BUFFER_BIT
Definition: gl.h:710
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
GLAPI void GLAPIENTRY glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
GLAPI void GLAPIENTRY glFlush(void)
GLAPI void GLAPIENTRY glClear(GLbitfield mask)
GLenum GLuint texture
Definition: glext.h:6295
#define GL_BGR_EXT
Definition: glext.h:3114
GLuint GLfloat * val
Definition: glext.h:7180
GLuint64EXT * result
Definition: glext.h:11304
GLdouble GLdouble z
Definition: glext.h:5874
#define gluPerspective
Definition: glu_mangle.h:28
#define gluBuild2DMipmaps
Definition: glu_mangle.h:35
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
#define IDB_BUTTERFLY3
Definition: resource.h:9
#define WEBPAGE1
Definition: resource.h:13
#define IDD_DLG_ABOUT
Definition: resource.h:11
#define IDABOUT
Definition: resource.h:5
#define WEBPAGE2
Definition: resource.h:14
#define IDB_BUTTERFLY1
Definition: resource.h:7
#define ROTATE
Definition: resource.h:12
#define IDB_BUTTERFLY2
Definition: resource.h:8
static HBITMAP
Definition: button.c:44
static HDC
Definition: imagelist.c:88
unsigned int UINT
Definition: ndis.h:50
#define KEY_READ
Definition: nt_native.h:1023
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define KEY_WRITE
Definition: nt_native.h:1031
static HGLRC(WINAPI *pwglCreateContextAttribsARB)(HDC hDC
#define LOWORD(l)
Definition: pedump.c:82
#define CONST
Definition: pedump.c:81
long LONG
Definition: pedump.c:60
LRESULT WINAPI DefScreenSaverProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:92
#define REG_DWORD
Definition: sdbapi.c:596
#define ShellExecute
Definition: shellapi.h:694
static PIXELFORMATDESCRIPTOR pfd
Definition: ssstars.c:67
Definition: bl.h:1331
Definition: tftpd.h:60
int tex
Definition: butterflies.c:23
float y
Definition: butterflies.c:25
float flap
Definition: butterflies.c:30
float yi
Definition: butterflies.c:27
float spinz
Definition: butterflies.c:28
float spinzi
Definition: butterflies.c:29
float x
Definition: butterflies.c:24
float fi
Definition: butterflies.c:31
float z
Definition: butterflies.c:26
LONG right
Definition: windef.h:308
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
int32_t INT_PTR
Definition: typedefs.h:64
unsigned char * LPBYTE
Definition: typedefs.h:53
#define _T(x)
Definition: vfdio.h:22
BOOL WINAPI wglDeleteContext(HGLRC hglrc)
Definition: wgl.c:514
HGLRC WINAPI wglCreateContext(HDC hdc)
Definition: wgl.c:383
BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
Definition: wgl.c:650
#define ZeroMemory
Definition: winbase.h:1737
#define GetModuleHandle
Definition: winbase.h:3852
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
DWORD COLORREF
Definition: windef.h:300
#define WINAPI
Definition: msvc.h:6
int WINAPI ChoosePixelFormat(_In_ HDC hdc, _In_ const PIXELFORMATDESCRIPTOR *ppfd)
BOOL WINAPI SetPixelFormat(_In_ HDC, _In_ int, _In_ const PIXELFORMATDESCRIPTOR *)
COLORREF WINAPI SetBkColor(_In_ HDC, _In_ COLORREF)
Definition: dc.c:999
#define PFD_SUPPORT_OPENGL
Definition: wingdi.h:306
struct tagPIXELFORMATDESCRIPTOR PIXELFORMATDESCRIPTOR
int WINAPI DescribePixelFormat(_In_ HDC hdc, _In_ int iPixelFormat, _In_ UINT nBytes, _Out_writes_bytes_opt_(nBytes) LPPIXELFORMATDESCRIPTOR ppfd)
BOOL WINAPI SwapBuffers(HDC)
Definition: wingl.c:187
#define PFD_DRAW_TO_WINDOW
Definition: wingdi.h:303
COLORREF WINAPI SetTextColor(_In_ HDC, _In_ COLORREF)
Definition: text.c:918
#define GetObject
Definition: wingdi.h:4468
#define PFD_DOUBLEBUFFER
Definition: wingdi.h:301
#define HKEY_CURRENT_USER
Definition: winreg.h:11
#define RegOpenKeyEx
Definition: winreg.h:520
#define RegSetValueEx
Definition: winreg.h:533
#define RegCreateKeyEx
Definition: winreg.h:501
#define RegQueryValueEx
Definition: winreg.h:524
#define SW_SHOWNORMAL
Definition: winuser.h:773
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
DWORD WINAPI GetSysColor(_In_ int)
#define WM_CTLCOLORSTATIC
Definition: winuser.h:1775
#define IMAGE_BITMAP
Definition: winuser.h:211
BOOL WINAPI CheckDlgButton(_In_ HWND, _In_ int, _In_ UINT)
#define IDCANCEL
Definition: winuser.h:834
#define WM_CREATE
Definition: winuser.h:1611
HBRUSH WINAPI GetSysColorBrush(_In_ int)
#define LR_CREATEDIBSECTION
Definition: winuser.h:1101
#define WM_COMMAND
Definition: winuser.h:1743
#define WM_INITDIALOG
Definition: winuser.h:1742
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
#define IDOK
Definition: winuser.h:833
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
UINT WINAPI IsDlgButtonChecked(_In_ HWND, _In_ int)
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
#define WM_TIMER
Definition: winuser.h:1745
HDC WINAPI GetDC(_In_opt_ HWND)
#define LoadImage
Definition: winuser.h:5827
#define WM_DESTROY
Definition: winuser.h:1612
#define MAKEINTRESOURCE
Definition: winuser.h:591
#define BST_CHECKED
Definition: winuser.h:197
#define DialogBox
Definition: winuser.h:5773
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
#define COLOR_3DFACE
Definition: winuser.h:932
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
unsigned char BYTE
Definition: xxhash.c:193