engine.h
class Engine
InitEngine(); //Load all resources into engine Lists
vector pSoundList;
vector pModelList;
sound.h
class Sound
filename
Load
...
model.h
class Model
vector PointerToSoundResource;
...
main.cpp
//include
Engine GameSystem;
main() {
GameSystem.InitEngine();
}
PointerToSoundResource should point to Sound items already loaded into memory in the GameSystem Object. But how would the class Model get access? though this of course will not work, it gets my point across. in model.h''s class body, GameSystem.pSoundList[0] , except, there is no GameSystem object in scope even though GameSystem is made in global scope=/
}
global class
Lets say i have 3 classes
Engine, Sound and Model
One way to do it would be to make your Engine class a Singleton so that there is only one instance that can be accessed by other classes. Engine would also need some kind of public interface to the sound list so that Model objects could get at it.
Start here:
http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&q=singleton+design+pattern
--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com
Start here:
http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&q=singleton+design+pattern
--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com
The Singleton works great, whenever I make a new Engine using the Instance function, it says no way and gives a pointer to the already existing Engine.
A few problems,
-Header File hell. Of course the class Engine is in Engine.h, but to include the two classes Sound and Model into the Engine class, I need to add their headers into Engine.h. And the problem; Sound.h and Model.h also include Engine.h, to create a pointer to the already existing engine. So its like a Loop of header files including each other, a mess.
To try and solve this, I added Engine.h to my main include header, stdafx.h, and defined,extern Engine *pEngine. And Took out #include "Engine.h" from Engine.cpp,Sound.h,Model.h;
I don''t know why this didnt work, but this is my error when I call a simple print member function of Engine from Model.cpp
pEngine->PrintSomething();
Model.cpp(6): error C2040: ''pEngine'' : ''int'' differs in levels of indirection from ''Engine *''
Also something odd, stdafx.h is
#ifndef STDAFX_H
#define STDAFX_H
...//body
#include "Engine.h"
extern Engine *pEngine = Engine::Instance();
#endif
I put a printf function for when a new Instance is craeted and when something else trys to create a second..and so on.
the result:
Instance Engine created.
Instance for Engine already created.
Instance for Engine already created.
Instance for Engine already created.
Engine::Instance was only called once! why did it try to contruct a new Engine 4 times?
Yup, I still yet have more questions. I''m doing my best to understand.
If I move that same idea, creating a Engine *pEngine in a header file, why does it have the same result? I define all my header Files, so they should never be called more then once!
Now, though I''m really not sure if this is part of it or not, I would like to toss this out. I''m using SDL to create my Window and OpenGL setup, this requires a project setting for Debug mode, Debug Multitreaded DLL. Is Engine being called for each thread maybe, something SDL is doing in the background? Why do I need that setting anyway, I understand mutitreading, but DLL? im compiling a exe, not a dll=/
Answering any one of those questions will help me, in the mean time, i''ll keep reading and working on it. Thanks much.
A few problems,
-Header File hell. Of course the class Engine is in Engine.h, but to include the two classes Sound and Model into the Engine class, I need to add their headers into Engine.h. And the problem; Sound.h and Model.h also include Engine.h, to create a pointer to the already existing engine. So its like a Loop of header files including each other, a mess.
To try and solve this, I added Engine.h to my main include header, stdafx.h, and defined,extern Engine *pEngine. And Took out #include "Engine.h" from Engine.cpp,Sound.h,Model.h;
I don''t know why this didnt work, but this is my error when I call a simple print member function of Engine from Model.cpp
pEngine->PrintSomething();
Model.cpp(6): error C2040: ''pEngine'' : ''int'' differs in levels of indirection from ''Engine *''
Also something odd, stdafx.h is
#ifndef STDAFX_H
#define STDAFX_H
...//body
#include "Engine.h"
extern Engine *pEngine = Engine::Instance();
#endif
I put a printf function for when a new Instance is craeted and when something else trys to create a second..and so on.
the result:
Instance Engine created.
Instance for Engine already created.
Instance for Engine already created.
Instance for Engine already created.
Engine::Instance was only called once! why did it try to contruct a new Engine 4 times?
Yup, I still yet have more questions. I''m doing my best to understand.
If I move that same idea, creating a Engine *pEngine in a header file, why does it have the same result? I define all my header Files, so they should never be called more then once!
Now, though I''m really not sure if this is part of it or not, I would like to toss this out. I''m using SDL to create my Window and OpenGL setup, this requires a project setting for Debug mode, Debug Multitreaded DLL. Is Engine being called for each thread maybe, something SDL is doing in the background? Why do I need that setting anyway, I understand mutitreading, but DLL? im compiling a exe, not a dll=/
Answering any one of those questions will help me, in the mean time, i''ll keep reading and working on it. Thanks much.
You don''t need to keep a global engine pointer in your program. Let the singleton manage the singularity of the instance. Everywhere you need to use the engine, just do this:
Engine * engine = Engine::Instance();
The Singleton will manage the one instance and make sure no more are created.
Now, thinking about your cyclic dependency problem (Engine depends on Sound, Sound depends on Engine), can you explain your design a little more? For instance, it sounds (no pun intended) like each model can have several sounds associated with it. Is that correct? Can the same sound belong to several different models? Come to think of it, why do models have to know about sounds? Is the model just the graphics part of your game object (vertices, normals, textures, materials, etc.)? Can you decouple those things...maybe have a GameObject that has a Model and a list of Sounds?
I''m just rambling off the top of my head. Sorry if those are dumb questions - I''ve been programming for a while, but am new to game programming and graphics.
--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com
Engine * engine = Engine::Instance();
The Singleton will manage the one instance and make sure no more are created.
Now, thinking about your cyclic dependency problem (Engine depends on Sound, Sound depends on Engine), can you explain your design a little more? For instance, it sounds (no pun intended) like each model can have several sounds associated with it. Is that correct? Can the same sound belong to several different models? Come to think of it, why do models have to know about sounds? Is the model just the graphics part of your game object (vertices, normals, textures, materials, etc.)? Can you decouple those things...maybe have a GameObject that has a Model and a list of Sounds?
I''m just rambling off the top of my head. Sorry if those are dumb questions - I''ve been programming for a while, but am new to game programming and graphics.
--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com
I solved the header problem, I had no reason to need the Engine pointer in the Sound header, just in the Sound.cpp, which does not have the problem at all. So all is well. Its really a bit more complex then my example, I have also been programming for too long this weekend=/
Thanks again for your tips
Thanks again for your tips
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement