ReactOS  0.4.12-dev-57-g7050ac4
3dtext.c
Go to the documentation of this file.
1 /*
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
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "3dtext.h"
22 
23 #include <winbase.h>
24 #include <wingdi.h>
25 #include <winuser.h>
26 #include <scrnsave.h>
27 #include <math.h>
28 #include <GL/glu.h>
29 
30 #include "resource.h"
31 
32 static HGLRC hRC; // Permanent Rendering Context
33 static HDC hDC; // Private GDI Device Context
34 
35 GLuint base; // Base Display List For The Font Set
36 GLfloat rot; // Used To Rotate The Text
39 
40 #define APPNAME _T("3DText")
41 
44 
45 // Build Our Bitmap Font
47 {
48  // Address Buffer For Font Storage
49  GLYPHMETRICSFLOAT gmf[256];
50  // Windows Font Handle
51  HFONT font;
52  size_t i;
53  TCHAR c;
54  GLfloat cellOriginX = 0.0f;
55  GLfloat stringOriginX;
56  GLfloat stringExtentX = 0.0f;
57  GLfloat stringExtentY = 0.0f;
58 
59  // Storage For 256 Characters
60  base = glGenLists(256);
61 
62  font = CreateFont(-12,
63  0, // Width Of Font
64  0, // Angle Of Escapement
65  0, // Orientation Angle
66  FW_BOLD, // Font Weight
67  FALSE, // Italic
68  FALSE, // Underline
69  FALSE, // Strikeout
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
76 
77  // Selects The Font We Created
78  SelectObject(hDC, font);
79 
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
88 
89  // Calculate the string extent
90  for (i = 0; i < _tcslen(m_Text); i++)
91  {
92  c = m_Text[i];
93 
94  stringOriginX = cellOriginX + gmf[c].gmfptGlyphOrigin.x;
95 
96  stringExtentX = stringOriginX + gmf[c].gmfBlackBoxX;
97  if (gmf[c].gmfBlackBoxY > stringExtentY)
98  stringExtentY = gmf[c].gmfBlackBoxY;
99 
100  cellOriginX = cellOriginX + gmf[c].gmfCellIncX;
101  }
102 
103  extentX = stringExtentX;
104  extentY = stringExtentY;
105 }
106 
107 // Delete The Font
109 {
110  // Delete all 256 characters
111  glDeleteLists(base, 256);
112 }
113 
114 // Custom GL "Print" Routine
116 {
117  // If there's no text, do nothing
118  if (text == NULL)
119  return;
120 
121  // Pushes The Display List Bits
123 
124  // Sets The Base Character to 32
125  glListBase(base);
126 
127  // Draws The Display List Text
128  glCallLists(_tcslen(text),
129 #ifdef UNICODE
131 #else
133 #endif
134  text);
135 
136  // Pops The Display List Bits
137  glPopAttrib();
138 }
139 
140 // Will Be Called Right After The GL Window Is Created
141 GLvoid InitGL(GLsizei Width, GLsizei Height)
142 {
143  // Clear The Background Color To Black
144  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
145 
146  // Enables Clearing Of The Depth Buffer
147  glClearDepth(1.0);
148 
149  // The Type Of Depth Test To Do
151 
152  // Enables Depth Testing
154 
155  // Enables Smooth Color Shading
157 
158  // Select The Projection Matrix
160 
161  // Reset The Projection Matrix
162  glLoadIdentity();
163 
164  // Calculate The Aspect Ratio Of The Window
165  gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);
166 
167  // Select The Modelview Matrix
169 
170  // Build The Font
171  BuildFont();
172 
173  // Enable Default Light (Quick And Dirty)
175 
176  // Enable Lighting
178 
179  // Enable Coloring Of Material
181 }
182 
183 // Handles Window Resizing
185 {
186  // Is Window Too Small (Divide By Zero Error)
187  if (Height == 0)
188  {
189  // If So Make It One Pixel Tall
190  Height = 1;
191  }
192 
193  // Reset The Current Viewport And Perspective Transformation
194  glViewport(0, 0, Width, Height);
195 
196  // Select The Projection Matrix
198 
199  // Reset The Projection Matrix
200  glLoadIdentity();
201 
202  // Calculate The Aspect Ratio Of The Window
203  gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);
204 
205  // Select The Modelview Matrix
207 }
208 
209 // Handles Rendering
211 {
212  // Save ticks count of previous frame here
213  static DWORD dwTicks = 0;
214 
215  // Clear The Screen And The Depth Buffer
217 
218  // Reset The View
219  glLoadIdentity();
220 
221  // Move One Unit Into The Screen
222  glTranslatef(0.0f, 0.0f, -10.0f);
223 
224  // Rotate On The X Axis
225  glRotatef(rot, 1.0f, 0.0f, 0.0f);
226 
227  // Rotate On The Y Axis
228  glRotatef(rot * 1.2f, 0.0f, 1.0f, 0.0f);
229 
230  // Rotate On The Z Axis
231  glRotatef(rot * 1.4f, 0.0f, 0.0f, 1.0f);
232 
233  // Move to the Left and Down before drawing
234  glTranslatef(-(extentX / 2.0f),
235  -(extentY / 2.0f),
236  0.0f);
237 
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))));
242 
243  // Print GL Text To The Screen
244  glPrint(m_Text);
245 
246  // Make The Text Blue
247  glColor3f(0.0f, 0.0f, 1.0f);
248 
249  // Increase The Rotation Variable
250  if(dwTicks)
251  rot += (GetTickCount() - dwTicks) / 20.0f;
252  dwTicks = GetTickCount();
253 }
254 
257 {
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
261  {
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
278  0, // Reserved (?)
279  0, 0, 0 // Layer Masks Ignored (?)
280  };
281 
282  switch (message)
283  {
284  case WM_CREATE:
285  LoadSettings();
286 
287  // Gets A Device Context For The Window
288  hDC = GetDC(hWnd);
289 
290  // Finds The Closest Match To The Pixel Format We Set Above
291  PixelFormat = ChoosePixelFormat(hDC, &pfd);
292 
293  // No Matching Pixel Format?
294  if (!PixelFormat)
295  {
296  MessageBox(0, _TEXT("Can't Find A Suitable PixelFormat."), _TEXT("Error"),MB_OK | MB_ICONERROR);
297 
298  // This Sends A 'Message' Telling The Program To Quit
299  PostQuitMessage(0);
300  break;
301  }
302 
303  // Can We Set The Pixel Mode?
304  if (!SetPixelFormat(hDC, PixelFormat, &pfd))
305  {
306  MessageBox(0, _TEXT("Can't Set The PixelFormat."), _TEXT("Error"), MB_OK | MB_ICONERROR);
307 
308  // This Sends A 'Message' Telling The Program To Quit
309  PostQuitMessage(0);
310  break;
311  }
312 
313  // Grab A Rendering Context
315 
316  // Did We Get One?
317  if (!hRC)
318  {
319  MessageBox(0, _TEXT("Can't Create A GL Rendering Context."), _TEXT("Error"), MB_OK | MB_ICONERROR);
320 
321  // This Sends A 'Message' Telling The Program To Quit
322  PostQuitMessage(0);
323  break;
324  }
325 
326  // Can We Make The RC Active?
327  if (!wglMakeCurrent(hDC, hRC))
328  {
329  MessageBox(0, _TEXT("Can't Activate GLRC."), _TEXT("Error"), MB_OK | MB_ICONERROR);
330 
331  // This Sends A 'Message' Telling The Program To Quit
332  PostQuitMessage(0);
333  break;
334  }
335 
336  // Grab Screen Info For The Current Window
337  GetClientRect(hWnd, &Screen);
338 
339  // Initialize The GL Screen Using Screen Info
340  InitGL(Screen.right, Screen.bottom);
341  break;
342 
343  case WM_DESTROY:
344  // Disable Fullscreen Mode
345  ChangeDisplaySettings(NULL, 0);
346 
347  // Deletes The Font Display List
348  KillFont();
349 
350  // Make The DC Current
352 
353  // Kill The RC
355 
356  // Free The DC
357  ReleaseDC(hWnd, hDC);
358  break;
359 
360  case WM_PAINT:
361  DrawGLScene();
362  SwapBuffers(hDC);
363  break;
364 
365  case WM_SIZE: // Resizing The Screen
366  // Resize To The New Window Size
367  ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));
368  break;
369 
370  default:
371  // Pass Windows Messages to the default screensaver window procedure
372  return DefScreenSaverProc(hWnd, message, wParam, lParam);
373  }
374 
375  return 0;
376 }
377 
378 //
379 // Dialogbox procedure for Configuration window
380 //
382 {
383  switch (uMsg)
384  {
385  case WM_INITDIALOG:
386  LoadSettings();
388  return TRUE;
389 
390  case WM_COMMAND:
391  switch (LOWORD(wParam))
392  {
393  case IDOK:
395  SaveSettings();
396 
397  /* Fall through */
398 
399  case IDCANCEL:
400  EndDialog(hDlg, IDCANCEL);
401  break;
402  }
403  return FALSE;
404 
405  case WM_CLOSE:
406  EndDialog(hDlg, 0);
407  break;
408 
409  default:
410  return FALSE;
411  }
412 
413  return TRUE;
414 }
415 
417 {
418  return TRUE;
419 }
static HGLRC hRC
Definition: 3dtext.c:32
#define GL_UNSIGNED_SHORT
Definition: gl.h:180
#define HDC
Definition: msvc.h:22
#define GL_COLOR_BUFFER_BIT
Definition: gl.h:716
GLAPI void GLAPIENTRY glClear(GLbitfield mask)
static HGLRC(WINAPI *pwglCreateContextAttribsARB)(HDC hDC
wchar_t UNICODE
Definition: ms-dtyp.idl:111
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
GLvoid DrawGLScene(GLvoid)
Definition: 3dtext.c:210
#define ANTIALIASED_QUALITY
Definition: wingdi.h:438
Definition: tftpd.h:59
#define TRUE
Definition: types.h:120
#define IDOK
Definition: winuser.h:824
BOOL WINAPI wglDeleteContext(HGLRC hglrc)
Definition: wgl.c:514
FLOAT x
Definition: wingdi.h:2699
#define FF_DONTCARE
Definition: wingdi.h:446
FLOAT gmfBlackBoxY
Definition: wingdi.h:2704
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
GLAPI GLuint GLAPIENTRY glGenLists(GLsizei range)
#define PFD_MAIN_PLANE
Definition: wingdi.h:297
HDC WINAPI GetDC(_In_opt_ HWND)
Definition: mk_font.cpp:20
const WCHAR * text
Definition: package.c:1827
GLvoid ReSizeGLScene(GLsizei Width, GLsizei Height)
Definition: 3dtext.c:184
#define GL_DEPTH_BUFFER_BIT
Definition: gl.h:710
BOOL CALLBACK ScreenSaverConfigureDialog(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: 3dtext.c:381
#define GL_UNSIGNED_BYTE
Definition: gl.h:178
#define CALLBACK
Definition: compat.h:27
GLAPI void GLAPIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
HWND hWnd
Definition: settings.c:17
GLAPI void GLAPIENTRY glDeleteLists(GLuint list, GLsizei range)
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
HINSTANCE hInstance
Definition: 3dtext.c:42
#define DEFAULT_CHARSET
Definition: wingdi.h:382
#define GL_SMOOTH
Definition: gl.h:339
LONG right
Definition: windef.h:298
GLvoid KillFont(GLvoid)
Definition: 3dtext.c:108
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
WPARAM wParam
Definition: combotst.c:138
BOOL WINAPI SetPixelFormat(_In_ HDC, _In_ int, _In_ const PIXELFORMATDESCRIPTOR *)
GLAPI void GLAPIENTRY glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define GL_LESS
Definition: gl.h:294
UINT_PTR WPARAM
Definition: windef.h:207
GLvoid BuildFont(GLvoid)
Definition: 3dtext.c:46
#define PFD_DOUBLEBUFFER
Definition: wingdi.h:300
const GLint const GLsizei GLsizei
Definition: dispatch.h:5271
void SaveSettings(void)
Definition: settings.c:113
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define gluPerspective
Definition: glu_mangle.h:28
void LoadSettings(void)
Definition: settings.c:53
#define GL_LIGHT0
Definition: gl.h:311
GLuint base
Definition: 3dtext.c:35
LONG_PTR LPARAM
Definition: windef.h:208
#define CLIP_DEFAULT_PRECIS
Definition: wingdi.h:424
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
GLfloat rot
Definition: 3dtext.c:36
#define CreateFont
Definition: wingdi.h:4420
GLAPI void GLAPIENTRY glPushAttrib(GLbitfield mask)
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
Definition: 3dtext.c:416
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
_STLP_DECLSPEC complex< float > _STLP_CALL cos(const complex< float > &)
#define wglUseFontOutlines
Definition: wingdi.h:4463
smooth NULL
Definition: ftsmooth.c:416
#define _TEXT(x)
Definition: tchar.h:1543
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
float GLfloat
Definition: gl.h:161
#define WM_DESTROY
Definition: winuser.h:1591
TCHAR m_Text[MAX_PATH]
Definition: settings.c:25
BOOL WINAPI SwapBuffers(HDC)
Definition: wingl.c:187
#define WGL_FONT_POLYGONS
Definition: wingdi.h:1306
char TCHAR
Definition: xmlstorage.h:189
#define IDC_MESSAGE_TEXT
Definition: resource.h:5
GLfloat f
Definition: glext.h:7540
#define _T(x)
Definition: vfdio.h:22
unsigned int BOOL
Definition: ntddk_ex.h:94
GLAPI void GLAPIENTRY glListBase(GLuint base)
#define GetDlgItemText
Definition: winuser.h:5651
#define DEFAULT_PITCH
Definition: wingdi.h:441
#define WM_SIZE
Definition: winuser.h:1593
GLAPI void GLAPIENTRY glCallLists(GLsizei n, GLenum type, const GLvoid *lists)
#define WM_CLOSE
Definition: winuser.h:1603
#define MAX_PATH
Definition: compat.h:26
unsigned int UINT
Definition: ndis.h:50
int WINAPI ChoosePixelFormat(_In_ HDC hdc, _In_ const PIXELFORMATDESCRIPTOR *ppfd)
#define GL_LIST_BIT
Definition: gl.h:719
BOOL fullscreen
Definition: 3dtext.c:43
unsigned long DWORD
Definition: ntddk_ex.h:95
#define GL_COLOR_MATERIAL
Definition: gl.h:340
#define GL_LIGHTING
Definition: gl.h:310
#define SetDlgItemText
Definition: winuser.h:5715
#define WM_PAINT
Definition: winuser.h:1602
#define PFD_TYPE_RGBA
Definition: wingdi.h:295
static PIXELFORMATDESCRIPTOR pfd
Definition: ssstars.c:67
INT PixelFormat
#define GL_PROJECTION
Definition: gl.h:246
#define MB_ICONERROR
Definition: winuser.h:781
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define MessageBox
Definition: winuser.h:5688
#define WM_COMMAND
Definition: winuser.h:1716
#define WINAPI
Definition: msvc.h:20
BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
Definition: wgl.c:650
GLfloat extentX
Definition: 3dtext.c:37
static HDC hDC
Definition: 3dtext.c:33
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
POINTFLOAT gmfptGlyphOrigin
Definition: wingdi.h:2705
HINSTANCE hInst
Definition: dxdiag.c:13
GLfloat extentY
Definition: 3dtext.c:38
FLOAT gmfBlackBoxX
Definition: wingdi.h:2703
#define FW_BOLD
Definition: wingdi.h:376
unsigned int GLuint
Definition: gl.h:159
#define GL_DEPTH_TEST
Definition: gl.h:301
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
GLAPI void GLAPIENTRY glLoadIdentity(void)
#define GL_MODELVIEW
Definition: gl.h:245
LRESULT CALLBACK ScreenSaverProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition: 3dtext.c:256
GLvoid InitGL(GLsizei Width, GLsizei Height)
Definition: 3dtext.c:141
#define MB_OK
Definition: winuser.h:784
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
#define WM_CREATE
Definition: winuser.h:1590
#define c
Definition: ke_i.h:80
struct tagPIXELFORMATDESCRIPTOR PIXELFORMATDESCRIPTOR
#define HIWORD(l)
Definition: typedefs.h:246
LRESULT WINAPI DefScreenSaverProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:95
GLAPI void GLAPIENTRY glClearDepth(GLclampd depth)
LONG bottom
Definition: windef.h:299
Definition: console.h:34
GLAPI void GLAPIENTRY glColor3f(GLfloat red, GLfloat green, GLfloat blue)
GLAPI void GLAPIENTRY glPopAttrib(void)
_STLP_DECLSPEC complex< float > _STLP_CALL sin(const complex< float > &)
#define PFD_SUPPORT_OPENGL
Definition: wingdi.h:305
#define IDCANCEL
Definition: winuser.h:825
LONG_PTR LRESULT
Definition: windef.h:209
#define PFD_DRAW_TO_WINDOW
Definition: wingdi.h:302
#define OUT_TT_PRECIS
Definition: wingdi.h:417
#define WM_INITDIALOG
Definition: winuser.h:1715
GLvoid glPrint(LPTSTR text)
Definition: 3dtext.c:115
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
GLAPI void GLAPIENTRY glDepthFunc(GLenum func)
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
HGLRC WINAPI wglCreateContext(HDC hdc)
Definition: wgl.c:383