# Writing a game engine

This topic is 2225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone,

I'm studying C++ for 1 year now and we need to make a game for school in a few months. We received a game engine for our game from school but I don't like that engine so I want to write my own engine.
Last year I made many projects how to make windows, how to paint lines, how to draw bitmaps, without an engine. And now, I think, I got the basics of everything for writing a game engine.

So I've started with writing and I got some questions about it,

I let the user write their " int WINAPI WinMain(...) {...} " and they include my engine to that project. So my engine got a method Startup and Shutdown. Is this smart to do?
To let the user use those 2 methods he need to ask the GameEngine instance first. I made my GameEngine object on the stack so I don't need to create/delete it. So in 1 method I send it to the user. ( GetInstance(); )
That method is static so the user can use all my other public methods from the GameEngine. Still I don't know if this is a good way to write an engine.. I could use some opinions / better options.

And last, I don't know what to use. Shall I make a Painter class that will paint all my stuff ( text, bitmaps, etc ), or shall I make all my objects independent and make it able that they can paint their self. ( Image1.Paint(); )

Have any of you got some experience with writing a game engine? So yes, am I going in the right direction?

Kind regards,
Jonathan

##### Share on other sites
With the engine we received we can only play sounds, draw bitmaps and draw text. Not that much. And I have done all those things in personal projects last year.
And to be honest, I agree with you that my engine will be a lot worse then the engine I have now. But I love to experience with stuff. :-)

I want to have the feeling with this game that -> I <- made it all on my own. Well... the game I "write" is Warcraft2 ( we don't need to be original and create our own game ).

But back to the point, I got an engine of school, so I have an example of an engine. With books, tutorials and forums I'll be able to write an engine in a month. ( I'm guessing a bit )
My engine just need some small extra features. But it will do almost the same as the engine now.

You can say: " why would you write an engine that can do all the stuff you can do with the engine you already have ".
Well: I just want the experience. :-)

Suppose I don't want follow the direction straight back. My game needs to be finished around month June. I think I can do this, but for that I need some help of how I should write my code. Please help me, I want to prove I can do this. :-)

B. Jonathan

##### Share on other sites
I'll be able to write an engine in a month.

Trust me, you won't
This is not meant to be rude or anything, but engines are really complex beasts, even when they only have very simple features

