Jump to content
  • Advertisement
Sign in to follow this  
geo2004

Need help, found problem with my code

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

If you intended to correct an error in the post then please contact us.

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 void LoadTex();
	static Map map;
	static GLuint ObjTex[6];
	static std::vector<Player *> play;
	static void AddPlayer(Player *);
	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 this post


Link to post
Share on other sites
Advertisement
How about this:



class Player {
public:
void Think() { // ... }
void SetDest(int x, int y) { // ... }
// ...
};


// Somewhere else
vector<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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Ha ha, I figured it out. *slaps forehead* I didn't change the function in my selection routine to play->Draw(); The selection works now, but I found one weird thing. If I have a player selected, then spawn more, I can move all of the new ones with out selecting them (I don't even know how to select mult. ones yet). Am I doing my glLoadName() in the wrong spot? I'm doing it each time before I draw the sprite.

Jeff

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!