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

testclientframework.cpp

00001 /// @cond EXCLUDEDTESTSOURCES
00002 /***************************************************************************
00003  * The contents of this file are subject to the Mozilla Public             *
00004  * License Version 1.1 (the "License"); you may not use this file          *
00005  * except in compliance with the License. You may obtain a copy of         *
00006  * the License at http://www.mozilla.org/MPL/                              *
00007  *                                                                         *
00008  * Software distributed under the License is distributed on an "AS         *
00009  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
00010  * implied. See the License for the specific language governing            *
00011  * rights and limitations under the License.                               *
00012  *                                                                         *
00013  * The Original Code is Game Network Framework (GaNeF).                    *
00014  *                                                                         *
00015  * The Initial Developers of the Original Code are                         *
00016  * Lars Langer and Emanuel Greisen                                         *
00017  * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
00018  * All Rights Reserved.                                                    *
00019  *                                                                         *
00020  * Contributor(s):                                                         *
00021  *   none yet....                                                          *
00022  *                                                                         *
00023  ***************************************************************************/
00024 #include "testclientframework.h"
00025 
00026 #include <SDL/SDL_image.h>
00027 
00028 
00029 TestClientFramework * TestClientFramework::theclient = 0;
00030 
00031 TestClientFramework::TestClientFramework()
00032    : ClientFramework(0),
00033    screen(0),
00034    font(0),
00035    is_running(false),
00036    my_own_bite(0),
00037    server_hostname("localhost")
00038 {
00039    registerConstructor(424242, &TestClientFramework::constructTestClientSnakeBite);
00040    TestClientFramework::theclient = this;
00041 }
00042 
00043 
00044 TestClientFramework::~TestClientFramework()
00045 {
00046    SDL_Quit();
00047    if(font) TTF_CloseFont(font);
00048 }
00049 
00050 
00051 void TestClientFramework::writeHandshakeInitMessage( ClientPacket * fp )
00052 {
00053 }
00054 
00055 void TestClientFramework::onHandshakingDiscontinue( ClientPacket * fp )
00056 {
00057 }
00058 
00059 bool TestClientFramework::onHandshakeDescription( ClientPacket * fp, ClientPacket * reply )
00060 {
00061    return true;
00062 }
00063 
00064 void TestClientFramework::onDisconnect( )
00065 {
00066    std::cout << "Disconnected\n";
00067 }
00068 
00069 void TestClientFramework::onConnect( )
00070 {
00071    std::cout << "Connected\n";
00072 }
00073 
00074 void TestClientFramework::addTestClientSnakeBite( TestClientSnakeBite * bite )
00075 {
00076    snakebites.push_back(bite);
00077 }
00078 
00079 
00080 bool TestClientFramework::initSDL( )
00081 {
00082    /* Initializes Audio and the CDROM, add SDL_INIT_VIDEO for Video */
00083    if(SDL_Init(SDL_INIT_VIDEO) < 0)
00084    {
00085       std::cout <<"Could not initialize SDL:" << SDL_GetError() << std::endl;
00086       SDL_Quit();
00087       return false;
00088    }
00089    atexit(SDL_Quit);
00090    // Init the screen
00091    screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
00092 
00093    /* start SDL_ttf */
00094    if(TTF_Init()==-1)
00095    {
00096       printf("TTF_Init: %s\n", TTF_GetError());
00097       return 2;
00098    }
00099    atexit(TTF_Quit); /* remember to quit SDL_ttf */
00100 
00101    // Init the font
00102    font = TTF_OpenFont("data/FreeMonoBold.ttf", 12);
00103 
00104    // Init the Snakes
00105    snakebite = IMG_Load("data/snakebite.png");
00106    snakehead = IMG_Load("data/snakehead.png");
00107    return true;
00108 }
00109 
00110 void TestClientFramework::mainLoop( )
00111 {
00112    std::cout << "Connecting to: " << server_hostname << std::endl;
00113    initServerConnection(server_hostname, 8000);
00114    is_running = true;
00115    while(is_running)
00116    {
00117       // First we get input from User:
00118       handleUserEvents( );
00119 
00120       // Then we get Packets from the framework
00121       keepAlive();
00122 
00123       // Then we paint the screen
00124       draw();
00125 
00126       // Then we sleep to let the CPU live
00127       psleep(50);
00128    }
00129 }
00130 
00131 void TestClientFramework::handleUserEvents( )
00132 {
00133    SDL_Event event;
00134    while ( SDL_PollEvent(&event) )
00135    {
00136       switch( event.type )
00137       {
00138          case SDL_QUIT:
00139             is_running = false;
00140             break;
00141          case SDL_KEYDOWN:
00142             switch(event.key.keysym.sym)
00143             {
00144                case SDLK_ESCAPE:
00145                   is_running = false;
00146                   break;
00147                default:
00148                   std::cout << "You pressed:" << event.key.keysym.sym << std::endl;
00149                   break;
00150             }
00151             break;
00152          case SDL_MOUSEMOTION:
00153             if(my_own_bite)
00154             {
00155                my_own_bite->sendWantedPosition(event.motion.x, event.motion.y);
00156             }
00157             break;
00158       }
00159    }
00160 }
00161 
00162 void TestClientFramework::draw( )
00163 {
00164    // Clear the entire screen with dark blue
00165    SDL_FillRect(screen,NULL,0xf0efff);
00166 
00167    // Write everything to screen
00168    int y = 0;
00169    writeToScreen("Packets  Sent: ", packetsSent(), 10, y+=10);
00170    writeToScreen("Bytes    Sent: ", bytesSent(), 10, y+=10);
00171    writeToScreen("OutQueue size: ", outQueueSize(), 10, y+=10);
00172    writeToScreen("Pending  Acks: ", getPendingAckCount(), 10, y+=10);
00173    writeToScreen("Packets  Resv: ", packetsReceived(), 10, y+=10);
00174    writeToScreen("Packets  Delv: ", packetsDelivered(), 10, y+=10);
00175    writeToScreen("Unrel No-Delv: ", getSkippedUnreliablePackets(), 10, y+=10);
00176    writeToScreen("Rel   No-Delv: ", getSkippedReliablePackets(), 10, y+=10);
00177    writeToScreen("Bytes    Resv: ", bytesReceived(), 10, y+=10);
00178    writeToScreen("InQueue  size: ", inQueueSize(), 10, y+=10);
00179    writeToScreen("Buffered pack: ", bufferedPackets(), 10, y+=10);
00180 
00181    // Paint the snakes
00182    for(std::vector<TestClientSnakeBite *>::iterator it = snakebites.begin(); it != snakebites.end(); it++)
00183    {
00184       TestClientSnakeBite * bite = *it;
00185       if(bite->isMine() && bite->isHead())
00186          my_own_bite = bite;
00187       paintTestClientSnakeBite(bite);
00188    }
00189 
00190    // And show it
00191    SDL_Flip(screen);
00192 }
00193 
00194 void TestClientFramework::writeToScreen( const std::string & str, int number, int x, int y )
00195 {
00196    char buffer[100];
00197    sprintf(buffer, "%i", number);
00198    std::string newstr = str + buffer;
00199    writeToScreen(newstr, x, y);
00200 }
00201 
00202 void TestClientFramework::writeToScreen( const std::string & str, int x, int y )
00203 {
00204    SDL_Color color = {0,0,0};
00205    SDL_Surface * text_surface;
00206    SDL_Rect dstrect;
00207    if(!(text_surface=TTF_RenderText_Blended(font,str.c_str(),color)))
00208    //if(!(text_surface=TTF_RenderText_Solid(font,str.c_str(),color)))
00209    {
00210     //handle error here, perhaps print TTF_GetError at least
00211    }
00212    else
00213    {
00214       // Set up where to blit
00215       dstrect.x = x;
00216       dstrect.y = y;
00217       dstrect.w = text_surface->w;
00218       dstrect.h = text_surface->h;
00219       SDL_BlitSurface(text_surface,NULL,screen,&dstrect);
00220       //perhaps we can reuse it, but I assume not for simplicity.
00221       SDL_FreeSurface(text_surface);
00222    }
00223 }
00224 
00225 ClientData * TestClientFramework::constructTestClientSnakeBite( ClientFramework * f, ClientPacket * packet )
00226 {
00227    TestClientSnakeBite * bite = new TestClientSnakeBite(f, packet);
00228    TestClientFramework::theclient->snakebites.push_back(bite);
00229    return bite;
00230 }
00231 
00232 void TestClientFramework::paintTestClientSnakeBite( TestClientSnakeBite * bite )
00233 {
00234    SDL_Rect srcrect;
00235    srcrect.x = 0; srcrect.y = 0; srcrect.w = 40; srcrect.h = 40;
00236    SDL_Rect dstrect;
00237    dstrect.x = bite->getX()-srcrect.w/2;
00238    dstrect.y = bite->getY()-srcrect.h/2;
00239    dstrect.w = srcrect.w;
00240    dstrect.h = srcrect.h;
00241    if(bite->isHead())
00242    {
00243       SDL_BlitSurface(snakehead, &srcrect, screen, &dstrect);
00244    }
00245    else
00246    {
00247       SDL_BlitSurface(snakebite, &srcrect, screen, &dstrect);
00248    }
00249 
00250 }
00251 
00252 void TestClientFramework::removeSnakeBite( TestClientSnakeBite * bite )
00253 {
00254    for(std::vector<TestClientSnakeBite *>::iterator it = snakebites.begin(); it != snakebites.end(); ++it)
00255    {
00256       if(*it == bite)
00257       {
00258          snakebites.erase(it);
00259          return;
00260       }
00261    }
00262 }
00263 
00264 
00265 /// @endcond
00266 

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