00001 #include <crtdbg.h>
00002
00003 #include "Common/Types.h"
00004 #include "Common/Console.h"
00005 #include "Common/Ini.h"
00006 #include "Common/Error.h"
00007 #include "Common/Directory.h"
00008 #include "Common/Command.h"
00009 #include "Common/LocalizedString.h"
00010
00011 #include "Movie/Avi.h"
00012
00013 #include "Interface/Interface.h"
00014
00015 #include "Game/ClientGameManager.h"
00016 #include "Game/ClientWorld.h"
00017
00018 #include "Mod/Mod.h"
00019
00020 #include "Client/Profile.h"
00021 #include "Client/Resource.h"
00022
00023 #include "Client/GameClient.h"
00024
00025 #define WINDOW_CLASSNAME "DinoClientWindow"
00026
00027 KGameClient* g_pGameClient;
00028
00029
00030 KGameClient::KGameClient( HINSTANCE hInstance )
00031 : KGame( hInstance )
00032 {
00033 m_pName = NULL;
00034 m_pInterface = NULL;
00035 g_pGameClient = this;
00036 m_pClientGameManager = NULL;
00037 m_pIniFile = new KIni( "Client.ini" );
00038 m_pProfileBank = new KProfileBank();
00039 m_pMod = NULL;
00040 }
00041
00042
00043 KGameClient::~KGameClient()
00044 {
00045 Deletep( m_pProfileBank );
00046 Deletep( m_pIniFile );
00047 }
00048
00049
00050 bool KGameClient::Init()
00051 {
00052 u32 Adapter = GetpIniFile()->ReadInt( "RENDER", "Adapter", 0 );
00053 u32 Width = GetpIniFile()->ReadInt( "RENDER", "Width", 640 );
00054 u32 Height = GetpIniFile()->ReadInt( "RENDER", "Height", 480 );
00055 u32 Bpp = GetpIniFile()->ReadInt( "RENDER", "Bpp", 16 );
00056 u32 FullScreen = GetpIniFile()->ReadInt( "RENDER", "Fullscreen", 0 );
00057 u32 RefreshRate = GetpIniFile()->ReadInt( "RENDER", "RefreshRate", 0 );
00058
00059 KGame::Init();
00060
00061
00062
00063
00064 #ifdef _DEBUG
00065 g_Console.Create( "Client Console" );
00066 #endif
00067 g_Console << "Initialising Client..." << KENDL;
00068
00069
00070
00071
00072 WNDCLASS WndClass;
00073
00074 memset( &WndClass, 0, sizeof( WndClass ) );
00075 WndClass.style = CS_HREDRAW | CS_VREDRAW;
00076 WndClass.lpfnWndProc = WindowProc;
00077 WndClass.hInstance = m_hInstance;
00078 WndClass.hIcon = NULL;
00079 WndClass.hCursor = NULL;
00080 WndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
00081 WndClass.lpszMenuName = NULL;
00082 WndClass.lpszClassName = WINDOW_CLASSNAME;
00083
00084 if ( !RegisterClass( &WndClass ) )
00085 {
00086 KError::Error( m_hWnd, "RegisterClass Failed." );
00087 return false;
00088 }
00089
00090 RECT ScreenRect;
00091 RECT WindowRect;
00092
00093 GetClientRect( GetDesktopWindow(), &ScreenRect );
00094 WindowRect.left = ( ScreenRect.right - Width ) / 2;
00095 WindowRect.top = ( ScreenRect.bottom - Height ) / 2;
00096 WindowRect.right = ScreenRect.right - WindowRect.left;
00097 WindowRect.bottom = ScreenRect.bottom - WindowRect.top;
00098
00099 DWORD Style;
00100
00101 if( FullScreen )
00102 Style = WS_POPUP;
00103 else
00104 Style = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
00105
00106 m_hWnd = CreateWindow( WINDOW_CLASSNAME,
00107 "Client .:|The Zythum Project|:. Copyright © 2003 Dino Productions",
00108 Style,
00109 WindowRect.left,
00110 WindowRect.top,
00111 WindowRect.right - WindowRect.left,
00112 WindowRect.bottom - WindowRect.top,
00113 NULL,
00114 NULL,
00115 m_hInstance,
00116 NULL );
00117
00118 if ( !m_hWnd )
00119 {
00120 KError::Error( m_hWnd, "CreateWindow Failed." );
00121 return false;
00122 }
00123
00124 if( FullScreen )
00125 {
00126 WindowRect.left = 0;
00127 WindowRect.top = 0;
00128 WindowRect.right = Width;
00129 WindowRect.bottom = Height;
00130 }
00131 else
00132 {
00133 GetWindowRect( m_hWnd, &WindowRect );
00134 Width = WindowRect.right - WindowRect.left;
00135 Height = WindowRect.bottom - WindowRect.top;
00136 }
00137
00138 AdjustWindowRect( &WindowRect, Style, false );
00139 SetWindowPos( m_hWnd, HWND_TOP, WindowRect.left,
00140 WindowRect.top,
00141 WindowRect.right - WindowRect.left,
00142 WindowRect.bottom - WindowRect.top,
00143 0 );
00144
00145 UpdateWindow( m_hWnd );
00146
00147 ShowWindow( m_hWnd, SW_HIDE );
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 s32 bPlayIntro = GetpIniFile()->ReadInt( "GENERAL", "PlayIntro", 1 );
00166
00167 if( bPlayIntro )
00168 {
00169 if( !PlayIntro() )
00170 return false;
00171 }
00172
00173
00174
00175
00176 if( !m_pProfileBank->LoadProfiles() )
00177 return false;
00178
00179
00180
00181
00182 if( !m_Net.Init() )
00183 {
00184 KError::Error( m_hWnd, "KGameClient::Init(...) : Cannot init network" );
00185 return false;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 g_Console << "Display OK." << KENDL;
00199
00200
00201
00202
00203 m_pName = (char*)m_pIniFile->ReadString( "GENERAL", "Name", "Player" );
00204 g_Console << "Client name : " << m_pName << KENDL;
00205
00206 GetpCommand()->RegisterCommand( "connect", CommandConnect, "Connect to a server", this );
00207 GetpCommand()->RegisterCommand( "disconnect", CommandDisconnect, "Disconnect from the server", this );
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 const char* m_pModName = m_pIniFile->ReadString( "GENERAL", "Mod", "Mod_Aeternis_Client" );
00218 m_ModFactory.DumpMods();
00219
00220 m_pMod = m_ModFactory.LoadMod( KStr( m_pModName ), this );
00221 if( m_pMod )
00222 {
00223 g_Console << "MOD: " << m_pMod->GetsName().GetpString() << " v" << KVERSION_GETMAJOR( m_pMod->GetVersion() ) << "." << KVERSION_GETMINOR( m_pMod->GetVersion() ) << KENDL;
00224
00225
00226 const char* m_pLanguage = m_pIniFile->ReadString( "GENERAL", "Language", "EN" );
00227 KStr sTextPath = g_Directory.GetPath( KStr( "Text" ), 1 );
00228 sTextPath += "Language.ini";
00229 g_LocalizedString.Init( sTextPath );
00230 g_LocalizedString.SetLanguagePrefix( (char*)m_pLanguage );
00231
00232
00233 m_pInterface = m_pMod->AllocInterface( m_hInstance, m_hWnd, Adapter, Width, Height, Bpp, FullScreen, RefreshRate );
00234 KASSERT( m_pInterface );
00235 if( !m_pInterface->Init() )
00236 {
00237 KError::Error( m_hWnd, "KGameClient::Init(...) : Cannot init display interface" );
00238 return false;
00239 }
00240
00241
00242 m_pClientGameManager = m_pMod->AllocClientGameManager( &m_Net, m_pInterface );
00243 KASSERT( m_pClientGameManager );
00244 if( m_pClientGameManager->Init( m_pInterface->GetpRender(), m_pInterface->GetpInput() ) )
00245 g_Console << "GameManager Init OK." << KENDL;
00246 else
00247 {
00248 KError::Error( NULL, "KGameClient::Init() : Cannot init game manager" );
00249 return false;
00250 }
00251 }
00252 else
00253 {
00254 KError::Error( NULL, "KGameClient::Init() : Cannot init game mod" );
00255 return false;
00256 }
00257
00258 g_Console << "Initialisation Client OK." << KENDL;
00259
00260 return true;
00261 }
00262
00263
00264 void KGameClient::CommandConnect( KStr Argument, void* pContext )
00265 {
00266 char pHostName[256];
00267 u16 Port;
00268 s32 i = 0;
00269 char c;
00270
00271 pHostName[0] = '\0';
00272 Port = 0;
00273
00274
00275 while( c = Argument.GetpString()[i] )
00276 {
00277 if( c == ' ' )
00278 break;
00279
00280 pHostName[i] = Argument.GetpString()[i];
00281 i ++;
00282 }
00283 pHostName[i] = '\0';
00284
00285
00286 Port = atoi( (const char*)&Argument.GetpString()[i] );
00287
00288 g_pGameClient->GetNet().Connect( pHostName, Port );
00289 }
00290
00291
00292 void KGameClient::CommandDisconnect( KStr Argument, void* pContext )
00293 {
00294 g_pGameClient->GetNet().Disconnect();
00295 }
00296
00297
00298 void KGameClient::Run()
00299 {
00300 MSG msg;
00301
00302 while( !m_bQuit )
00303 {
00304 if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
00305 {
00306 DispatchMessage( &msg );
00307 TranslateMessage( &msg );
00308 if ( msg.message == WM_QUIT )
00309 m_bQuit = true;
00310 }
00311 else
00312 {
00313
00314 if( IsIconic( m_hWnd ) )
00315 Sleep( 10 );
00316 #ifdef _DEBUG
00317 else
00318 Sleep( 1 );
00319 #endif
00320
00321
00322
00323
00324
00325 g_Time.RefreshTime();
00326 m_pInterface->DisplayInterface();
00327
00328
00329 m_pClientGameManager->Manage();
00330
00331 m_Net.Manage();
00332 }
00333 }
00334 }
00335
00336
00337 void KGameClient::End()
00338 {
00339 g_Console << "Shutdown Client..." << KENDL;
00340
00341 GetpCommand()->UnregisterCommand( "connect" );
00342 GetpCommand()->UnregisterCommand( "disconnect" );
00343
00344
00345
00346
00347 m_Net.End();
00348
00349
00350
00351
00352 m_pProfileBank->SaveProfiles();
00353
00354
00355
00356
00357 if( m_pClientGameManager->End() )
00358 g_Console << "GameManager End OK." << KENDL;
00359 else
00360 KError::Error( NULL, "KGameClient::End() : Cannot shutdown gamemanager" );
00361 SafeDeletep( m_pClientGameManager );
00362
00363
00364
00365
00366 m_pInterface->End();
00367 SafeDeletep( m_pInterface );
00368
00369
00370
00371
00372 m_ModFactory.UnloadMod( m_pMod );
00373 m_pMod = NULL;
00374
00375
00376
00377
00378 g_Console.Close();
00379
00380 KGame::End();
00381 }
00382
00383
00384 bool KGameClient::PlayIntro()
00385 {
00386 KStr sFileName;
00387 KAvi Avi;
00388
00389 sFileName = g_Directory.GetPath( KStr( "Videos" ) );
00390
00391
00392
00393
00394 sFileName += "The Falling Man.avi";
00395
00396 if( !Avi.OpenAVI( sFileName ) )
00397 return false;
00398
00399 Avi.Play();
00400
00401 MSG msg;
00402 bool m_bQuit = false;
00403
00404 while( !m_bQuit )
00405 {
00406 if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
00407 {
00408 if(( msg.message == WM_KEYDOWN ) && ( msg.wParam = VK_ESCAPE ) )
00409 {
00410 Avi.Stop();
00411 break;
00412 }
00413
00414 DispatchMessage( &msg );
00415 TranslateMessage( &msg );
00416 if ( msg.message == WM_QUIT )
00417 m_bQuit = true;
00418 }
00419 else
00420 {
00421
00422 if( IsIconic( m_hWnd ) )
00423 Sleep( 10 );
00424 else
00425 {
00426 KRect Rect;
00427 GetClientRect( m_hWnd, (RECT*)&Rect );
00428 Avi.Display( GetDC( m_hWnd ), Rect );
00429
00430 if( !Avi.IsPlaying() )
00431 break;
00432 }
00433 }
00434 }
00435 Avi.CloseAVI();
00436
00437 if( m_bQuit )
00438 return false;
00439
00440 return true;
00441 }
00442
00443
00444 LRESULT CALLBACK KGameClient::WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
00445 {
00446 switch( uMsg )
00447 {
00448 case WM_CREATE:
00449 break;
00450
00451 case WM_CLOSE:
00452 PostQuitMessage( 0 );
00453 break;
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 case WM_MOUSEMOVE:
00516 SetCursor( NULL );
00517 break;
00518
00519 case WM_EXITSIZEMOVE:
00520
00521
00522 break;
00523 }
00524
00525 return DefWindowProc( hWnd, uMsg, wParam, lParam);
00526 }
00527