00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "tile.h"
00026 #include "app.h"
00027 #include "frame.h"
00028 #include "glcanvas.h"
00029 #include "texture.h"
00030
00031 #include <wx/file.h>
00032
00033 extern ofstream logFile;
00034
00035 namespace WDS
00036 {
00037
00038 extern int g_DirTable[8][3];
00039
00040
00041 DECLARE_APP(CApp)
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 static int g_aCosts[] = { 100, 6, 7, 10, 13, 14, 16, 19, 28};
00055
00056 bool CTile::m_bLoaded = false;
00057 CTexture* CTile::m_pTerrains[9][241];
00058 CTexture* CTile::m_pFrames[8];
00059 CTexture* CTile::m_pBuildings[500];
00060 CTexture* CTile::m_pStreets[15];
00061 CTexture* CTile::m_pBorders[15];
00062 CTexture* CTile::m_pUnits[RACE_NUMOF] = { NULL, NULL, NULL, NULL };
00063
00064
00065
00066
00067
00068 CTile::CTile()
00069 {
00070 m_State = PATH_NONE;
00071 m_Parent = -1;
00072 m_CostF = 0;
00073 m_CostG = 0;
00074
00075 m_nTerrain1 = -1;
00076 m_nTerrain2 = 0;
00077 m_nBuilding = 0;
00078
00079 m_nFrame = 0;
00080 m_nBorder = 0;
00081 m_nStreet = 0;
00082 m_nUnit = 0;
00083 m_nFOW = 1;
00084 }
00085
00086 CTile::~CTile()
00087 {
00088 }
00089
00090
00091
00092
00093
00094
00095 void CTile::Init( )
00096 {
00097 if( m_bLoaded )
00098 return;
00099
00100 wxStopWatch sw;
00101 logFile << "Loading graphics..." << endl;
00102
00103 CGLCanvas *pCanvas = GetGLCanvas();
00104 char buf[64];
00105 int i, j;
00106
00107 int n = sizeof( CTile );
00108
00109
00110 for( i=0; i<9; i++ )
00111 {
00112 for( j=0; j<241; j++ )
00113 {
00114 sprintf( buf, "data/terrain/%d-%d.png", i, j );
00115
00116 if( wxFile::Exists( buf ) )
00117 m_pTerrains[i][j] = pCanvas->LoadTexture( buf, true );
00118 else
00119 m_pTerrains[i][j] = NULL;
00120 }
00121 }
00122
00123
00124 for( i=0; i<8; i++ )
00125 {
00126 sprintf( buf, "data/frame/frame%d.png", i+1 );
00127 m_pFrames[i] = pCanvas->LoadTexture( buf, true );
00128 }
00129
00130
00131 for( i=0; i<500; i++ )
00132 {
00133 sprintf( buf, "data/buildings/%d.png", i+1 );
00134
00135 if( wxFile::Exists( buf ) )
00136 m_pBuildings[i] = pCanvas->LoadTexture( buf, true );
00137 else
00138 m_pBuildings[i] = NULL;
00139 }
00140
00141
00142 for( i=0; i<15; i++ )
00143 {
00144 sprintf( buf, "data/overlay/bo-%d.png", i+1 );
00145 m_pBorders[i] = pCanvas->LoadTexture( buf, true );
00146 }
00147
00148
00149 for( i=0; i<15; i++ )
00150 {
00151 sprintf( buf, "data/overlay/s-%d.png", i+1 );
00152 m_pStreets[i] = pCanvas->LoadTexture( buf, true );
00153 }
00154
00155
00156 for( i=0; i<4; i++ )
00157 {
00158 sprintf( buf, "data/units/%d.png", i+1 );
00159 m_pUnits[i] = pCanvas->LoadTexture( buf, true );
00160 }
00161
00162 logFile << "-> Graphics loaded in " << sw.Time() << " ms." << endl;
00163
00164 m_bLoaded = true;
00165 }
00166
00167
00168
00169
00170
00171 void CTile::Shut( )
00172 {
00173 if( !m_bLoaded )
00174 return;
00175
00176 CGLCanvas *pCanvas = GetGLCanvas();
00177 int i, j;
00178
00179
00180 for( i=0; i<9; i++ )
00181 {
00182 for( j=0; j<241; j++ )
00183 {
00184 if( m_pTerrains[i][j] )
00185 {
00186 pCanvas->DeleteTexture( m_pTerrains[i][j] );
00187 m_pTerrains[i][j] = NULL;
00188 }
00189 }
00190 }
00191
00192
00193 for( i=0; i<8; i++ )
00194 {
00195 if( m_pFrames[i] )
00196 {
00197 pCanvas->DeleteTexture( m_pFrames[i] );
00198 m_pFrames[i] = NULL;
00199 }
00200 }
00201
00202
00203 for( i=0; i<500; i++ )
00204 {
00205 if( m_pBuildings[i] )
00206 {
00207 pCanvas->DeleteTexture( m_pBuildings[i] );
00208 m_pBuildings[i] = NULL;
00209 }
00210 }
00211
00212
00213 for( i=0; i<15; i++ )
00214 {
00215 if( m_pBorders[i] )
00216 {
00217 pCanvas->DeleteTexture( m_pBorders[i] );
00218 m_pBorders[i] = NULL;
00219 }
00220 }
00221
00222
00223 for( i=0; i<15; i++ )
00224 {
00225 if( m_pStreets[i] )
00226 {
00227 pCanvas->DeleteTexture( m_pStreets[i] );
00228 m_pStreets[i] = NULL;
00229 }
00230 }
00231
00232
00233 for( i=0; i<4; i++ )
00234 {
00235 if( m_pUnits[i] )
00236 {
00237 pCanvas->DeleteTexture( m_pUnits[i] );
00238 m_pUnits[i] = NULL;
00239 }
00240 }
00241
00242 m_bLoaded = false;
00243 }
00244
00245
00246
00247
00248
00249 void CTile::SetFogOfWar( int nFOW )
00250 {
00251 m_nFOW = nFOW;
00252 }
00253
00254 void CTile::SetFrame( int nID )
00255 {
00256 if( nID < 1 || nID > 8 )
00257 return;
00258
00259 m_nFrame = nID;
00260 }
00261
00262 void CTile::SetTerrain ( int nID, int nBorder )
00263 {
00264 if( nID < 0 || nID > 8 ||
00265 nBorder < 0 || nBorder> 240 )
00266 return;
00267
00268 m_nTerrain1 = nID;
00269 m_nTerrain2 = nBorder;
00270 }
00271
00272 void CTile::SetBuilding( int nID )
00273 {
00274 if( nID < 0 || nID > 500 )
00275 return;
00276
00277 m_nBuilding = nID;
00278 }
00279
00280 void CTile::SetBorder( int nID )
00281 {
00282 if( nID < 0 || nID > 15 )
00283 return;
00284
00285 m_nBorder = nID;
00286 }
00287
00288 void CTile::SetStreet( int nID )
00289 {
00290 if( nID < 0 || nID > 15 )
00291 return;
00292
00293 m_nStreet = nID;
00294 }
00295
00296 void CTile::SetUnit( int nID )
00297 {
00298
00299 switch( nID )
00300 {
00301 case 0: m_nUnit = 0; break;
00302 case 1: m_nUnit |= 1; break;
00303 case 2: m_nUnit |= 2; break;
00304 case 3: m_nUnit |= 4; break;
00305 case 4: m_nUnit |= 8; break;
00306 }
00307 }
00308
00312 void CTile::Reset( )
00313 {
00314 SetFogOfWar( 1 );
00315 SetBuilding( 0 );
00316 SetBorder ( 0 );
00317 SetUnit ( 0 );
00318 SetStreet ( 0 );
00319 }
00320
00321
00322
00323
00324
00325
00326 void CTile::Draw( Rect &rcTile, bool bFOW )
00327 {
00328
00329 if( m_nFrame > 0 )
00330 DrawTile( rcTile, m_pFrames[m_nFrame-1] );
00331
00332
00333 if( m_nFOW && bFOW )
00334 {
00335 glColor3f( 0.5f, 0.5f, 0.5f );
00336
00337
00338 if( m_nTerrain1 != -1 )
00339 DrawTile( rcTile, m_pTerrains[(int)m_nTerrain1][(int)m_nTerrain2] );
00340
00341 glColor3f( 1.0f, 1.0f, 1.0f );
00342 }
00343 else
00344 {
00345
00346 if( m_nTerrain1 != -1 )
00347 DrawTile( rcTile, m_pTerrains[(int)m_nTerrain1][(int)m_nTerrain2] );
00348
00349
00350
00351 if( m_nBorder > 0 )
00352 DrawTile( rcTile, m_pBorders[m_nBorder-1] );
00353
00354
00355 if( m_nStreet > 0 )
00356 DrawTile( rcTile, m_pStreets[m_nStreet-1] );
00357
00358
00359 if( m_nBuilding > 0 )
00360 DrawTile( rcTile, m_pBuildings[m_nBuilding-1] );
00361
00362
00363 if( m_nUnit & 1 )
00364 DrawTile( rcTile, m_pUnits[0] );
00365 if( m_nUnit & 2 )
00366 DrawTile( rcTile, m_pUnits[1] );
00367 if( m_nUnit & 4 )
00368 DrawTile( rcTile, m_pUnits[2] );
00369 if( m_nUnit & 8 )
00370 DrawTile( rcTile, m_pUnits[3] );
00371 }
00372 }
00373
00374
00375
00376
00377
00378 void CTile::DrawOverlay( Rect &rcTile )
00379 {
00380
00381 if( m_State == PATH_ACTIVE )
00382 {
00383
00384 wxPoint ptCenter( rcTile.x+63, rcTile.y+70 );
00385
00386
00387 GetGLCanvas()->DrawCircle( ptCenter, 32, 16.0f, 1.0f, 0.0f, 0.0f );
00388
00389
00390 if( m_Parent != -1 )
00391 {
00392 wxPoint ptDest;
00393 int xm = g_DirTable[(int)m_Parent][0];
00394 int ym = g_DirTable[(int)m_Parent][1];
00395 ptDest.x = ptCenter.x - ( (xm-ym) * 63 );
00396 ptDest.y = ptCenter.y - ( (xm+ym) * 35 );
00397
00398 GetGLCanvas()->DrawLine( ptCenter, ptDest, 1.0f, 1.0f, 0.0f, 0.0f );
00399 }
00400 }
00401 }
00402
00403
00404
00405
00406
00407 void CTile::DrawTile( Rect &rcTile, CTexture *pTex )
00408 {
00409 if( pTex == NULL )
00410 return;
00411
00412 float fw = 126.0f/128.0f;
00413 float fh = 105.0f/128.0f;
00414
00415 pTex->Bind();
00416 glBegin(GL_QUADS);
00417 glTexCoord2f(0.0f, 0.0f); glVertex2i( rcTile.x, rcTile.y );
00418 glTexCoord2f(0.0f, fh ); glVertex2i( rcTile.x, rcTile.Bottom() );
00419 glTexCoord2f(fw, fh ); glVertex2i( rcTile.Right(), rcTile.Bottom() );
00420 glTexCoord2f(fw, 0.0f); glVertex2i( rcTile.Right(), rcTile.y );
00421 glEnd();
00422 }
00423
00424
00425
00426
00427
00428
00429 float CTile::GetWeigth( )
00430 {
00431
00432 int m_nWeigth = g_aCosts[(int)m_nTerrain1];
00433
00434 if( !IsStreet() )
00435 return m_nWeigth;
00436 else
00437 return m_nWeigth * 0.7f;
00438 }
00439
00440
00441
00442
00443
00444 CGLCanvas* CTile::GetGLCanvas( )
00445 {
00446 CGLCanvas *pCanvas = wxGetApp().GetFrame()->GetGLCanvas();
00447 return pCanvas;
00448 }
00449
00450 }
00451
00452