Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Creotex Engine ~ Part 1

Sign in to follow this  


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! laugh.png ( 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! cool.png

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:

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 tongue.png
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? ph34r.png
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.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;
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. blink.png
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! tongue.png
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! wink.png You can also follow this blog to make it easier. And so I also know people are interested.
Sign in to follow this  


Recommended Comments

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! :)

Share this comment

Link to comment
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! :)


Share this comment

Link to comment
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 [url="http://lspiroengine.com/?p=83"]here[/url].

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

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!