Creotex Engine ~ Part 1

Published September 01, 2012
Advertisement
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!
Example:

#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif


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
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(pCmdLine);

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

// Initialize and run the application
if(pApplication->Initialize(hInstance))
{
pApplication->Run();
}

// Shutdown and deallocate the application
SafeDeleteCore(pApplication);
return TRUE;
}


So I'm Initializing the Application, running if succeeded, and shutting down.
And for those who wonder what "SafeDeleteCore" is:

template
inline void SafeDeleteCore(T &pObject)
{
if(pObject != nullptr){
pObject->Shutdown();
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
m_pGame->GameStart();


MSG msg = {0};
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// GameUpdate
m_pGame->GameUpdate();

// GameRender
m_pGame->GameRender();

Sleep(1);
}
}

// GameEnd
m_pGame->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
{
public:
/////////////////////////////////////////////////////
// Constructor(s) & Destructor
/////////////////////////////////////////////////////
Window();
virtual ~Window();

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

LRESULT CALLBACK HandleEvents(HWND, UINT, WPARAM, LPARAM);

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

static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);



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;
if(m_bFullscreen)
{
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);
}
else
{
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(),
WS_CAPTION | WS_POPUPWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP | WS_MINIMIZEBOX,
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
SetForegroundWindow(m_hMainWnd);
SetFocus(m_hMainWnd);

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

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

// Remove the window
DestroyWindow(m_hMainWnd);
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);
::UpdateWindow(m_hMainWnd);
}
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)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CLOSE:
PostQuitMessage(0);
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.
0 likes 3 comments

Comments

Pthalicus
Thanks for this post Jonathan :)

You mentioned this in your post:
[CODE]
return ((Window*)GetWindow(hWnd, 0))->HandleEvents(hWnd, msg, wParam, lParam);
[/CODE]
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! :)
September 02, 2012 09:04 AM
EngineProgrammer
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! :)


~EngineProgrammer
September 02, 2012 08:46 PM
L. Spiro
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
September 13, 2012 01:43 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement