2 * Copyright 2000 Jeff Molofee http://nehe.gamedev.net/ (Original code)
3 * Copyright 2006 Eric Kohl
4 * Copyright 2007 Marc Piulachs (marc.piulachs@codexchange.net) - minor modifications , converted to screensaver
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 static HGLRC hRC
; // Permanent Rendering Context
33 static HDC hDC
; // Private GDI Device Context
35 GLuint base
; // Base Display List For The Font Set
36 GLfloat rot
; // Used To Rotate The Text
37 GLfloat extentX
= 0.0f
;
38 GLfloat extentY
= 0.0f
;
40 #define APPNAME _T("3DText")
43 BOOL fullscreen
= FALSE
;
45 // Build Our Bitmap Font
46 GLvoid
BuildFont(GLvoid
)
48 // Address Buffer For Font Storage
49 GLYPHMETRICSFLOAT gmf
[256];
50 // Windows Font Handle
54 GLfloat cellOriginX
= 0.0f
;
55 GLfloat stringOriginX
;
56 GLfloat stringExtentX
= 0.0f
;
57 GLfloat stringExtentY
= 0.0f
;
59 // Storage For 256 Characters
60 base
= glGenLists(256);
62 font
= CreateFont(-12,
64 0, // Angle Of Escapement
65 0, // Orientation Angle
66 FW_BOLD
, // Font Weight
70 DEFAULT_CHARSET
, // Character Set Identifier
71 OUT_TT_PRECIS
, // Output Precision
72 CLIP_DEFAULT_PRECIS
, // Clipping Precision
73 ANTIALIASED_QUALITY
, // Output Quality
74 FF_DONTCARE
|DEFAULT_PITCH
, // Family And Pitch
75 _T("Tahoma")); // Font Name
77 // Selects The Font We Created
78 SelectObject(hDC
, font
);
80 wglUseFontOutlines(hDC
, // Select The Current DC
81 0, // Starting Character
82 255, // Number Of Display Lists To Build
83 base
, // Starting Display Lists
84 0.0f
, // Deviation From The True Outlines
85 0.2f
, // Font Thickness In The Z Direction
86 WGL_FONT_POLYGONS
, // Use Polygons, Not Lines
87 gmf
); // Address Of Buffer To Receive Data
89 // Calculate the string extent
90 for (i
= 0; i
< _tcslen(m_Text
); i
++)
94 stringOriginX
= cellOriginX
+ gmf
[c
].gmfptGlyphOrigin
.x
;
96 stringExtentX
= stringOriginX
+ gmf
[c
].gmfBlackBoxX
;
97 if (gmf
[c
].gmfBlackBoxY
> stringExtentY
)
98 stringExtentY
= gmf
[c
].gmfBlackBoxY
;
100 cellOriginX
= cellOriginX
+ gmf
[c
].gmfCellIncX
;
103 extentX
= stringExtentX
;
104 extentY
= stringExtentY
;
108 GLvoid
KillFont(GLvoid
)
110 // Delete all 256 characters
111 glDeleteLists(base
, 256);
114 // Custom GL "Print" Routine
115 GLvoid
glPrint(LPTSTR text
)
117 // If there's no text, do nothing
121 // Pushes The Display List Bits
122 glPushAttrib(GL_LIST_BIT
);
124 // Sets The Base Character to 32
127 // Draws The Display List Text
128 glCallLists(_tcslen(text
),
136 // Pops The Display List Bits
140 // Will Be Called Right After The GL Window Is Created
141 GLvoid
InitGL(GLsizei Width
, GLsizei Height
)
143 // Clear The Background Color To Black
144 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
146 // Enables Clearing Of The Depth Buffer
149 // The Type Of Depth Test To Do
150 glDepthFunc(GL_LESS
);
152 // Enables Depth Testing
153 glEnable(GL_DEPTH_TEST
);
155 // Enables Smooth Color Shading
156 glShadeModel(GL_SMOOTH
);
158 // Select The Projection Matrix
159 glMatrixMode(GL_PROJECTION
);
161 // Reset The Projection Matrix
164 // Calculate The Aspect Ratio Of The Window
165 gluPerspective(45.0f
, (GLfloat
)Width
/ (GLfloat
)Height
, 0.1f
, 100.0f
);
167 // Select The Modelview Matrix
168 glMatrixMode(GL_MODELVIEW
);
173 // Enable Default Light (Quick And Dirty)
177 glEnable(GL_LIGHTING
);
179 // Enable Coloring Of Material
180 glEnable(GL_COLOR_MATERIAL
);
183 // Handles Window Resizing
184 GLvoid
ReSizeGLScene(GLsizei Width
, GLsizei Height
)
186 // Is Window Too Small (Divide By Zero Error)
189 // If So Make It One Pixel Tall
193 // Reset The Current Viewport And Perspective Transformation
194 glViewport(0, 0, Width
, Height
);
196 // Select The Projection Matrix
197 glMatrixMode(GL_PROJECTION
);
199 // Reset The Projection Matrix
202 // Calculate The Aspect Ratio Of The Window
203 gluPerspective(45.0f
, (GLfloat
)Width
/ (GLfloat
)Height
, 0.1f
, 100.0f
);
205 // Select The Modelview Matrix
206 glMatrixMode(GL_MODELVIEW
);
210 GLvoid
DrawGLScene(GLvoid
)
212 // Save ticks count of previous frame here
213 static DWORD dwTicks
= 0;
215 // Clear The Screen And The Depth Buffer
216 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
221 // Move One Unit Into The Screen
222 glTranslatef(0.0f
, 0.0f
, -10.0f
);
224 // Rotate On The X Axis
225 glRotatef(rot
, 1.0f
, 0.0f
, 0.0f
);
227 // Rotate On The Y Axis
228 glRotatef(rot
* 1.2f
, 0.0f
, 1.0f
, 0.0f
);
230 // Rotate On The Z Axis
231 glRotatef(rot
* 1.4f
, 0.0f
, 0.0f
, 1.0f
);
233 // Move to the Left and Down before drawing
234 glTranslatef(-(extentX
/ 2.0f
),
238 // Pulsing Colors Based On The Rotation
239 glColor3f((1.0f
* (GLfloat
)(cos(rot
/ 20.0f
))),
240 (1.0f
* (GLfloat
)(sin(rot
/ 25.0f
))),
241 (1.0f
- 0.5f
* (GLfloat
)(cos(rot
/ 17.0f
))));
243 // Print GL Text To The Screen
246 // Make The Text Blue
247 glColor3f(0.0f
, 0.0f
, 1.0f
);
249 // Increase The Rotation Variable
251 rot
+= (GetTickCount() - dwTicks
) / 20.0f
;
252 dwTicks
= GetTickCount();
256 ScreenSaverProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
258 RECT Screen
; // Used Later On To Get The Size Of The Window
259 GLuint PixelFormat
; // Pixel Format Storage
260 static PIXELFORMATDESCRIPTOR pfd
= // Pixel Format Descriptor
262 sizeof(PIXELFORMATDESCRIPTOR
), // Size Of This Pixel Format Descriptor
263 1, // Version Number (?)
264 PFD_DRAW_TO_WINDOW
| // Format Must Support Window
265 PFD_SUPPORT_OPENGL
| // Format Must Support OpenGL
266 PFD_DOUBLEBUFFER
, // Must Support Double Buffering
267 PFD_TYPE_RGBA
, // Request An RGBA Format
268 16, // Select A 16Bit Color Depth
269 0, 0, 0, 0, 0, 0, // Color Bits Ignored (?)
270 0, // No Alpha Buffer
271 0, // Shift Bit Ignored (?)
272 0, // No Accumulation Buffer
273 0, 0, 0, 0, // Accumulation Bits Ignored (?)
274 16, // 16Bit Z-Buffer (Depth Buffer)
275 0, // No Stencil Buffer
276 0, // No Auxiliary Buffer (?)
277 PFD_MAIN_PLANE
, // Main Drawing Layer
279 0, 0, 0 // Layer Masks Ignored (?)
287 // Gets A Device Context For The Window
290 // Finds The Closest Match To The Pixel Format We Set Above
291 PixelFormat
= ChoosePixelFormat(hDC
, &pfd
);
293 // No Matching Pixel Format?
296 MessageBox(0, _TEXT("Can't Find A Suitable PixelFormat."), _TEXT("Error"),MB_OK
| MB_ICONERROR
);
298 // This Sends A 'Message' Telling The Program To Quit
303 // Can We Set The Pixel Mode?
304 if (!SetPixelFormat(hDC
, PixelFormat
, &pfd
))
306 MessageBox(0, _TEXT("Can't Set The PixelFormat."), _TEXT("Error"), MB_OK
| MB_ICONERROR
);
308 // This Sends A 'Message' Telling The Program To Quit
313 // Grab A Rendering Context
314 hRC
= wglCreateContext(hDC
);
319 MessageBox(0, _TEXT("Can't Create A GL Rendering Context."), _TEXT("Error"), MB_OK
| MB_ICONERROR
);
321 // This Sends A 'Message' Telling The Program To Quit
326 // Can We Make The RC Active?
327 if (!wglMakeCurrent(hDC
, hRC
))
329 MessageBox(0, _TEXT("Can't Activate GLRC."), _TEXT("Error"), MB_OK
| MB_ICONERROR
);
331 // This Sends A 'Message' Telling The Program To Quit
336 // Grab Screen Info For The Current Window
337 GetClientRect(hWnd
, &Screen
);
339 // Initialize The GL Screen Using Screen Info
340 InitGL(Screen
.right
, Screen
.bottom
);
344 // Disable Fullscreen Mode
345 ChangeDisplaySettings(NULL
, 0);
347 // Deletes The Font Display List
350 // Make The DC Current
351 wglMakeCurrent(hDC
, NULL
);
354 wglDeleteContext(hRC
);
357 ReleaseDC(hWnd
, hDC
);
365 case WM_SIZE
: // Resizing The Screen
366 // Resize To The New Window Size
367 ReSizeGLScene(LOWORD(lParam
), HIWORD(lParam
));
371 // Pass Windows Messages to the default screensaver window procedure
372 return DefScreenSaverProc(hWnd
, message
, wParam
, lParam
);
379 // Dialogbox procedure for Configuration window
381 BOOL CALLBACK
ScreenSaverConfigureDialog(HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
387 SetDlgItemText(hDlg
, IDC_MESSAGE_TEXT
, m_Text
);
391 switch (LOWORD(wParam
))
394 GetDlgItemText(hDlg
, IDC_MESSAGE_TEXT
, m_Text
, MAX_PATH
);
400 EndDialog(hDlg
, IDCANCEL
);
416 BOOL WINAPI
RegisterDialogClasses(HANDLE hInst
)