Click here to Skip to main content
12,952,792 members (35,923 online)
Click here to Skip to main content


93 bookmarked
Posted 15 Aug 2008

TetroGL: An OpenGL Game Tutorial in C++ for Win32 Platforms - Part 2

, 29 Mar 2009 CPOL
Learn how to load images, display them on the screen and manage them efficiently and how to display animations.
#ifndef _MAINWINDOW_H_
#define _MAINWINDOW_H_

#include <Windows.h>
#include "GL/gl.h"
#include "Image.h"
#include "AnimatedSprite.h"

// The main window class. It wraps the HANDLE of the window and initializes the 
// openGL rendering context. It is also in charge of processing the different
// event messages.
class CMainWindow
	CMainWindow(int iWidth, int iHeight, bool bFullScreen);

	// Called by the application class to update the game logic
	void Update(DWORD dwCurrentTime);
	// Called by the application class when the window need to be redrawn.
	void Draw();

	// Register the window class with the correct window procedure (OnEvent)
	void RegisterWindowClass();
	// Create the rendering context used by OpenGL
	void CreateContext();
	// Initialize openGL
	void InitGL();

	// Called when a WM_SIZE message is received
	void OnSize(GLsizei width, GLsizei height);

	// Static function which will be the window procedure callback
    static LRESULT CALLBACK OnEvent(HWND Handle, UINT Message, WPARAM wParam, LPARAM lParam);
	// Processes the messages that were received in OnEvent.
	void ProcessEvent(UINT Message, WPARAM wParam, LPARAM lParam);

	void UpdateAnimation();

	// The window handle
	HWND	m_hWindow;
	// The window device context
    HDC     m_hDeviceContext;
	// The openGL context.
    HGLRC   m_hGLContext;    

	// Specifies if the window is fullscreen.
	bool m_bFullScreen;

	// The image for the grass.
	TImagePtr m_pGrassImg;

	// Images for the trees
	TImagePtr m_pTreesImg[16];

	// The animated sprite of the knight
	CAnimatedSprite* m_pKnightSprite;
	// Which keys are currently pressed
	bool m_KeysDown[4];
	// The last direction of the knight
	std::string m_strLastDir;

#endif  // _MAINWINDOW_H_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Cedric Moonen
Belgium Belgium
I am a 29 years old guy and I live with my girlfriend in Hoegaarden, little city from Belgium well known for its white beer Smile | :) .
I studied as an industrial engineer in electronics but I oriented myself more towards software development when I started to work.
Currently I am working in a research centre in mechatronica. I mainly develop in C++ but I also do a bit of Java.
When I have so spare time, I like to read (mainly fantasy) and play electric guitar.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 29 Mar 2009
Article Copyright 2008 by Cedric Moonen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid