Sign in to follow this  

Game Engine (C++)

This topic is 3629 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

Hi, I'm trying to write my game engine. But I dont even know If I'm following the right way.. Let me explain;I am creating the input,sound,d3d.. in seperate files as classes, they all have initialiaze() and clean() functions and special functions,and I have an engine class that includes all of the other classes like input,sound and d3d. In its initialiaze function, I call all of the initialiaze functions of the member classes and in the clean() I follow the same way. Till now I explained you about my engine and the question is, do I do something "at least" acceptable :) ? And here comes the more specific question. I can't continue to writing without it :/. I have a Map class and it loads "level" files. In level files(i.e. "level1.mlvl") in the first line there is an integer which is "the number of the sprites that will be used in this level".And the following lines there are file names that will be used to load sprites. there is a LoadSpriteFromFile(string filename) function in 3ddrawing class but I cant use it from my Map class! TO SHORTEN: I have a X class that contains Y and Z classes and I cant use Y.sth() function from Z class..

Share this post


Link to post
Share on other sites
You probably should not be writing an engine.

Quote:

Till now I explained you about my engine and the question is, do I do something "at least" acceptable :) ?

What you've described is far too general to comment on, although it's entirely possible that having "init" and "clean" functions is just useless interface homogenization that you do not actually require (why can't you use constructors and destructors; your entry and exist points are already controlled?)

Quote:

there is a LoadSpriteFromFile(string filename) function in 3ddrawing class but I cant use it from my Map class!

TO SHORTEN: I have a X class that contains Y and Z classes and I cant use Y.sth() function from Z class..

You need an instance of a class to call a non-static member of that class. Your load functions are improperly designed, as they should not require an instance (as such, should not be members), but should rather return them.

Without more information I cannot provide more answers.

Share this post


Link to post
Share on other sites
Hmm I think you are right its very general question but I cant explain it more, its hard :)

And I bet there are many different ways of creating engines but can you reply to my first question? Is creating all parts of the engine in seperate classes and than binding them in one single class acceptable? Or should I change my point of view?

Share this post


Link to post
Share on other sites
Quote:

And I bet there are many different ways of creating engines but can you reply to my first question? Is creating all parts of the engine in seperate classes and than binding them in one single class acceptable? Or should I change my point of view?

I already did. It's probably not; it's too monolithic an approach, suggesting too many "manager" type objects that are frequently not a good idea. Without specifics, it's hard to tell. Frankly, the nature of your questions suggest to me you you're out of your depth.

Write what you need to produce whatever game you have in mind. What you actually need, right now. Rarely do you actually need all those listed subsystems up and running at once.

Share this post


Link to post
Share on other sites
Quote:

Original post by Eralp
TO SHORTEN: I have a X class that contains Y and Z classes and I cant use Y.sth() function from Z class..


So pass Z a reference to Y (either in the constructor or in a member function, whichever is more appropriate in your situation).

Quote:

Original post by jpetrie
Your load functions are improperly designed, as they should not require an instance (as such, should not be members), but should rather return them.


What do you mean? Why can't LoadSpriteFromFile() be a member function (for example, of a class that manages a cache of sprites to save memory)?

Share this post


Link to post
Share on other sites
I actually dont have to write an engine, yes you are right but I think OOP is very enjoyable :) so I want to use all of my OOP knowledge.. I once wrote a game with directx and c++ but it was very bad programmed, though it was smooth and it ran fast.So I dont want to repeat it.I didnt even know a thing about pointers at that time or vectors.. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
So pass Z a reference to Y (either in the constructor or in a member function, whichever is more appropriate in your situation).


I though of that too to pass a reference, but what would it be if I pass all of the classes to all of the classes. And I want to pass the ONE class that has been initialiazed by the main engine class.Do single-ton classes solve my problem, u think?

Share this post


Link to post
Share on other sites
Quote:
Original post by Eralp
I though of that too to pass a reference, but what would it be if I pass all of the classes to all of the classes. And I want to pass the ONE class that has been initialiazed by the main engine class.Do single-ton classes solve my problem, u think?


Definetly not.

Do you really need all the classes to have access to all the classes? If the answer is yes, then your design is way too coupled and you need to rethink it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Quote:
Original post by Eralp
I though of that too to pass a reference, but what would it be if I pass all of the classes to all of the classes. And I want to pass the ONE class that has been initialiazed by the main engine class.Do single-ton classes solve my problem, u think?


Definetly not.

Do you really need all the classes to have access to all the classes? If the answer is yes, then your design is way too coupled and you need to rethink it.


Actually I dont need all the classes to have access to all the classes but I have a question in my mind "What if I have to when I continue my project?" cuz when you do something than you can repeat the same solution.

Thank you for your helps I will take my time reading the links you provided.
See you

Share this post


Link to post
Share on other sites
Quote:

What do you mean? Why can't LoadSpriteFromFile() be a member function (for example, of a class that manages a cache of sprites to save memory)?

Because of the Single Reponsibility Principal, and because object-obsession is counterproductive.

A class that caches resources dilutes its responsibilities by becoming a class that loads resources (sprites) as well. Especially as the number of resource types loaded increases; the class becomes a container for caching logic as well as for resource-type-specific format marshalling.

Object-obsession is about turning everything into an object and everything else into a member function. Member functions should represent semantic actions corresponding to the object model; nothing in the real world can "load" itself from something else, completely re-inventing it's state, so a sprite should not have a "Load" member function or similar. Loading is a process of creation, and should be expressed as such by the loading method being external -- a non-member of some kind. Even disregarding attempts at real-world analogy (which are not always justified), writing a loading method as a member function also decreases encapsulation and maintainability -- for example by requiring the object to be implemented as a 'resetable' object, which introduces a lot of extra pitfalls.

Quote:

I actually dont have to write an engine, yes you are right but I think OOP is very enjoyable :) so I want to use all of my OOP knowledge.. I once wrote a game with directx and c++ but it was very bad programmed, though it was smooth and it ran fast.So I dont want to repeat it.I didnt even know a thing about pointers at that time or vectors.. :)

Writing an engine isn't the only way to practice OO design. Writing a game suffices perfectly well -- if not better. Don't completely abandon the work you did on your previous game. Revisit it and determine what worked well (and reuse that) and needed tweaking (and tweak that).

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:
Original post by Gage64
What do you mean? Why can't LoadSpriteFromFile() be a member function (for example, of a class that manages a cache of sprites to save memory)?

Because of the Single Reponsibility Principal, and because object-obsession is counterproductive.

A class that caches resources dilutes its responsibilities by becoming a class that loads resources (sprites) as well. Especially as the number of resource types loaded increases; the class becomes a container for caching logic as well as for resource-type-specific format marshalling.


I don't think it has to violate the SRP. For example, you could have a cache class that doesn't know what it's caching (a template class for example), and does nothing else. Then you can have an object loader, possibly an abstract base class that can be derived to implement loading of different formats, and does nothing else. Then you can have a factory-type class that uses both classes to create objects. This last class delegates all operations to the other classes, so I wouldn't say that it violates the SRP, or maybe I'm misunderstanding you?

Quote:
Loading is a process of creation, and should be expressed as such by the loading method being external -- a non-member of some kind.


By that logic, shouldn't constructors be external as well (after all, you can put the loading functionality in the constructor)?

Quote:
writing a loading method as a member function also decreases encapsulation and maintainability -- for example by requiring the object to be implemented as a 'resetable' object, which introduces a lot of extra pitfalls.


I don't see why. The loading parameters are just creation parameters that the object needs to be created (by moving the loading functionality into the constructor this becomes more apparent).

Share this post


Link to post
Share on other sites
Quote:

I don't think it has to violate the SRP. For example, you could have a cache class that doesn't know what it's caching (a template class for example), and does nothing else. Then you can have an object loader, possibly an abstract base class that can be derived to implement loading of different formats, and does nothing else. Then you can have a factory-type class that uses both classes to create objects. This last class delegates all operations to the other classes, so I wouldn't say that it violates the SRP, or maybe I'm misunderstanding you?

No, a correctly implemented factory wouldn't violate SRP or cause any of the other maintainability problems. But that's not what you originally mentioned and it's not, for what it's worth, at all what the OP was talking about ("there is a LoadSpriteFromFile(string filename) function in 3ddrawing class") which most certainly does.

In a factory, you're externalizing the format-specific code to the producer callbacks or subclasses, freeing them from mucking up the gears of the factory itself and causing maintainability problems; you're also placing singular responsibilities in each functionality unit.

Quote:

By that logic, shouldn't constructors be external as well (after all, you can put the loading functionality in the constructor)?

No. Constructors are only part of the act of creating a new instance. Calling new is the other (so is simply declaration). These are both externalized concepts. In terms of maintainability, constructors start to toe the line...

Quote:

I don't see why. The loading parameters are just creation parameters that the object needs to be created (by moving the loading functionality into the constructor this becomes more apparent).

...because of this. It starts to become a more contextual issue -- what kind of stuff are you doing in the file IO process, how involved is it, how much of it is parsing a format versus initialization an instance. Constructors initialize, they do not create. Their name is unfortunate here (just like C++ "references" are when you start to discuss "references the concept" versus "references the C++ type category").

Share this post


Link to post
Share on other sites

This topic is 3629 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.

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

Sign in to follow this