Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 20 Jun 2012
Offline Last Active Nov 02 2012 08:44 AM

#4963038 Preventing Chaos in your game code

Posted by Vero on 25 July 2012 - 01:00 PM

Hi Everyone, I've been coding a simple game in C++ with OpenGL. I'm pretty new to creating games and though I've had a lot of college experience in coding with C++ and Java and .Net I've never really developed anything professionally on this large a scale. The thing is I'm worried things will become more and more disorganized as I progress in making the game.

So far the game is a maze where you load the maze data from a text file and then you have to navigate to the goal. It will progress from there but I want to go over how I'm implementing it.

the Game Loop:
Level1= new Grid("data/Testlvl.txt",20,70);
max_fps=2; //TODO may need to incorporate a file load or mabye just play game from start to finish
int fcount=0;  //frame counter
long long fps_sum=0LL; //sum of frame rate
long long start=milliseconds_now();
//intialise player speed
Level1->player->Velocity.x=40; //40 pixels per second
while(!done) //Game LOOOP!
  if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) //is there a message?
  {  //peek will not halt program while it looks for a message
   if(msg.message==WM_QUIT) // have we received a quit message?
   else{ //if not deal with the window messages
	TranslateMessage(&msg); //Translate the Message
	DispatchMessage(&msg); //Dispatch the Message
  else{ // if there are no messages
   // Draw the Scene. Watch for ESC Key and Quit Messages From DrawGLScene()
   if((active && !DrawGLScene())|| keys[VK_ESCAPE])
	done=TRUE;  // ESC Signalled a Quit
   else{ //not time to quit, update screen
	//drew scene in the if statement
	if(fcount < 60)
	SwapBuffers(hDC); //SwapBuffers (double buffering)
  }  if (!gameover && active)				// If Game Isn't Over And Programs Active Move Objects
   if (keys[VK_RIGHT])
   if (keys[VK_LEFT])
   if (keys[VK_DOWN])
   if (keys[VK_UP])

   Level1->Step(start);// what happens next?
  //outside of live play:
  if(keys['A'] && !ap)
  if (!keys['A'])					 // If 'A' Key Has Been Released
   ap=FALSE;				   // ap Becomes FALSE
  if(keys['Z'] && !zp && max_fps>2)
  if (!keys['Z'])					 // If 'A' Key Has Been Released
   zp=FALSE;				   // ap Becomes FALSE

  //*********update scene****************
  /// Beat first Level Snippet
  ///end of snippet

So right now inorder to switch scenes in the game loop I set global flags to true and false ( good or bed way to do it) and the DrawGLScene() will check the flags and determine which scene to draw,

After declaring a few setup variables I move everything along via time, I even force a max frame rate so that i can control the FPS. (I did this starting out just because I could and now I'm thinking about turning the FPS into something like a reward system collect items increase the FPS makes for smooth gameplay)

All my classes are contained within the Grid Class, the Grid Class will contain a player and a List of objects (coins enemies and such all will inherit the MObject class which has a bunch of virtual functions so I can iterate through the list and call Step() and Draw() functions. Step basically updates all the objects positions and other such statuses given that point in time. everything goes through Grid so when the user enters any key commands the Grid can determine what information flows to what objects it's managing. I keep most things public right now but I hope to eventually makes the objects private and get implement public functions to keep things from getting confuzzled.

so kind of breaking it down to the flow so far psuedo codeish:
		  [chooses what scene to draw based on flags]
    	  assuming game is running 
          Draw header()
                    Draws grid 
                    Draws Players
                    Draws Goal
                    todo: draw enemies
                    todo: draw items
     Check for input
          Move player, enemies
			   update animated items, players enemies,

          Are game changingg conditions met
               progress game

I'd really appreciate constructive criticism and how to make it better, methods to keep things organized, what your own ways to coding the loop are.

I hope my post makes some sense too