Writing different features like drawing something on screen, playing some sounds, etc. can be done rather primitively in a rather short amount of time (note that I'm saying primitively as in not bug-free or stable at all); add these up and you'll already be a couple of weeks in just by writing primitive and buggy features. Throwing all these features together does not give you an engine, an engine should be able to support a variety of projects and should give a developer some freedom in what they want to design, so your engine needs to be able to support that freedom without throwing errors at the developer all the time

To make sure an engine actually works requires a vast amount of debugging and will require you to write countless test cases to cover each aspect of the features your engine supports
So all in all, saying that you can write a good, bug-free and working engine in a month's time with 1 year of C++ experience is quite ridiculous

##### Share on other sites
Teachers can react very sensitve to the word "engine", because whatever you can write in 1 month or 6 months, it's not going to be an "engine".

Just write the game. Write reusable code. Those will be your first building blocks for your own "engine" (for the next project).

Have you watched Creating Prelude of the Chambered? At no point of time I saw Notch writing an "engine" there.

Ps: Just speaking out of experience.

##### Share on other sites
If you don't yet know what way you'll write the engine, and if you have to come online asking for advice on basic structure for it, that should be sending you a pretty clear warning signal.

##### Share on other sites
Writing an engine to write a game is foolhardy at best -- what criteria is lacking in other engines that yours fulfills? Of course, that is not meant to be an insult -- but rarely do you need to roll your own if all you want to do is make a good game.

Writing a game and ending up with an engine is a much more acceptable scenario. But really, what is more important to you -- making this engine or making the game? Because splitting your priorities, especially at this stage, is a very very stupid thing to do -- choose something available and get going if you want that game made; if you want to just make an engine, make the game and scrutinize re-usable parts. There. That's your "engine".

Best of luck!

##### Share on other sites
Out of curiosity, why don't you like the engine you got from school?

Also, I agree with the comments. Writing an engine is not an easy task. I suggest you focus on the game and use what has been given to you. You say you can write an engine in one month and finish the game in the remaining months... let's assume for a moment that it's an accurate time estimate. That's one extra month you can use for making the game shine. IMO having a polished game will cause a bigger wow factor than having your own engine.

It's also good practice for you if you break in to the industry. Unless you become an engine programmer, you will be using an engine written by another team (or company altogether). Even if you are an engine programmer, you probably won't be writing all the code yourself and will have to deal with code from other developers.

So again, what is wrong with the engine you got from school?

##### Share on other sites
Do you get the provided engine's source code?
If so I would write your game with that and find ways to extend the provided functionality as needed by modifying the engine.

There is no shame is saving time by using what is available. You can just spend your time focusing on more interesting aspects of the game.

I like your ambition. Make something cool.

##### Share on other sites
Thanks for all those replies.

Ok, so the engine I received last year is more a 'sort-of-engine'. I have the source code of it ( around 600-700 lines of code )
Last year I searched whole the engine out and looked up for every function that was in there. I even looked up Multi-threading and used that in my game last year.

I am some of the 200 students that is studying more C++ after school. C++ was new to me last year. And the things I learned ( last school year ) are:
- keyword inline
- how to make a Singleton. ( GetInstance() )
- How to make a window.
- How to draw lines and bitmaps in that window.
- How to react on events ( mouse / keybord )
- Added an Xbox controller to my computer and wrote the code for it so I can play my game with the controller.

That doens't seem much. But I learned that all after school and I am happy with it

This school year I want to do;
- write a sort-of-engine -> I just want to write my game with it. This year I use that 'engine' only for myself. No one else will use it now.
- Learn networking and implement it in the 'engine'

I already made a LAN-connection last weekend for networking ( connection via router ).

Maybe the word 'engine' was a bit too much. So if I change that word into 'sort-of-engine'.. I only was to use it for myself. I just need to be able to write a game in it.
After this school year I got 2months vacation so then I'll try so change/improve that 'sort-of-engine'.
I think I'll be able to manage it. And I know how to write an engine by the way. But the are lots of ways and I want to use a good one.

The 'engine' I got from school I don't like because:
- it don't have any performance at all / terrible names for datatypes ( maybe because we need to understand it )
Code I use: Object* m_pObject;
Code the 'engine' have: Object* m_MyObjectPtr;
- The WinMain we never wrote because this was already implemented in the engine. Almost no one of my class knows how WinMain works.

These are the 2 things I don't like. I know the priority is to make the game. But I want to do more.. I am already 3weeks ahead with the lessons.

##### Share on other sites
I was thinking about 3 ways:

1. Use a Factory class that will load all my Bitmaps/Souds/etc. Then use a Painter class that will paint it all.
With this I should need 3 pointers (or objects) -> GameEngine / Factory / Painter.

2. The second way was to use a Singleton with for the GameEngine.
In that GameEngine I allocate 3 pointers ( ResourceLoader / Painter / InputState ) And I use SIngleton for the class GameEngine.
So if the user want to paint something he can receive the Painter pointer trough the GameEngine instance.
example in WinMain:
Painter* pPainter = GameEngine::GetInstance()->GetPainter();
pPainter->DrawText( "Hello there", xPos, yPos );

Here I got an adventage that I only need to make the pointers once.

3. The third way is to use inheritance.
I write 3 classes ResourceLoader / Painter / InputState
And then for the GameEngine: class GameEngine : public ResourceLoader, public Painter, public InputState { }

The adventage of this is that I can do anything with only the GetInstance().

So.. Is one of these ways good enough? Or are there better ones.

##### Share on other sites
Conversations like this always make me cringe. They remind me of my youth when I wanted to do everything that everyone was telling me not to do because they've already done it and have learnt from it. The key is just that, people who tell us not to do it have learnt not to do it themselves - they were probably just as gung ho as these guys asking for advice.

Yes it's probably better to write a game then end up with an engine but this kid has obviously got a lot of passion for learning and creating so let him make his own 'mistakes' - hell, I've been writing a game engine for years and it's my sanctuary from everything else in life. Will it be cryengine 5? Absolutely not but it gives me great enjoyment.

As a lot of the guys have said, your school grades come first so concentrate on using their engine to make your game, but alongside it, why not write your own engine or at least examine theirs and see how yours could be better.

You remind me of me, I'm as successful as I ever imagined I could be (in finance dev i might add) and that's from learning from my own mistakes, not everyone else's. I say grades first, then engine. You might get bored in 3 weeks but find it out for yourself.

Although saying that, it is very useful if you can have some kind of game genre idea in mind that your engine might 'suit' more.

Good luck and let us know how you get on

Added - before I get jumped on, I'm not saying anyone's advice is wrong, just that OP sounds so passionate about it, he's probably going to do it anyway, so perhaps a different bit of guidance would be better...

##### Share on other sites
This is going to sound rude. Do you know what a game engine is? Define it in your own words.

Me or OP?

##### Share on other sites
...we need to make a game for school in a few months. ...
...
Ok, so the engine I received last year is more a 'sort-of-engine'. I have the source code of it ( around 600-700 lines of code )...
Perhaps I'm biased towards being lazy (3 hours-day commute for high school, 5-hours day commute for university)... but I think you should save your time. If the framework you got for this game is 700 lines, odds are your professor expects you to do tic-tac-toe or "snake" games. Nothing much than that.
I appreciate your enthusiasm. Really. However, make sure you'll land on the same metric as other students. Get the job done first, then relax. Nothing is even remotely comparable to figure out you won't hit the deadline.

I want to tell you a few stories - the story of the well-written code I have presented to my professors.
1. A small echo server. Prof: "I'm sorry I cannot give you more than full grade. As a matter of fact, I think nobody else this year will pass this exam." (as a matter of fact, nobody else passed the exam).
Long story short: wasted effort.
2. A web-based, interactive 3D presentation system (like 5 years ago). Prof: "I wasn't expecting this. Every major component of this system is an order of magnitude more complex than you'd need for full grade. I'm going to keep this for your thesis".
Long story short: wasted effort.
3. Shit in assembler. Prof: "Pretty much the best code I've seen in the last 10 years".
Long story short: good to hear, but in the end, other people got full grade as well, and I trashed 2 extra days, wasted effort.
4. An interactive, in-depth analysis of different shading models, with primordial shader mangling system. I'm not going to say what happened there.
Long story short: wasted effort.

So... make sure you don't do more work than you really need to. Edited by Krohm

##### Share on other sites

[quote name='turbello' timestamp='1322887757' post='4890039']...we need to make a game for school in a few months. ...
...
Ok, so the engine I received last year is more a 'sort-of-engine'. I have the source code of it ( around 600-700 lines of code )...
Perhaps I'm biased towards being lazy (3 hours-day commute for high school, 5-hours day commute for university)... but I think you should save your time. If the framework you got for this game is 700 lines, odds are your professor expects you to do tic-tac-toe or "snake" games. Nothing much than that.
I appreciate your enthusiasm. Really. However, make sure you'll land on the same metric as other students. Get the job done first, then relax. Nothing is even remotely comparable to figure out you won't hit the deadline.

I want to tell you a few stories - the story of the well-written code I have presented to my professors.
1. A small echo server. Prof: "I'm sorry I cannot give you more than full grade. As a matter of fact, I think nobody else this year will pass this exam." (as a matter of fact, nobody else passed the exam).
Long story short: wasted effort.
2. A web-based, interactive 3D presentation system (like 5 years ago). Prof: "I wasn't expecting this. Every major component of this system is an order of magnitude more complex than you'd need for full grade. I'm going to keep this for your thesis".
Long story short: wasted effort.
3. Shit in assembler. Prof: "Pretty much the best code I've seen in the last 10 years".
Long story short: good to hear, but in the end, other people got full grade as well, and I trashed 2 extra days, wasted effort.
4. An interactive, in-depth analysis of different shading models, with primordial shader mangling system. I'm not going to say what happened there.
Long story short: wasted effort.

So... make sure you don't do more work than you really need to.
[/quote]

If you learned anything doing those things then it wasn't wasted effort though, but i'd definitly agree that it is a bad idea to risk missing a deadline due to being overly abitious, if you've allready completed the assignment however it is definitly a good idea to try to take things further if you have time, learning is never a bad thing.

##### Share on other sites

Thanks for standing behind me
And yes I am going to do it.

Last year I learned not to do too much at a time. Normally I would have done networking then but I didn't because then I would fail my game. I am a very good planner and I got a lot of free time now. So now it's the time to learn many new things. But I don't want to learn bad ways of programming so I'm asking for a bit help in the right direction.

@freeworld:

An engine in my words. A (big)piece of code that makes it able to write an application with methods in that engine. Stuff that makes it easier to draw lines, bitmaps, the window.
I would like to avoid to write HDC, PAINTSTRUCT, WNDCLASSEX, etc in my game code. The engine must handle everything.

@Krohm:

Experience is so not a waste. Like networking: I want to use WinSock and not RakNet. Some people said to me " Why would you learn to work with WInsock, you will never use it again later ". I replied " I just want to have the experience. "
My teachers don't expect tic-tac-toe. That is too easy for them. Last year I made a Dragonball Z - Supersonic warrios and I had 17/20. And my game wasn't even the best sadly enough. I want to have the best game this year.

I understand the point that some of you think I won't be able to reach the deadline. But I love to take risks, it makes life so much interesting. if I fail I'll learned my lesson. But I got a great confidence that I will succeed. Atleast if I get some help..

##### Share on other sites

Conversations like this always make me cringe. They remind me of my youth when I wanted to do everything that everyone was telling me not to do because they've already done it and have learnt from it. The key is just that, people who tell us not to do it have learnt not to do it themselves - they were probably just as gung ho as these guys asking for advice.

Yes it's probably better to write a game then end up with an engine but this kid has obviously got a lot of passion for learning and creating so let him make his own 'mistakes' - hell, I've been writing a game engine for years and it's my sanctuary from everything else in life. Will it be cryengine 5? Absolutely not but it gives me great enjoyment.

As a lot of the guys have said, your school grades come first so concentrate on using their engine to make your game, but alongside it, why not write your own engine or at least examine theirs and see how yours could be better.

You remind me of me, I'm as successful as I ever imagined I could be (in finance dev i might add) and that's from learning from my own mistakes, not everyone else's. I say grades first, then engine. You might get bored in 3 weeks but find it out for yourself.

Although saying that, it is very useful if you can have some kind of game genre idea in mind that your engine might 'suit' more.

Good luck and let us know how you get on

Added - before I get jumped on, I'm not saying anyone's advice is wrong, just that OP sounds so passionate about it, he's probably going to do it anyway, so perhaps a different bit of guidance would be better...

A game engine is just a library of code and tools that can be reused on another game. As such, there is no need to 'make' one -- just make a game and re-factor that into an 'engine'. Unless you have experience, you're not likely to be able to correctly wage which parts will be reused later on and so there is no real way for you to make a good engine -- you mash together general tools and code but all that will do is confuse your codebase (since you will try to keep it generalized so you can reuse it, instead of specialized; which would serve the game better and still allow for reusable parts to be picked out later on).

[quote name='Krohm' timestamp='1323163180' post='4891030']
[quote name='turbello' timestamp='1322887757' post='4890039']...we need to make a game for school in a few months. ...
...
Ok, so the engine I received last year is more a 'sort-of-engine'. I have the source code of it ( around 600-700 lines of code )...
Perhaps I'm biased towards being lazy (3 hours-day commute for high school, 5-hours day commute for university)... but I think you should save your time. If the framework you got for this game is 700 lines, odds are your professor expects you to do tic-tac-toe or "snake" games. Nothing much than that.
I appreciate your enthusiasm. Really. However, make sure you'll land on the same metric as other students. Get the job done first, then relax. Nothing is even remotely comparable to figure out you won't hit the deadline.

I want to tell you a few stories - the story of the well-written code I have presented to my professors.
1. A small echo server. Prof: "I'm sorry I cannot give you more than full grade. As a matter of fact, I think nobody else this year will pass this exam." (as a matter of fact, nobody else passed the exam).
Long story short: wasted effort.
2. A web-based, interactive 3D presentation system (like 5 years ago). Prof: "I wasn't expecting this. Every major component of this system is an order of magnitude more complex than you'd need for full grade. I'm going to keep this for your thesis".
Long story short: wasted effort.
3. Shit in assembler. Prof: "Pretty much the best code I've seen in the last 10 years".
Long story short: good to hear, but in the end, other people got full grade as well, and I trashed 2 extra days, wasted effort.
4. An interactive, in-depth analysis of different shading models, with primordial shader mangling system. I'm not going to say what happened there.
Long story short: wasted effort.

So... make sure you don't do more work than you really need to.
[/quote]

If you learned anything doing those things then it wasn't wasted effort though, but i'd definitly agree that it is a bad idea to risk missing a deadline due to being overly abitious, if you've allready completed the assignment however it is definitly a good idea to try to take things further if you have time, learning is never a bad thing.
[/quote]
Agreed, though I think the lesson here is "do the minimal amount of work for the grade, and do the rest for your own learning experience". Though it always bugged me how easily teachers handed out A+ grades for things that really weren't that complex or time consuming.

I was once given 6+ months to make a (paraphrased quote): "game where you walk around in a 3d environment with no textures and only one primary light source". Well geez teach, that sure seems like a tough nut to crack!

##### Share on other sites
Look above and you see the last post.

The ways I have in mind in code.
http://pastebin.com/VUMEjVpC

##### Share on other sites

I understand the point that some of you think I won't be able to reach the deadline. But I love to take risks, it makes life so much interesting. if I fail I'll learned my lesson. But I got a great confidence that I will succeed. Atleast if I get some help..

I really don't get why you would do this. Have you even talked to your teacher about using a self-built engine? In my experience teachers or professors do not appreciate it if you use an alternative to the software they provide, unless explicitly stated otherwise. It takes much more work for them to grade your code since they'll first have to figure out how the software you used functions, and in a lot of cases they will even refuse to grade it because of this

Also, I think you don't quite fully grasp what an engine is and what it's supposed to provide (based on your post describing your framework); as DarklyDreaming said, mashing together a bunch of tools probably won't give you a usable engine. It requires detailed knowledge of what it takes to build an actual game, and last time I checked the only way to getting that knowledge is by, you know, writing games

Going from 'zero' to 'hero' in engine development in a single month is also something that's just absolutely not feasible, a month is a very very very small amount of time when it comes to software development, and if you still have to learn everything along the road (which is always paired with lots of mistakes) you're just setting yourself up for failure

Look, if you want to write an engine that's fine, but wasting 1 month of valuable time which you could spend on actually completing your assignment as dictated by your teacher on quickly mashing a buggy engine together is just irresponsible if you're actually determined on passing
Also don't assume you would get a lot of help on your engine, the general consensus is 'Write games, not engines', and this especially applies to beginners. Adding the aspect of the 1 month timeframe for developing it will make getting help even more difficult

##### Share on other sites
I asked my teacher if I could write my own code for the game and he said yes with a big smile. He gave some encouragement that it will very good for my portfolio but he also said it will be hard and I need to start now. Well I'm starting now..
I got a really good teacher, and he is funny too. ( he always tells a big and cool story of a subject )

And hey,! You just said, " You go from 'zero' to 'hero' ... I am doing that and then you say I'm setting up for failure..

My teacher really believes I can do this, so do I. I don't need any discourage now..
And I am not starting from zero. I know how to load/draw images, text, lines, etc. I know how events work. I got enough basics.

Look at those ways in the previous link please and tell me which one is the best.

##### Share on other sites
Don't be discouraged!
Just keep in mind that you're making this 'engine' only for this current project's needs, and not because it's the great next engine that people will use for years to come, and that way you'll always be working towards your project's goals.

If you've got to cut corners or do things not-quite perfect, it's ok, as longs as you're helping the project along.

##### Share on other sites
That is the point of my engine that I want to write now. ( sort-of-engine I mean )
I'm not writing an engine now for millions of people because there are a lot better engines that I can't beat with my experience.

But like I said, I don't want to learn the 'wrong' way of programming. There are tons of wrong ways and I want to learn zero of them. And so I am asking for the good ways here. I wrote some code http://pastebin.com/VUMEjVpC

I like the 4th way, because I gives me some freedom. But is that a good way of programming? No idea.
Are there better ways then the ways I have in mind? Edited by turbello

##### Share on other sites
I still got a Painter and Recource class but I use heritance here.
[font="Courier New"]class GameEngine: public Painter, public ResourceLoader[/font][/quote]This is a common pattern, where you inherit a class simply to gain it's functionality. Using inheritance like this is often simpler to implement than using composition.
However, it does bend a few OO rules -- usually inheritance is for [font="Lucida Console"]is a[/font], and composition is for [font="Lucida Console"]has a[/font]. It's tempting (and sometimes valid) to use inheritance for this, but generally using member variables is more correct in regards to large-scale engineering concerns.
[font="Courier New"]GameEngine engine = GameEngine::GetInstance();[/font]
[font="Courier New"]engine.StartUp( 640, 480, _T("Game Engine"), 60 );[/font]
[font="Courier New"]engine.Shutdown();[/font][/quote]You can simplify this by using constructors and destructors, and instead of using globals/singletons, you can give these variables the same scope as main has:class GameEngine { public: GameEngine( int w, int h, char* t, int r ) : painter(w,h,t,r) , resourceLoader() { StartUp(); } ~GameEngine() { Shutdown(); }//N.B. calls painter.~Painter() and loader.~Loader() Painter painter; Loader loader; }; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { GameEngine engine( 640, 480, _T("Game Engine"), 60 ); Image* pImage = new Image( engine.loader, "image.bmp" ); while( engine.ProcessMessages() ) engine.painter.DrawImage( pImage, xPos, yPos ); delete pImage; return 0; //N.B. engine.~GameEngine() is called here }You can also pull the painter/loader out of the game engine object like this:class Painter { public: Painter( GameEngine& e ) : engine(e) {} private: GameEngine& engine; }; class Loader { public: Loader( GameEngine& e ) : engine(e) {} private: GameEngine& engine; }; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { GameEngine engine( 640, 480, _T("Game Engine"), 60 ); Painter painter( engine ); Loader loader( engine ); Image* pImage = new Image( loader, "image.bmp" ); while( engine.ProcessMessages() ) painter.DrawImage( pImage, xPos, yPos ); delete pImage; return 0; //N.B. resourceLoader.~ResourceLoader() is called here //N.B. painter.~Painter() is called here //N.B. engine.~GameEngine() is called here }

##### Share on other sites

I want to have the feeling with this game that -> I <- made it all on my own.
[/quote]

Just because you used a nailgun to build that house doesn't mean it was any less of an accomplishment than using a hammer; it just means you built a better house in less time.