Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

GameState class and static members

This topic is 3591 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

Ok here is the code :
#ifndef INC_GAMESTATE_H
#define INC_GAMESTATE_H


#include "Drawing.h"
#include "Input.h"

class GameState
{
public:
	virtual void checkInput()=0;
	virtual void update()=0;
	virtual void draw()=0;
protected:
	static Drawing m_Drawing;
	static Input m_Input;
};

#endif //INC_GAMESTATE_H


Basically every game state will derive from this one and implement the functions.But I want every game state to be able to use m_Drawing to draw and m_Input to check input is this a viable solution? I could create m_Drawing and m_Input in the class that contains the game states and pass them as parameters to checkInput(Input& input) and draw(Drawing& drawing).Which one do you guys prefer ^^

Share this post


Link to post
Share on other sites
Advertisement
What does CheckInput() do? I would imagine it's something like:

while (input.hasMore()) {
msg = input.readMessage();
if (msg == MOVE_LEFT) ...
...
}


So every class will need to implement the same as above.

Using reverse dependency, the above can be factored out:

void run() {
while (running) {
GameState * state = getCurrentState();
while (input.hasMore()) {
msg = input.readMessage();
state->CheckInput(msg);
}
state->update();
state->draw(drawing);
}
};
and the checkInput just becomes:
void checkInput(Message & m) {
switch (m.type) {
case ....
}
}


Isn't this cleaner?

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!