Back to index

extremetuxracer  0.5beta
game_type_select.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (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 
00020 
00021 #include "game_type_select.h"
00022 
00023 #include "ppgltk/audio/audio.h"
00024 #include "ppgltk/alg/defs.h"
00025 
00026 #include "game_mgr.h"
00027 
00028 
00029 GameTypeSelect::GameTypeSelect()
00030 {
00031        pp::Vec2d pos(0,0);
00032 
00033     mp_enterEventBtn = new pp::Button(pos,
00034                                  pp::Vec2d(300, 40), 
00035                                  "button_label", 
00036                                  _("Enter a campaign") );
00037     mp_enterEventBtn->setHilitFontBinding( "button_label_hilit" );
00038     mp_enterEventBtn->signalClicked.Connect(pp::CreateSlot(this,&GameTypeSelect::eventSelect));
00039 
00040     mp_practiceBtn = new pp::Button(pos,
00041                               pp::Vec2d(300, 40),
00042                               "button_label",
00043                               _("Practice")      );
00044     mp_practiceBtn->setHilitFontBinding( "button_label_hilit" );
00045     mp_practiceBtn->signalClicked.Connect(pp::CreateSlot(this,&GameTypeSelect::practice));
00046 
00047     mp_highscoreBtn = new pp::Button(pos,
00048                               pp::Vec2d(300, 40),
00049                               "button_label",
00050                               _("Highscore")     );
00051     mp_highscoreBtn->setHilitFontBinding( "button_label_hilit" );
00052     mp_highscoreBtn->signalClicked.Connect(pp::CreateSlot(this,&GameTypeSelect::highscore));
00053 
00054        mp_configureBtn = new pp::Button(pos,
00055                               pp::Vec2d(300, 40),
00056                               "button_label",
00057                               _("Configuration") );
00058     mp_configureBtn->setHilitFontBinding( "button_label_hilit" );
00059     mp_configureBtn->signalClicked.Connect(pp::CreateSlot(this,&GameTypeSelect::configuration));
00060 
00061        mp_creditsBtn = new pp::Button(pos,
00062                               pp::Vec2d(300, 40),
00063                               "button_label",
00064                               _("Credits") );
00065     mp_creditsBtn->setHilitFontBinding( "button_label_hilit" );
00066        mp_creditsBtn->signalClicked.Connect(pp::CreateSlot(this,&GameTypeSelect::credits));
00067 
00068        mp_quitBtn = new pp::Button(pos,
00069                            pp::Vec2d(300, 40),
00070                            "button_label",
00071                            _("Quit") );
00072 
00073        mp_quitBtn->setHilitFontBinding( "button_label_hilit" );
00074     mp_quitBtn->signalClicked.Connect(pp::CreateSlot(this,&GameTypeSelect::quit));
00075 
00076     play_music( "start_screen" );  
00077 }
00078 
00079 GameTypeSelect::~GameTypeSelect()
00080 {
00081        delete mp_enterEventBtn;
00082        delete mp_practiceBtn;
00083        delete mp_highscoreBtn;
00084        delete mp_configureBtn;
00085        delete mp_creditsBtn;
00086        delete mp_quitBtn;   
00087 }
00088 
00089 void
00090 GameTypeSelect::loop(float timeStep)
00091 {
00092        update_audio();
00093 
00094     set_gl_options( GUI );
00095 
00096     clear_rendering_context();
00097 
00098     setWidgetPositions();
00099 
00100     UIMgr.setupDisplay();
00101 
00102        drawSnow(timeStep);
00103 
00104     theme.drawMenuDecorations();
00105 
00106     UIMgr.draw();
00107        
00108     reshape( getparam_x_resolution(), getparam_y_resolution() );
00109 
00110     winsys_swap_buffers();
00111 }
00112 
00113 
00114 void
00115 GameTypeSelect::setWidgetPositions()
00116 {
00117     pp::Button* button_list[] = { mp_enterEventBtn,
00118                               mp_practiceBtn,
00119                               mp_highscoreBtn,
00120                               mp_configureBtn,
00121                               mp_creditsBtn,
00122                               mp_quitBtn };
00123     int w = getparam_x_resolution();
00124     int h = getparam_y_resolution();
00125     int box_height;
00126     int box_max_y;
00127     int top_y;
00128     int bottom_y;
00129     int num_buttons = sizeof( button_list ) / sizeof( button_list[0] );
00130     int i;
00131     int tot_button_height = 0;
00132     int button_sep =0;
00133     int cur_y_pos;
00134 
00135     box_height = 210;
00136     box_max_y = h - 128;
00137 
00138     bottom_y = int(0.4*h - box_height/2);
00139 
00140     if ( bottom_y + box_height > box_max_y ) {
00141        bottom_y = box_max_y - box_height;
00142     }
00143 
00144     top_y = bottom_y + box_height;
00145 
00146     for (i=0; i<num_buttons; i++) {
00147               tot_button_height += int(button_list[i]->getHeight());
00148     }
00149 
00150     if ( num_buttons > 1 ) {
00151        button_sep = ( top_y - bottom_y - tot_button_height ) / 
00152            ( num_buttons - 1 );
00153        button_sep = MAX( 0, button_sep );
00154     }
00155 
00156     cur_y_pos = top_y;
00157     for (i=0; i<num_buttons; i++) {
00158        cur_y_pos -= int(button_list[i]->getHeight());
00159        button_list[i]->setPosition(pp::Vec2d( w/2.0 - button_list[i]->getWidth()/2.0,
00160                        cur_y_pos ) );
00161        cur_y_pos -= button_sep;
00162     }
00163 }
00164 
00165 bool
00166 GameTypeSelect::keyPressEvent(SDLKey key)
00167 {
00168        switch (key){
00169               case 'q':
00170               case 27: // Esc 
00171                      winsys_exit(0);
00172               return true;;
00173               case 'e':
00174               case 13: // Enter
00175                      mp_enterEventBtn->simulateMouseClick();
00176               return true;
00177               case 'p':
00178                      mp_practiceBtn->simulateMouseClick();
00179                      return true;
00180               case 'c':
00181                      mp_creditsBtn->simulateMouseClick();
00182                      return true;
00183               default:
00184                      return false;
00185        }      
00186 }
00187 
00188 void
00189 GameTypeSelect::eventSelect()
00190 {
00191        gameMgr->reset(GameMgr::EVENT);
00192 
00193        set_game_mode( EVENT_SELECT );
00194        UIMgr.setDirty();
00195 }
00196 
00197 void
00198 GameTypeSelect::credits()
00199 {
00200        set_game_mode( CREDITS );
00201        UIMgr.setDirty();
00202 }
00203 
00204 void
00205 GameTypeSelect::practice()
00206 {
00207        gameMgr->reset(GameMgr::PRACTICING);
00208 
00209        set_game_mode( RACE_SELECT );
00210        UIMgr.setDirty();
00211 }
00212 
00213 void
00214 GameTypeSelect::highscore()
00215 {
00216        set_game_mode( HIGHSCORE );
00217        UIMgr.setDirty();
00218 }
00219 
00220 void
00221 GameTypeSelect::configuration()
00222 {
00223        set_game_mode( CONFIGURATION );
00224        UIMgr.setDirty();
00225 }
00226 
00227 void
00228 GameTypeSelect::quit()
00229 {
00230        winsys_exit( 0 );
00231 }