OO Design Question
Moderator: Coders of Rage
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
OO Design Question
If I have a class, and inside one of my class functions I wish to call a function that is part of my main code. How can I do this if my class is written as part of example.cpp and the function I want to call is part of main.cpp with the #include example.h ?
My love is like a Haddoken, it's downright fierce!
Re: OO Design Question
The real question is why would you want to do that?Maevik wrote:If I have a class, and inside one of my class functions I wish to call a function that is part of my main code. How can I do this if my class is written as part of example.cpp and the function I want to call is part of main.cpp with the #include example.h ?
Maybe it's a smart design choice, but maybe, there is a better way to do it, so what are you trying to achieve exactly?
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: OO Design Question
You would need the function prototype for the function that's in main.cpp to be present in example.cpp or example.h. Then you can call it normally. However, in an OO design, it is uncommon to have (m)any functions besides main() not be in a class of some sort (in other words, main() is the only function not inside a class). You may want to take another look at your design.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: OO Design Question
ok, lets say for instance I want to make a game using the blitting functions similar to those in lazyfoo's SDL tutorials. Let's also say that I want my objects to handle blitting their own surface to the screen.
So based on what Marauder is saying, I shouldn't even have the applySurface() function in my main code in the first place?
Could I then build a class called like "screen" or something, and give it those functions ( load Images, blit surfaces, initialize video mode... ) then pass the instance of my screen class into the squirrel.processInput() function along with &event ?
Would this be the /better/ way to do it? Is it good practice to create a class for something if you know you will only make one instance of it?
edit: edited for clarity
Code: Select all
//The blitting function
void applySurface( int x , int y , SDL_Surface* source , SDL_Surface* destination , SDL_Rect* clip = NULL )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source , clip , destination , &offset );
}
Code: Select all
//An Example Class, let's say its a squirrel
class squirrel
{
private:
int xLoc;
int yLoc;
SDL_Surface image;
public:
void processInput( SDL_Event* event );
}
Code: Select all
//An example of what the processInput() function might look like
void squirred::processInput( SDL_Event* event)
{
if( event.type == SDL_KEYDOWN )
{
if( event.key.keysym.sym == SDLK_UP )
{
yLoc -= 5;
}
else if( event.key.keysym.sym == SDLK_UP )
{
yLoc += 5;
}
/* blah blah blah, test screen limits and left and right and all that stuff */
}
//Here is where I would put the blit function to have my squirrel function draw itself to the screen.
}
Could I then build a class called like "screen" or something, and give it those functions ( load Images, blit surfaces, initialize video mode... ) then pass the instance of my screen class into the squirrel.processInput() function along with &event ?
Code: Select all
void squirred::processInput( SDL_Event* event , myScreenClass* screen )
{
.
.
.
screen.applySurface( xLoc , yLoc , image , screen.screenSurface );
}
edit: edited for clarity
My love is like a Haddoken, it's downright fierce!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: OO Design Question
I have something like
The game invokes the Renderer on all of its objects. Renderer does all the drawing. Then the game tells the renderer it's done sending objects, and the Renderer finishes. You could also pass the Renderer a list of objects and make it iterate through them, instead of one at a time, then it would know when it should wrap up the frame without the Game telling it to.
Code: Select all
class Game {
Renderer renderer;
vector<Object*> objects;
void init();
void handleInput();
void update(); //Update positions
void drawAll();
};
Code: Select all
class Object {
int x, y;
SDL_Surface* mySurface; //This is also an element in Renderer::loadedTextures
public:
SDL_Surface* getSurface();
};
Code: Select all
class Renderer {
...
SDL_Surface* screen;
SDL_Surface* background;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
map<string, SDL_Surface*> loadedTextures;
public:
void draw(SDL_Surface* surf, int x, int y);
void finishFrame();
~Renderer(); //This frees all the textures.
};
Code: Select all
void Game::drawAll() {
for (vector<Object*>::iterator i = objects.begin(); i != objects.end(); ++i) {
renderer.draw(i->getSurface(), i->getX(), i->getY());
renderer.finishFrame();
}
Code: Select all
void Renderer::draw(SDL_Surface* surf, int x, int y) {
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(surf, NULL, screen, &offset);
}
Code: Select all
void Renderer::finishFrame() {
SDL_Flip(screen);
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 255, 255, 255)); //Reset
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: OO Design Question
Yeah, lazyfoo's tutorials are great, his code is terrible.. The design you suggested should be done like that, instead use something similar to what MarauderIIC suggested. I use something similar to this. (Not as complicated as that yet, but as I see it, I'll be there soon).
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: OO Design Question
Thanks for all the constructive feedback guys. This is immesely helpful.
My love is like a Haddoken, it's downright fierce!
- 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: OO Design Question
Thanks Marauder, that helped answer part of my question posted elsewhere.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: OO Design Question
And it helped me too!
thanks for the advice
thanks for the advice
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: OO Design Question
I was trying to see how far I could get trying to build an engine with this design. I came to a compiler error along the way that I absolutely cannot figure out. I thought that since I'm essentially trying to mirror your code, you might be in a position to help me :DMarauderIIC wrote:I have something likeCode: Select all
class Game { Renderer renderer; vector<Object*> objects; void init(); void handleInput(); void update(); //Update positions void drawAll(); };
Code: Select all
class Object { int x, y; SDL_Surface* mySurface; //This is also an element in Renderer::loadedTextures public: SDL_Surface* getSurface(); };
Code: Select all
class Renderer { ... SDL_Surface* screen; SDL_Surface* background; const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; map<string, SDL_Surface*> loadedTextures; public: void draw(SDL_Surface* surf, int x, int y); void finishFrame(); ~Renderer(); //This frees all the textures. };
Code: Select all
void Game::drawAll() { for (vector<Object*>::iterator i = objects.begin(); i != objects.end(); ++i) { renderer.draw(i->getSurface(), i->getX(), i->getY()); renderer.finishFrame(); }
Code: Select all
void Renderer::draw(SDL_Surface* surf, int x, int y) { SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface(surf, NULL, screen, &offset); }
The game invokes the Renderer on all of its objects. Renderer does all the drawing. Then the game tells the renderer it's done sending objects, and the Renderer finishes. You could also pass the Renderer a list of objects and make it iterate through them, instead of one at a time, then it would know when it should wrap up the frame without the Game telling it to.Code: Select all
void Renderer::finishFrame() { SDL_Flip(screen); SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 255, 255, 255)); //Reset }
Code: Select all
#pragma once
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"
#include <string>
#include <vector>
#include "Renderer.h"
class Game
{
private:
//Renderer rend;
public:
Game(void);
bool init();
void input( SDL_Event* event , Renderer* rend );
~Game(void);
};
Code: Select all
error C2061: syntax error : identifier 'Renderer'
It doesn't seem to be a problem with the Renderer class, since if I initialize an instance in the main function it works fine.
Is there something special I need to do when making a class a member of another class?
My love is like a Haddoken, it's downright fierce!
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: OO Design Question
So this just started working when I created a test class to see if it was happening with any other classes that I make members. So although I was still confused about what had happened and why, I continued on with my project. After about 10 minutes, it just stopped compiling again. The exact line I changed between successful compiles was adding this line ( and only this line ) to the Renderer class deconstructor
Even after I removed the line it would not compile again. This doesn't make sense to me at all.
Code: Select all
SDL_FreeSurface( background );
My love is like a Haddoken, it's downright fierce!
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: OO Design Question
did you allocate background? That may be the problem.Maevik wrote:So this just started working when I created a test class to see if it was happening with any other classes that I make members. So although I was still confused about what had happened and why, I continued on with my project. After about 10 minutes, it just stopped compiling again. The exact line I changed between successful compiles was adding this line ( and only this line ) to the Renderer class deconstructorEven after I removed the line it would not compile again. This doesn't make sense to me at all.Code: Select all
SDL_FreeSurface( background );
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: OO Design Question
That wouldn't stop it from compiling. Maevik, what error message are you getting when you compile?Ginto8 wrote:did you allocate background? That may be the problem.Maevik wrote:So this just started working when I created a test class to see if it was happening with any other classes that I make members. So although I was still confused about what had happened and why, I continued on with my project. After about 10 minutes, it just stopped compiling again. The exact line I changed between successful compiles was adding this line ( and only this line ) to the Renderer class deconstructorEven after I removed the line it would not compile again. This doesn't make sense to me at all.Code: Select all
SDL_FreeSurface( background );
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: OO Design Question
error C2061: syntax error : identifier 'Renderer'
It's been happening on and off still, to the point where I'm almost convinced it's a Visual Studio 9 bug. I'm really hesitant to chok it up to that though, especially when I'm learning. I just can't find any reason for the behavior I'm seeing, and I'll change something seemingly unrelated, then it wont compile, then when I change it back, it still wont compile.
It's been happening on and off still, to the point where I'm almost convinced it's a Visual Studio 9 bug. I'm really hesitant to chok it up to that though, especially when I'm learning. I just can't find any reason for the behavior I'm seeing, and I'll change something seemingly unrelated, then it wont compile, then when I change it back, it still wont compile.
My love is like a Haddoken, it's downright fierce!
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: OO Design Question
I'm not sure what you're asking. I didn't put background on the heap if that's what you mean, but I did load the file and optimize it with SDL_DisplayFormat(), it works and displays the background ~half the time.Ginto8 wrote:did you allocate background? That may be the problem.Maevik wrote:So this just started working when I created a test class to see if it was happening with any other classes that I make members. So although I was still confused about what had happened and why, I continued on with my project. After about 10 minutes, it just stopped compiling again. The exact line I changed between successful compiles was adding this line ( and only this line ) to the Renderer class deconstructorEven after I removed the line it would not compile again. This doesn't make sense to me at all.Code: Select all
SDL_FreeSurface( background );
My love is like a Haddoken, it's downright fierce!