Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Hud.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TSGL - Teddy Space Game Library
00004     Copyright (C) 2002 Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: Hud.cpp,v 1.1 2002/01/08 20:47:05 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "config.h"
00026 #include "Teddy/SpaceGame/Hud.h"
00027 #include "Teddy/SpaceGame/PlayerShip.h"
00028 #include "Teddy/SpaceGame/RoamInstance.h"
00029 #include "Teddy/SpaceGame/RoamSphere.h"
00030 #include "Teddy/SpaceGame/Root.h"
00031 #include "Teddy/SpaceGame/ShipType.h"
00032 #include "Teddy/SpaceGame/version.h"
00033 #include "Teddy/Graphics/View.h"
00034 #include "Teddy/Graphics/Features.h"
00035 #include "Teddy/Graphics/ImageFileTexture.h"
00036 #include "Teddy/Maths/Vector.h"
00037 #include "Teddy/Models/ModelInstance.h"
00038 #include "Teddy/PhysicalComponents/GradientFill.h"
00039 #include "Teddy/PhysicalComponents/LayoutConstraint.h"
00040 #include "Teddy/PhysicalComponents/Style.h"
00041 #include "Teddy/PhysicalComponents/WindowFrame.h"
00042 #include "Teddy/Scenes/Camera.h"
00043 #include "Teddy/Scenes/Scene.h"
00044 #include <cstdio>
00045 using namespace Teddy::Graphics;
00046 using namespace Teddy::Models;
00047 using namespace Teddy::PhysicalComponents;
00048 using namespace Teddy::Scenes;
00049 
00050 
00051 namespace Teddy     {
00052 namespace SpaceGame {
00053 
00054 
00056 Hud::Hud( Root *root )
00057 :
00058 Area("Hud")
00059 {
00060     this->root    = root;
00061     target_matrix = NULL;
00062 
00063     constraint    = new LayoutConstraint();
00064     constraint->local_x_offset_pixels  =  10;
00065     constraint->local_y_offset_pixels  =  24;
00066     constraint->parent_x_fill_relative =   1;
00067     constraint->local_x_fill_pixels    = -20;
00068 
00069 #if defined( _DEBUG )
00070     constraint->local_y_fill_pixels    =  95;
00071 #else
00072     constraint->local_y_fill_pixels    =  50;
00073 #endif
00074 
00075 #if !defined( USE_TINY_GL )
00076     this->insert(
00077         new Teddy::PhysicalComponents::GradientFill(  
00078             Color( 0.3f, 0.5f, 0.0f, 0.8f ),
00079             Color( 0.3f, 0.5f, 0.0f, 0.8f ),
00080             Color( 0.1f, 0.4f, 0.0f, 0.5f ),
00081             Color( 0.1f, 0.4f, 0.0f, 0.5f )
00082         )
00083     );
00084 
00085     WindowFrame *f1 = new WindowFrame( "Heads Up Display" );
00086     this->insert( f1 );
00087         
00088 #endif
00089 
00090     ordering = post_self;
00091 }
00092 
00093 
00095 void Hud::setTargetMatrix( Matrix *target ){
00096     this->target_matrix = target;
00097 }
00098 
00099 
00100 extern double speed;
00101 
00102 
00104 void Hud::drawSelf(){
00105     char        hud[100];
00106     const char *name;
00107     char       *unit;
00108     Ship       *ship     = root->getObjectManager()->getPlayerShip();
00109     float       dst      = 0;
00110     float       distance = 0;
00111     double      radius   = 0;
00112     double      angle    = 0;
00113     double      kal_h    = 0;
00114 
00115     view->enable( BLEND );  
00116     view->enable( TEXTURE_2D );
00117     view->setBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00118 
00119     if( ship->getTarget() != NULL ){
00120         name     = ship->getTarget()->getName();
00121         distance = ship->distanceTo( *(ship->getTarget()) );
00122         dst      = distance;
00123         unit     = "m";
00124         if( fabs(distance)>1000.0f ){
00125             distance *= 0.001f;
00126             unit      = "km";
00127         }
00128         if( fabs(distance)>1000.0f ){
00129             distance *= 0.001f;
00130             unit      = "tkm";
00131         }
00132         if( fabs(distance)>1000.0f ){
00133             distance *= 0.001f;
00134             unit      = "Mkm";
00135         }
00136     }else{
00137         name = "-";
00138         unit = "";
00139     }        
00140 
00141     float alpha = 0.9f;
00142 
00143 #if 0
00144     for( int i=0; i<10; i++ ){
00145         average_priority_class[i] = alpha * average_priority_class[i] + (1-alpha) * (float)(priority_class[i]);
00146     }
00147 #endif
00148     color( 0.5f, 1.0f, 0.5f, 1.0f );
00149 
00150     sprintf(
00151         hud,
00152         "Teddy SpaceGame %d.%d % 6.1f fps % 6.1f ms Alt: % 6.1f Target: %s %.3f %s",
00153         TEDDY__SPACE_GAME__VERSION_MAJOR,
00154         TEDDY__SPACE_GAME__VERSION_MINOR,
00155         view->getFps(),
00156         view->getLastFrame(),
00157         0.0f, // roam_height,
00158         name,
00159         distance,
00160         unit
00161     );
00162     drawString( style->small_font, hud, 5, 8 );
00163 
00164     float  speed = ship->getSpeed() * 100.0f;
00165     char  *speed_unit = "m/s";
00166     if( fabs(speed) < 0.1f ){
00167         speed      *= 100.0f;
00168         speed_unit  = "cm/s";
00169     }
00170     if( fabs(speed) < 0.1f ){
00171         speed      *= 10.0f;
00172         speed_unit  = "mm/s";
00173     }
00174     if( fabs(speed) > 1000.0f ){
00175         speed      *= 0.001f;
00176         speed_unit  = "km/s";
00177     }
00178     if( fabs(speed) > 1000.0f ){
00179         speed      *= 0.001f;
00180         speed_unit  = "tkm/s";
00181     }
00182     if( fabs(speed) > 1000.0f ){
00183         speed      *= 0.001f;
00184         speed_unit  = "Mkm/s";
00185     }
00186 
00187     color( 1.0f, 0.8f, 0.3f, 1.0f );
00188     sprintf(
00189         hud,
00190         "Pitch: % 2.2f Roll % 2.2f Speed %.3f %s",
00191         ship->getPitch() * 1000,
00192         ship->getRoll()  * 1000,
00193         speed,
00194         speed_unit
00195     );
00196     drawString( style->small_font, hud, 5, 17 );
00197 
00198 #ifdef _DEBUG
00199     int row;
00200 //  drawString( style->small_font, "Debug Matrix:", 50, 50 );
00201     if( target_matrix != NULL ){
00202         for( row=0; row<4; row++ ){
00203             sprintf(
00204                 hud,
00205                 " % 7.2f % 7.2f % 7.2f % 7.2f",
00206                 target_matrix->m[0][row],
00207                 target_matrix->m[1][row],
00208                 target_matrix->m[2][row],
00209                 target_matrix->m[3][row]
00210             );
00211             drawString( style->small_font, hud, 5, 50+10*row );
00212         }
00213     }
00214 
00215 #if 0
00216 
00217     color( 0.8f, 1.0f, 0.8f, 1.0f );
00218     sprintf( hud, "X: % 10.3f", (float)ship->getPosition().x ); drawString( style->small_font, hud, 280, 50 );
00219     sprintf( hud, "Y: % 10.3f", (float)ship->getPosition().y ); drawString( style->small_font, hud, 280, 60 );
00220     sprintf( hud, "Z: % 10.3f", (float)ship->getPosition().z ); drawString( style->small_font, hud, 280, 70 );
00221 
00222 #else
00223 
00224     if( ship->getTarget() != NULL ){
00225         Vector tpos   = ship->getTarget()->getPosition();
00226         Vector cpos   = ship->getPosition();
00227         Vector delta  = tpos - cpos;
00228         Vector cview  = ship->getViewAxis();
00229         Vector cup    = ship->getUpAxis();
00230         Vector cright = ship->getRightAxis();
00231         delta .normalize();
00232         float  view_dp  = cview  | delta;
00233         float  up_dp    = cup    | delta;
00234         float  right_dp = cright | delta;
00235         float  brk_dst  = ship->getBrakeDistance();
00236 
00237         color( 0.8f, 1.0f, 0.8f, 1.0f );
00238         sprintf( hud, "V: %- 6.2f", view_dp  ); drawString( style->small_font, hud, 280, 50 );
00239         sprintf( hud, "U: %- 6.2f", up_dp    ); drawString( style->small_font, hud, 280, 60 );
00240         sprintf( hud, "R: %- 6.2f", right_dp ); drawString( style->small_font, hud, 280, 70 );
00241 //      sprintf( hud, "U: %- 6.2f", ship->getPitchDistance() ); drawString( style->small_font, hud, 280, 60 );
00242 //      sprintf( hud, "R: %- 6.2f", ship->getRollDistance () ); drawString( style->small_font, hud, 280, 70 );
00243         sprintf( hud, "D: %- 6.2f", brk_dst  ); drawString( style->small_font, hud, 280, 80 );
00244 
00245     }
00246 #endif
00247 
00248 #if 0
00249     view->color( 0.6f, 0.8f, 1.0f, 1.0f );
00250     char c = (roam_update==true)?'T':'F';
00251     sprintf( hud, "Err: % 5.4f %c",   (float)roam_const*1000.0f, c ); drawString( style->small_font, hud, 400, 50 );
00252     sprintf( hud, "Tri: % 7d",        roam_triangle_count          ); drawString( style->small_font, hud, 400, 60 );
00253     sprintf( hud, "D/C: % 3d / % 3d", Scene::drawn, Scene::culled  ); drawString( style->small_font, hud, 400, 70 );
00254 #endif
00255 
00256 #endif
00257 
00258     view->disable( TEXTURE_2D );
00259 
00260     color( 0.0f, 0.0f, 0.0f, 70.5f );
00261     drawFillRect( 40-20-1, 30-2,  44+20+1, 40+1 );
00262     drawFillRect(100-20-1, 30-2, 104+20+1, 40+1 );
00263     drawFillRect(200   -1, 30-2, 400   +1, 40+1 );
00264 
00265     color( 1.0f, 1.0f, 0.0f, 0.95f );
00266     drawRect ( 40-20-2, 30-3,  44+20+2, 40+2 );
00267     drawRect (100-20-2, 30-3, 104+20+2, 40+2 );
00268     drawRect (200     , 30-3, 400     , 40+2 );
00269 
00270     color( 1.0f, 1.0f, 1.0f, 1.0f );
00271     beginLines();
00272     vertex2i(  42, 30 );
00273     vertex2i(  42, 40 );
00274     vertex2i( 102, 30 );
00275     vertex2i( 102, 40 );
00276     vertex2i( 300, 30 );
00277     vertex2i( 300, 40 );
00278     end();
00279 
00280 
00281     color( 0.25f, 1.0f, 0.25f, 0.75f );
00282 
00283     int roll  = (int)(ship->getRoll () * 20 / ship->getShipType()->getMaxRoll () + 0.5);
00284     int pitch = (int)(ship->getPitch() * 20 / ship->getShipType()->getMaxPitch() + 0.5);
00285     int spd   = (int)(ship->getSpeed() * 15 + 0.5);
00286     drawFillRect(  40 + roll , 30,  44 + roll , 40 );
00287     drawFillRect( 100 + pitch, 30, 104 + pitch, 40 );
00288     if( spd == 0 ){
00289         spd = 1;
00290     }
00291     if( spd > 0 ){
00292         color( 0.5f, 1.0f, 0.25f, 0.75f );
00293     }else{
00294         color( 1.0f, 0.5f, 0.25f, 0.75f );
00295     }
00296     drawFillRect( 300 , 30, 300 + spd, 40 );
00297 
00298 }
00299 
00300 
00301 };  //  namespace SpaceGame
00302 };  //  namespace Teddy
00303