Advertisement Jump to content
Sign in to follow this  

Question about gamestates and flow

This topic is 4882 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 currenty a little puzzled about what is a good way to organize execution flow and with gamestates. I'm trying to make the good old tetris clone and have looked at both Aaron's tutorials and Rob Loach's source code. (Many thanx to the authors) For the sake of learning, since I have figured out how these examples work, I will try to make a tetris myself. So far i think my biggest stumbling block is not the classes or implementation of the routines, but the gamestates and interaction between classes. The big picture so to speak. I have some drawing routines scattered over different classes (just stubs), not in a central place, and still don't know how to approach the gamestates. Would it make sense to make a single object of a GameState class? And would it make sense to lump all the drawing together in one place, a function or a class maybe? My purpose is not getting things done quickly but learning to program, I prefer it very organised in classes and clear. Any advice would be most welcome. Sorry if I'm not so clear myself and if this post is a bit lengthy. I always seem to do that in English. P.S. I'm programming in C++ with SDL.

Share this post

Link to post
Share on other sites
I prefer making everything In my game an entity, and that entity handles itself, i.e. draws/loads and stuff. But I have a wrapper class for sdl that does the dirty work. So my entity can say just "DrawImg(currentAnimationFrame, posX, posY)" or "LoadBmp(filename)". And the SdlWrapper takes care of the blitting and loading, all the dirtywork. So you could say Im using a bit of both.

Im not entirely sure how to do thisthough , I havent really done so many games so that you can say I "usually"... I will follow this thread with interest. :)

Share this post

Link to post
Share on other sites
So your entities call SdlWrapper, is that right? And how do you then organize the calls to the drawing routines of your entities?
But yes, good idea, I think this will make my design more consistent

Share this post

Link to post
Share on other sites
Yes, every entity calls and registers with this "master gamestate class" youre talking about. There the gamestatclass gives the entity a number. In the main loop (owned by the gamestateclass) reaches "Sync screen" or "Draw World" or whatever you might call it, all registred entities are called from 0 to 24 (or how many active entities we now have).

std::vector <cEntity> EntityDataBase;

EntityDataBase[ i ].Draw();

Not straight copied from my project but you should get the idea. The draw function then looks which animation is played, and if its time to go to the next frame, and then draws itself.

void cEntity::Draw() {
int timeDifference = SDL_GetTicks() - frameTimeStart; // how much time has passed?

if( timeDifference >= Animation[currentAnimation][currentFrame].displayTime ) { // time to switch frame?
if( currentFrame == Animation[currentAnimation].size() ) // we're on the last fram
currentFrame = 0; // so go back to the first frame
currentFrame++; // otherwise go to next frame!

frameTimeStart = SDL_GetTicks(); // store the time which the new frame started showing

Sdl->DrawImg( Animation[currentAnimation][currentFrame].image, posX, posY );

void SdlWrapper::DrawImg(SDL_Surface* img, int x, int y) { // draws the image at those coordinates on the screen
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);

Maybe I should add that I havent finished this project really and Im having some problems. But the idea seems fine to me in theory.

There :) Hope it helped! :)

Share this post

Link to post
Share on other sites
Alright, thanx Mizipzor, I'll go for a similar approach and start filling in the details, untill I get stuck maybe. The only thing I didn't get is how you would make a vector of everything that needs to be drawn. Do you make a CEntity base class and inherit the different classes?

This 'master gamestate class' I realize will actually be the class of an instance of the 'whole' game. Which actually might be nice, because I can wrap some of the global crap in it's data members. It will look something like this:
class Game {
//Starts game, returns false on error:
bool Initialize (int WIDTH, int HEIGHT, Uint32 FLAG);
//Every member function returns a GAMESTATE enumeration:
GAMESTATE Settings ();
GAMESTATE RequestExit();
void HandleGenericEvents();
GAMESTATE CurrentState;
//Bunch of global data and maybe some general blitting and timing routines here

//And some pseudo code for the game loop in InGame():
if (TimeForNewFrame) {
Entity1.OnFrame() //Wanna draw some?
Entity2.OnFrame() //...etcetera.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!