Sign in to follow this  
Black Knight

GameState class and static members

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
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

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