10 /* missing declare from gl.h */
11 #define GL_BGR_EXT 0x80E0
13 HINSTANCE hInstance
; // Holds The Instance Of The Application
15 GLuint texture
[3]; //stores texture objects and display list
17 LPCTSTR registryPath
= ("Software\\Microsoft\\ScreenSavers\\Butterflies");
21 struct object
// Create A Structure Called Object
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)
34 struct object obj
[50];
35 //object obj[50]; // Create 50 Objects Using The Object Structure
45 DWORD valtype
, valsize
, val
;
49 result
= RegOpenKeyEx(HKEY_CURRENT_USER
, registryPath
, 0, KEY_READ
, &skey
);
50 if(result
!= ERROR_SUCCESS
)
55 result
= RegQueryValueEx(skey
, "Rotate", 0, &valtype
, (LPBYTE
)&val
, &valsize
);
56 if(result
== ERROR_SUCCESS
)
66 result
= RegCreateKeyEx(HKEY_CURRENT_USER
, registryPath
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &skey
, &disp
);
67 if(result
!= ERROR_SUCCESS
)
71 RegSetValueEx(skey
, "Rotate", 0, REG_DWORD
, (CONST BYTE
*)&val
, sizeof(val
));
76 void SetObject(int loop
) // Sets The Initial Value Of Each Object (Random)
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
88 void LoadGLTextures() // Creates Textures From Bitmaps In The Resource File
90 HBITMAP hBMP
; // Handle Of The Bitmap
91 BITMAP BMP
; // Bitmap Structure
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
};
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)
100 hBMP
=(HBITMAP
)LoadImage(GetModuleHandle(NULL
),MAKEINTRESOURCE(Texture
[loop
]), IMAGE_BITMAP
, 0, 0, LR_CREATEDIBSECTION
);
101 if (hBMP
) // Does The Bitmap Exist?
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
109 glTexParameteri(GL_TEXTURE_2D
,GL_TEXTURE_MAG_FILTER
,GL_LINEAR
); // Linear Filtering
110 glTexParameteri(GL_TEXTURE_2D
,GL_TEXTURE_MIN_FILTER
,GL_LINEAR_MIPMAP_LINEAR
); // Mipmap Linear Filtering
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
);
118 HGLRC
InitOGLWindow(HWND hWnd
)
120 HDC hDC
= GetDC(hWnd
);
122 PIXELFORMATDESCRIPTOR pfd
;
125 ZeroMemory(&pfd
, sizeof(PIXELFORMATDESCRIPTOR
));
127 pfd
.nSize
= sizeof(PIXELFORMATDESCRIPTOR
);
129 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DRAW_TO_WINDOW
| PFD_DOUBLEBUFFER
;
133 nFormat
= ChoosePixelFormat(hDC
, &pfd
);
134 DescribePixelFormat(hDC
, nFormat
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
);
135 SetPixelFormat(hDC
, nFormat
, &pfd
);
137 hRC
= wglCreateContext(hDC
);
138 wglMakeCurrent(hDC
, hRC
);
140 ReleaseDC(hWnd
, hDC
);
145 void InitOpenGL(GLsizei width
, GLsizei height
)
149 if (height
==0) // Prevent A Divide By Zero By
151 height
=1; // Making Height Equal One
154 glViewport(0,0,width
,height
); // Reset The Current Viewport
156 glMatrixMode(GL_PROJECTION
); // Select The Projection Matrix
157 glLoadIdentity(); // Reset The Projection Matrix
159 // Calculate The Aspect Ratio Of The Window
160 gluPerspective (45.0f
, (GLfloat
)(width
)/(GLfloat
)(height
),1.0f
, 1000.0f
);
162 glMatrixMode(GL_MODELVIEW
); // Select The Modelview Matrix
164 // Start Of User Initialization
165 LoadGLTextures(); // Load The Textures From Our Resource File
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)
178 for (loop
=0; loop
<50; loop
++) // Loop To Initialize 50 Objects
180 SetObject(loop
); // Call SetObject To Assign New Random Values
188 glClear (GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
); // Clear Screen And Depth Buffer
190 for (loop
=0; loop
<50; loop
++) // Loop Of 50 (Draw 50 Objects)
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
198 glRotatef((obj
[loop
].spinz
),0.0f
,0.0f
,1.0f
); // Spin On The Z-Axis
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)|/
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)
211 glEnd(); // Done Drawing Triangles
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
217 if (obj
[loop
].y
<-18.0f
) // Is Object Off The Screen?
219 SetObject(loop
); // If So, Reassign New Values
222 if ((obj
[loop
].flap
>1.0f
) || (obj
[loop
].flap
<-1.0f
)) // Time To Change Flap Direction?
224 obj
[loop
].fi
=-obj
[loop
].fi
; // Change Direction By Making fi = -fi
228 Sleep(15); // Create A Short Delay (15 Milliseconds)
234 BOOL
AboutProc(HWND hdlg
, UINT msg
, WPARAM wpm
, LPARAM lpm
){
237 case WM_CTLCOLORSTATIC
:
238 if(((HWND
)lpm
== GetDlgItem(hdlg
, WEBPAGE1
)) || ((HWND
)lpm
== GetDlgItem(hdlg
, WEBPAGE2
)))
240 SetTextColor((HDC
)wpm
, RGB(0,0,255));
241 SetBkColor((HDC
)wpm
, (COLORREF
)GetSysColor(COLOR_3DFACE
));
242 return((int)GetSysColorBrush(COLOR_3DFACE
));
248 EndDialog(hdlg
, LOWORD(wpm
));
251 ShellExecute(NULL
, "open", "http://nehe.gamedev.net", NULL
, NULL
, SW_SHOWNORMAL
);
254 ShellExecute(NULL
, "open", "http://www.thaputer.com", NULL
, NULL
, SW_SHOWNORMAL
);
261 LRESULT WINAPI
ScreenSaverProc(HWND hWnd
, UINT message
,
262 WPARAM wParam
, LPARAM lParam
)
265 static DWORD timer
= 1;
275 hRC
= InitOGLWindow(hWnd
);
276 GetClientRect (hWnd
, &WindowRect
);
277 width
= WindowRect
.right
- WindowRect
.left
;
278 height
= WindowRect
.bottom
- WindowRect
.top
;
279 InitOpenGL(width
,height
);
280 SetTimer(hWnd
, timer
, 5, NULL
);
286 ReleaseDC(hWnd
, hDC
);
289 wglMakeCurrent(NULL
, NULL
);
290 wglDeleteContext(hRC
);
294 return DefScreenSaverProc(hWnd
, message
, wParam
, lParam
);
297 BOOL WINAPI
ScreenSaverConfigureDialog(HWND hDlg
, UINT message
,
298 WPARAM wParam
, LPARAM lParam
)
304 CheckDlgButton(hDlg
, ROTATE
, dRotate
);
307 switch (LOWORD(wParam
))
310 dRotate
= (IsDlgButtonChecked(hDlg
, ROTATE
) == BST_CHECKED
);
312 EndDialog(hDlg
, TRUE
);
315 EndDialog(hDlg
, TRUE
);
318 DialogBox(hInstance
, MAKEINTRESOURCE(IDD_DLG_ABOUT
), hDlg
, (DLGPROC
)AboutProc
);
326 BOOL WINAPI
RegisterDialogClasses(HANDLE hInst
)