D:/Zythum/DinoKod/Client/GameClient.cpp

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         // Console
00063         //
00064 #ifdef _DEBUG
00065         g_Console.Create( "Client Console" );
00066 #endif
00067         g_Console << "Initialising Client..." << KENDL;
00068 
00069         //
00070         // Creation de la fenetre
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;//LoadIcon( m_hInstance, MAKEINTRESOURCE(IDI_ICON_APP));
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 //      ShowWindow( m_hWnd, SW_NORMAL );
00147         ShowWindow( m_hWnd, SW_HIDE );
00148 
00149 //      GetClientRect( GetDesktopWindow(), &ClientRect );
00150 /*
00151         // Ajout de l'icone dans la TNA
00152         NOTIFYICONDATA  nid;
00153         ZeroMemory( &nid, sizeof( nid ) );
00154         nid.cbSize      = sizeof( nid );
00155         nid.hWnd        = m_hWnd;//(HWND)GetCurrentProcess();
00156         nid.uID         = 0;
00157         nid.uFlags      = NIF_ICON;
00158         nid.hIcon       = LoadIcon( m_hInstance, MAKEINTRESOURCE(IDI_ICON_APP));
00159         if( !Shell_NotifyIcon( NIM_ADD, &nid ) )
00160                 KError::Warning( NULL, "KGameLobby::Init() : Cannot init TNA ico" );
00161 */
00162         //
00163         // Intro
00164         //
00165         s32             bPlayIntro = GetpIniFile()->ReadInt( "GENERAL", "PlayIntro", 1 );
00166         
00167         if( bPlayIntro )
00168         {
00169                 if( !PlayIntro() )
00170                         return false;
00171         }
00172 
00173         //
00174         //      Profile
00175         //
00176         if( !m_pProfileBank->LoadProfiles() )
00177                 return false;
00178 
00179         //
00180         // Network
00181         //
00182         if( !m_Net.Init() )
00183         {
00184                 KError::Error( m_hWnd, "KGameClient::Init(...) : Cannot init network" );
00185                 return false;
00186         }
00187 /*
00188         //
00189         // Display
00190         //
00191         m_pInterface = new KClientInterface( m_hInstance, m_hWnd, Adapter, Width, Height, Bpp, FullScreen, RefreshRate );
00192         if( !m_pInterface->Init() )
00193         {
00194                 KError::Error( m_hWnd, "KGameClient::Init(...) : Cannot init display" );
00195                 return false;
00196         }
00197 */
00198         g_Console << "Display OK." << KENDL;
00199 
00200         //
00201         // INIT General
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 //      m_Net.Connect( "dino", 0 );
00210 
00211 //      KStr    sTiti = "Titi";
00212 //      char*   pToto = sTiti;
00213 
00214         //
00215         //      MOD Init
00216         //
00217         const char* m_pModName = m_pIniFile->ReadString( "GENERAL", "Mod", "Mod_Aeternis_Client" );
00218         m_ModFactory.DumpMods();
00219 //      m_pMod = m_ModFactory.LoadMod( KStr("Mod_TeamAssault"), this );
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                 // Locatisation
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                 // Interface
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                 // Game Manager
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         // Host
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         // Port
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                         // Display
00314                         if( IsIconic( m_hWnd ) )
00315                                 Sleep( 10 );
00316 #ifdef _DEBUG
00317                         else
00318                                 Sleep( 1 );
00319 #endif
00320 
00321 //                      KRect   Rect;
00322 //                      GetClientRect( m_hWnd, (RECT*)&Rect );
00323 //                      g_Avi.Display( GetDC( m_hWnd ), Rect );
00324 
00325                         g_Time.RefreshTime();
00326                         m_pInterface->DisplayInterface();
00327 
00328                         // GameManager
00329                         m_pClientGameManager->Manage();
00330                         // Network
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         // Network
00346         //
00347         m_Net.End();
00348 
00349         //
00350         //      Profile
00351         //
00352         m_pProfileBank->SaveProfiles();
00353 
00354         //
00355         // Arret du game manager
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         // Display
00365         //
00366         m_pInterface->End();
00367         SafeDeletep( m_pInterface );
00368 
00369         //
00370         //      Mod
00371         //
00372         m_ModFactory.UnloadMod( m_pMod );
00373         m_pMod = NULL;
00374 
00375         //
00376         // Console
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 //      sFileName += "Final Fantasy.avi";
00391 //      sFileName += "EAX.avi";
00392 //      sFileName += "Chicken Run 1.avi";
00393 //      sFileName += "Outro.avi";
00394         sFileName += "The Falling Man.avi";
00395 //      sFileName = "\\\\marie\\C\\FTP\\DivX\\American Pie 2.avi";
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                         // Display
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         case WM_KEYDOWN:
00456                 switch( wParam )
00457                 {
00458                 case VK_F1:
00459                         g_pGameClient->GetNet().Connect( KStr(""), 0 );
00460                         break;
00461                 case VK_F2:
00462                         g_pGameClient->GetpCommand()->ExecCommand( KStr("map"), KStr("wake") );
00463                         break;
00464                 case VK_F3:
00465                         g_pGameClient->GetpCommand()->ExecCommand( KStr("map"), KStr("dinomap03") );
00466                         break;
00467                 case VK_F4:
00468                         g_pGameClient->GetpCommand()->ExecCommand( KStr("map"), KStr("aztec2") );
00469                         break;
00470                 case VK_F5:
00471                         g_pGameClient->GetpCommand()->ExecCommand( KStr("map"), KStr("imotep") );
00472                         break;
00473                 case VK_F6:
00474                         g_pGameClient->GetpCommand()->ExecCommand( KStr("map"), KStr("test") );
00475                         break;
00476                 case VK_F8:
00477                         g_pGameClient->GetpCommand()->ExecCommand( KStr("map"), KStr("aeternis") );
00478                         break;
00479                 case VK_F9:
00480                         g_pGameClient->GetpCommand()->ExecCommand( KStr("flymode"), KStr("1") );
00481                         break;
00482                 }
00483                 break;
00484         case WM_CHAR:
00485                 {
00486                         if( g_pGameClient->GetInterface() )
00487                         {
00488                                 if( g_pGameClient->GetInterface()->OnKeyDown( (u32)wParam, (u32)lParam ) )
00489                                         break;
00490                 }
00491                 break;
00492 
00493         case WM_MOUSEMOVE:
00494                 SetCursor( NULL );
00495                 if( g_pGameClient->GetInterface() )
00496                         g_pGameClient->GetInterface()->OnMouseMove( KPt( LOWORD(lParam), HIWORD(lParam) ) );
00497                 break;
00498         case WM_LBUTTONDOWN:
00499                 if( g_pGameClient->GetInterface() )
00500                         g_pGameClient->GetInterface()->OnLButtonDown( KPt( LOWORD(lParam), HIWORD(lParam) ) );
00501                 break;
00502         case WM_LBUTTONUP:
00503                 if( g_pGameClient->GetInterface() )
00504                         g_pGameClient->GetInterface()->OnLButtonUp( KPt( LOWORD(lParam), HIWORD(lParam) ) );
00505                 break;
00506         case WM_RBUTTONDOWN:
00507                 if( g_pGameClient->GetInterface() )
00508                         g_pGameClient->GetInterface()->OnRButtonDown( KPt( LOWORD(lParam), HIWORD(lParam) ) );
00509                 break;
00510         case WM_RBUTTONUP:
00511                 if( g_pGameClient->GetInterface() )
00512                         g_pGameClient->GetInterface()->OnRButtonUp( KPt( LOWORD(lParam), HIWORD(lParam) ) );
00513                 break;
00514 */
00515         case WM_MOUSEMOVE:
00516                 SetCursor( NULL );
00517                 break;
00518 
00519         case WM_EXITSIZEMOVE:
00520 //              if( g_pGameClient->GetInterface() )
00521 //                      g_pGameClient->GetInterface()->Reset( hWnd, 0, 0, 0, 0 );
00522                 break;
00523         }
00524 
00525         return DefWindowProc( hWnd, uMsg, wParam, lParam);
00526 }
00527  

Generated on Sun Mar 25 20:02:10 2007 for Zythum Project by  doxygen 1.5.1-p1