Sign in to follow this  
the_vipah

Class/Object interaction question

Recommended Posts

Hello, I am pretty new to OO programming concepts in general. Currently I am attempting to learn the basics of classes/objects/inheritance etc.. by writing a simple pacman clone. What I have is a CMap class and a CPac class. When I am doing certain things with my CPac class such as collision detection with the walls of the maze, I need to retrieve certain things from my CMap class. So far I've simply declared both in a main.cpp file as globals then used the "extern <variable>" in my CPac header file in order to make use of its methods. My question is...there has to be a better way right? One thing I was thinking was passing a pointer to the CMap object to the CPac class's contructor then using it to access whatever methods I need. I see many examples of code that make use of the "->" operator with an object which tells me that pointers are being used. However I'm not sure if this would work OR if its the correct way to do it. Even if someone could just refer me to the correct terminology so I can read up on it... Forgive me if this is a bit confused :) Thanks for any help! -Olivier

Share this post


Link to post
Share on other sites
since you are practicing inheritance and such, why not make a CEntity class and derive not only CPac from it, but CGhost as well. then you can give them all a pointer/reference to the CMap, and the CMap can hold pointers to them all in a container of some sort.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
perhaps have a level class which contains the pac class instances and the map instance.


class CLevel
{
public:
void resolve_level_collisions()
{
//detect collisions between pacman and map
}

private:
CPac Pacman_;
CMap Map_;
};



And then maybe have a game class


class CGame
{
public:
void update_level()
{
pCurrentLevel->Update();
pCurrentLevel->resolve_level_collision();
}

private:
CLevel* pCurrentLevel;
};



Something like that?

Im not a fan of globals, they just dont belong anywhere, its nice to have things wrapped up in correctly names classes.

Share this post


Link to post
Share on other sites
Alright so in the above scenario, where do we enter the game code? Do we still use a "main" function? I am also curious as to where we initially call our "CGhost" and "CPac" classes....are they directly connected to the "CMap" class, or are they somehow a part of the "CGame" class and their pointers passed on to the map?

The actually interaction between each item is where my problem lies, as well as the general structure...What I really need some reasonably simple code which directly illustrates the relationships in a game situation. Anyone able to point me to that? I've read "Focus on SDL", however the message based system he develops later in the book just fly's right over my head :(

Share this post


Link to post
Share on other sites
You do not "call" classes. This may be at the heart of a deeper misunderstanding.

A class is really a sort of template, from which you create instances at runtime.

I think I need to see (code) just what you are currently doing to be properly helpful.

Share this post


Link to post
Share on other sites
Heh, yes, my use of terminology is rather poor, I have to admit.

When I said "call" I meant create an instance of a class.

Here is an example of what I have. Basically i'd be interested in how you would organize all this:

my player class:

class CPaccy
{
private:
int m_x;
int m_y;
int m_move;
int m_key;
SDL_Surface *m_tileset;

public:
CPaccy();
CPaccy(const char * p_filename);
~CPaccy();
void draw();
void keypressed(int p_key);
void move();
};



my map class:

class CMap
{
private:
SDL_Surface *m_tileset;
sTile * m_mapdata;
int m_mapheight;
int m_mapwidth;
int m_viewx;
int m_viewy;
char m_tilemap[10];

void DrawIMG(SDL_Surface *img, int destx, int desty,int srcw, int srch, int srcx, int srcy);
void DrawIMG(SDL_Surface *img, int x, int y);

public:
CMap();
~CMap();
void draw();
void setcell (int p_x, int p_y, struct sTile * p_tile);
void savemap (const char *p_filename);
void loadmap (const char *p_filename);
int getmapwidth ();
int getmapheight ();
int getcollide(int p_x, int p_y);
};



Everything is controled currently from a main.cpp source file with "extern" used to link a global pointer to the main drawing surface as well as some other bits. What I've got is kinda 50/50 OO I guess. Any opinions would be excellent.

-Olivier

Share this post


Link to post
Share on other sites

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