• 12
• 14
• 13
• 10
• 11

# Need help, found problem with my code

This topic is 3934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need some help/a suggestion. In my 2D game, when I set the destination coordinates of a player, I am using a function called SetDest to do so. The problem is, I realized that I have my Game class set this for the player, and not my Player class. The reason I did it this way in the first place, and am asking now, is because I'm not sure how to set those coordinates in the Player class. Here is what my Game class look like:
class Game
{
public:
Game(void);
~Game(void);
void Init(int,int);
static void Think();                        // Need to Fix this...
static void Draw();
static Map map;
static GLuint ObjTex[6];
static std::vector<Player *> play;
static int Select(int,int);
static void DrawSprites();                  // and this...
static void Mouse(int,int,int,int);
static void Normal(unsigned char key,int,int);
static void SetDest(int PlayerID, int x, int y);  // and this.
};


If I set the destination, I just call the SetDest function in Game, and set things accordingly there: play[ID].DestX = x...etc How do I make it so I can do it in the Player class? Shouldn't it be done there? I want to do, in a for loop, if it is selected: play.SetDest(x,y); But in the Player class, it doesn't know what the vector 'play' is, how can I fix this? Or should I continue to let the Game class do it? Also, I think I need to have the Player class think for itself, as well as draw its own sprites... Jeff

##### Share on other sites

class Player {public:    void Think() { // ... }    void SetDest(int x, int y)  { // ... }    // ...};// Somewhere elsevector<Player*> players;for (int i = 0; i < players.size(); ++i) {    // Get shortcut (for a nicer syntex)    Player *p = &players;    p->SetDest(x, y);    p->Think();    // ...}

##### Share on other sites
Well I realized that I was asking the question for a dumb reason, but I'm still having an issue.

I am doing this in my Game.cpp, in the draw function(also calls draw for map):
for(unsigned int i=0;i<play.size();i++)		play->Draw(i);

then in Player.cpp:
void Player::Draw(int n){	glLoadName(n);	glBlendFunc(GL_DST_COLOR,GL_ZERO);	glBindTexture(GL_TEXTURE_2D,ObjTex[Tex+1]);	glBegin(GL_TRIANGLE_STRIP);		glTexCoord2f(1.0f,1.0f);glVertex3f(x+40.0f, y+40.0f, 0.0f);		glTexCoord2f(1.0f,0.0f);glVertex3f(x+40.0f, y, 0.0f);		glTexCoord2f(0.0f,1.0f);glVertex3f(x, y+40.0f, 0.0f);		glTexCoord2f(0.0f,0.0f);glVertex3f(x, y, 0.0f);	glEnd();	glBlendFunc(GL_ONE,GL_ONE);	glBindTexture(GL_TEXTURE_2D,ObjTex[Tex]);	glBegin(GL_TRIANGLE_STRIP);		glTexCoord2f(1.0f,1.0f);glVertex3f(x+40.0f, y+40.0f, 0.0f);		glTexCoord2f(1.0f,0.0f);glVertex3f(x+40.0f, y, 0.0f);		glTexCoord2f(0.0f,1.0f);glVertex3f(x, y+40.0f, 0.0f);		glTexCoord2f(0.0f,0.0f);glVertex3f(x, y, 0.0f);	glEnd();}

But it complains about my attributes being non-static:
error C2597: illegal reference to non-static member 'BaseObject::Tex'...for all of them, x, y, dx, dy, etc.

It is doing this because my Draw function has to be static, because I'm calling it via OpenGL, in my main()....glutDisplayFunc(game.Draw);

Is there a workaround for it complaining about the attributes being non-static?

Thanks so far.
Jeff

##### Share on other sites
For basic design issues: A static function should not deal with individual units of a class, it should only affect the class as a whole.

As you have it, the "draw" function should not be static, and it should just draw one player. Maybe you could put a universal "drawEverything" function in the Game class that would call the draw functions of each of the players in your vector<Player *> play?

##### Share on other sites
Thats they way I have it, but now that you say that I see my mistake. In my Main() function, OpenGL uses game.Draw: glutDisplayFunc(game.Draw);

Then inside game.Draw:
void Game::Draw(){		map.Draw();	Think();                               // Will soon put this inside for loop below, so its play->Think();	for(unsigned int i=0;i<play.size();i++)		play->Draw(i);	glutSwapBuffers();}

I had my Player::Draw function static as well, I fixed that, and it builds and runs. However now when i select a player and move it, it'll move all of them currently on the screen. That obviously means its a problem when I do glLoadName();,since it was working fine before. Anyone see (in my Player::Draw code above) what might be wrong?

Jeff