Jump to content

April 2017 »

23 242526272829

Recent Entries

Recent Comments

- - - - -

Creotex Engine ~ Part 1

Engine Programming Games Creotex
4: Adsense

Hello ladies and gentlemen,

Welcome to my journal!
So part 1 of writing an engine. I hope you guys are all so excited as me! Posted Image ( I know you do )

Before I step into DirectX graphics I first want to create a window.
For people who haven't learned DirectX yet, for popping up a window you don't need any DirectX libs.
DirectX will just need the handle and instance of the window and will turn everything to a 3D space.

By the way, didn't mentioned it before, I'm using Unicode!
#ifdef _UNICODE
	#define tstring wstring
	#define tstring string

So.. Writing a game engine. How should I start? How should I write? How will my structure look like?
Don't worry already got the idea! Posted Image

The plan is that my WinMain function creates an Application, which handles the core and game.
This is how it looks like:
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PTSTR pCmdLine, int nCmdShow)
	// Terminate if heap is corrupted
	HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);

	// Avoid warnings of unused parameters

	// Allocate the application
	Application* pApplication = new Application();

	// Initialize and run the application

	// Shutdown and deallocate the application
	return TRUE;

So I'm Initializing the Application, running if succeeded, and shutting down.
And for those who wonder what "SafeDeleteCore" is:
template<class T>
inline void SafeDeleteCore(T &pObject)
	if(pObject != nullptr){
		delete pObject;
		pObject = nullptr;

My application has 4 data members:
core: Window*, System*, Graphics*
game: Game*

I think you all know what to do with those data members when you have 3 methods... So not going to show such simple code....
Ok maybe a bit.. Because all of you are so lovely Posted Image
This is the Run method:
void Application::Run()
	// GameStart

	MSG msg = {0};
	while(msg.message != WM_QUIT)
		   // GameUpdate

		   // GameRender


	// GameEnd

So we got the WinMain, Application.... That was it I think?
Yes everything is done, we have written our engine. Now we can create games like Starcraft!!

Or is it? Posted Image
No there is more!!!! *screaming* *crying* *beating up the kid next door*

We still need that window on our screen! or maybe if you stare long enough it will magically appear.. You can give it a shot.
So for the window class. A window class needs to store some information about the window so you can easily change something on your screen.
class Window
	// Constructor(s) & Destructor
	virtual ~Window();

	// Public Methods
	bool Initialize(HINSTANCE hInstance);
	void Shutdown();
	void ShowWindow();


	HWND  m_hMainWnd;
	HINSTANCE m_hInstance;
	tstring  m_sWindowTitle;
	int   m_iWindowWidth;
	int   m_iWindowHeight;
	bool  m_bFullscreen;


The Window.cpp is the largest piece of code I have at the moment. Here it comes!
bool Window::Initialize(HINSTANCE hInstance)
	// Store the instance of the application
	m_hInstance = hInstance;

	// Setup the window class with default settings
	WNDCLASSEX wndclass;
	wndclass.style   = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra  = 0;
	wndclass.cbWndExtra  = 0;
	wndclass.hInstance  = m_hInstance;
	wndclass.hIcon   = LoadIcon(NULL, IDI_WINLOGO);
	wndclass.hIconSm  = wndclass.hIcon;
	wndclass.hCursor  = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255,255,255));
	wndclass.lpszMenuName = m_sWindowTitle.c_str();
	wndclass.lpszClassName = m_sWindowTitle.c_str();
	wndclass.cbSize   = sizeof(WNDCLASSEX);

	// Register the window class
	if(!RegisterClassEx(&wndclass)) {
	  return false;

	// Create new screen settings if fullscreen
	int posX = 0, posY = 0;
	  DEVMODE dmScreenSettings;
	  memset(&dmScreenSettings, 0,sizeof(dmScreenSettings));
	  dmScreenSettings.dmSize   = sizeof(dmScreenSettings);
	  dmScreenSettings.dmPelsWidth = (unsigned long)m_iWindowWidth;
	  dmScreenSettings.dmPelsHeight = (unsigned long)m_iWindowHeight;
	  dmScreenSettings.dmBitsPerPel = 32;
	  dmScreenSettings.dmFields  = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
	  ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
	  posX = (GetSystemMetrics(SM_CXSCREEN) - m_iWindowWidth) /2;
	  posY = (GetSystemMetrics(SM_CYSCREEN) - m_iWindowHeight) /2;

	// Create the window with the screen settings and get the handle to it
	m_hMainWnd = CreateWindowEx(WS_EX_APPWINDOW, m_sWindowTitle.c_str(), m_sWindowTitle.c_str(),
		   posX, posY, m_iWindowWidth, m_iWindowHeight,
		   NULL, NULL, m_hInstance, NULL );

	if(!m_hMainWnd) {
	  return false;

	// Set window as main focus ~ sets higher priority to this thread

	return true;
void Window::Shutdown()
	// Show the mouse cursor ~ just in case the mouse was hidden in run-time

	// Fix the display settings if leaving full screen mode
	if(m_bFullscreen) {
	  ChangeDisplaySettings(NULL, 0);

	// Remove the window
	m_hMainWnd = NULL;

	// Remove the application's instance
	UnregisterClass(m_sWindowTitle.c_str(), m_hInstance);
	m_hInstance = NULL;
void Window::ShowWindow()
	::ShowWindow(m_hMainWnd, SW_SHOW);
LRESULT CALLBACK Window::HandleEvents(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
	return DefWindowProc(hWnd, msg, wParam, lParam);

// Window Procedure
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
	  return 0;
	case WM_CLOSE:
	  return 0;

	return ((Window*)GetWindow(hWnd, 0))->HandleEvents(hWnd, msg, wParam, lParam);

So now we should have a nice window appeared on our screen. If not, initialize you data members!
I initialized my data members on a default size 640*480 and title: Default Window

While I was re-programming my framework I found something cool but strange at the same time..
	return ((Window*)GetWindow(hWnd, 0))->HandleEvents(hWnd, msg, wParam, lParam);

This should crash normally but it doesn't. Posted Image
I never allocated that window, or received a pointer of an existing pointer and still. The HandleEvents() method can be called.
I'm sure I'm opening many eyes right now... Yes have a clear look at the code! Posted Image
I thought if was the SetFocus(); method at the start but they make no difference at all. Also you can leave out the "static" keyword. Still doesn't crash.

So this is creating new possibilities I think? You don't need a singleton anymore to call the HandleEvents. Or a static global pointer of your own class. Everything can be done now through this method.

So thank you for your time ladies and gentlemen!! Now I'm going to write the system and graphics class for the next entry. So be sure to have a look! Posted Image You can also follow this blog to make it easier. And so I also know people are interested.

Sep 02 2012 03:04 AM
Thanks for this post Jonathan :)

You mentioned this in your post:
return ((Window*)GetWindow(hWnd, 0))->HandleEvents(hWnd, msg, wParam, lParam);
The reason this doesn't crash is because in HandleEvents(), you're only calling the default window procedure and not modifying the data within the class object. If you were to modify the data members of the Window class inside HandleEvents(), you'd probably corrupt memory. This is because you're casting the HWND returned from GetWindow() to a pointer to your custom window class, but HWND != Window*. You could probably try breaking it, by setting the window title inside the HandleEvents() method.
Hope this is useful, and I'm looking forward to your future posts! :)
Sep 02 2012 02:46 PM
MajorTom, thanks and I'm glad you are looking forward to my future posts! :)

About the function you are right indeed,I've tried to modify a data member of the class inside the HandleEvents method and my window didn't want to launch. Thanks for the answer! :)

Sep 13 2012 07:43 AM
I recommend against Unicode support.
All of my previous engines supported Unicode only to eventually prove to be a pain in the ass. Finally I realized that UTF-8 is the way to go.

The basic reason for using Unicode is to be sure that you can open files of any path, display strings of any language, etc.
As a beginner, I knew that Unicode was the “safe” way to go. Finally I learned that this was the only reason I was supporting Unicode. It was based off a lack of understanding.

UTF-8 not only supports all Unicode characters, but can easily be extended to support more than the standard supports.
Not only can it fully satisfy all of your current needs, be used to open any file, or to print any character in any language, it is also future-proof.
Furthermore, when you move onto systems such as iOS, UTF-8 is actually the required format for opening files, which means you will have to waste time converting your Unicode strings to UTF-8.

And then there is the matter of sending data over networks, which will require you to convert to UTF-8 anyway in order to keep packet sizes smaller.

I go into more details on this subject here.

Ultimately there is nothing to gain from supporting raw Unicode, plus the fact that L"" strings are not the same sizes across compilers/platforms. For the sake of consistency, forward-compatibility, and efficiency it is best to use UTF-8 strings only.

L. Spiro

Note: GameDev.net moderates comments.