src/interface/game_msg.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Messages s'affichant en haut de l'ecran (et écrit dans la console).
00020  *****************************************************************************/
00021 
00022 #include "game_msg.h"
00023 #include <iostream>
00024 #include "../game/time.h"
00025 #include "../graphic/video.h"
00026 #include "../graphic/font.h"
00027 #include "../include/app.h"
00028 #include "game_msg.h"
00029 
00030 // Hauteur de la police de caractere "mini"
00031 #define HAUT_POLICE_MINI 12 // pixels
00032 
00033 // Interligne police "mini" (pour les messages)
00034 #define INTERLIGNE_MINI 3 // pixels
00035 
00036 // Lifespan of messages
00037 #define MSG_LIFESPAN 7000 // ms
00038 
00039 const uint NBR_MSG_MAX = 14;
00040 
00041 GameMessages * GameMessages::singleton = NULL;
00042 
00043 GameMessages * GameMessages::GetInstance() {
00044   if (singleton == NULL) {
00045     singleton = new GameMessages();
00046   }
00047   return singleton;
00048 }
00049 
00050 GameMessages::GameMessages() {
00051 }
00052 
00053 // Clean up the message list
00054 void GameMessages::Reset(){
00055   iterator i;
00056   for( i=liste.begin(); i != liste.end(); i++){
00057     Message * msg = *i;
00058     assert(msg); /* the message must be valid if nothing went wrong */
00059     delete (msg);
00060     msg = NULL;
00061   }
00062   liste.clear();
00063 }
00064 
00065 void GameMessages::Draw(){
00066   // Display messages
00067   uint msgy = 50;
00068 
00069   for( iterator i=liste.begin(); i != liste.end(); ++i ){
00070     (*i)->DrawCenterTop(AppWormux::GetInstance()->video.window.GetWidth()/2, msgy);
00071     msgy += HAUT_POLICE_MINI + INTERLIGNE_MINI;
00072   }
00073 }
00074 
00075 // Erase messages older than MSG_LIFESPAN
00076 void GameMessages::Refresh(){
00077   iterator i;
00078   for( i=liste.begin(); i != liste.end(); ){
00079     Message * msg = *i;
00080     if( MSG_LIFESPAN < Time::GetInstance()->Read() - msg->get_time() ){
00081       delete (msg);
00082       /* erase method return the next element */
00083       i = liste.erase (i);
00084     }
00085     else /* nothing was removed, take next */
00086       i++;
00087   }
00088 }
00089 
00090 // Add a message to the end of a the list of messages
00091 void GameMessages::Add(const std::string &message){
00092   // Debug message
00093   std::cout << "o MSG: " << message << std::endl;
00094   // Add message at the end of the list
00095   Message * newMessage = new Message(message, white_color, Font::GetInstance(Font::FONT_SMALL), Time::GetInstance()->Read());
00096   liste.push_back (newMessage);
00097 
00098   /* if there are too many messages, remove some of them */
00099   while( NBR_MSG_MAX < liste.size()) {
00100     Message * msg = liste.front();
00101     assert(msg); /* the message must be valid if nothing went wrong */
00102     liste.pop_front();
00103     delete msg;
00104   }
00105 }
00106 

Generated on Mon Jan 1 13:10:57 2007 for Wormux by  doxygen 1.4.7