TitleScene.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright © 2009, 2010 James Legg.
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 */
00011 #include <config.h>
00012 #include "TitleScene.h"
00013 #include "../Graphics/Window.h"
00014 #include "BasicFonts.h"
00015 #include "Menu.h"
00016 #include "SubMenuItem.h"
00017 
00018 #include <GL/gl.h>
00019 #include <cmath>
00020 #include <sstream>
00021 #include <iostream>
00022 
00023 
00024 namespace UI
00025 {
00026 
00027 TitleScene::TitleScene()
00028     :   t(0),
00029         logo_texture("data/icons/r512.png"),
00030         main_menu(new Menu()),
00031         // main menu items
00032         mi_single_player(L"Single player"),
00033         mi_multiplayer(L"Play"),
00034         mi_replay(L"Watch Replay"),
00035         mi_options(L"Options"),
00036         mi_quit(L"Quit"),
00037         // single player submenu items
00038         mi_sp_grand_prix(L"Grand Prix"),
00039         mi_sp_time_trial(L"Time Trial"),
00040         mi_sp_practice(L"Practice"),
00041         // multiplayer submenu items
00042         mi_mp_c0(L"Track 0", "data/tracks/0")
00043     ,   mi_mp_c1(L"Track 1", "data/tracks/1")
00044     ,   mi_mp_c2(L"Track 2", "data/tracks/2")
00045     ,   mi_mp_c3(L"Track 3", "data/tracks/3")
00046     ,   mi_mp_c4(L"Track 4", "data/tracks/4")
00047 {
00048     // add items to the main menu
00049     //main_menu->insert_item(&mi_single_player);
00050     main_menu->insert_item(&mi_multiplayer);
00051     main_menu->insert_item(&mi_replay);
00052     //main_menu->insert_item(&mi_options);
00053     //main_menu->insert_item(&mi_quit);
00054     
00055     // add items to the single player submenu
00056     Menu * single_player_menu = mi_single_player.get_menu();
00057     single_player_menu->insert_item(&mi_sp_grand_prix);
00058     single_player_menu->insert_item(&mi_sp_time_trial);
00059     single_player_menu->insert_item(&mi_sp_practice);
00060     
00061     // add items to the multiplayer menu
00062     Menu * multiplayer_menu = mi_multiplayer.get_menu();
00063     multiplayer_menu->insert_item(&mi_mp_c0);
00064     multiplayer_menu->insert_item(&mi_mp_c1);
00065     multiplayer_menu->insert_item(&mi_mp_c2);
00066     multiplayer_menu->insert_item(&mi_mp_c3);
00067     multiplayer_menu->insert_item(&mi_mp_c4);
00068 }
00069 
00070 TitleScene::~TitleScene()
00071 {
00072 }
00073 
00074 void TitleScene::take_input(Engine::InputReport & report)
00075 {
00076     // Menu interaction
00077     main_menu->take_input(report);
00078 }
00079 
00080 void TitleScene::update_logic(unsigned int milliseconds_elapsed)
00081 {
00082     t += float(milliseconds_elapsed) * 0.0001;
00083     fps = 1000.0 / float(milliseconds_elapsed);
00084     if (main_menu->want_to_quit())
00085     {
00086         main_loop->pop_scene();
00087     }
00088 }
00089 
00090 void TitleScene::draw()
00091 {
00092     glClearColor(sin(-t) * 0.5 + 0.5,
00093                  sin(-t + 2.0 * M_PI / 3.0) * 0.5 + 0.5,
00094                  sin(-t + 4.0 * M_PI / 3.0) * 0.5 + 0.5,
00095                  0.0);
00096     glClear(GL_COLOR_BUFFER_BIT);
00097     Graphics::Window::get_instance().set_ortho_projection();
00098     
00099     glEnable(GL_TEXTURE_2D);
00100     glEnable(GL_BLEND);
00101     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00102     
00103     // draw logo in the middle.
00104     logo_texture.bind();
00105     glLoadIdentity();
00106     glTranslatef((float) Graphics::Window::get_instance().get_width() / 2.0,
00107                  (float) Graphics::Window::get_instance().get_height() / 2.0,
00108                  0.0 );
00109     float scale = 1.0 / (t * 5.0) + 1.0;
00110     glScalef(scale, scale, scale);
00111     glColor4f(1.0, 1.0, 1.0, 1.0 / scale);  
00112     glBegin(GL_QUADS);
00113         glTexCoord2i(0, 1); glVertex2i(-256, -256);
00114         glTexCoord2i(0, 0); glVertex2i(-256, 256);
00115         glTexCoord2i(1, 0); glVertex2i(256, 256);
00116         glTexCoord2i(1, 1); glVertex2i(256, -256);
00117     glEnd();
00118     glLoadIdentity();
00119     
00120     // write build information in lower right corner.
00121 #ifdef NDEBUG
00122 #define build_message "Built on " __DATE__ " at " __TIME__ " in release mode."
00123 #else
00124 #define build_message "Built on " __DATE__ " at " __TIME__ " in debug mode."
00125 #endif
00126     // shadow
00127     glColor3ub(0, 0, 0);
00128 #ifdef HAVE_FTGL_2_1_3
00129     FTTextureFont & font = BasicFonts::get_instance().small_font;
00130     #define push
00131     #define pop
00132 #else
00133     // old ftgl uses a different name and it messes with the matrix.
00134     FTGLTextureFont & font = BasicFonts::get_instance().small_font;
00135     #define push glPushMatrix();
00136     #define pop glPopMatrix();
00137 #endif
00138     glTranslatef(8.0, 15.0, 0.0); push; font.Render(build_message); pop;
00139     glTranslatef(-2.0, 0.0, 0.0); push; font.Render(build_message); pop;
00140     glTranslatef(1.0, 1.0, 0.0); push; font.Render(build_message); pop;
00141     glTranslatef(0.0, -2.0, 0.0); push; font.Render(build_message); pop;
00142     // text
00143     glColor3ub(255, 255, 255);
00144     glTranslatef(0.0, 1.0, 0.0);
00145     font.Render(build_message);
00146     glLoadIdentity();
00147 #undef build_message    
00148 #undef push
00149 #undef pop
00150 
00151     // show the fps counter in adebug build.
00152 #ifndef NDEBUG
00153     char msg[10] = "f/s: 000";
00154     int fpsi = fps > 999 ? 999 : int(fps); 
00155     msg[5] = fpsi / 100 + '0';
00156     msg[6] = (fpsi / 10) % 10 + '0';
00157     msg[7] = fpsi % 10 + '0';
00158     glTranslatef(Graphics::Window::get_instance().get_width() - 150, 15, 0);
00159     font.Render(msg);
00160     glLoadIdentity();
00161 #endif
00162     
00163     // move to middle & draw menu there.
00164     glTranslatef(float (Graphics::Window::get_instance().get_width() / 2),
00165                  float (Graphics::Window::get_instance().get_height() / 2),
00166                  0.0 );
00167     main_menu->draw();
00168     glLoadIdentity();
00169 }
00170 
00171 void TitleScene::do_sound()
00172 {
00173 }
00174 
00175 void TitleScene::attach_main_loop(MainLoop & main_loop)
00176 {
00177     Scene::attach_main_loop(main_loop);
00178     // also tell the GameStartMenuItems to use this main loop.
00179     mi_mp_c0.set_main_loop(main_loop);
00180     mi_mp_c1.set_main_loop(main_loop);
00181     mi_mp_c2.set_main_loop(main_loop);
00182     mi_mp_c3.set_main_loop(main_loop);
00183     mi_mp_c4.set_main_loop(main_loop);
00184     mi_replay.set_main_loop(main_loop);
00185 }
00186 
00187 }

Get Racer at SourceForge.net. Fast, secure and Free Open Source software downloads

Generated at Mon Sep 6 00:41:13 2010 by Doxygen version 1.4.7 for Racer version svn335.