display.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // Das Tank - Copyright (C) 2006, 2007 Windsor Schmidt <windsor@windsorworld.net>
00004 //
00005 // This program is free software; you can redistribute it and/or modify it
00006 // under the terms of the GNU General Public License as published by the Free
00007 // Software Foundation; either version 2 of the License, or (at your option)
00008 // any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful, but WITHOUT
00011 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 // more details.
00014 //
00015 // You should have received a copy of the GNU General Public License along with
00016 // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017 // Place, Suite 330, Boston, MA 02111-1307 USA
00018 //
00020 
00021 
00022 #ifndef STDAFX
00023 #include "stdafx.h"
00024 #endif
00025 
00026 GLuint textures[256]; // store references to all the textures used in the game
00027 CAnimation * animations[256]; // store references to all the animations used in the game
00028 static float stripeAlpha; // transparency of big text background stripe displayed in-game
00029 
00036 void InitGL(int Width, int Height)               // we call this right after our OpenGL window is created
00037 {
00038        glViewport(0, 0, Width, Height);
00039        glClearColor(0.282f, 0.282f, 0.282f, 0.0f);
00040        glClearDepth(1.0);                                      // enables clearing of the depth buffer
00041        glDepthFunc(GL_LESS);                                   // the type of depth test to do
00042        glEnable(GL_DEPTH_TEST);                         // enables depth testing
00043        glEnable( GL_TEXTURE_2D );                       // enable texture mapping
00044        glShadeModel(GL_SMOOTH);                         // enables smooth color shading
00045 
00046        glMatrixMode(GL_PROJECTION);
00047        glLoadIdentity();                                       // reset the projection matrix
00048 
00049        gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // calculate window aspect ratio
00050 
00051        glMatrixMode(GL_MODELVIEW);
00052 }
00053 
00054 
00061 void DrawGLScene(CWorld * ThisWorld)
00062 {
00063        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);            // clear the screen and the depth buffer
00064        glLoadIdentity();                                                                   // reset the view
00065        glEnable(GL_TEXTURE_2D);
00066        
00067        Vector vDistance, vDistanceReverse, vMidpoint;
00068        float distance;
00069 
00070        // get distance and midpoint between players
00071        vDistance = ThisWorld->Player[0]->Position - ThisWorld->Player[1]->Position;
00072        distance = vDistance.Magnitude();
00073        vMidpoint = vDistance * ThisWorld->fZoomBias;
00074 
00075        // set translate and zoom for drawing game world
00076        glPushMatrix();
00077 
00078        switch(ThisWorld->iStartingState)
00079        {
00080        case STARTINGSTATE_IDLE: // zoom in a little
00081               glTranslatef(-(ThisWorld->Player[0]->Position.x - vMidpoint.x),
00082                      -(ThisWorld->Player[0]->Position.y - vMidpoint.y),
00083                      -25.0f);
00084               break;
00085 
00086        case STARTINGSTATE_READY: // zoom in on green tank
00087               glTranslatef(-ThisWorld->Player[1]->Position.x,-ThisWorld->Player[0]->Position.y, -6.0f);
00088               break;
00089 
00090        case STARTINGSTATE_SET: // zoom in on brown tank
00091               glTranslatef(-ThisWorld->Player[0]->Position.x,-ThisWorld->Player[0]->Position.y, -6.0f);
00092               break;
00093 
00094        case STARTINGSTATE_GO: // normal view applies
00095               glTranslatef(-(ThisWorld->Player[0]->Position.x - vMidpoint.x),
00096                      -(ThisWorld->Player[0]->Position.y - vMidpoint.y),
00097                      -20.0f - (distance * 0.8)); // zoom out as players move away from each other
00098               break;
00099 
00100        case STARTINGSTATE_RUN: // normal view applies
00101               glTranslatef(-(ThisWorld->Player[0]->Position.x - vMidpoint.x),
00102                      -(ThisWorld->Player[0]->Position.y - vMidpoint.y),
00103                      -20.0f - (distance * 0.8)); // zoom out as players move away from each other
00104                      ThisWorld->fFinalZoom = -20.0f - (distance * 0.8);
00105               break;
00106 
00107        case STARTINGSTATE_VICTORY: // zoom in on dead player
00108 
00109               ThisWorld->fFinalZoom *= 0.9997;
00110               ThisWorld->fZoomBias *= 0.9996;
00111 
00112               if(ThisWorld->Player[1]->iHealth <= 0)
00113               {
00114                      vMidpoint.Reverse();
00115 
00116                      glTranslatef(-(ThisWorld->Player[1]->Position.x - vMidpoint.x),
00117                             -(ThisWorld->Player[1]->Position.y - vMidpoint.y),
00118                             ThisWorld->fFinalZoom); // zoom out as players move away from each other
00119               }
00120               else
00121               {
00122                      glTranslatef(-(ThisWorld->Player[0]->Position.x - vMidpoint.x),
00123                             -(ThisWorld->Player[0]->Position.y - vMidpoint.y),
00124                             ThisWorld->fFinalZoom); // zoom out as players move away from each other
00125               }
00126               break;
00127        }
00128 
00129        DrawTiles(ThisWorld);
00130        DrawObjects(ThisWorld);
00131        ThisWorld->DrawEffects();
00132 
00133        glPopMatrix();
00134 
00135        // set translate and zoom for drawing HUD
00136        glPushMatrix();
00137 
00138        glScalef(.1,.1,1);
00139        glTranslatef(-((float) PLAYFIELD_SIZE/2), -((float) PLAYFIELD_SIZE/2), -4.2f);      
00140        DrawHUD(ThisWorld);
00141 
00142        glPopMatrix();
00143 }
00144 
00145 
00153 void DrawHUD(CWorld * ThisWorld)
00154 {
00155        glPushMatrix();
00156        glTranslatef(0,0,0.2);
00157 
00158        glDisable(GL_TEXTURE_2D);
00159        
00160        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
00161 
00162        glEnable (GL_BLEND);
00163 
00164        // left-side health bar
00165        glColor4f(0.9,0.3,0.2,0.9);
00166        DrawBarGraph(PLAYFIELD_SIZE + 0.2, PLAYFIELD_SIZE - 1.0,
00167                              -5.0, 8.0, ThisWorld->Player[1]->iHealth);
00168 
00169        // right-side health bar
00170        glColor4f(0.2,0.5,0.9,0.9);
00171        DrawBarGraph(PLAYFIELD_SIZE + 0.2, PLAYFIELD_SIZE - 1.0, 
00172                              PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE - 8.0, ThisWorld->Player[0]->iHealth);
00173 
00174        // left-side transparent health bar background area
00175        glBegin(GL_QUADS);
00176        glColor4f(0.0,0.0,0.0,0.25);
00177        glVertex2f(-5.0, PLAYFIELD_SIZE + 0.2);
00178        glVertex2f(8.0, PLAYFIELD_SIZE + 0.2);
00179        glVertex2f(8.0, PLAYFIELD_SIZE - 1.0);
00180        glVertex2f(-5.0, PLAYFIELD_SIZE - 1.0);
00181 
00182        // right-side transparent health bar background area
00183        glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE + 0.2);
00184        glVertex2f(PLAYFIELD_SIZE - 8.0, PLAYFIELD_SIZE + 0.2);
00185        glVertex2f(PLAYFIELD_SIZE - 8.0, PLAYFIELD_SIZE - 1.0);
00186        glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE - 1.0);
00187        glEnd();
00188        
00189        glEnable(GL_TEXTURE_2D);
00190        glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
00191 
00192        // left-side weapon display card
00193        switch(ThisWorld->Player[1]->iWeapon)
00194        {
00195        case WEAPONID_NULL:
00196               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_TANK));
00197               break;
00198        case WEAPONID_PEA_SHOT:
00199               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_PS));
00200               break;
00201        case WEAPONID_BOUNCY_SHOT:
00202               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_BS));
00203               break;
00204        case WEAPONID_SUPER_BULLET:
00205               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_SB));
00206               break;
00207        case WEAPONID_LAND_MINE:
00208               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_LM));
00209               break;
00210        case WEAPONID_HEAT_SEEKER:
00211               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_HS));
00212               break;
00213        }
00214 
00215        glBegin(GL_QUADS);
00216        glColor3f(1.0,1.0,1.0);
00217        glTexCoord2d(0.0, 1.0); glVertex2f(-5.0, PLAYFIELD_SIZE - 1.5);
00218        glTexCoord2d(1.0, 1.0); glVertex2f(-1.0, PLAYFIELD_SIZE - 1.5);
00219        glTexCoord2d(1.0, 0.0); glVertex2f(-1.0, PLAYFIELD_SIZE - 5.5);
00220        glTexCoord2d(0.0, 0.0); glVertex2f(-5.0, PLAYFIELD_SIZE - 5.5);
00221        glEnd();
00222 
00223        // right-side weapon display card
00224        switch(ThisWorld->Player[0]->iWeapon)
00225        {
00226        case WEAPONID_NULL:
00227               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_TANK));
00228               break;
00229        case WEAPONID_PEA_SHOT:
00230               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_PS));
00231               break;
00232        case WEAPONID_BOUNCY_SHOT:
00233               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_BS));
00234               break;
00235        case WEAPONID_SUPER_BULLET:
00236               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_SB));
00237               break;
00238        case WEAPONID_LAND_MINE:
00239               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_LM));
00240               break;
00241        case WEAPONID_HEAT_SEEKER:
00242               glBindTexture( GL_TEXTURE_2D, GetTexture(TEXID_POWERUP_HS));
00243               break;
00244        }
00245 
00246        glBegin(GL_QUADS);
00247        glTexCoord2d(1.0, 1.0); glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE - 1.5);
00248        glTexCoord2d(0.0, 1.0); glVertex2f(PLAYFIELD_SIZE + 1.0, PLAYFIELD_SIZE - 1.5);
00249        glTexCoord2d(0.0, 0.0); glVertex2f(PLAYFIELD_SIZE + 1.0, PLAYFIELD_SIZE - 5.5);
00250        glTexCoord2d(1.0, 0.0); glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE - 5.5);
00251        glEnd();
00252 
00253        glDisable(GL_TEXTURE_2D);
00254        glDisable(GL_BLEND);
00255 
00256        // left-side HUD border
00257        glBegin(GL_QUADS);
00258        glColor3f(0.0,0.0,0.0);
00259        glVertex2f(-5.0, PLAYFIELD_SIZE + 0.3); // top
00260        glVertex2f(8.0, PLAYFIELD_SIZE + 0.3);
00261        glVertex2f(8.0, PLAYFIELD_SIZE + 0.2);
00262        glVertex2f(-5.0, PLAYFIELD_SIZE + 0.2);
00263        glVertex2f(-5.0, PLAYFIELD_SIZE - 1.0); // bottom
00264        glVertex2f(8.0, PLAYFIELD_SIZE - 1.0);
00265        glVertex2f(8.0, PLAYFIELD_SIZE - 1.1);
00266        glVertex2f(-5.0, PLAYFIELD_SIZE - 1.1);
00267        glVertex2f(-5.1, PLAYFIELD_SIZE + 0.3); // left
00268        glVertex2f(-5.0, PLAYFIELD_SIZE + 0.3);
00269        glVertex2f(-5.0, PLAYFIELD_SIZE - 1.1);
00270        glVertex2f(-5.1, PLAYFIELD_SIZE - 1.1);
00271        glVertex2f(8.0, PLAYFIELD_SIZE + 0.3); // right
00272        glVertex2f(8.1, PLAYFIELD_SIZE + 0.3);
00273        glVertex2f(8.1, PLAYFIELD_SIZE - 1.1);
00274        glVertex2f(8.0, PLAYFIELD_SIZE - 1.1);
00275 
00276        // right-side HUD border
00277        glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE + 0.3); // top
00278        glVertex2f(PLAYFIELD_SIZE - 8.0, PLAYFIELD_SIZE + 0.3);
00279        glVertex2f(PLAYFIELD_SIZE - 8.0, PLAYFIELD_SIZE + 0.2);
00280        glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE + 0.2);
00281        glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE - 1.0); // bottom
00282        glVertex2f(PLAYFIELD_SIZE - 8.0, PLAYFIELD_SIZE - 1.0);
00283        glVertex2f(PLAYFIELD_SIZE - 8.0, PLAYFIELD_SIZE - 1.1);
00284        glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE - 1.1);
00285        glVertex2f(PLAYFIELD_SIZE + 5.1, PLAYFIELD_SIZE + 0.3); // left
00286        glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE + 0.3);
00287        glVertex2f(PLAYFIELD_SIZE + 5.0, PLAYFIELD_SIZE - 1.1);
00288        glVertex2f(PLAYFIELD_SIZE + 5.1, PLAYFIELD_SIZE - 1.1);
00289        glVertex2f(PLAYFIELD_SIZE - 8.0, PLAYFIELD_SIZE + 0.3); // right
00290        glVertex2f(PLAYFIELD_SIZE - 8.1, PLAYFIELD_SIZE + 0.3);
00291        glVertex2f(PLAYFIELD_SIZE - 8.1, PLAYFIELD_SIZE - 1.1);
00292        glVertex2f(PLAYFIELD_SIZE - 8.0, PLAYFIELD_SIZE - 1.1);
00293 
00294        // left-side HUD background area
00295        glColor3f(0.25,0.25,0.25);
00296        glVertex2f(-5.5, PLAYFIELD_SIZE+1);
00297        glVertex2f(-0.5, PLAYFIELD_SIZE+1);
00298        glVertex2f(-0.5, -1.0);
00299        glVertex2f(-5.5, -1.0);
00300 
00301        // right-side HUD background area
00302        glVertex2f(PLAYFIELD_SIZE + 5.5, PLAYFIELD_SIZE+1);
00303        glVertex2f(PLAYFIELD_SIZE + 0.5, PLAYFIELD_SIZE+1);
00304        glVertex2f(PLAYFIELD_SIZE + 0.5, -1.0);
00305        glVertex2f(PLAYFIELD_SIZE + 5.5, -1.0);
00306 
00307        // left-side HUD background frame
00308        glColor3f(0.0,0.0,0.0);
00309        glVertex2f(-10.0, PLAYFIELD_SIZE+1);
00310        glVertex2f(-0.4, PLAYFIELD_SIZE+1);
00311        glVertex2f(-0.4, -1.0);
00312        glVertex2f(-10.0, -1.0);
00313 
00314        // right-side HUD background frame
00315        glVertex2f(PLAYFIELD_SIZE + 10.0, PLAYFIELD_SIZE+1);
00316        glVertex2f(PLAYFIELD_SIZE + 0.4, PLAYFIELD_SIZE+1);
00317        glVertex2f(PLAYFIELD_SIZE + 0.4, -1.0);
00318        glVertex2f(PLAYFIELD_SIZE + 10.0, -1.0);
00319        glEnd();
00320 
00321 
00322        glPopMatrix();
00323 }
00324 
00325 
00333 void DrawTiles(CWorld * ThisWorld)
00334 {
00335        glDisable(GL_BLEND);
00336 
00337        int iX, iY;
00338        for(iY = 0; iY < MAP_SIZE; iY++)
00339        {
00340               for(iX = 0; iX < MAP_SIZE; iX++)
00341               {
00342                      glPushMatrix(); // save matrix state
00343                      glTranslatef(iX * CELL_SIZE + CELL_SIZE / 2, iY * CELL_SIZE + CELL_SIZE / 2, TILEMAP_DISTANCE);
00344                      glColor3f(1.0,1.0,1.0);
00345                      glBindTexture( GL_TEXTURE_2D, textures[ThisWorld->tiles[(iY * MAP_SIZE) + iX].iTexture]);
00346                      glBegin(GL_QUADS);                        // start drawing a polygon (4 sided)
00347                      glTexCoord2d(0.0,1.0); glVertex3f(-1.0f, 1.0f, 0.0f);   // Top Left
00348                      glTexCoord2d(1.0,1.0); glVertex3f( 1.0f, 1.0f, 0.0f);   // Top Right
00349                      glTexCoord2d(1.0,0.0); glVertex3f( 1.0f,-1.0f, 0.0f);   // Bottom Right
00350                      glTexCoord2d(0.0,0.0); glVertex3f(-1.0f,-1.0f, 0.0f);   // Bottom Left       
00351                      glEnd();                                         // done with the polygon
00352                      glPopMatrix();
00353               }
00354        }
00355 }
00356 
00357 
00365 void DrawObjects(CWorld * ThisWorld)
00366 {
00367        // draw world objects
00368        CObject * Current = ThisWorld->Objects;
00369 
00370        glEnable(GL_BLEND);
00371        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00372        
00373        glAlphaFunc ( GL_GREATER, 0.1 ) ;
00374     glEnable ( GL_ALPHA_TEST ) ;
00375 
00376        while(Current != NULL)
00377        {
00378               if(Current->bVisible)
00379               {
00380                      Current->Draw();
00381               }
00382               Current = Current->Next; // move down the list
00383        }
00384 }
00385 
00393 void UpdateAnimation(CWorld * ThisWorld)
00394 {
00395        CObject * Current = ThisWorld->Objects;
00396        int frames = 0;
00397        
00398        while(Current != NULL)
00399        {
00400               if(Current->bAnimating) // if the current object has an animation in progress
00401               {      
00402                      // if this object has elapsed it's animation speed timeout
00403                      if(ThisWorld->iCurrentTime > (Current->iAnimTimeStamp + Current->iNormalAnimSpeed))
00404                      {
00405                             // if we're on the last frame
00406                             if(Current->iCurrentFrame == (animations[Current->iNormalAnim]->iTotalFrames - 2))
00407                             {
00408                                    // stop animating
00409                                    if(Current->bAnimCycle == ANIM_CYCLE_ONESHOT)
00410                                    {
00411                                           Current->bAnimating = false;
00412                                           Current->iCurrentFrame = 0;
00413                                    }
00414                                    // or just cycle
00415                                    if(Current->bAnimCycle == ANIM_CYCLE_CYCLE)
00416                                    {
00417                                           Current->iCurrentFrame = 0;
00418                                    }
00419                             }
00420                             else // otherwise just advance the frame
00421                             {
00422                                    Current->iCurrentFrame ++;
00423                             }
00424 
00425                             // reset the time stamp
00426                             Current->iAnimTimeStamp = ThisWorld->iCurrentTime;
00427                      }
00428               }
00429               Current = Current->Next; // move down the list
00430        }
00431 }
00432 
00433 
00437 void InitStripeAlpha(void)
00438 {
00439        stripeAlpha = 1;
00440 }
00441 
00442 
00450 void DrawStripe(float top, float bottom, float alpha)
00451 {
00452        if(stripeAlpha > 0)
00453        {
00454        glPushMatrix();
00455        glLoadIdentity();                         // reset the view
00456        glDisable(GL_TEXTURE_2D);
00457        glEnable(GL_BLEND);
00458        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00459        glColor4f(1, 1, 1, stripeAlpha);
00460        glTranslatef(0,0,-3.0f);
00461        glBegin(GL_QUADS);                        // start drawing a polygon (4 sided)
00462        glVertex3f(-10, bottom, 0.0f);     // Top Left
00463        glVertex3f( 10, bottom, 0.0f);     // Top Right
00464        glVertex3f( 10, top, 0.0f);        // Bottom Right
00465        glVertex3f(-10, top, 0.0f);        // Bottom Left       
00466        glEnd();                                         // done with the polygon
00467        glPopMatrix();
00468        stripeAlpha-=.002;
00469        }
00470 }
00471 
00472 
00478 void DrawBarGraph(float top, float bottom, float left, float right, float fillPercent)
00479 {
00480        float diff, fillTo;
00481 
00482        diff = right - left;
00483        
00484        fillTo = (diff / 100) * fillPercent;
00485 
00486        glBegin(GL_QUADS);
00487        glVertex2f(left, top);
00488        glVertex2f(left + fillTo, top);
00489        glVertex2f(left + fillTo, bottom);
00490        glVertex2f(left, bottom);
00491        glEnd();
00492 }
00493 
00500 void InitTextures()
00501 {
00502        textures[TEXID_DIRT] = LoadTextureTGA("tex\\tile_dirt.tga", 1);
00503        textures[TEXID_GRASS] = LoadTextureTGA("tex\\tile_grass.tga", 1);
00504        textures[TEXID_SAND] = LoadTextureTGA("tex\\tile_sand.tga", 1);
00505        textures[TEXID_MUD] = LoadTextureTGA("tex\\tile_mud.tga", 1);
00506        textures[TEXID_WATER] = LoadTextureTGA("tex\\tile_water.tga", 1);
00507        textures[TEXID_LAVA] = LoadTextureTGA("tex\\tile_lava.tga", 1);
00508        textures[TEXID_ICE] = LoadTextureTGA("tex\\tile_ice.tga", 1);
00509        textures[TEXID_TARMAC] = LoadTextureTGA("tex\\tile_tarmac.tga", 1);
00510 
00511 
00512        textures[TEXID_TANK] = LoadTextureTGA("tex\\tank01.tga", 1);
00513        textures[TEXID_PEA_SHOT] = LoadTextureTGA("tex\\pea_shot.tga", 1);
00514        textures[TEXID_BOUNCY_SHOT] = LoadTextureTGA("tex\\bouncy_shot.tga", 1);
00515        textures[TEXID_SUPER_BULLET] = LoadTextureTGA("tex\\super_bullet.tga", 1);
00516        textures[TEXID_LAND_MINE] = LoadTextureTGA("tex\\land_mine.tga", 1);
00517        textures[TEXID_HEAT_SEEKER] = LoadTextureTGA("tex\\heat_seeker.tga", 1);
00518        textures[TEXID_POWERUP_HEALTH] = LoadTextureTGA("tex\\health_crate.tga", 1);
00519        textures[TEXID_POWERUP_PS] = LoadTextureTGA("tex\\PS_crate.tga", 1);
00520        textures[TEXID_POWERUP_BS] = LoadTextureTGA("tex\\BS_crate.tga", 1);
00521        textures[TEXID_POWERUP_SB] = LoadTextureTGA("tex\\SB_crate.tga", 1);
00522        textures[TEXID_POWERUP_LM] = LoadTextureTGA("tex\\LM_crate.tga", 1);
00523        textures[TEXID_POWERUP_HS] = LoadTextureTGA("tex\\HS_crate.tga", 1);
00524        textures[TEXID_TNT_BLOCK] = LoadTextureTGA("tex\\TNT_crate.tga", 1);
00525        textures[TEXID_WALL_BLOCK] = LoadTextureTGA("tex\\wallblock.tga", 1);
00526        textures[TEXID_CRATE] = LoadTextureTGA("tex\\crate.tga", 1);
00527        textures[TEXID_MENUBACK] = LoadTextureTGA("tex\\menuback.tga", 1);
00528        textures[TEXID_EZRAS_HEAD] = LoadTextureTGA("tex\\ezras_head.tga", 1);
00529        
00530        animations[ANIMID_TANKROLL] = new CAnimation(1.0, 3, true);
00531        animations[ANIMID_TANKROLL]->ipFrameTex[0] = LoadTextureTGA("tex\\tank01.tga", 1);
00532        animations[ANIMID_TANKROLL]->ipFrameTex[1] = LoadTextureTGA("tex\\tank02.tga", 1);
00533        animations[ANIMID_TANKROLL]->ipFrameTex[2] = LoadTextureTGA("tex\\tank03.tga", 1);
00534 
00535        animations[ANIMID_GREENTANKEXPLODE] = new CAnimation(1.0, 4, true);
00536        animations[ANIMID_GREENTANKEXPLODE]->ipFrameTex[0] = LoadTextureTGA("tex\\g_tankexplode01.tga", 1);
00537        animations[ANIMID_GREENTANKEXPLODE]->ipFrameTex[1] = LoadTextureTGA("tex\\g_tankexplode02.tga", 1);
00538        animations[ANIMID_GREENTANKEXPLODE]->ipFrameTex[2] = LoadTextureTGA("tex\\g_tankexplode03.tga", 1);
00539        animations[ANIMID_GREENTANKEXPLODE]->ipFrameTex[3] = LoadTextureTGA("tex\\g_tankexplode04.tga", 1);
00540 
00541        animations[ANIMID_BROWNTANKEXPLODE] = new CAnimation(1.0, 4, true);
00542        animations[ANIMID_BROWNTANKEXPLODE]->ipFrameTex[0] = LoadTextureTGA("tex\\b_tankexplode01.tga", 1);
00543        animations[ANIMID_BROWNTANKEXPLODE]->ipFrameTex[1] = LoadTextureTGA("tex\\b_tankexplode02.tga", 1);
00544        animations[ANIMID_BROWNTANKEXPLODE]->ipFrameTex[2] = LoadTextureTGA("tex\\b_tankexplode03.tga", 1);
00545        animations[ANIMID_BROWNTANKEXPLODE]->ipFrameTex[3] = LoadTextureTGA("tex\\b_tankexplode04.tga", 1);
00546 
00547        animations[ANIMID_CRATECRUSH] = new CAnimation(0.7, 3, true);
00548        animations[ANIMID_CRATECRUSH]->ipFrameTex[0] = LoadTextureTGA("tex\\cratecrush01.tga", 1);
00549        animations[ANIMID_CRATECRUSH]->ipFrameTex[1] = LoadTextureTGA("tex\\cratecrush02.tga", 1);
00550        animations[ANIMID_CRATECRUSH]->ipFrameTex[2] = LoadTextureTGA("tex\\cratecrush03.tga", 1);
00551 
00552        animations[ANIMID_EXPLOSION] = new CAnimation(1.0, 3, true);
00553        animations[ANIMID_EXPLOSION]->ipFrameTex[0] = LoadTextureTGA("tex\\explosion01.tga", 1);
00554        animations[ANIMID_EXPLOSION]->ipFrameTex[1] = LoadTextureTGA("tex\\explosion02.tga", 1);
00555        animations[ANIMID_EXPLOSION]->ipFrameTex[2] = LoadTextureTGA("tex\\explosion03.tga", 1);
00556 
00557        animations[ANIMID_GREEN_HOP_IN] = new CAnimation(0.45, 24, false);
00558        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[0] = LoadTextureTGA("tex\\g_hopin01.tga", 1);
00559        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[1] = LoadTextureTGA("tex\\g_hopin02.tga", 1);
00560        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[2] = LoadTextureTGA("tex\\g_hopin03.tga", 1);
00561        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[3] = LoadTextureTGA("tex\\g_hopin04.tga", 1);
00562        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[4] = LoadTextureTGA("tex\\g_hopin05.tga", 1);
00563        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[5] = LoadTextureTGA("tex\\g_hopin06.tga", 1);
00564        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[6] = LoadTextureTGA("tex\\g_hopin07.tga", 1);
00565        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[7] = LoadTextureTGA("tex\\g_hopin08.tga", 1);
00566        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[8] = LoadTextureTGA("tex\\g_hopin09.tga", 1);
00567        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[9] = LoadTextureTGA("tex\\g_hopin10.tga", 1);
00568        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[10] = LoadTextureTGA("tex\\g_hopin11.tga", 1);
00569        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[11] = LoadTextureTGA("tex\\g_hopin12.tga", 1);
00570        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[12] = LoadTextureTGA("tex\\g_hopin13.tga", 1);
00571        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[13] = LoadTextureTGA("tex\\g_hopin14.tga", 1);
00572        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[14] = LoadTextureTGA("tex\\g_hopin15.tga", 1);
00573        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[15] = LoadTextureTGA("tex\\g_hopin16.tga", 1);
00574        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[16] = LoadTextureTGA("tex\\g_hopin17.tga", 1);
00575        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[17] = LoadTextureTGA("tex\\g_hopin18.tga", 1);
00576        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[18] = LoadTextureTGA("tex\\g_hopin19.tga", 1);
00577        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[19] = LoadTextureTGA("tex\\g_hopin20.tga", 1);
00578        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[20] = LoadTextureTGA("tex\\g_hopin21.tga", 1);
00579        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[21] = LoadTextureTGA("tex\\g_hopin22.tga", 1);
00580        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[22] = LoadTextureTGA("tex\\g_hopin23.tga", 1);
00581        animations[ANIMID_GREEN_HOP_IN]->ipFrameTex[23] = LoadTextureTGA("tex\\g_hopin24.tga", 1);
00582 
00583        animations[ANIMID_BROWN_HOP_IN] = new CAnimation(0.45, 24, false);
00584        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[0] = LoadTextureTGA("tex\\b_hopin01.tga", 1);
00585        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[1] = LoadTextureTGA("tex\\b_hopin02.tga", 1);
00586        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[2] = LoadTextureTGA("tex\\b_hopin03.tga", 1);
00587        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[3] = LoadTextureTGA("tex\\b_hopin04.tga", 1);
00588        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[4] = LoadTextureTGA("tex\\b_hopin05.tga", 1);
00589        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[5] = LoadTextureTGA("tex\\b_hopin06.tga", 1);
00590        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[6] = LoadTextureTGA("tex\\b_hopin07.tga", 1);
00591        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[7] = LoadTextureTGA("tex\\b_hopin08.tga", 1);
00592        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[8] = LoadTextureTGA("tex\\b_hopin09.tga", 1);
00593        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[9] = LoadTextureTGA("tex\\b_hopin10.tga", 1);
00594        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[10] = LoadTextureTGA("tex\\b_hopin11.tga", 1);
00595        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[11] = LoadTextureTGA("tex\\b_hopin12.tga", 1);
00596        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[12] = LoadTextureTGA("tex\\b_hopin13.tga", 1);
00597        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[13] = LoadTextureTGA("tex\\b_hopin14.tga", 1);
00598        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[14] = LoadTextureTGA("tex\\b_hopin15.tga", 1);
00599        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[15] = LoadTextureTGA("tex\\b_hopin16.tga", 1);
00600        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[16] = LoadTextureTGA("tex\\b_hopin17.tga", 1);
00601        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[17] = LoadTextureTGA("tex\\b_hopin18.tga", 1);
00602        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[18] = LoadTextureTGA("tex\\b_hopin19.tga", 1);
00603        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[19] = LoadTextureTGA("tex\\b_hopin20.tga", 1);
00604        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[20] = LoadTextureTGA("tex\\b_hopin21.tga", 1);
00605        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[21] = LoadTextureTGA("tex\\b_hopin22.tga", 1);
00606        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[22] = LoadTextureTGA("tex\\b_hopin23.tga", 1);
00607        animations[ANIMID_BROWN_HOP_IN]->ipFrameTex[23] = LoadTextureTGA("tex\\b_hopin24.tga", 1);
00608 
00609 }
00610 
00611 
00617 GLuint GetTexture(int texID)
00618 {
00619        return textures[texID];
00620 }
00621 
00623 GLuint GetFrame(int animID, int frame)
00624 {
00625        return animations[animID]->ipFrameTex[frame];
00626 }
00627 
00629 int GetTotalFrames(int animID)
00630 {
00631        return animations[animID]->iTotalFrames;
00632 }
00633 
00635 CAnimation * GetAnimation(int animID)
00636 {
00637        return animations[animID];
00638 }
00639 
00645 GLuint LoadTextureRAW( const char * filename, int wrap )
00646 {
00647     GLuint texture;
00648     int width, height;
00649     BYTE * data;
00650     FILE * file;
00651 
00652     // open texture data
00653     fopen_s(&file, filename, "rb" );
00654     if ( file == NULL ) return 0;
00655 
00656     // allocate buffer
00657     width = 256;
00658     height = 256;
00659     data = (BYTE *) malloc( width * height * 3 );
00660 
00661     // read texture data
00662     fread( data, width * height * 3, 1, file );
00663     fclose( file );
00664 
00665     // allocate a texture name
00666     glGenTextures( 1, &texture );
00667 
00668     // select our current texture
00669     glBindTexture( GL_TEXTURE_2D, texture );
00670 
00671     // select modulate to mix texture with color for shading
00672     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00673 
00674     // when texture area is small, bilinear filter the closest mipmap
00675     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00676     // when texture area is large, bilinear filter the first mipmap
00677     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00678 
00679     // if wrap is true, the texture wraps over at the edges (repeat)
00680     //       ... false, the texture ends at the edges (clamp)
00681     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
00682                      wrap ? GL_REPEAT : GL_CLAMP );
00683     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
00684                      wrap ? GL_REPEAT : GL_CLAMP );
00685 
00686     // build our texture mipmaps
00687     gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
00688                        GL_RGB, GL_UNSIGNED_BYTE, data );
00689 
00690     // free buffer
00691     free( data );
00692 
00693     return texture;
00694 }
00695 
00696 
00697 GLenum texFormat;
00698 
00699 int checkSize (int x) // make sure its a power of 2
00700 {
00701     if (x == 2        || x == 4 || 
00702         x == 8        || x == 16 || 
00703         x == 32  || x == 64 ||
00704         x == 128 || x == 256 || x == 512)
00705         return 1;
00706     else return 0;
00707 }
00708 
00709 unsigned char *getRGBA (FILE *s, int size) // reads in RGBA data for a 32bit image
00710 {
00711     unsigned char *rgba;
00712     unsigned char temp;
00713     int bread;
00714     int i;
00715 
00716     rgba = (unsigned char *) malloc (size * 4); 
00717 
00718     if (rgba == NULL)
00719         return 0;
00720 
00721     bread = (int) fread (rgba, sizeof (unsigned char), size * 4, s); 
00722 
00723     /* TGA is stored in BGRA, make it RGBA */
00724     if (bread != size * 4)
00725     {
00726         free (rgba);
00727         return 0;
00728     }
00729 
00730     for (i = 0; i < size * 4; i += 4 )
00731     {
00732         temp = rgba[i];
00733         rgba[i] = rgba[i + 2];
00734         rgba[i + 2] = temp;
00735     }
00736 
00737     texFormat = GL_RGBA;
00738     return rgba;
00739 }
00740 
00741 unsigned char *getRGB (FILE *s, int size) // reads in RGB data for a 24bit image
00742 {
00743     unsigned char *rgb;
00744     unsigned char temp;
00745     int bread;
00746     int i;
00747 
00748     rgb = (unsigned char *) malloc (size * 3); 
00749 
00750     if (rgb == NULL)
00751         return 0;
00752 
00753     bread = (int) fread (rgb, sizeof (unsigned char), size * 3, s);
00754 
00755     if (bread != size * 3)
00756     {
00757         free (rgb);
00758         return 0;
00759     }
00760 
00761     /* TGA is stored in BGR, make it RGB */
00762     for (i = 0; i < size * 3; i += 3)
00763     {
00764         temp = rgb[i];
00765         rgb[i] = rgb[i + 2];
00766         rgb[i + 2] = temp;
00767     }
00768 
00769     texFormat = GL_RGB;
00770 
00771     return rgb;
00772 }
00773 
00774 unsigned char *getGray (FILE *s, int size) // gets the grayscale image data used as an alpha channel
00775 {
00776     unsigned char *grayData;
00777     int bread;
00778 
00779     grayData = (unsigned char *) malloc (size);
00780 
00781     if (grayData == NULL)
00782         return 0;
00783 
00784     bread = (int) fread (grayData, sizeof (unsigned char), size, s);
00785 
00786     if (bread != size)
00787     {
00788         free (grayData);
00789         return 0;
00790     }
00791 
00792     texFormat = GL_ALPHA;
00793 
00794     return grayData;
00795 }
00796 
00797 char *getData (FILE *s, int sz, int iBits) // gets the image data for the specified bit depth
00798 {
00799     if (iBits == 32)
00800         return (char *) getRGBA (s, sz);
00801     else if (iBits == 24)
00802         return (char *) getRGB (s, sz);   
00803     else if (iBits == 8)
00804         return (char *) getGray (s, sz);
00805        return 0;
00806 }
00807 
00808 int returnError (FILE *s, int error) // called when there is an error loading the .tga file
00809 {
00810     fclose (s);
00811     return error;
00812 }
00813 
00820 GLuint LoadTextureTGA (const char *filename, int wrap)
00821 {
00822     unsigned char type[4];
00823     unsigned char info[7];
00824     unsigned char *imageData = NULL;
00825     int imageWidth, imageHeight;
00826     int imageBits, size;
00827     FILE *s;
00828        GLuint texture = NULL;
00829 
00830     if ((fopen_s(&s, filename, "r+bt")))
00831         return TGA_FILE_NOT_FOUND;
00832 
00833     fread (&type, sizeof (char), 3, s); // read in colormap info and image type, byte 0 ignored
00834     fseek (s, 12, SEEK_SET);                     // seek past the header and useless info
00835     fread (&info, sizeof (char), 6, s);
00836 
00837     if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
00838         returnError (s, TGA_BAD_IMAGE_TYPE);
00839 
00840     imageWidth = info[0] + info[1] * 256; 
00841     imageHeight = info[2] + info[3] * 256;
00842     imageBits =      info[4]; 
00843 
00844     size = imageWidth * imageHeight; 
00845 
00846     /* make sure dimension is a power of 2 */
00847     if (!checkSize (imageWidth) || !checkSize (imageHeight))
00848         returnError (s, TGA_BAD_DIMENSION);
00849 
00850     /* make sure we are loading a supported type */
00851     if (imageBits != 32 && imageBits != 24 && imageBits != 8)
00852         returnError (s, TGA_BAD_BITS);
00853 
00854     imageData = (unsigned char *) getData (s, size, imageBits);
00855 
00856     /* no image data */
00857     if (imageData == NULL)
00858         returnError (s, TGA_BAD_DATA);
00859 
00860     fclose (s);
00861 
00862        glGenTextures( 1, &texture );
00863        glBindTexture( GL_TEXTURE_2D, texture );
00864 
00865     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00866     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
00867     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00868     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
00869     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP );
00870     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight, GL_RGBA, GL_UNSIGNED_BYTE, imageData );
00871 
00872        free(imageData);
00873 
00874        return texture;
00875 }
00876 
00880 void DrawCircle(float radius)
00881 {
00882        glBegin(GL_LINE_LOOP);
00883        for (int i=0; i < 360; i+=20)
00884        {
00885               float degInRad = DegreesToRadians(i);
00886               glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
00887        }
00888        glEnd();
00889 }
00890 
00894 void DrawDisc(float radius)
00895 {
00896        glBegin(GL_POLYGON);
00897        for (int i=0; i < 360; i+=20)
00898        {
00899               float degInRad = DegreesToRadians(i);
00900               glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
00901        }
00902        glEnd();
00903 }
00904 
00908 void DrawStar(float radius)
00909 {
00910        float degInRad;
00911 
00912        glBegin(GL_LINES);
00913        for (int i=0; i < 360; i+=20)
00914        {
00915               degInRad = DegreesToRadians(i);
00916               glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
00917               glVertex2f(cos(degInRad + 0.1)*(radius*1.1),sin(degInRad+ 0.1)*(radius*1.1));
00918        }
00919        glEnd();
00920 }

 

Copyright Windsor Schmidt 2006 - All rights reserved.