Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

testclient/testclientframework.cpp

/// @cond EXCLUDEDTESTSOURCES
/***************************************************************************
 * The contents of this file are subject to the Mozilla Public             *
 * License Version 1.1 (the "License"); you may not use this file          *
 * except in compliance with the License. You may obtain a copy of         *
 * the License at http://www.mozilla.org/MPL/                              *
 *                                                                         *
 * Software distributed under the License is distributed on an "AS         *
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
 * implied. See the License for the specific language governing            *
 * rights and limitations under the License.                               *
 *                                                                         *
 * The Original Code is Game Network Framework (GaNeF).                    *
 *                                                                         *
 * The Initial Developers of the Original Code are                         *
 * Lars Langer and Emanuel Greisen                                         *
 * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
 * All Rights Reserved.                                                    *
 *                                                                         *
 * Contributor(s):                                                         *
 *   none yet....                                                          *
 *                                                                         *
 ***************************************************************************/
#include "testclientframework.h"

#include <SDL/SDL_image.h>


TestClientFramework * TestClientFramework::theclient = 0;

TestClientFramework::TestClientFramework()
   : ClientFramework(0),
   screen(0),
   font(0),
   is_running(false),
   my_own_bite(0),
   server_hostname("localhost")
{
   registerConstructor(424242, &TestClientFramework::constructTestClientSnakeBite);
   TestClientFramework::theclient = this;
}


TestClientFramework::~TestClientFramework()
{
   SDL_Quit();
   if(font) TTF_CloseFont(font);
}


void TestClientFramework::writeHandshakeInitMessage( ClientPacket * fp )
{
}

void TestClientFramework::onHandshakingDiscontinue( ClientPacket * fp )
{
}

bool TestClientFramework::onHandshakeDescription( ClientPacket * fp, ClientPacket * reply )
{
   return true;
}

void TestClientFramework::onDisconnect( )
{
   std::cout << "Disconnected\n";
}

void TestClientFramework::onConnect( )
{
   std::cout << "Connected\n";
}

void TestClientFramework::addTestClientSnakeBite( TestClientSnakeBite * bite )
{
   snakebites.push_back(bite);
}


bool TestClientFramework::initSDL( )
{
   /* Initializes Audio and the CDROM, add SDL_INIT_VIDEO for Video */
   if(SDL_Init(SDL_INIT_VIDEO) < 0)
   {
      std::cout <<"Could not initialize SDL:" << SDL_GetError() << std::endl;
      SDL_Quit();
      return false;
   }
   atexit(SDL_Quit);
   // Init the screen
   screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);

   /* start SDL_ttf */
   if(TTF_Init()==-1)
   {
      printf("TTF_Init: %s\n", TTF_GetError());
      return 2;
   }
   atexit(TTF_Quit); /* remember to quit SDL_ttf */

   // Init the font
   font = TTF_OpenFont("data/FreeMonoBold.ttf", 12);

   // Init the Snakes
   snakebite = IMG_Load("data/snakebite.png");
   snakehead = IMG_Load("data/snakehead.png");
   return true;
}

void TestClientFramework::mainLoop( )
{
   std::cout << "Connecting to: " << server_hostname << std::endl;
   initServerConnection(server_hostname, 8000);
   is_running = true;
   while(is_running)
   {
      // First we get input from User:
      handleUserEvents( );

      // Then we get Packets from the framework
      keepAlive();

      // Then we paint the screen
      draw();

      // Then we sleep to let the CPU live
      psleep(50);
   }
}

void TestClientFramework::handleUserEvents( )
{
   SDL_Event event;
   while ( SDL_PollEvent(&event) )
   {
      switch( event.type )
      {
         case SDL_QUIT:
            is_running = false;
            break;
         case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {
               case SDLK_ESCAPE:
                  is_running = false;
                  break;
               default:
                  std::cout << "You pressed:" << event.key.keysym.sym << std::endl;
                  break;
            }
            break;
         case SDL_MOUSEMOTION:
            if(my_own_bite)
            {
               my_own_bite->sendWantedPosition(event.motion.x, event.motion.y);
            }
            break;
      }
   }
}

