GLWindow
Before we do any hardcore OpenGL programming, we need a framework for creating a window. Sure, you could use SDL or GLUT, but I want to play around with Win32. For that, we shall go to the old standard and copy NeHe’s code. Lesson 45 has a very nice example, but that’s C code. Let’s modify that to make it C++ worthy and create a GLWindow class. So, if you download NeHe’s Win32 code, you’ll see a lot of similarities to GLWindow, but I also made my own modifications
The GLWindow class is meant to be an easy to use framework for setting up an OpenGL project in no time. It allows access to most aspects of the code, making it easily expanded. The main workflow consists of subclassing GLWindow and overriding some of it’s virtual methods. Defaults are provided, so there is no need to override all of the methods. Once a subclass is defined, you instantiate it and call mainloop() to run the program. Piece of cake. Here’s the class structure.
class GLWindow
{
public:
GLWindow();
~GLWindow(void);
void redirectIOToConsole();
void terminateApplication();
void toggleFullscreen();
bool changeScreenResolution(int width, int height, int bitsPerPixel);
bool createWindow();
bool destroyWindow();
LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
bool registerWindowClass();
void mainLoop(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
virtual bool init();
virtual void deinit();
virtual void update(DWORD elapsedTime);
virtual void draw();
virtual void reshape(int width, int height);
virtual bool handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
virtual void onLMouseDown();
virtual void onLMouseUp();
virtual void onMMouseDown();
virtual void onMMouseUp();
virtual void onRMouseDown();
virtual void onRMouseUp();
virtual void onMouseMove(int x, int y, WPARAM keys);
virtual void onMouseWheel(WPARAM keys, int wheelDelta, int x, int y);
char title[256];
int starting_width, current_width;
int starting_height, current_height;
int bitsPerPixel;
bool isFullScreen;
protected:
HINSTANCE hInstance;
HWND hWnd;
HDC hDC;
HGLRC hRC;
char className[64];
bool isProgramLooping;
bool createFullscreen;
bool isMessagePumpActive;
bool isVisible;
DWORD lastTickCount;
bool keys[256];
private:
void sethWnd(HWND hWND) ;
static LRESULT CALLBACK staticWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};
See the rest of GLWindow