My "First" Game - Issues with classes and organizing
Moderator: PC Supremacists
- Sanshin77
- Chaos Rift Regular
- Posts: 160
- Joined: Tue Mar 10, 2009 9:36 am
- Current Project: C++/SDL engine, zaActionWizardMagic game
- Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
- Programming Language of Choice: C++
My "First" Game - Issues with classes and organizing
Hi, you guys probably haven't seen me before, coz, well, I just joined.
I just started my first big project, using OpenGL, GLUT and C++. Ive only done unfinished, small and bad games earlier(the best I've got is a Pong clone in Flash) and I want to get serious with this one. I've been coding on this for some hours, over 2 days and have alot of the classes I need(unfinished) with the including stuff working, as well as setting up a window.
Anyways, I would want to get some advice for my new project, set let's get onto it:
1. Main/Engine class. I have a class that's named Engine which is sort of the "center", it talks to the Render class, as well as keyboard class etc. Is it common to name a class like this Engine? Eventually what do you usually call it?
2. Rendering Squares. I got my own square class, it has coordinates for the different points and functions to set the position etc, it will also have a function to load a bmp(and maybe png). What I'm wondering is: Where would you place the rendering function for the square? Would you do a Render::RenderSquare(Square *SquarePointer) or would you rather have Square::RenderThis()
3. Tilesystem, Collisions and more. I'm soon implementing a "strict" tilesystem(meening you cant stop between two Tiles, or move diagonally), where would you place this stuff? Could someone give me some general ideas on which classes a 2D tile-based game would have.
I know there's not one answer to these questions, but I want to know what/how you other guys like doing this kind of stuff. Please be polite and correct me if I said anything weird, that's not making sense or anything else, as Im not used to talking about code.
(Edit: Ive asked some more questions about using classes further down the post and edited the Subject/Title of the topic...)
I just started my first big project, using OpenGL, GLUT and C++. Ive only done unfinished, small and bad games earlier(the best I've got is a Pong clone in Flash) and I want to get serious with this one. I've been coding on this for some hours, over 2 days and have alot of the classes I need(unfinished) with the including stuff working, as well as setting up a window.
Anyways, I would want to get some advice for my new project, set let's get onto it:
1. Main/Engine class. I have a class that's named Engine which is sort of the "center", it talks to the Render class, as well as keyboard class etc. Is it common to name a class like this Engine? Eventually what do you usually call it?
2. Rendering Squares. I got my own square class, it has coordinates for the different points and functions to set the position etc, it will also have a function to load a bmp(and maybe png). What I'm wondering is: Where would you place the rendering function for the square? Would you do a Render::RenderSquare(Square *SquarePointer) or would you rather have Square::RenderThis()
3. Tilesystem, Collisions and more. I'm soon implementing a "strict" tilesystem(meening you cant stop between two Tiles, or move diagonally), where would you place this stuff? Could someone give me some general ideas on which classes a 2D tile-based game would have.
I know there's not one answer to these questions, but I want to know what/how you other guys like doing this kind of stuff. Please be polite and correct me if I said anything weird, that's not making sense or anything else, as Im not used to talking about code.
(Edit: Ive asked some more questions about using classes further down the post and edited the Subject/Title of the topic...)
Last edited by Sanshin77 on Fri Mar 13, 2009 10:56 am, edited 1 time in total.
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
My YouTube Channel: http://www.youtube.com/user/Zanchill
Re: My "First" Game - Classes and organizing
First of all, don't worry about your Engine class, unless this is going to be a serious commercial project, don't worry about what all of your classes are called. I am making a game engine and don't even have an Engine class right now, but don't worry about what I'm doing. Your square class should use an x/y position and width/height instead of positions for individual coordinates for each corner unless you want to do things to it like skewing. If your project is going to be small, it would be okay to use Render::RenderSquare(Square *SquarePointer), but if it is a big project or you want to keep it extra organized, use Square::RenderThis() (I recommend Square::RenderThis). A class for a tile system would be something like:
Please note that this is a VERY basic Tilemap class, and I do more 3d programming than 2d.
Code: Select all
class Tilemap
{
public:
void SetTile(int TileColumn, int TileRow);
void GetTile(int TileColumn, int TileRow);
private:
Square Tiles[][];
};
Personal Youtube: http://www.youtube.com/user/xXShadowAsasNXx
Prelimse Studios Youtube: http://www.youtube.com/user/PrelimseStudios
Prelimse Studios Website: http://www.prelimsestudios.com/
Prelimse Studios Youtube: http://www.youtube.com/user/PrelimseStudios
Prelimse Studios Website: http://www.prelimsestudios.com/
- ismetteren
- Chaos Rift Junior
- Posts: 276
- Joined: Mon Jul 21, 2008 4:13 pm
Re: My "First" Game - Classes and organizing
Will the square be a game object. If it will, then i am not sure if it is a good idea to make a game object render itself. I am no expert or anything, so maybe i am wrong?Cole S. wrote:but if it is a big project or you want to keep it extra organized, use Square::RenderThis() (I recommend Square::RenderThis). A class for a tile system would be something like:
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: My "First" Game - Classes and organizing
I usually do Square::Render() which then calls Render::ToScreen(SDL_Image *source, int x, int y), or something like that. That way the Render class is always handling the actual rendering function, but you don't have to have a separate render function for every type of object you would ever need to render. Dunno if this is the best way, but it's worked for me thus far.ismetteren wrote: Will the square be a game object. If it will, then i am not sure if it is a good idea to make a game object render itself. I am no expert or anything, so maybe i am wrong?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: My "First" Game - Classes and organizing
I have Renderer handle it.
Short answer to OP: Sit down and draw out some relationships between classes and class member variables/member function headers. If you're using Linux/KDE, Umbrello is good for this. If not, ArgoUML is buggy and hard to use and the beta is more stable than the release build and I'd really recommend googling for UML diagrammers.
Short answer to OP: Sit down and draw out some relationships between classes and class member variables/member function headers. If you're using Linux/KDE, Umbrello is good for this. If not, ArgoUML is buggy and hard to use and the beta is more stable than the release build and I'd really recommend googling for UML diagrammers.
Last edited by MarauderIIC on Wed Mar 11, 2009 3:09 pm, edited 1 time in total.
Reason: ...class member...
Reason: ...class member...
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- ismetteren
- Chaos Rift Junior
- Posts: 276
- Joined: Mon Jul 21, 2008 4:13 pm
Re: My "First" Game - Classes and organizing
My render class(actully a view class) will be called every frame, and draw all actors that needs to be drawed. combined with an event system, i can make another view/rendere that has an AI, wich instead of draws to the screen, tells the AI what happened in a way it can understand it. The view class will also be handeling the input, there could be other kinds of view's for other things(network, splitscreen). I havent really had a change to test how good it works, because i am having some troubles with the event system :P
I read it in a very big book, so it must be great.. or.. :S
I read it in a very big book, so it must be great.. or.. :S
- Sanshin77
- Chaos Rift Regular
- Posts: 160
- Joined: Tue Mar 10, 2009 9:36 am
- Current Project: C++/SDL engine, zaActionWizardMagic game
- Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
- Programming Language of Choice: C++
Re: My "First" Game - Classes and organizing
Thanks for great replies guys... I actually already did an even simpler map class, consisting of one square, the background, as well as a two dimensional array of positions.The character will snap to the next tile on key release.
I've got some real problems too, if anyone could help me out, this is about Glut.. : (I've removed some of the content to shorten it, includes and irrelevant functions...)
Engine.h:
Engine.cpp:
The uppercase data types are my own typedefs...
Errors:
"...kieraClient/Engine.cpp:12: error: argument of type 'void (Engine::)()' does not match 'void (*)(int)' " (glutTimerFunc)
"...kieraClient/Engine.cpp:14: error: argument of type 'void (Engine::)()' does not match 'void (*)()' " (glutDisplayFunc)
Ive tried declaring GameDrawer as a static void instead but that generates more errors at the glutTimerFunc...
Ofcourse i'll have a separate function for updating, which will be the one that uses glutTimerFunc, it will be something like pEngine->eGame.Update(). If someone could show me the right use of pointers and data types for this, I'd greatly appreciate it...
I've got some real problems too, if anyone could help me out, this is about Glut.. : (I've removed some of the content to shorten it, includes and irrelevant functions...)
Engine.h:
Code: Select all
class Engine
{
public:
VOID SetPointers();
VOID InitEngine(int argc, char** argv);
Render eRender;
Game eGame;
private:
void GameDrawer();
Engine* thisPointer;
Game* pGame;
};
Code: Select all
VOID Engine::InitEngine(int argc, char* argv[]){
glutInit(&argc, argv);
eRender.InitRendering();
pGame = &eGame;
glutTimerFunc(DELAY_MS, GameDrawer, 0);
glutDisplayFunc(this->GameDrawer);
}
VOID Engine::GameDrawer(){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
pGame->Player.background.Draw();
glutSwapBuffers();
}
Errors:
"...kieraClient/Engine.cpp:12: error: argument of type 'void (Engine::)()' does not match 'void (*)(int)' " (glutTimerFunc)
"...kieraClient/Engine.cpp:14: error: argument of type 'void (Engine::)()' does not match 'void (*)()' " (glutDisplayFunc)
Ive tried declaring GameDrawer as a static void instead but that generates more errors at the glutTimerFunc...
Ofcourse i'll have a separate function for updating, which will be the one that uses glutTimerFunc, it will be something like pEngine->eGame.Update(). If someone could show me the right use of pointers and data types for this, I'd greatly appreciate it...
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
My YouTube Channel: http://www.youtube.com/user/Zanchill
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: My "First" Game - Classes and organizing
Because
And GameDrawer is of type (Engine::*)(int value), or something like that.
Try making GameDrawer static (again) and passing it as
Alternatively, create a helper function, ieBut I really don't recommend it, it sort of breaks it, don't you think?
Or, if engine is a singleton, you're in business:
I'm a bit weak when it comes to passing function pointers, but that should do it.
See http://www.parashift.com/c++-faq-lite/p ... l#faq-33.1 and related questions on that page.
More edit: And why did you/would you declare a custom VOID type? That seems strange.
Code: Select all
void glutTimerFunc(unsigned int msecs,void (*func)(int value), value);
Try making GameDrawer static (again) and passing it as
Code: Select all
void glutTimerFunc(DELAY_MS, Engine::GameDrawer, 0);
Code: Select all
Engine* engine;
void draw_wrapper(int value) {
engine->GameDrawer(value);
}
Or, if engine is a singleton, you're in business:
Code: Select all
void draw_wrapper(int value) {
Engine* engine = Engine::GetInstance();
engine->GameDrawer(value);
}
...glutTimerFunc(DELAY, draw_wrapper, 0)...
See http://www.parashift.com/c++-faq-lite/p ... l#faq-33.1 and related questions on that page.
More edit: And why did you/would you declare a custom VOID type? That seems strange.
Last edited by MarauderIIC on Thu Mar 12, 2009 3:44 pm, edited 4 times in total.
Reason: added q
Reason: added q
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Sanshin77
- Chaos Rift Regular
- Posts: 160
- Joined: Tue Mar 10, 2009 9:36 am
- Current Project: C++/SDL engine, zaActionWizardMagic game
- Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
- Programming Language of Choice: C++
Re: My "First" Game - Classes and organizing
Thanks alot Marauder, Ive got it to compile now, finally!
There's one more thing I wonder, how do I go about drawing (non-static) stuff now?
GameDrawer function:
This line produces errors:
KieraClient2/Engine.h:22: error: invalid use of member 'Engine::pGame' in static member function.
I understand why this happens, because its called from a static function. So how can I get my instance of the Game class into this static function?
I guess I'll heave to learn how to use the static keyword. Never really needed it when I didn't use alot of classes...
------------------------------------------------------------------------------------------------------------------------------------------------------------
Edit:
Did a try, and I guess I came closer:
From GameDrawer function:
From "Engine" class header:
Errors:
"Engine::spEngine", referenced from:
__ZN6Engine8spEngineE$non_lazy_ptr in Engine.o
ld: symbol(s) not found
There's one more thing I wonder, how do I go about drawing (non-static) stuff now?
GameDrawer function:
Code: Select all
pGame->Player.background.Draw();
KieraClient2/Engine.h:22: error: invalid use of member 'Engine::pGame' in static member function.
I understand why this happens, because its called from a static function. So how can I get my instance of the Game class into this static function?
I guess I'll heave to learn how to use the static keyword. Never really needed it when I didn't use alot of classes...
------------------------------------------------------------------------------------------------------------------------------------------------------------
Edit:
Did a try, and I guess I came closer:
From GameDrawer function:
Code: Select all
Engine *enginePointer = (Engine*)spEngine;
enginePointer->eGame.Player.background.Draw();
From "Engine" class header:
Code: Select all
class Engine
{
public:
VOID SetPointers();
VOID InitEngine(int argc, char** argv);
Render eRender;
Game eGame;
static void *spEngine;
private:
static void GameDrawer();
static void GoUpdateGame(int value);
Engine* GetInstance(){return thisPointer;}
Engine* thisPointer;
Game* pGame;
};
"Engine::spEngine", referenced from:
__ZN6Engine8spEngineE$non_lazy_ptr in Engine.o
ld: symbol(s) not found
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
My YouTube Channel: http://www.youtube.com/user/Zanchill
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: My "First" Game - Issues with classes and organizing
What does that glut... function do? I'm thinking you might want to use something else.
Also, you would have to pull spEngine from "Engine::"... so, yeah, how you have it wouldn't work, it seems like. Try looking up Singleton pattern (also look up the criticisms of it).
Also, you would have to pull spEngine from "Engine::"... so, yeah, how you have it wouldn't work, it seems like. Try looking up Singleton pattern (also look up the criticisms of it).
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Sanshin77
- Chaos Rift Regular
- Posts: 160
- Joined: Tue Mar 10, 2009 9:36 am
- Current Project: C++/SDL engine, zaActionWizardMagic game
- Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
- Programming Language of Choice: C++
Re: My "First" Game - Issues with classes and organizing
MDK Helped me alot tonight, big thanks to him In the end I solved it by placing the main function in the Engine.cpp file, then declare the Engine Instance pointer globally and made the Update, draw and key event functions normal functions in the Engine.cpp, while not being a part of the class.
Got the Square::Draw() function working as well as a almost complete Tilesystem(collisions remaining...should take short time, since I'm now using a currentRow, NextRow etc system). I'll post some screenshots once I can load my sprites!
Got the Square::Draw() function working as well as a almost complete Tilesystem(collisions remaining...should take short time, since I'm now using a currentRow, NextRow etc system). I'll post some screenshots once I can load my sprites!
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
My YouTube Channel: http://www.youtube.com/user/Zanchill