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.
31 static HGLRC hRC
; // Permanent Rendering Context
32 static HDC hDC
; // Private GDI Device Context
34 GLuint base
; // Base Display List For The Font Set
35 GLfloat rot
; // Used To Rotate The Text
36 GLfloat extentX
= 0.0f
;
37 GLfloat extentY
= 0.0f
;
39 #define APPNAME _T("3DText")
42 BOOL fullscreen
= FALSE
;
44 // Build Our Bitmap Font
45 GLvoid
BuildFont(GLvoid
)
47 // Address Buffer For Font Storage
48 GLYPHMETRICSFLOAT gmf
[256];
49 // Windows Font Handle
53 GLfloat cellOriginX
= 0.0f
;
54 GLfloat stringOriginX
;
55 GLfloat stringExtentX
= 0.0f
;
56 GLfloat stringExtentY
= 0.0f
;
58 // Storage For 256 Characters
59 base
= glGenLists(256);
61 font
= CreateFont(-12,
63 0, // Angle Of Escapement
64 0, // Orientation Angle
65 FW_BOLD
, // Font Weight
69 DEFAULT_CHARSET
, // Character Set Identifier
70 OUT_TT_PRECIS
, // Output Precision
71 CLIP_DEFAULT_PRECIS
, // Clipping Precision
72 ANTIALIASED_QUALITY
, // Output Quality
73 FF_DONTCARE
|DEFAULT_PITCH
, // Family And Pitch
74 _T("Tahoma")); // Font Name
76 // Selects The Font We Created
77 SelectObject(hDC
, font
);
79 wglUseFontOutlines(hDC
, // Select The Current DC
80 0, // Starting Character
81 255, // Number Of Display Lists To Build
82 base
, // Starting Display Lists
83 0.0f
, // Deviation From The True Outlines
84 0.2f
, // Font Thickness In The Z Direction
85 WGL_FONT_POLYGONS
, // Use Polygons, Not Lines
86 gmf
); // Address Of Buffer To Recieve Data
88 // Calculate the string extent
89 for (i
= 0; i
< _tcslen(m_Text
); i
++)
93 stringOriginX
= cellOriginX
+ gmf
[c
].gmfptGlyphOrigin
.x
;
95 stringExtentX
= stringOriginX
+ gmf
[c
].gmfBlackBoxX
;
96 if (gmf
[c
].gmfBlackBoxY
> stringExtentY
)
97 stringExtentY
= gmf
[c
].gmfBlackBoxY
;
99 cellOriginX
= cellOriginX
+ gmf
[c
].gmfCellIncX
;
102 extentX
= stringExtentX
;
103 extentY
= stringExtentY
;
107 GLvoid
KillFont(GLvoid
)
109 // Delete all 256 characters
110 glDeleteLists(base
, 256);
113 // Custom GL "Print" Routine
114 GLvoid
glPrint(LPTSTR text
)
116 // If there's no text, do nothing
120 // Pushes The Display List Bits
121 glPushAttrib(GL_LIST_BIT
);
123 // Sets The Base Character to 32
126 // Draws The Display List Text
127 glCallLists(_tcslen(text
),
135 // Pops The Display List Bits
139 // Will Be Called Right After The GL Window Is Created
140 GLvoid
InitGL(GLsizei Width
, GLsizei Height
)
142 // Clear The Background Color To Black
143 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
145 // Enables Clearing Of The Depth Buffer
148 // The Type Of Depth Test To Do
149 glDepthFunc(GL_LESS
);
151 // Enables Depth Testing
152 glEnable(GL_DEPTH_TEST
);
154 // Enables Smooth Color Shading
155 glShadeModel(GL_SMOOTH
);
157 // Select The Projection Matrix
158 glMatrixMode(GL_PROJECTION
);
160 // Reset The Projection Matrix
163 // Calculate The Aspect Ratio Of The Window
164 gluPerspective(45.0f
, (GLfloat
)Width
/ (GLfloat
)Height
, 0.1f
, 100.0f
);
166 // Select The Modelview Matrix
167 glMatrixMode(GL_MODELVIEW
);
172 // Enable Default Light (Quick And Dirty)
176 glEnable(GL_LIGHTING
);
178 // Enable Coloring Of Material
179 glEnable(GL_COLOR_MATERIAL
);
182 // Handles Window Resizing
183 GLvoid
ReSizeGLScene(GLsizei Width
, GLsizei Height
)
185 // Is Window Too Small (Divide By Zero Error)
188 // If So Make It One Pixel Tall
192 // Reset The Current Viewport And Perspective Transformation
193 glViewport(0, 0, Width
, Height
);
195 // Select The Projection Matrix
196 glMatrixMode(GL_PROJECTION
);
198 // Reset The Projection Matrix
201 // Calculate The Aspect Ratio Of The Window
202 gluPerspective(45.0f
, (GLfloat
)Width
/ (GLfloat
)Height
, 0.1f
, 100.0f
);
204 // Select The Modelview Matrix
205 glMatrixMode(GL_MODELVIEW
);
209 GLvoid
DrawGLScene(GLvoid
)
211 // Clear The Screen And The Depth Buffer
212 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
217 // Move One Unit Into The Screen
218 glTranslatef(0.0f
, 0.0f
, -10.0f
);
220 // Rotate On The X Axis
221 glRotatef(rot
, 1.0f
, 0.0f
, 0.0f
);
223 // Rotate On The Y Axis
224 glRotatef(rot
* 1.2f
, 0.0f
, 1.0f
, 0.0f
);
226 // Rotate On The Z Axis
227 glRotatef(rot
* 1.4f
, 0.0f
, 0.0f
, 1.0f
);
229 // Move to the Left and Down before drawing
230 glTranslatef(-(extentX
/ 2.0f
),
234 // Pulsing Colors Based On The Rotation
235 glColor3f((1.0f
* (GLfloat
)(cos(rot
/ 20.0f
))),
236 (1.0f
* (GLfloat
)(sin(rot
/ 25.0f
))),
237 (1.0f
- 0.5f
* (GLfloat
)(cos(rot
/ 17.0f
))));
239 // Print GL Text To The Screen
242 // Make The Text Blue
243 glColor3f(0.0f
, 0.0f
, 1.0f
);
245 // Increase The Rotation Variable
250 ScreenSaverProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
252 RECT Screen
; // Used Later On To Get The Size Of The Window
253 GLuint PixelFormat
; // Pixel Format Storage
254 static PIXELFORMATDESCRIPTOR pfd
= // Pixel Format Descriptor
256 sizeof(PIXELFORMATDESCRIPTOR
), // Size Of This Pixel Format Descriptor
257 1, // Version Number (?)
258 PFD_DRAW_TO_WINDOW
| // Format Must Support Window
259 PFD_SUPPORT_OPENGL
| // Format Must Support OpenGL
260 PFD_DOUBLEBUFFER
, // Must Support Double Buffering
261 PFD_TYPE_RGBA
, // Request An RGBA Format
262 16, // Select A 16Bit Color Depth
263 0, 0, 0, 0, 0, 0, // Color Bits Ignored (?)
264 0, // No Alpha Buffer
265 0, // Shift Bit Ignored (?)
266 0, // No Accumulation Buffer
267 0, 0, 0, 0, // Accumulation Bits Ignored (?)
268 16, // 16Bit Z-Buffer (Depth Buffer)
269 0, // No Stencil Buffer
270 0, // No Auxiliary Buffer (?)
271 PFD_MAIN_PLANE
, // Main Drawing Layer
273 0, 0, 0 // Layer Masks Ignored (?)
281 // Gets A Device Context For The Window
284 // Finds The Closest Match To The Pixel Format We Set Above
285 PixelFormat
= ChoosePixelFormat(hDC
, &pfd
);
287 // No Matching Pixel Format?
290 MessageBox(0, _TEXT("Can't Find A Suitable PixelFormat."), _TEXT("Error"),MB_OK
| MB_ICONERROR
);
292 // This Sends A 'Message' Telling The Program To Quit
297 // Can We Set The Pixel Mode?
298 if (!SetPixelFormat(hDC
, PixelFormat
, &pfd
))
300 MessageBox(0, _TEXT("Can't Set The PixelFormat."), _TEXT("Error"), MB_OK
| MB_ICONERROR
);
302 // This Sends A 'Message' Telling The Program To Quit
307 // Grab A Rendering Context
308 hRC
= wglCreateContext(hDC
);
313 MessageBox(0, _TEXT("Can't Create A GL Rendering Context."), _TEXT("Error"), MB_OK
| MB_ICONERROR
);
315 // This Sends A 'Message' Telling The Program To Quit
320 // Can We Make The RC Active?
321 if (!wglMakeCurrent(hDC
, hRC
))
323 MessageBox(0, _TEXT("Can't Activate GLRC."), _TEXT("Error"), MB_OK
| MB_ICONERROR
);
325 // This Sends A 'Message' Telling The Program To Quit
330 // Grab Screen Info For The Current Window
331 GetClientRect(hWnd
, &Screen
);
333 // Initialize The GL Screen Using Screen Info
334 InitGL(Screen
.right
, Screen
.bottom
);
338 // Disable Fullscreen Mode
339 ChangeDisplaySettings(NULL
, 0);
341 // Deletes The Font Display List
344 // Make The DC Current
345 wglMakeCurrent(hDC
, NULL
);
348 wglDeleteContext(hRC
);
351 ReleaseDC(hWnd
, hDC
);
359 case WM_SIZE
: // Resizing The Screen
360 // Resize To The New Window Size
361 ReSizeGLScene(LOWORD(lParam
), HIWORD(lParam
));
365 // Pass Windows Messages to the default screensaver window procedure
366 return DefScreenSaverProc(hWnd
, message
, wParam
, lParam
);
373 // Dialogbox procedure for Configuration window
375 BOOL CALLBACK
ScreenSaverConfigureDialog(HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
381 SetDlgItemText(hDlg
, IDC_MESSAGE_TEXT
, m_Text
);
385 switch (LOWORD(wParam
))
388 GetDlgItemText(hDlg
, IDC_MESSAGE_TEXT
, m_Text
, MAX_PATH
);
394 EndDialog(hDlg
, IDCANCEL
);
410 BOOL WINAPI
RegisterDialogClasses(HANDLE hInst
)