Example #1
0
void Game_Main()
{
   HDC hdc = GetDC(g_hwnd);
   RECT rect;
   Ball* ball = NULL;
   static Archnoid::USHORT birthRate = 0;

   // white out the playing bounds
   GetClientRect(g_hwnd,&rect);
   FillRect(g_hdcBuffer,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
   
   // draw the playing bounds
   SelectObject( g_hdcBuffer, GetStockObject( WHITE_BRUSH ) );
   Rectangle(g_hdcBuffer, g_gameArea.GetWestWall(), g_gameArea.GetNorthWall(), 
                          g_gameArea.GetEastWall(), g_gameArea.GetSouthWall());

   //g_blockCollection.RandomMoveAll();
   //g_blockCollection.RandomGravitateToCenterAll();
   // HAUKAP - which ball to gravitate to?
   g_blockCollection.RandomGravitateToPointAll( g_balls.GetFirstBall()->GetCntrX(), g_balls.GetFirstBall()->GetCntrY() );

   if( ++birthRate >= GamePlayDefaults::RESPAWN_RATE )
   {
      birthRate = 0;
      g_blockCollection.NewGenerations();
   }
   
   ball = g_balls.GetFirstBall();
   while( ball )
   {
      ball->Move();

      // did ball go out of bounds?
      CollisionType collision = g_gameArea.OutOfBounds(*ball);
      if( !collision )
      {
         // did ball collide with one of the blocks?
         collision = g_blockCollection.IsCollision(*ball);
      }

      if( collision )
      {
         ball->UnMove();
         if( VERTICAL_COLLISION(collision) )
            ball->ResolveCollision(Vertical);
         else if( HORIZONTAL_COLLISION(collision) )
            ball->ResolveCollision(Horizontal);
      }

      ball = g_balls.GetNextBall();
   }


      
   // draw the blocks   
   SelectObject( g_hdcBuffer, GetStockObject( LTGRAY_BRUSH ) );
   const Block* block = g_blockCollection.GetFirstBlock();
   while( block )
   {
      switch(BlockDefaults::SHAPE)
      {
      case ShapeCircle: 
         Ellipse(g_hdcBuffer, block->GetX(), block->GetY(),
                              block->GetX() + block->GetWidth(),
                              block->GetY() + block->GetHeight());
         break;
      case ShapeRect: 
         Rectangle(g_hdcBuffer,block->GetX(),block->GetY(),
                               block->GetX()+block->GetWidth(),
                               block->GetY()+block->GetHeight());
         break;
      default: assert(!"invalid shape");
      }
      block = g_blockCollection.GetNextBlock();
   }

   // draw the ball
   SelectObject( g_hdcBuffer, GetStockObject( BLACK_BRUSH ) );
   ball = g_balls.GetFirstBall();
   while( ball )
   {
      Ellipse( g_hdcBuffer, ball->GetULX(), ball->GetULY(),
                            ball->GetLRX(), ball->GetLRY());
      ball = g_balls.GetNextBall();
   }

   // draw debug/info text
   const char escMsg[] = "Hit any key to exit";
   TextOut(g_hdcBuffer,5,5,escMsg, (int)strlen(escMsg));


   char buf[256];
   std::string buf1( "Blocks: " );
   buf1 += _itoa_s( g_blockCollection.GetNumBlocks(), buf, 10);
   TextOut(g_hdcBuffer,5,20,buf1.c_str(), (int)buf1.size());

   Archnoid::USHORT y = 40;
   Archnoid::USHORT i = 1;
   ball = g_balls.GetFirstBall();
   while( ball )
   {
      sprintf_s( buf, "Ball[%d] [x:%d y:%d sp:%f vX:%f vY:%f]", i, 
               ball->GetCntrX(), ball->GetCntrY(),
               ball->GetSpeed(), ball->GetVectX(), ball->GetVectY() );
      TextOut(g_hdcBuffer, 5, y, buf, (int)strlen(buf) );

      ball = g_balls.GetNextBall();
      y += 15;
      ++i;
   }

   // copy the game area over to the main hdc
   BitBlt(hdc,0,0,g_gameArea.GetEastWall(),g_gameArea.GetSouthWall(),
          g_hdcBuffer,0,0,SRCCOPY);    

   ReleaseDC(g_hwnd,hdc);
   Sleep(GamePlayDefaults::SLEEPTIME);
}