void TestClientFramework::draw( )
{
   // Clear the entire screen with dark blue
   SDL_FillRect(screen,NULL,0xf0efff);

   // Write everything to screen
   int y = 0;
   writeToScreen("Packets  Sent: ", packetsSent(), 10, y+=10);
   writeToScreen("Bytes    Sent: ", bytesSent(), 10, y+=10);
   writeToScreen("OutQueue size: ", outQueueSize(), 10, y+=10);
   writeToScreen("Pending  Acks: ", getPendingAckCount(), 10, y+=10);
   writeToScreen("Packets  Resv: ", packetsReceived(), 10, y+=10);
   writeToScreen("Packets  Delv: ", packetsDelivered(), 10, y+=10);
   writeToScreen("Unrel No-Delv: ", getSkippedUnreliablePackets(), 10, y+=10);
   writeToScreen("Rel   No-Delv: ", getSkippedReliablePackets(), 10, y+=10);
   writeToScreen("Bytes    Resv: ", bytesReceived(), 10, y+=10);
   writeToScreen("InQueue  size: ", inQueueSize(), 10, y+=10);
   writeToScreen("Buffered pack: ", bufferedPackets(), 10, y+=10);

   // Paint the snakes
   for(std::vector<TestClientSnakeBite *>::iterator it = snakebites.begin(); it != snakebites.end(); it++)
   {
      TestClientSnakeBite * bite = *it;
      if(bite->isMine() && bite->isHead())
         my_own_bite = bite;
      paintTestClientSnakeBite(bite);
   }

   // And show it
   SDL_Flip(screen);
}

void TestClientFramework::writeToScreen( const std::string & str, int number, int x, int y )
{
   char buffer[100];
   sprintf(buffer, "%i", number);
   std::string newstr = str + buffer;
   writeToScreen(newstr, x, y);
}

void TestClientFramework::writeToScreen( const std::string & str, int x, int y )
{
   SDL_Color color = {0,0,0};
   SDL_Surface * text_surface;
   SDL_Rect dstrect;
   if(!(text_surface=TTF_RenderText_Blended(font,str.c_str(),color)))
   //if(!(text_surface=TTF_RenderText_Solid(font,str.c_str(),color)))
   {
    //handle error here, perhaps print TTF_GetError at least
   }
   else
   {
      // Set up where to blit
      dstrect.x = x;
      dstrect.y = y;
      dstrect.w = text_surface->w;
      dstrect.h = text_surface->h;
      SDL_BlitSurface(text_surface,NULL,screen,&dstrect);
      //perhaps we can reuse it, but I assume not for simplicity.
      SDL_FreeSurface(text_surface);
   }
}

ClientData * TestClientFramework::constructTestClientSnakeBite( ClientFramework * f, ClientPacket * packet )
{
   TestClientSnakeBite * bite = new TestClientSnakeBite(f, packet);
   TestClientFramework::theclient->snakebites.push_back(bite);
   return bite;
}

void TestClientFramework::paintTestClientSnakeBite( TestClientSnakeBite * bite )
{
   SDL_Rect srcrect;
   srcrect.x = 0; srcrect.y = 0; srcrect.w = 40; srcrect.h = 40;
   SDL_Rect dstrect;
   dstrect.x = bite->getX()-srcrect.w/2;
   dstrect.y = bite->getY()-srcrect.h/2;
   dstrect.w = srcrect.w;
   dstrect.h = srcrect.h;
   if(bite->isHead())
   {
      SDL_BlitSurface(snakehead, &srcrect, screen, &dstrect);
   }
   else
   {
      SDL_BlitSurface(snakebite, &srcrect, screen, &dstrect);
   }

}

void TestClientFramework::removeSnakeBite( TestClientSnakeBite * bite )
{
   for(std::vector<TestClientSnakeBite *>::iterator it = snakebites.begin(); it != snakebites.end(); ++it)
   {
      if(*it == bite)
      {
         snakebites.erase(it);
         return;
      }
   }
}


/// @endcond


Generated on Mon Feb 6 12:24:50 2006 for Ganef by  doxygen 1.4.4