Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

SteveBe

Excuse this question but I just dont know

This topic is 5558 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'm writing a Pong game using GDI. Thought it might be a good start. Now the game is pretty much complete but the code is messy because its not broken down using gamestates. Now I want to tidy everything up by using gamestates but I just can't work out how to do it properly. I know you have to use GameInit(), GameMain(), GameLost() etc. etc. But without using global variables and passing by parameter, how does the game keep track of all the variables. For example if I want to initialise ball, bats, score, colors etc in GameINit(), how do I then share all of these things with GameMain() without passing them all as parameters. Please help me. [edited by - SteveBe on April 1, 2003 9:37:23 AM]

Share this post


Link to post
Share on other sites
Advertisement
Use globals. Really, there''s nothing wrong with them - you can wrap them all up in a struct {} if it makes you feel better

People may say it''s bad OOP style to use globals, but style shouldn''t be more important than it actually working!

The only ''object-based'' approaches to global variables that I know of are static member variables, or Singletons. Global variables are usually much simpler...

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

Share this post


Link to post
Share on other sites
alternatively, make the Game a class, if you prefer.

class Game
{
public:

void Init ();
void Lost ();
void Run ();

private:

int BallX,BallY;
int Score;
};

so, all your game variables are "global" only to the Game itself, which is nice!


edit: typo

[edited by - morebeer on April 1, 2003 9:54:20 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by SteveBe
I''m writing a Pong game using GDI. Thought it might be a good start. Now the game is pretty much complete but the code is messy because its not broken down using gamestates.

Now I want to tidy everything up by using gamestates but I just can''t work out how to do it properly. I know you have to use GameInit(), GameMain(), GameLost() etc. etc. But without using global variables and passing by parameter, how does the game keep track of all the variables. For example if I want to initialise ball, bats, score, colors etc in GameINit(), how do I then share all of these things with GameMain() without passing them all as parameters.

Please help me.



[edited by - SteveBe on April 1, 2003 9:37:23 AM]


You need to create structures to bind your bat variables together, same for the ball, scores, ect... You can even have a structure for the all game, which contains Ball, Bat1, Bat2, ScoreTable... Much more manageable. C++ is actually more intuitive for good design. You need a little bit of Object oriented design. A big scary word for a very simple concept.

example


  

struct Vector2
{
float x;
float y;
};

struct sColor
{
float r, g, b, a;
};
struct sBat
{
Vector2 m_Pos;
sColor m_Color;

void Move(float dy) { m_Pos.y += dy; }

void Render(void)
{
/// use pos and color to render the bat

....
....
....
}
};

struct sScore
{
int m_Score;

void Render(void);
};

struct sPlayer
{
sBat m_Bat;
sScore m_Score;
char m_KeyPress;

void Update(void)
{
if (m_KeyPress = ''w'')
{
m_Bat.Move(1.0f);
}
else if (m_KeyPress = ''x'')
{
m_Bat.Move(-1.0f);
}
}

void Render(void)
{
m_Bat.Render();
m_Score.Render();
}
};

struct sBall
{
Vector2 m_Pos;
sColour m_Color;

void Move(Vector2 NewPos) { m_Pos = NewPos; }

void Render(void)
{
/// use pos and color to render the bat

....
....
....
}
};

struct sGame
{
sPlayer m_Player1;
sPlayer m_Player2;
sBall m_Ball;

void Update(void)
{
m_Player1.Update();
m_Player1.Update();
m_Ball.Update();
}

void Render(void)
{
m_Player1.Render();
m_Player1.Render();
m_Ball.Render();
}

void Init(void)
{
....
....
....
}
void Shutdown(void)
{
}
};



////////////////////// Main /////////////////////////

sGame TheGame;

void Main(void)
{
TheGame.Init();

while(1)
{
TheGame.Update();
TheGame.Render();
}
TheGame.Shutdown();
}


Share this post


Link to post
Share on other sites
quote:
Original post by superpig
People may say it''s bad OOP style to use globals, but style shouldn''t be more important than it actually working!


"Style" means it will still work when you do a small change tomorrow.





"If there is a God, he is a malign thug."
-- Mark Twain

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!