Back to index

supertuxkart  0.5+dfsg1
race_results_gui.cpp
Go to the documentation of this file.
00001 //  $Id: race_results_gui.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Eduardo Hernandez Munoz
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include <iostream>
00021 #include <SDL/SDL.h>
00022 
00023 #include "race_results_gui.hpp"
00024 #include "widget_manager.hpp"
00025 #include "kart_properties.hpp"
00026 #include "world.hpp"
00027 #include "menu_manager.hpp"
00028 #include "race_manager.hpp"
00029 #include "highscore_manager.hpp"
00030 #include "unlock_manager.hpp"
00031 #include "translation.hpp"
00032 
00033 enum WidgetTokens
00034 {
00035     WTOK_TITLE,
00036     WTOK_HIGHSCORES,
00037     WTOK_RESULTS,
00038     WTOK_CONTINUE,
00039     WTOK_RESTART_RACE,
00040     WTOK_SETUP_NEW_RACE,
00041     WTOK_FIRST_HIGHSCORE,
00042     //Add 3 because the maximum number of highscores is 3
00043     WTOK_FIRST_RESULT = WTOK_FIRST_HIGHSCORE + 3
00044 };
00045 
00046 RaceResultsGUI::RaceResultsGUI()
00047 {
00048     widget_manager->addTitleWgt( WTOK_TITLE, 60, 7, _("Result") );
00049     widget_manager->breakLine();
00050 
00051     widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 60, 5 );
00052     widget_manager->breakLine();
00053 
00054     widget_manager->addTextWgt( WTOK_RESULTS, 50, 7, _("Race results") );
00055     widget_manager->addTextWgt( WTOK_HIGHSCORES, 50, 7, _("Highscores") );
00056     widget_manager->breakLine();
00057 
00058     widget_manager->switchOrder();
00059 
00060     const unsigned int MAX_STR_LEN = 60;
00061     const unsigned int NUM_KARTS = race_manager->getNumKarts();
00062 
00063     int*  order = new int [NUM_KARTS];
00064     m_score = new char[NUM_KARTS * MAX_STR_LEN];
00065     unsigned int max_name_len = 1;
00066 
00067     for(unsigned int i=0; i < NUM_KARTS; i++)
00068     {
00069         Kart *k = world->getKart(i);             // Display even for eliminated karts!
00070         order[k->getPosition()-1] = i;
00071         const std::string& s = k->getName();
00072         unsigned int l = (unsigned int)s.size();
00073         if(l>max_name_len) max_name_len = l;
00074     }   // for i
00075 
00076     for(unsigned int i = 0; i < NUM_KARTS; ++i)
00077     {
00078         const Kart *KART = world->getKart(order[i]);
00079         const std::string& KART_NAME = KART->getName();
00080         char sTime[20];
00081         const float T      = KART->getFinishTime();
00082         TimeToString(T, sTime);
00083 
00084         //This shows position + driver name + time + points earned + total points
00085         if(race_manager->getRaceMode()==RaceManager::RM_GRAND_PRIX)
00086         {
00087             sprintf((char*)(m_score + MAX_STR_LEN * i), "%d. %s %s +%d %d",
00088                 KART->getPosition(), KART_NAME.c_str(), sTime,
00089                 race_manager->getPositionScore(i+1),
00090                 race_manager->getKartScore(order[i]));
00091         }
00092         else
00093             {
00094             sprintf((char*)(m_score + MAX_STR_LEN * i), "%d. %s %s",
00095                 KART->getPosition(), KART_NAME.c_str(), sTime);
00096         }
00097 
00098         widget_manager->addTextWgt( WTOK_FIRST_RESULT + i, 50, 7,
00099             (char*)(m_score + MAX_STR_LEN * i) );
00100     }
00101 
00102     delete[] order;
00103 
00104     widget_manager->breakLine();
00105     widget_manager->switchOrder();
00106 
00107     const Highscores *hs = world->getHighscores();
00108     int num_scores = hs->getNumberEntries();
00109     m_highscores = new char[num_scores * MAX_STR_LEN];
00110 
00111     for(int i=0; i<num_scores; i++)
00112     {
00113         std::string kart_name, name;
00114         float T;
00115         hs->getEntry(i, kart_name, name, &T);
00116         const int   MINS   = (int) floor ( T / 60.0 ) ;
00117         const int   SECS   = (int) floor ( T - (float) ( 60 * MINS ) ) ;
00118         const int   TENTHS = (int) floor ( 10.0f * (T - (float)(SECS + 60*MINS)));
00119         sprintf((char*)( m_highscores + MAX_STR_LEN * i ),
00120                 "%s: %3d:%02d.%01d", name.c_str(), MINS, SECS, TENTHS);
00121         widget_manager->addTextWgt( WTOK_FIRST_HIGHSCORE + i, 50, 7,
00122             (char*)( m_highscores+MAX_STR_LEN*i ) );
00123     }
00124     widget_manager->breakLine();
00125     widget_manager->breakLine();
00126 
00127     widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 60, 5 );
00128     widget_manager->breakLine();
00129 
00130     // If a new feature was unlocked, only offer 'continue' otherwise add the 
00131     // full menu choices. The new feature menu returns to this menu, and will
00132     // then display the whole menu.
00133     if(unlock_manager->getUnlockedFeatures().size()>0)
00134     {
00135         widget_manager->addTextButtonWgt( WTOK_CONTINUE, 60, 7, _("Continue") );
00136     } else
00137     {
00138         if(race_manager->getRaceMode()==RaceManager::RM_GRAND_PRIX)
00139         {
00140             widget_manager->addTextButtonWgt( WTOK_CONTINUE, 60, 7, _("Continue Grand Prix"));
00141         }
00142         else
00143         {
00144             widget_manager->addTextButtonWgt( WTOK_CONTINUE, 60, 7, _("Back to the main menu"));
00145         }
00146         widget_manager->breakLine();
00147 
00148         widget_manager->addTextButtonWgt( WTOK_RESTART_RACE, 60, 7, _("Race in this track again"));
00149         widget_manager->breakLine();
00150 
00151         if(race_manager->getRaceMode()==RaceManager::RM_QUICK_RACE)
00152         {
00153             widget_manager->addTextButtonWgt( WTOK_SETUP_NEW_RACE, 60, 7, _("Setup New Race"));
00154         }
00155     }   // if !unlock_manager has something unlocked*/
00156 
00157     widget_manager->layout(WGT_AREA_ALL);
00158 }  // RaceResultsGUI
00159 
00160 //-----------------------------------------------------------------------------
00161 RaceResultsGUI::~RaceResultsGUI()
00162 {
00163     widget_manager->reset();
00164     delete[] m_score;
00165     delete[] m_highscores;
00166 }   // ~RaceResultsGUI
00167 
00168 //-----------------------------------------------------------------------------
00169 void RaceResultsGUI::select()
00170 {
00171     // Push the unlocked-feature menu in for now
00172     if(unlock_manager->getUnlockedFeatures().size()>0)
00173     {
00174         // Push the new feature menu on the stack, from where
00175         // control will be returned to this menu.
00176         menu_manager->pushMenu(MENUID_UNLOCKED_FEATURE);
00177         return;
00178     }
00179     switch( widget_manager->getSelectedWgt() )
00180     {
00181     case WTOK_CONTINUE:
00182         // Gets called when:
00183         // 1) something was unlocked
00184         // 2) a Grand Prix is run
00185         // 3) "back to the main menu" otherwise
00186         world->unpause();
00187         race_manager->next();
00188         break;
00189     case WTOK_RESTART_RACE:
00190         world->unpause();
00191         menu_manager->popMenu();
00192         race_manager->restartRace();
00193         break;
00194     case WTOK_SETUP_NEW_RACE:
00195         world->unpause();
00196         race_manager->exit_race();
00197         menu_manager->pushMenu(MENUID_CHARSEL_P1);
00198         break;
00199 
00200     default:
00201         break;
00202     }
00203 }   // select
00204 //-----------------------------------------------------------------------------
00205 void
00206 RaceResultsGUI::handle(GameAction ga, int value)
00207 {
00208   // Attempts to close the menu are silently discarded
00209   // since they do not make sense at this point.
00210   if (ga == GA_LEAVE)
00211    return;
00212   else
00213     BaseGUI::handle(ga, value);
00214 }
00215 
00216 /* EOF */