Jump to content
  • Advertisement
Sign in to follow this  
turbello

Writing a game engine

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

If you intended to correct an error in the post then please contact us.

Recommended Posts


At least if I know how to make the user able to make the GameEngine object only once without a singleton. Is this even possible?


You'll be the only user, right? I'm sure you can keep yourself from making more GameEngine objects ;)


Ok and thanks, I will progress on this forum but where? Should I post it here ir is there a special topic for WIP's?


Not sure about this, my guess is on this same post, but maybe posting progress would be off-topic...

Share this post


Link to post
Share on other sites
Advertisement
Hmm I don't think that will solve the problem.

Suppose I write the GameEngine with that bool;

GameEngine object1;
GameEngine object2;
GameEngine object3;

object1.Initialize();
object2.Initialize();
object3.Initialize();


I can only have 1 GameEngine object but with that code I can write multiple objects.
I'm not sure but I think a 'static bool m_bInitialized'' will help.

As soon this is set to true once, it will always be true in the whole application ( at least if you don't change it to false again ). I'll test in tomorrow.


Yes I'm the only user, but I asked in the question " suppose if the engine is for more users ". Edited by turbello

Share this post


Link to post
Share on other sites

[quote name='turbello' timestamp='1323200709' post='4891189']
At least if I know how to make the user able to make the GameEngine object only once without a singleton. Is this even possible?

You'll be the only user, right? I'm sure you can keep yourself from making more GameEngine objects ;)
[/quote]
Agreed; however I'd argue there is a valuable lesson in writing fault tolerant code too.

Share this post


Link to post
Share on other sites

Hmm I don't think that will solve the problem.

Suppose I write the GameEngine with that bool;

GameEngine object1;
GameEngine object2;
GameEngine object3;

object1.Initialize();
object2.Initialize();
object3.Initialize();


I can only have 1 GameEngine object but with that code I can write multiple objects.
I'm not sure but I think a 'static bool m_bInitialized'' will help.

As soon this is set to true once, it will always be true in the whole application ( at least if you don't change it to false again ). I'll test in tomorrow.


Yes I'm the only user, but I asked in the question " suppose if the engine is for more users ".


Correct, from the above perspective nothing stops you short of using some global/static locking mechanism or singleton; however at some point we must assume that the developer has to hold some level of responsibility. If you are writing this for yourself, then that responsibility is on you. If you were writing a framework for others, then there is a certain level of responsibility on the developer using your framework. We can only document how classes are to be used and how they interact and expect others to follow the blueprint and if they deviate from that, consequences must be dealt with.

Just because the singleton pattern will prevent the above from being a problem does not warrant it's use was my underlining concern. The use of singletons have a time and place and it's usually rare to non-existent. They can make our lives easier at first but lots of times, many of us have used them to later regret the use because a future requirement goes against the use of a singleton. For example, imagine you limit a class to a single use per application; however you decide to consider threading options downstream. If that class needs to be created in each local thread, the singleton pattern won't work for this and now if you've referenced that class as a singleton throughout your code, now you have a huge effort to retrofit it as a non-singleton.

Anyhow, as gsamour said, you're the only user so it's ok - you shouldn't be creating multiple copies of that class anyhow if you designed it that way. That's ultimately nothing different than say my EntitySystem class which can be created multiple times if I wish to have various entity systems running in my engine, but I only need one and the engine is responsible for creating that instance for me, I simply use the allocated object in my game code :).

Share this post


Link to post
Share on other sites

class MyClass
{
public:
MyClass() : mInitialized(false) { }
~MyClass() { }

void initialize() {
assert(!mInitialized);
// do initialization here
mInitialized = true;
}

void shutdown() {
assert(mInitialized);
// do shutdown logic
mInitialized = false;
}

private:
bool mInitialized;
};





Change that to a static.
class MyClass
{
public:
MyClass() {
assert(!mInitialized);
mInitialized = true;
}
~MyClass() { }

void initialize() {
// do initialization here
}

void shutdown() {
assert(mInitialized);
// do shutdown logic
mInitialized = false;
}

private:
static bool mInitialized;
};



bool MyClass::mInitialized = false;

Now it is not possible to make 2 of this class (threading issues aside).
Add #ifdef _DEBUG for less release-mode clutter.


L. Spiro

Share this post


Link to post
Share on other sites

You have a deadline of a few months and have only been studying for 1 year. There is a very small chance that you would be able to write a “better” game engine with the same features as theirs plus the actual game on top of it.

Additionally, the chances are absolute that your engine will not be better than theirs. That is not an insult, but a fact. Everyone makes a crappy engine the first time.
Engines are all I make and my first one, with 8 years of C++ experience and 3 or 4 of professional game-development experience (and 5 or 4 prior years of hobby game programming) at the time, was crappy.


The right direction to go is straight back to the folder containing the engine they gave you.
Save engine programming for hobby time. Not for the graded deadline time.


L. Spiro


I'm with you, I've written a few engines in my time and every time they fall short of exactly what I want to achieve. If anything, dude should take that engine they gave you, and improve it wherever you can to make it easier to use. (If it's a c-style engine, do some wrapping, if it's a c++ style engine, find some ways to improve the functionality in a few key areas)

For turbello, I spent my uni time writing engines too, in the time frame I had to write them, they were pretty crappy, I learnt a fair deal writing them, but in hindsight I would have learnt more if I spent that time adding new features to an existing framework, then I would have been learning new features and not how to build simple engines in DirectX/OpenGL. And yes I'm working on my own engine at the moment, but that's because I want a specifically structured engine, with specific supported features; but I'm still a hypocrite regardless.

From what it looks like, you're not building anything advanced (GDI drawing basically) that's only a few weeks work, but still it is better to spend those few weeks porting the code they gave you to a better structure (because your engine and theirs will have a lot of matching code segments here and there ) then it is going through the hell of windows programming in a from scratch project. Thinking about it, for my win32 stuff I'm writing now, I wish to God I had some fucking base code to port to my project, because windows programming sucks.

And I'm probably going to be crucified for this, but in the code you posted, I prefer the third method, because it reduces the number of pointers you've got flying around, in fact I think it's brilliant.

Share this post


Link to post
Share on other sites
At least if I know how to make the user able to make the GameEngine object only once without a singleton. Is this even possible?
If you write clean code, it shouldn't matter if the user decides to make 2 game engine objects... IMO you're worrying way to much about something that has zero impact on the project.
a) your code should still work if the user creates 2 game engines, so there's no reason to spend time programming this restriction into your project.
b) the user isn't going to create 2 game engines anyway, because the user is you! So you're spending time programming features that the project doesn't require...

Share this post


Link to post
Share on other sites

Yes I'm the only user, but I asked in the question " suppose if the engine is for more users ".


I laughed and I cried. Have you even worked on your actual assignment yet? You should take the majority of the advice given here and use the code that was given to you. If you believe building an engine is so easy, then modifying and existing framework, should be a breeze.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!