11 HINSTANCE hInstance
; // Holds The Instance Of The Application
13 GLuint texture
[3]; //stores texture objects and display list
15 LPCTSTR registryPath
= ("Software\\Microsoft\\ScreenSavers\\Butterflies");
19 struct object
// Create A Structure Called Object
21 int tex
; // Integer Used To Select Our Texture
22 float x
; // X Position
23 float y
; // Y Position
24 float z
; // Z Position
25 float yi
; // Y Increase Speed (Fall Speed)
26 float spinz
; // Z Axis Spin
27 float spinzi
; // Z Axis Spin Speed
28 float flap
; // Flapping Triangles :)
29 float fi
; // Flap Direction (Increase Value)
32 struct object obj
[50];
33 //object obj[50]; // Create 50 Objects Using The Object Structure
43 DWORD valtype
, valsize
, val
;
47 result
= RegOpenKeyEx(HKEY_CURRENT_USER
, registryPath
, 0, KEY_READ
, &skey
);
48 if(result
!= ERROR_SUCCESS
)
53 result
= RegQueryValueEx(skey
, "Rotate", 0, &valtype
, (LPBYTE
)&val
, &valsize
);
54 if(result
== ERROR_SUCCESS
)
64 result
= RegCreateKeyEx(HKEY_CURRENT_USER
, registryPath
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &skey
, &disp
);
65 if(result
!= ERROR_SUCCESS
)
69 RegSetValueEx(skey
, "Rotate", 0, REG_DWORD
, (CONST BYTE
*)&val
, sizeof(val
));
74 void SetObject(int loop
) // Sets The Initial Value Of Each Object (Random)
76 obj
[loop
].tex
=rand()%3; // Texture Can Be One Of 3 Textures
77 obj
[loop
].x
=rand()%34-17.0f
; // Random x Value From -17.0f To 17.0f
78 obj
[loop
].y
=18.0f
; // Set y Position To 18 (Off Top Of Screen)
79 obj
[loop
].z
=-((rand()%30000/1000.0f
)+10.0f
); // z Is A Random Value From -10.0f To -40.0f
80 obj
[loop
].spinzi
=(rand()%10000)/5000.0f
-1.0f
; // spinzi Is A Random Value From -1.0f To 1.0f
81 obj
[loop
].flap
=0.0f
; // flap Starts Off At 0.0f;
82 obj
[loop
].fi
=0.05f
+(rand()%100)/1000.0f
; // fi Is A Random Value From 0.05f To 0.15f
83 obj
[loop
].yi
=0.001f
+(rand()%1000)/10000.0f
; // yi Is A Random Value From 0.001f To 0.101f
86 void LoadGLTextures() // Creates Textures From Bitmaps In The Resource File
88 HBITMAP hBMP
; // Handle Of The Bitmap
89 BITMAP BMP
; // Bitmap Structure
92 // The ID Of The 3 Bitmap Images We Want To Load From The Resource File
93 byte Texture
[]={ IDB_BUTTERFLY1
, IDB_BUTTERFLY2
, IDB_BUTTERFLY3
};
95 glGenTextures(sizeof(Texture
), &texture
[0]); // Generate 3 Textures (sizeof(Texture)=3 ID's)
96 for (loop
=0; loop
<sizeof(Texture
); loop
++) // Loop Through All The ID's (Bitmap Images)
98 hBMP
=(HBITMAP
)LoadImage(GetModuleHandle(NULL
),MAKEINTRESOURCE(Texture
[loop
]), IMAGE_BITMAP
, 0, 0, LR_CREATEDIBSECTION
);
99 if (hBMP
) // Does The Bitmap Exist?
101 GetObject(hBMP
,sizeof(BMP
), &BMP
); // Get The Object
102 // hBMP: Handle To Graphics Object
103 // sizeof(BMP): Size Of Buffer For Object Information
104 // Buffer For Object Information
105 glPixelStorei(GL_UNPACK_ALIGNMENT
,4); // Pixel Storage Mode (Word Alignment / 4 Bytes)
106 glBindTexture(GL_TEXTURE_2D
, texture
[loop
]); // Bind Our Texture
107 glTexParameteri(GL_TEXTURE_2D
,GL_TEXTURE_MAG_FILTER
,GL_LINEAR
); // Linear Filtering
108 glTexParameteri(GL_TEXTURE_2D
,GL_TEXTURE_MIN_FILTER
,GL_LINEAR_MIPMAP_LINEAR
); // Mipmap Linear Filtering
110 // Generate Mipmapped Texture (3 Bytes, Width, Height And Data From The BMP)
111 gluBuild2DMipmaps(GL_TEXTURE_2D
, 3, BMP
.bmWidth
, BMP
.bmHeight
, GL_BGR_EXT
, GL_UNSIGNED_BYTE
, BMP
.bmBits
);
116 HGLRC
InitOGLWindow(HWND hWnd
)
118 HDC hDC
= GetDC(hWnd
);
120 PIXELFORMATDESCRIPTOR pfd
;
123 ZeroMemory(&pfd
, sizeof(PIXELFORMATDESCRIPTOR
));
125 pfd
.nSize
= sizeof(PIXELFORMATDESCRIPTOR
);
127 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DRAW_TO_WINDOW
| PFD_DOUBLEBUFFER
;
131 nFormat
= ChoosePixelFormat(hDC
, &pfd
);
132 DescribePixelFormat(hDC
, nFormat
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
);
133 SetPixelFormat(hDC
, nFormat
, &pfd
);
135 hRC
= wglCreateContext(hDC
);
136 wglMakeCurrent(hDC
, hRC
);
138 ReleaseDC(hWnd
, hDC
);
143 void InitOpenGL(GLsizei width
, GLsizei height
)
147 if (height
==0) // Prevent A Divide By Zero By
149 height
=1; // Making Height Equal One
152 glViewport(0,0,width
,height
); // Reset The Current Viewport
154 glMatrixMode(GL_PROJECTION
); // Select The Projection Matrix
155 glLoadIdentity(); // Reset The Projection Matrix
157 // Calculate The Aspect Ratio Of The Window
158 gluPerspective (45.0f
, (GLfloat
)(width
)/(GLfloat
)(height
),1.0f
, 1000.0f
);
160 glMatrixMode(GL_MODELVIEW
); // Select The Modelview Matrix
162 // Start Of User Initialization
163 LoadGLTextures(); // Load The Textures From Our Resource File
165 glClearColor (0.0f
, 0.0f
, 0.0f
, 0.5f
); // Black Background
166 glClearDepth (1.0f
); // Depth Buffer Setup
167 glDepthFunc (GL_LEQUAL
); // The Type Of Depth Testing (Less Or Equal)
168 glDisable(GL_DEPTH_TEST
); // Disable Depth Testing
169 glShadeModel (GL_SMOOTH
); // Select Smooth Shading
170 glHint (GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
); // Set Perspective Calculations To Most Accurate
171 glEnable(GL_TEXTURE_2D
); // Enable Texture Mapping
172 glBlendFunc(GL_ONE
,GL_SRC_ALPHA
); // Set Blending Mode (Cheap / Quick)
176 for (loop
=0; loop
<50; loop
++) // Loop To Initialize 50 Objects
178 SetObject(loop
); // Call SetObject To Assign New Random Values
186 glClear (GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
); // Clear Screen And Depth Buffer
188 for (loop
=0; loop
<50; loop
++) // Loop Of 50 (Draw 50 Objects)
190 glLoadIdentity (); // Reset The Modelview Matrix
191 glBindTexture(GL_TEXTURE_2D
, texture
[obj
[loop
].tex
]); // Bind Our Texture
192 glTranslatef(obj
[loop
].x
,obj
[loop
].y
,obj
[loop
].z
); // Position The Object
193 glRotatef(45.0f
,1.0f
,0.0f
,0.0f
); // Rotate On The X-Axis
196 glRotatef((obj
[loop
].spinz
),0.0f
,0.0f
,1.0f
); // Spin On The Z-Axis
198 glBegin(GL_TRIANGLES
); // Begin Drawing Triangles
199 // First Triangle _____
200 glTexCoord2f(1.0f
,1.0f
); glVertex3f( 1.0f
, 1.0f
, 0.0f
); // (2)| / (1)
201 glTexCoord2f(0.0f
,1.0f
); glVertex3f(-1.0f
, 1.0f
, obj
[loop
].flap
); // | /
202 glTexCoord2f(0.0f
,0.0f
); glVertex3f(-1.0f
,-1.0f
, 0.0f
); // (3)|/
205 glTexCoord2f(1.0f
,1.0f
); glVertex3f( 1.0f
, 1.0f
, 0.0f
); // /|(1)
206 glTexCoord2f(0.0f
,0.0f
); glVertex3f(-1.0f
,-1.0f
, 0.0f
); // / |
207 glTexCoord2f(1.0f
,0.0f
); glVertex3f( 1.0f
,-1.0f
, obj
[loop
].flap
); // (2)/____|(3)
209 glEnd(); // Done Drawing Triangles
211 obj
[loop
].y
-=obj
[loop
].yi
; // Move Object Down The Screen
212 obj
[loop
].spinz
+=obj
[loop
].spinzi
; // Increase Z Rotation By spinzi
213 obj
[loop
].flap
+=obj
[loop
].fi
; // Increase flap Value By fi
215 if (obj
[loop
].y
<-18.0f
) // Is Object Off The Screen?
217 SetObject(loop
); // If So, Reassign New Values
220 if ((obj
[loop
].flap
>1.0f
) || (obj
[loop
].flap
<-1.0f
)) // Time To Change Flap Direction?
222 obj
[loop
].fi
=-obj
[loop
].fi
; // Change Direction By Making fi = -fi
226 Sleep(15); // Create A Short Delay (15 Milliseconds)
232 BOOL
AboutProc(HWND hdlg
, UINT msg
, WPARAM wpm
, LPARAM lpm
){
235 case WM_CTLCOLORSTATIC
:
236 if(((HWND
)lpm
== GetDlgItem(hdlg
, WEBPAGE1
)) || ((HWND
)lpm
== GetDlgItem(hdlg
, WEBPAGE2
)))
238 SetTextColor((HDC
)wpm
, RGB(0,0,255));
239 SetBkColor((HDC
)wpm
, (COLORREF
)GetSysColor(COLOR_3DFACE
));
240 return((int)GetSysColorBrush(COLOR_3DFACE
));
246 EndDialog(hdlg
, LOWORD(wpm
));
249 ShellExecute(NULL
, "open", "http://nehe.gamedev.net", NULL
, NULL
, SW_SHOWNORMAL
);
252 ShellExecute(NULL
, "open", "http://www.thaputer.com", NULL
, NULL
, SW_SHOWNORMAL
);
259 LRESULT WINAPI
ScreenSaverProc(HWND hWnd
, UINT message
,
260 WPARAM wParam
, LPARAM lParam
)
263 static DWORD timer
= 1;
273 hRC
= InitOGLWindow(hWnd
);
274 GetClientRect (hWnd
, &WindowRect
);
275 width
= WindowRect
.right
- WindowRect
.left
;
276 height
= WindowRect
.bottom
- WindowRect
.top
;
277 InitOpenGL(width
,height
);
278 SetTimer(hWnd
, timer
, 5, NULL
);
284 ReleaseDC(hWnd
, hDC
);
287 wglMakeCurrent(NULL
, NULL
);
288 wglDeleteContext(hRC
);
292 return DefScreenSaverProc(hWnd
, message
, wParam
, lParam
);
295 BOOL WINAPI
ScreenSaverConfigureDialog(HWND hDlg
, UINT message
,
296 WPARAM wParam
, LPARAM lParam
)
302 CheckDlgButton(hDlg
, ROTATE
, dRotate
);
305 switch (LOWORD(wParam
))
308 dRotate
= (IsDlgButtonChecked(hDlg
, ROTATE
) == BST_CHECKED
);
310 EndDialog(hDlg
, TRUE
);
313 EndDialog(hDlg
, TRUE
);
316 DialogBox(hInstance
, MAKEINTRESOURCE(IDD_DLG_ABOUT
), hDlg
, (DLGPROC
)AboutProc
);
324 BOOL WINAPI
RegisterDialogClasses(HANDLE hInst
)