BOOL done=FALSE; // Bool Variable To Exit Loop
// Ask The User Which Screen Mode They Prefer
if (MessageBox(NULL, "Would You Like To Run In Fullscreen Mode?", "Start FullScreen?", MB_YESNO|MB_ICONQUESTION) == IDNO) {
fullscreen=FALSE; // Windowed Mode
}
The title of our Window has changed.
// Create Our OpenGL Window
if (!CreateGLWindow("NeHe & Giuseppe D'Agata's 2D Font Tutorial", 640, 480, 16, fullscreen)) {
return 0; // Quit If Window Was Not Created
}
while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
} else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
} else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received?
{
done=TRUE; // ESC or DrawGLScene Signalled A Quit
} else // Not Time To Quit, Update Screen
{
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
}
}
// Shutdown
The last thing to do is add KillFont() to the end of KillGLWindow() just like I'm showing below. It's important to add this line. It cleans things up before we exit our program.
if (!UnregisterClass("OpenGL", hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL, "Could Not Unregister Class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
KillFont(); // Destroy The Font
}
I think I can officially say that my site now teaches every possible way to write text to the screen {grin}. All in all, I think this is a fairly good tutorial. The code can be used on any computer that can run OpenGL, it's easy to use, and writing text to the screen using this method requires very little processing power.
I'd like to thank Giuseppe D'Agata for the original version of this tutorial. I've modified it heavily, and converted it to the new base code, but without him sending me the code I probably wouldn't have written the tutorial. His version of the code had a few more options, such as spacing the characters, etc, but I make up for it with the extremely cool 3D object {grin}.
I hope everyone enjoys this tutorial. If you have questions, email Giuseppe D'Agata or myself.
* DOWNLOAD Visual C++ Code For This Lesson.
* DOWNLOAD Borland C++ Code For This Lesson. (Conversion by Patrick Salmons)
* DOWNLOAD Cygwin Code For This Lesson. (Conversion by Stephan Ferraro)
* DOWNLOAD Delphi Code For This Lesson. (Conversion by Marc Aarts)
* DOWNLOAD Game GLUT Code For This Lesson. (Conversion by Milikas Anastasios)
* DOWNLOAD Irix / GLUT Code For This Lesson. (Conversion by Rob Fletcher)
* DOWNLOAD Java Code For This Lesson. (Conversion by Jeff Kirby)
* DOWNLOAD Linux/GLX Code For This Lesson. (Conversion by Mihael Vrbanec)
* DOWNLOAD Linux/SDL Code For This Lesson. (Conversion by Ti Leggett)
* DOWNLOAD Mac OS Code For This Lesson. (Conversion by Anthony Parker)
* DOWNLOAD Mac OS X/Cocoa Code For This Lesson. (Conversion by Bryan Blackburn)
* DOWNLOAD Visual C++ / OpenIL Code For This Lesson. (Conversion by Denton Woods)
* DOWNLOAD Visual Basic Code For This Lesson. (Conversion by Ross Dawson)
Lesson 18
Quadrics are a way of drawing complex objects that would usually take a few FOR loops and some background in trigonometry.
We'll be using the code from lesson seven. We will add 7 variables and modify the texture to add some variety :)
#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input/Output
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The GLaux Library
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
bool light; // Lighting ON/OFF
bool lp; // L Pressed?
bool fp; // F Pressed?
bool sp; // Spacebar Pressed? ( NEW )
int part1; // Start Of Disc ( NEW )
int part2; // End Of Disc ( NEW )
int p1=0; // Increase 1 ( NEW )
int p2=1; // Increase 2 ( NEW )
GLfloat xrot; // X Rotation
GLfloat yrot; // Y Rotation
GLfloat xspeed; // X Rotation Speed
GLfloat yspeed; // Y Rotation Speed
GLfloat z=-5.0f; // Depth Into The Screen