Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

Genvs.c

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  GenVS.c                                                                             */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description:                                                                        */
00006 /*                                                                                      */
00007 /*  Copyright (c) 1997, 1999, Eclipse Entertainment; All rights reserved.               */
00008 /*                                                                                      */
00009 /*  See the accompanying file LICENSE.TXT for terms on the use of this library.         */
00010 /*  This library is distributed in the hope that it will be useful but WITHOUT          */
00011 /*  ANY WARRANTY OF ANY KIND and without any implied warranty of MERCHANTABILITY        */
00012 /*  or FITNESS FOR ANY PURPOSE.  Refer to LICENSE.TXT for more details.                 */
00013 /*                                                                                      */
00014 /****************************************************************************************/
00015 #include <Stdio.h>
00016 #include <Dos.h>
00017 #include <Math.h>
00018 #include <Windows.H>
00019 #include <MMSystem.H>
00020 #include <StdLib.h>
00021 #include <Assert.h>
00022 #include <Time.h>
00023 #include <direct.h>             //_chdir()
00024 
00025 #include "Genesis.h"
00026 #include "bitmap.h"
00027 #include "Errorlog.h"
00028 
00029 #include "GameMgr.h"
00030 #include "NetMgr.h"
00031 
00032 #include "Host.h"
00033 #include "Client.h"
00034 #include "Console.h"
00035 
00036 #include "DrvList.h"
00037 
00038 #include "Menu.h"
00039 #include "GMenu.h"
00040 #include "Text.h"
00041 #include "AutoSelect.h"
00042 
00043 #define CLIP_CURSOR
00044 //#define DO_CAPTURE
00045 
00046 void            GenVS_Error(const char *Msg, ...);
00047 
00048 LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);
00049 
00050 static char *GetCommandText(char *CmdLine, char *Data, geBoolean Cmd);
00051 
00052 void ShutdownAll(void);
00053 
00054 static geBoolean NewKeyDown(int KeyCode);
00055 static void GetMouseInput(HWND hWnd, int Width, int Height);
00056 
00057 #define STARTING_WIDTH  (500)
00058 #define STARTING_HEIGHT (400)
00059 
00060 // Misc global info
00061 geBoolean                               PopupD3DLog     = GE_FALSE;
00062 BOOL                                    Running                 = TRUE;
00063 BOOL                                    GameRunning             = FALSE;
00064 BOOL                                    ResetMouse              = TRUE;
00065 char                                    AppName[256]    = "Genesis3D Sample Game";
00066 
00067 #define DEFAULT_LEVEL   "Levels\\GenVS.BSP"
00068 
00069 geFloat                                 EffectScale;
00070 geBoolean                               ChangingDisplayMode = GE_FALSE; 
00071 int                                             ChangeDisplaySelection;
00072 
00073 // Game Manager
00074 static GameMgr                  *GMgr = NULL;
00075 
00076 //      Genesis Objects
00077 geEngine                                *Engine = NULL;
00078 
00079 // Misc objects
00080 static Console_Console  *Console = NULL;
00081 static Host_Host                *Host = NULL;
00082 
00083 geSound_System  *SoundSys = NULL;
00084 
00085 static char                             PlayerName[MENU_MAXSTRINGSIZE];
00086 static char                             IPAddress[MENU_MAXSTRINGSIZE];
00087 
00088 float                                   MainTime;
00089 
00090 static geBoolean                MenusCreated;
00091 ModeList                           *DriverModeList;
00092 int                                             DriverModeListLength;
00093 
00094 //=====================================================================================
00095 //      GetCommandText
00096 //=====================================================================================
00097 static char *GetCommandText(char *CmdLine, char *Data, geBoolean Cmd)
00098 {
00099         geBoolean       Quote;
00100         int32           dp;
00101         char            ch;
00102 
00103         while (1)
00104         {
00105                 ch = *CmdLine;
00106                 
00107                 if (ch == 0 || ch == '\n')
00108                         return NULL;
00109 
00110                 if (ch == ' ')                          // Skip white space
00111                 {
00112                         CmdLine++;
00113                         continue;
00114                 }
00115 
00116                 if (ch == '-')
00117                 {
00118                         if (!Cmd)
00119                                 return NULL;            // FIXME:  Return error!!!
00120                         CmdLine++;
00121                         break;
00122                 }
00123                 else 
00124                 {
00125                         if (Cmd)
00126                                 return NULL;                    // FIXME:  Return error!!!
00127                         break;
00128                 }
00129 
00130                 CmdLine++;
00131         }
00132 
00133         if (ch == 0 || ch == '\n')
00134                 return NULL;
00135 
00136         if (ch == '"')
00137         {
00138                 Quote = GE_TRUE;
00139                 ch = *++CmdLine;
00140         }
00141         else
00142                 Quote = GE_FALSE;
00143 
00144         dp = 0;
00145 
00146         while (1)
00147         {
00148                 if (Quote)
00149                 {
00150                         if (ch == '"')
00151                         {
00152                                 break;
00153                         }
00154                         
00155                         if (ch == '\n' || ch == 0)
00156                                 return NULL;            // Quote not found!!!
00157                 }
00158                 else
00159                 {
00160                         if (ch == ' ' || ch == '\n' || ch == 0)
00161                                 break;
00162                 }
00163                 
00164                 ch = *CmdLine++;
00165                 Data[dp++] = ch;
00166         }
00167 
00168         Data[dp-1] = 0;
00169 
00170         return CmdLine;
00171                 
00172 }
00173 
00174 void AdjustPriority(int adjustment)
00175 {
00176         //HANDLE thread = GetCurrentThread();
00177         //SetThreadPriority(thread,adjustment);
00178 }
00179 
00180 
00181 float   GlobalMouseSpeedX;
00182 float   GlobalMouseSpeedY;
00183 uint32  GlobalMouseFlags;       
00184 
00185 static void SubLarge(LARGE_INTEGER *start, LARGE_INTEGER *end, LARGE_INTEGER *delta)
00186 {
00187         _asm {
00188                 mov ebx,dword ptr [start]
00189                 mov esi,dword ptr [end]
00190 
00191                 mov eax,dword ptr [esi+0]
00192                 sub eax,dword ptr [ebx+0]
00193 
00194                 mov edx,dword ptr [esi+4]
00195                 sbb edx,dword ptr [ebx+4]
00196 
00197                 mov ebx,dword ptr [delta]
00198                 mov dword ptr [ebx+0],eax
00199                 mov dword ptr [ebx+4],edx
00200         }
00201 }
00202 
00203 Host_Init               HostInit;
00204 geBoolean               ShowStats,Mute;
00205 
00206 geVFile *                       MainFS;
00207 
00208 //=====================================================================================
00209 //      NewKeyDown
00210 //=====================================================================================
00211 static geBoolean NewKeyDown(int KeyCode)
00212 {
00213         if (GetAsyncKeyState(KeyCode) & 1)
00214                         return GE_TRUE;
00215 
00216         return GE_FALSE;
00217 }
00218 
00219 static void PickMode(HWND hwnd, HANDLE hInstance, geBoolean NoSelection, geBoolean ManualSelection, 
00220                 ModeList *List, int ModeListLength, int *Selection);
00221 
00222 
00223 //=====================================================================================
00224 //      WinMain
00225 //=====================================================================================
00226 #pragma warning (disable: 4028)
00227 int WINAPI WinMain(HANDLE hInstance, HANDLE hPrevInstance,
00228                                    LPSTR lpszCmdParam, int nCmdShow)
00229 {
00230 #pragma warning (default: 4028)
00231         geDriver                *Driver     = NULL;
00232         geDriver_Mode   *DriverMode = NULL;
00233 
00234         SYSTEMTIME              SystemTime;
00235         MSG                             Msg;
00236         char                    *CmdLine = lpszCmdParam;
00237         int32                   i;
00238         LARGE_INTEGER   Freq, OldTick, CurTick;
00239         char                    TempName[256];
00240         int32                   TempNameLength;
00241         geBoolean               ManualPick=GE_FALSE;
00242 
00243         MainTime = 0.0f;
00244 
00245         AdjustPriority(THREAD_PRIORITY_NORMAL);
00246 
00247         for (i=0; i<255; i++)
00248         {
00249                 NewKeyDown(i);          // Need to flush all the keys
00250         }
00251 
00252         GetSystemTime(&SystemTime);
00253 
00254 #if 0
00255         if      (SystemTime.wYear > 1999 || SystemTime.wMonth > 11)
00256         {
00257                 MessageBox(NULL,"The time limit on this demo has expired. \n" 
00258                         "Please contact Eclipse Entertainment for more information.",
00259                         "Notice",MB_OK | MB_TASKMODAL);
00260                 return 0;
00261         }
00262 #endif
00263 
00264 
00265 // set the currrent directory to where the exe is
00266         {
00267                 int i;
00268                 char    PathBuf[_MAX_PATH];
00269 
00270                 // get the exe's path and name
00271                 if (GetModuleFileName(hInstance,PathBuf,_MAX_PATH-1)==0)
00272                 {
00273                         GenVS_Error("Could not get exe file name.");
00274                 }
00275 
00276                 // strip off exe name to leave path
00277                 for (i=strlen(PathBuf)-1; i>0; i--)
00278                         {
00279                                 if (PathBuf[i]=='\\')
00280                                         {
00281                                                 PathBuf[i]=0;
00282                                                 break;
00283                                         }
00284                         }
00285                 if (i==0)
00286                         {
00287                                 GenVS_Error("Could not parse exe's path from exe name.");
00288                         }
00289                 // move the current working directory to the executables directory.
00290                 // this is a little rude
00291                 if (_chdir(PathBuf)==-1)
00292                         {
00293                                 GenVS_Error("Could not change current working directory to exe's path.");
00294                         }
00295         }
00296         
00297 
00298         // Setup the appname
00299         sprintf(AppName, "GTest v1.0 "__DATE__","__TIME__"");
00300 
00301         // Get defaults
00302         ShowStats = Mute = GE_FALSE;
00303 
00304         strcpy(IPAddress, "");                                                  // Deafult our IP address to blank (LAN game)
00305         TempNameLength = sizeof(TempName);
00306         if( GetUserName( TempName, &TempNameLength ) == 0 )
00307         {
00308                 strcpy( PlayerName, "Player" );
00309         }
00310         else
00311         {
00312                 strncpy( PlayerName, TempName, sizeof( PlayerName ) );
00313                 PlayerName[ sizeof( PlayerName ) - 1 ] = '\0';
00314         }
00315 
00316 
00317         HostInit.Mode = HOST_MODE_SINGLE_PLAYER;
00318         HostInit.DemoMode = HOST_DEMO_NONE;
00319         HostInit.LevelHack[0] = 0;
00320         HostInit.DemoFile[0] = 0;
00321 
00322         GetCurrentDirectory(sizeof(TempName), TempName);
00323         MainFS = geVFile_OpenNewSystem(NULL,
00324                                                                    GE_VFILE_TYPE_DOS,
00325                                                                    TempName,
00326                                                                    NULL,
00327                                                                    GE_VFILE_OPEN_READONLY | GE_VFILE_OPEN_DIRECTORY);
00328         assert(MainFS);
00329         
00330 
00331         while (1)
00332         {       
00333                 char                    Data[MAX_PATH];
00334 
00335                 if (!(CmdLine = GetCommandText(CmdLine, Data, GE_TRUE)))
00336                         break;
00337 
00338                 if (!stricmp(Data, "Server"))
00339                 {
00340                         if (!HostInit.LevelHack[0])     
00341                                 strcpy(HostInit.LevelHack, DEFAULT_LEVEL);
00342 
00343                         HostInit.Mode = HOST_MODE_SERVER_CLIENT;
00344                 }
00345                 else if (!stricmp(Data, "Dedicated"))
00346                 {
00347                         HostInit.Mode = HOST_MODE_SERVER_DEDICATED;
00348                 }
00349                 else if (!stricmp(Data, "Client"))
00350                 {
00351                         HostInit.Mode = HOST_MODE_CLIENT;
00352                 }
00353                 else if (!stricmp(Data, "BotPathDebug"))
00354                 {
00355                         extern geBoolean PathLight; // hacked in
00356                         PathLight = GE_TRUE;
00357                 }
00358                 else if (!stricmp(Data, "Record"))
00359                 {
00360                         HostInit.DemoMode = HOST_DEMO_RECORD;
00361 
00362                         if (!(CmdLine = GetCommandText(CmdLine, Data, GE_FALSE)))
00363                                 GenVS_Error("No demo name specified on command line.");
00364 
00365                         strcpy(HostInit.DemoFile, Data);
00366                 }
00367                 else if (!stricmp(Data, "Play"))
00368                 {
00369                         HostInit.DemoMode = HOST_DEMO_PLAY;
00370 
00371                         if (!(CmdLine = GetCommandText(CmdLine, Data, GE_FALSE)))
00372                                 GenVS_Error("No demo name specified on command line.");
00373 
00374                         strcpy(HostInit.DemoFile, Data);
00375                 }
00376                 else if (!stricmp(Data, "Name"))
00377                 {
00378                         // Get name
00379                         if (!(CmdLine = GetCommandText(CmdLine, Data, GE_FALSE)))
00380                                 GenVS_Error("No name specified on command line.");
00381                         
00382                         strcpy(PlayerName, Data);
00383                 }
00384                 else if (!stricmp(Data, "IP"))
00385                 {
00386                         // Get name
00387                         if (!(CmdLine = GetCommandText(CmdLine, Data, GE_FALSE)))
00388                                 GenVS_Error("No IP Address specified on command line.");
00389                         
00390                         if (strlen(Data) >= NETMGR_MAX_IP_ADDRESS)
00391                                 GenVS_Error("MaxIP Address string on command line.\n");
00392 
00393                         strcpy(IPAddress, Data);                // User wants to override IP at command line
00394                 }
00395                 else if (!stricmp(Data, "Map"))
00396                 {
00397                         // Get name
00398                         if (!(CmdLine = GetCommandText(CmdLine, Data, GE_FALSE)))
00399                                 GenVS_Error("No map name specified on command line.");
00400                         sprintf(HostInit.LevelHack, "Levels\\%s", Data);
00401                         //sprintf(HostInit.UserLevel, "Levels\\%s", Data);
00402                 }
00403                 else if (!stricmp(Data, "Gamma"))
00404                 {
00405                         // Get name
00406                         if (!(CmdLine = GetCommandText(CmdLine, Data, GE_FALSE)))
00407                                 GenVS_Error("No gamma value specified on command line.");
00408                         
00409                         // <>
00410                         UserGamma = (float)atof(Data);
00411                 }
00412                 else if (!stricmp(Data, "ShowStats"))
00413                 {
00414                         ShowStats = GE_TRUE;
00415                 }
00416                 else if (!stricmp(Data, "Mute"))
00417                 {
00418                         Mute = GE_TRUE;
00419                 }
00420                 else if (!stricmp(Data, "VidMode"))
00421                 {
00422                         GenVS_Error("VidMode Parameter no longer supported");
00423                 }
00424                 else if (!stricmp(Data,"PickMode"))
00425                 {
00426                         ManualPick=GE_TRUE;
00427                 }
00428                 else
00429                         GenVS_Error("Unknown Option: %s.", Data);
00430         }
00431 
00432 
00433         
00434         //
00435         //      Create the Game Mgr
00436         //
00437         GMgr = GameMgr_Create(hInstance, STARTING_WIDTH, STARTING_HEIGHT, AppName);
00438         
00439         if (!GMgr)
00440                 GenVS_Error("Could not create the game mgr.");
00441 
00442         Engine = GameMgr_GetEngine(GMgr);
00443         if (!Engine)
00444                 GenVS_Error("Failed to create the geEngine Object");
00445 
00446         DriverModeList = ModeList_Create(Engine,&DriverModeListLength);
00447         if (DriverModeList == NULL)
00448                 {
00449                         GenVS_Error("Failed to create a list of available drivers - Make sure the driver dll's are in the right directory.");
00450                 }
00451         AutoSelect_SortDriverList(DriverModeList, DriverModeListLength);
00452 
00453         geEngine_EnableFrameRateCounter(Engine, ShowStats);
00454 
00455         
00456         do 
00457                 {
00458                         HWND hWnd;
00459                         VidMode VidMode;
00460 
00461                         // Pick mode
00462                         PickMode(GameMgr_GethWnd(GMgr),hInstance,ChangingDisplayMode, ManualPick, 
00463                                          DriverModeList, DriverModeListLength, &ChangeDisplaySelection);
00464                         ChangingDisplayMode = 0;                
00465 
00466                         // Text, and Menu should go in GameMgr to make the hierarchy clean
00467                         // Client, and Host could then look down on Menu, and grab default items out of it...
00468                         if (!Text_Create( Engine ))
00469                                 GenVS_Error("Text_Create failed.");
00470 
00471                         if (!GMenu_Create( Engine , DriverModeList, DriverModeListLength, ChangeDisplaySelection))
00472                                 GenVS_Error("GMenu_Create failed.");
00473                         
00474                         MenusCreated = GE_TRUE;
00475 
00476                         // Set the text in the menus for the name/ipaddress etc...
00477                         {
00478                                 Menu_SetStringText( GMenu_GetMenu(GMenu_NameEntry), GMenu_NameEntry, PlayerName );
00479                                 Menu_SetStringText( GMenu_GetMenu(GMenu_IPEntry), GMenu_IPEntry, IPAddress );
00480                         }
00481 
00482                         // Get the sound system
00483                         SoundSys = GameMgr_GetSoundSystem(GMgr);
00484                         //assert(SoundSys);
00485 
00486                         if (SoundSys)
00487                                 {
00488                                         if ( Mute )
00489                                                 geSound_SetMasterVolume(SoundSys, 0.0f );
00490                                 }
00491 
00492                         // Get the console
00493                         Console = GameMgr_GetConsole(GMgr);
00494                         assert(Console);
00495                         
00496                         HostInit.hWnd = GameMgr_GethWnd(GMgr);
00497                         strcpy(HostInit.ClientName, PlayerName);
00498 
00499                         if (!GameMgr_ClearBackground(GMgr, 0, 0, NULL))
00500                                 GenVS_Error("GameMgr_ClearBackground failed.");
00501 
00502                         // If a map has not been set, and not recording a demo, then play demos...
00503                         if (HostInit.DemoMode != HOST_DEMO_RECORD && !HostInit.LevelHack[0] && HostInit.Mode != HOST_MODE_CLIENT)
00504                                 HostInit.DemoMode = HOST_DEMO_PLAY;
00505                         else
00506                                 GMenu_SetActive(GE_FALSE);                      // Menu should start out as non active when recording a demo...
00507 
00508                         // If the user wants to load a level at the command prompt, or wants to play back a demo
00509                         // then load a host now, else just let them do it through the menus later...
00510                         if (HostInit.LevelHack[0] || HostInit.DemoMode != HOST_DEMO_NONE || HostInit.Mode == HOST_MODE_CLIENT)
00511                         {
00512                                 if (!HostInit.LevelHack[0])     
00513                                         strcpy(HostInit.LevelHack, DEFAULT_LEVEL);
00514 
00515                                 Host = Host_Create(Engine, &HostInit, GMgr, GameMgr_GetVidMode(GMgr));
00516 
00517                                 if (!Host)
00518                                         GenVS_Error("Could not create the host!\n");
00519                         }
00520                         else            // Make sure a defrault level is set!!!
00521                                 strcpy(HostInit.LevelHack, DEFAULT_LEVEL);
00522 
00523                 #if 1
00524                         // <>
00525                         geEngine_SetGamma(Engine, UserGamma);
00526                 #endif
00527 
00528                         QueryPerformanceFrequency(&Freq);
00529                         QueryPerformanceCounter(&OldTick);
00530 
00531                         //SetCapture(GameMgr_GethWnd(GMgr));
00532                         ShowCursor(FALSE);
00533                         
00534                         #ifdef CLIP_CURSOR      
00535                         {
00536                                 RECT    ClipRect;
00537                                 RECT    ClientRect;
00538                                 POINT   RPoint;
00539 
00540                                 GetClientRect(GameMgr_GethWnd(GMgr), &ClientRect);
00541                                 RPoint.x                =ClientRect.left;
00542                                 RPoint.y                =ClientRect.top;
00543                                 ClientToScreen(GameMgr_GethWnd(GMgr), &RPoint);
00544                                 ClipRect.left   =RPoint.x;
00545                                 ClipRect.top    =RPoint.y;
00546 
00547                                 RPoint.x                =ClientRect.right;
00548                                 RPoint.y                =ClientRect.bottom;
00549                                 ClientToScreen(GameMgr_GethWnd(GMgr), &RPoint);
00550                                 ClipRect.right  =RPoint.x;
00551                                 ClipRect.bottom =RPoint.y;
00552                                 ClipCursor(&ClipRect);
00553                         }
00554                         #endif
00555                         
00556                         Running = TRUE;
00557                         VidMode = GameMgr_GetVidMode(GMgr);
00558                         hWnd    = GameMgr_GethWnd(GMgr);
00559 
00560 
00561                         while (Running)
00562                         {
00563                                 LARGE_INTEGER   DeltaTick;
00564                                 float                   ElapsedTime;
00565                                 geWorld                 *World;
00566                                 geCamera                *Camera;
00567 
00568                                 GameRunning     =TRUE;
00569 
00570                                 // see runtime menu options for stats and video mode changing.  (Mike)
00571 
00572                                 SetCursor(NULL);
00573                                 #ifdef DO_CAPTURE
00574                                 SetCapture(hWnd);
00575                                 #endif
00576 
00577                                 // Get timing info
00578                                 QueryPerformanceCounter(&CurTick);
00579 
00580                                 SubLarge(&OldTick, &CurTick, &DeltaTick);
00581 
00582                                 OldTick = CurTick;
00583 
00584                                 if (DeltaTick.LowPart > 0)
00585                                         ElapsedTime =  1.0f / (((float)Freq.LowPart / (float)DeltaTick.LowPart));
00586                                 else 
00587                                         ElapsedTime = 0.001f;
00588 
00589                                 //MainTime += ElapsedTime;
00590 
00591                                 // Get the mouse input (FIXME:  Move this into client?)
00592                                 {
00593                                         int Width, Height;
00594                                         VidMode_GetResolution(VidMode,&Width,&Height);
00595                                         GetMouseInput(hWnd,Width,Height);
00596                                 }
00597 
00598                                 // Do a host frame (physics, etc)
00599                                 if (Host)
00600                                 {
00601                                         if (!Host_Frame(Host, ElapsedTime))
00602                                                 GenVS_Error("Host_Frame failed...");
00603                                 }
00604 
00605                                 // Get the world, and the camera from the GameMgr
00606                                 World = GameMgr_GetWorld(GMgr);
00607                                 Camera = GameMgr_GetCamera(GMgr);;
00608 
00609                                 if (!GameMgr_Frame(GMgr, ElapsedTime))
00610                                         GenVS_Error("GameMgr_Frame failed...");
00611 
00612                                 // Begin frame
00613                                 if (!GameMgr_BeginFrame(GMgr, World, GE_FALSE))
00614                                 {
00615                                         GenVS_Error("GameMgr_BeginFrame failed.\n");
00616                                 }
00617 
00618                                 if (!Host || !World)
00619                                         GameMgr_ClearBackground(GMgr, 0, 0, NULL);
00620 
00621                                 if (Host)
00622                                 {
00623                                         if (!Host_RenderFrame(Host, ElapsedTime))
00624                                                 GenVS_Error("Host_RenderFrame failed in main game loop.\n");
00625                                 }
00626 
00627                                 Console_Frame(Console, ElapsedTime);
00628 
00629                                 /*
00630                                 if (Host)
00631                                 {
00632                                         if (!World)
00633                                                 GameMgr_ClearBackground(GMgr, 0, 0, NULL);
00634 
00635                                         if (Host_RenderFrame(Host, ElapsedTime)==GE_FALSE)
00636                                                 GenVS_Error("Host_RenderFrame failed in main game loop.\n");
00637 
00638                                         Console_Frame(Console, ElapsedTime);
00639                                 }
00640                                 else
00641                                         GameMgr_ClearBackground(GMgr, 0, 0, NULL);
00642                                 */
00643 
00644                                 if (!ShowStats && World)
00645                                 {
00646                                         Console_XYPrintf(Console,0,0,0,"Driver: %s %s",
00647                                                 DriverModeList[ChangeDisplaySelection].DriverNamePtr,
00648                                                 DriverModeList[ChangeDisplaySelection].ModeNamePtr);
00649                                         
00650                                         if (!SoundSys)
00651                                         {
00652                                                 Console_XYPrintf(Console,0,1,0,"(No sound device found)");
00653                                         }
00654                                 }
00655                                 
00656                                 // Draw manu
00657                                 GMenu_Draw();
00658 
00659                                 //      End Engine frame
00660                                 if (!GameMgr_EndFrame(GMgr))
00661                                 {
00662                                         GenVS_Error("GameMgr_EndFrame failed.\n");
00663                                 }
00664 
00665                                 // Do the'ol message pump
00666                                 
00667                                 while (PeekMessage( &Msg, NULL, 0, 0, PM_NOREMOVE))
00668                                 {
00669                                         if (!GetMessage(&Msg, NULL, 0, 0 ))
00670                                         {
00671                                                 PostQuitMessage(0);
00672                                                 Running=0;
00673                                                 break;
00674                                         }
00675 
00676                                         TranslateMessage(&Msg); 
00677                                         DispatchMessage(&Msg);
00678                                         if (ChangingDisplayMode)
00679                                                 {
00680                                                         break;
00681                                                 }
00682                                 }
00683                                 
00684                                 
00685 
00686                                 if (ChangingDisplayMode)
00687                                         {
00688                                                 Running = 0;            
00689                                                 GameRunning     = FALSE;
00690                                         }
00691                         } 
00692                         
00693                                 if (ChangingDisplayMode)
00694                                         {
00695                                                 if (MenusCreated)
00696                                                         {
00697                                                                 Text_Destroy();
00698                                                                 GMenu_Destroy();
00699                                                                 MenusCreated = GE_FALSE;
00700                                                         }
00701 
00702                                                 if (Host)
00703                                                         {
00704                                                                 Host_Destroy(Host);
00705                                                                 Host = NULL;
00706                                                         }
00707                                                 
00708                                                 if ( GetCapture() )
00709                                                         {
00710                                                                 ReleaseCapture();
00711                                                         }
00712                                                 ShowCursor(TRUE);
00713                                         }
00714 
00715                         
00716                 }
00717         while (ChangingDisplayMode != GE_FALSE);
00718 
00719         ShutdownAll();
00720         return (0);
00721 }
00722 
00723 
00724 static void PickMode(HWND hwnd, HANDLE hInstance, geBoolean NoSelection, geBoolean ManualSelection, 
00725                 ModeList *List, int ListLength, int *ListSelection)
00726 {
00727 
00728         assert( hwnd != 0 );
00729         assert( hInstance != 0 );
00730         assert( List != NULL );
00731         assert( ListSelection != NULL );
00732 
00733         GameMgr_PrepareToChangeMode(GMgr);
00734 
00735         if (!NoSelection && !ManualSelection)
00736                         {
00737                                 if (AutoSelect_PickDriver(GameMgr_GethWnd(GMgr),Engine,List, ListLength, ListSelection)==GE_FALSE)
00738                                         {
00739                                                 geErrorLog_AddString(-1,"Automatic video mode selection failed to find good mode.  Trying manual selection.",NULL);
00740                                                 ManualSelection = GE_TRUE;
00741                                         }
00742                         }
00743 
00744         
00745         if (NoSelection || ManualSelection)
00746                 {
00747                         while (1)
00748                                 {
00749                                         if (!NoSelection)
00750                                                 {
00751                                                         if (DrvList_PickDriver(hInstance, GameMgr_GethWnd(GMgr), List, ListLength, ListSelection)==GE_FALSE)
00752                                                                 {
00753                                                                         geErrorLog_AddString(-1,"Driver pick dialog failed",NULL);
00754                                                                         ShutdownAll();
00755                                                                         exit(1);
00756                                                                 }
00757                                                 }
00758                                         NoSelection = GE_FALSE;
00759 
00760                                         if ( *ListSelection < 0 )       // no selection made
00761                                                 {
00762                                                         ShutdownAll();
00763                                                         exit(1);
00764                                                 }
00765 
00766                                         geEngine_ShutdownDriver(Engine);
00767 
00768                                         if(List[*ListSelection].InAWindow)
00769                                                 {
00770                                                         GameMgr_ResetMainWindow(hwnd,List[*ListSelection].Width,List[*ListSelection].Height);
00771                                                 }
00772                                         if ( (List[*ListSelection].DriverType == MODELIST_TYPE_D3D_PRIMARY)   ||
00773                                                  (List[*ListSelection].DriverType == MODELIST_TYPE_D3D_SECONDARY) ||
00774                                                  (List[*ListSelection].DriverType == MODELIST_TYPE_D3D_3DFX) )
00775                                                 {
00776                                                         PopupD3DLog = GE_TRUE;
00777                                                 }
00778                                         else
00779                                                 {
00780                                                         PopupD3DLog = GE_FALSE;
00781                                                 }
00782                                         if (!geEngine_SetDriverAndMode(Engine, List[*ListSelection].Driver, List[*ListSelection].Mode))
00783                                                 {
00784                                                         if ( GetCapture() )  // just in case
00785                                                                 {
00786                                                                         ReleaseCapture();
00787                                                                 }
00788                                                         GameMgr_ResetMainWindow(hwnd,STARTING_WIDTH,STARTING_HEIGHT);
00789                                                         geErrorLog_AddString(-1, "geEngine_SetDriverAndMode failed. (continuing)", NULL);
00790                                                         MessageBox(GameMgr_GethWnd(GMgr), "Driver failed to properly set the selected mode.","Error:",MB_OK);
00791                                                 }
00792                                         else
00793                                                 { 
00794                                                         break;
00795                                                 }
00796                                 }
00797                 }
00798 
00799 
00800 
00801         // Set the driver and the mode through the game manager...
00802         if (!GameMgr_SetDriverAndMode(GMgr, List[*ListSelection].Driver, List[*ListSelection].Mode, 
00803                                                                                 List[*ListSelection].Width,  List[*ListSelection].Height))
00804                 GenVS_Error("GameMgr_SetDriverAndMode failed.");
00805         
00806 
00807         EffectScale     = 0.3f; // this is always 0.3 ?
00808 
00809 }
00810 
00811 //=====================================================================================
00812 //      ShutdownAll
00813 //=====================================================================================
00814 void ShutdownAll(void)
00815 {
00816         if ( GetCapture() )
00817         {
00818                 ReleaseCapture();
00819         }
00820         ShowCursor(TRUE);
00821 
00822         // Free each object (sub objects are freed by their parents...)
00823         if (MenusCreated)
00824         {
00825                 Text_Destroy();
00826                 GMenu_Destroy();
00827                 MenusCreated = GE_FALSE;
00828         }
00829 
00830         if (Host)
00831         {
00832                 Host_Destroy(Host);
00833                 Host = NULL;
00834         }
00835 
00836         if (GMgr)
00837                 {
00838                         HWND hWnd = GameMgr_GethWnd(GMgr);
00839                         if (hWnd)
00840                                 {
00841                                         ShowWindow(hWnd, SW_HIDE);
00842                                         UpdateWindow(hWnd);
00843                                 }
00844                 }
00845 
00846         if (GMgr)
00847                 GameMgr_Destroy(GMgr);
00848         
00849         geVFile_Close(MainFS);
00850 
00851         GMgr                    = NULL;
00852         Engine                  = NULL;
00853         Console                 = NULL;
00854         SoundSys                = NULL;
00855         Host                    = NULL;
00856 
00857         #ifdef _DEBUG
00858         {
00859                 char    Str[1024];
00860                 uint32  Count;
00861 
00862                 Count = geBitmap_Debug_GetCount();
00863 
00864                 sprintf(Str, "Final Bitmap count: %i\n", Count);
00865                 OutputDebugString(Str);
00866 
00867                 MessageBox(NULL, Str,
00868                         "GenVS MSG",MB_OK | MB_TASKMODAL);
00869 
00870         }
00871         #endif
00872 
00873         #ifdef CLIP_CURSOR      
00874                 ClipCursor(NULL);
00875         #endif
00876 
00877 }
00878 
00879 extern uint32   GlobalButtonBits;
00880 
00881 //=====================================================================================
00882 //      WndProc
00883 //=====================================================================================
00884 LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
00885 {
00886         switch(iMessage)
00887         {
00888                 case WM_SYSCOMMAND:
00889                         if (wParam == SC_SCREENSAVE)
00890                                 return 1;
00891                         break;
00892                 case WM_DISPLAYCHANGE:
00893                         {
00894                                 if (DriverModeList)
00895                                         {
00896                                                 if (DriverModeList[ChangeDisplaySelection].InAWindow)
00897                                                         ChangingDisplayMode=GE_TRUE;
00898                                         }
00899                                 break;
00900                         }
00901                 case WM_ACTIVATEAPP:
00902                 {
00903                         if(Engine)
00904                         {
00905                                 geEngine_Activate(Engine, wParam);
00906                         }
00907 
00908                         if(GameRunning)
00909                         {
00910 
00911                         #ifdef CLIP_CURSOR      
00912                                 if(wParam && GMgr)
00913                                 {
00914                                         RECT    ClipRect;
00915                                         RECT    ClientRect;
00916                                         POINT   RPoint;
00917                                                   
00918                                         GetClientRect(GameMgr_GethWnd(GMgr), &ClientRect);
00919                                         RPoint.x                =ClientRect.left;
00920                                         RPoint.y                =ClientRect.top;
00921                                         ClientToScreen(GameMgr_GethWnd(GMgr), &RPoint);
00922                                         ClipRect.left   =RPoint.x;
00923                                         ClipRect.top    =RPoint.y;
00924 
00925                                         RPoint.x                =ClientRect.right;
00926                                         RPoint.y                =ClientRect.bottom;
00927                                         ClientToScreen(GameMgr_GethWnd(GMgr), &RPoint);
00928                                         ClipRect.right  =RPoint.x;
00929                                         ClipRect.bottom =RPoint.y;
00930                                         ClipCursor(&ClipRect);
00931                                         ResetMouse      =TRUE;
00932                                 }
00933                                 else
00934                                 {
00935                                         ResetMouse      =FALSE;
00936                                         ClipCursor(NULL);
00937                                 }
00938                         #endif
00939                         }
00940                         if ( wParam )
00941                                 {
00942                                         AdjustPriority(THREAD_PRIORITY_HIGHEST);
00943                                 }
00944                         else
00945                                 {
00946                                         AdjustPriority(THREAD_PRIORITY_NORMAL);
00947                                 }
00948                         return  0;
00949                 }
00950                 case WM_MOVE:
00951                 {
00952                         if(GameRunning)
00953                         {
00954                                 geRect          ClipRect;
00955                                 RECT            ClientRect;
00956                                 POINT           RPoint;
00957                                 geBoolean       Ret;
00958 
00959                                 GetClientRect(GameMgr_GethWnd(GMgr), &ClientRect);
00960                                 RPoint.x                =ClientRect.left;
00961                                 RPoint.y                =ClientRect.top;
00962                                 ClientToScreen(GameMgr_GethWnd(GMgr), &RPoint);
00963                                 ClipRect.Left   =RPoint.x;
00964                                 ClipRect.Top    =RPoint.y;
00965 
00966                                 RPoint.x                =ClientRect.right;
00967                                 RPoint.y                =ClientRect.bottom;
00968                                 ClientToScreen(GameMgr_GethWnd(GMgr), &RPoint);
00969                                 ClipRect.Right  =RPoint.x;
00970                                 ClipRect.Bottom =RPoint.y;
00971                                 Ret = geEngine_UpdateWindow(Engine);
00972 
00973                                 assert(Ret == GE_TRUE);
00974                         }
00975                         return  0;
00976                 }
00977                 case WM_MOUSEMOVE:
00978                 {
00979                         return FALSE;
00980                         break;
00981                 }
00982 
00983                 case WM_RBUTTONDOWN:
00984                 case WM_LBUTTONUP:
00985                 case WM_RBUTTONUP:
00986                 case WM_LBUTTONDOWN:
00987                 {
00988                         switch ( iMessage )
00989                         {
00990                                 case WM_LBUTTONDOWN:
00991                                         GlobalMouseFlags |= 1;
00992                                         lParam = VK_LBUTTON;
00993                                         break;
00994                                 case WM_RBUTTONDOWN:
00995                                         GlobalMouseFlags |= 2;
00996                                         lParam = VK_RBUTTON;
00997                                         break;
00998                                 case WM_LBUTTONUP:
00999                                         GlobalMouseFlags &= ~1;
01000                                         lParam = VK_LBUTTON;
01001                                         break;
01002                                 case WM_RBUTTONUP:
01003                                         GlobalMouseFlags &= ~2;
01004                                         lParam = VK_RBUTTON;
01005                                         break;
01006                         }
01007 
01008                         // intentionally falls through if required
01009                         if ( GMenu_IsAMenuActive() == GE_FALSE )
01010                         {
01011                                 break;
01012                         }
01013                 }
01014 
01015                 case (WM_KEYDOWN):
01016                 {
01017                         // locals
01018                         int32                   Result;
01019 
01020                         // process keystroke result //undone
01021                         Result = GMenu_Key( wParam, lParam );
01022 
01023                         switch ( Result )
01024                         {
01025                                 
01026                                 case GMenu_DoNothing:
01027                                 {
01028                                         break;
01029                                 }
01030 
01031                                 case GMenu_UserSinglePlayerGame:
01032                                 case GMenu_SinglePlayerGame:
01033                                 case GMenu_SinglePlayerGame1:
01034                                 case GMenu_SinglePlayerGame2:
01035                                 case GMenu_SinglePlayerGame3:
01036                                 case GMenu_StartGame:
01037                                 case GMenu_StartGame1:
01038                                 case GMenu_StartGame2:
01039                                 case GMenu_Connect:
01040                                 {
01041                                         char    TempString[64];
01042 
01043                                         // Init the host struct
01044                                         if (HostInit.DemoMode == HOST_DEMO_PLAY)        // Make sure we are not in demo play mode...
01045                                                 HostInit.DemoMode = HOST_DEMO_NONE;
01046 
01047                                         HostInit.hWnd = GameMgr_GethWnd(GMgr);
01048                                         
01049                                         // Make sure there is a levelname just in case we are not in demo mode or somthing...
01050                                         strcpy(HostInit.LevelHack, "Levels\\GenVS.BSP");
01051 
01052                                         // zap old host
01053                                         if ( Host != NULL )
01054                                         { 
01055                                                 Host_Destroy( Host );
01056                                                 Host = NULL;
01057                                         }
01058                                 
01059                                         // set host type
01060                                         switch ( Result )
01061                                         {
01062                                                 case GMenu_UserSinglePlayerGame:
01063                                                 {
01064                                                         strcpy(HostInit.LevelHack, HostInit.UserLevel);
01065                                                         HostInit.Mode = HOST_MODE_SINGLE_PLAYER;
01066                                                         break;
01067                                                 }
01068 
01069                                                 case GMenu_SinglePlayerGame:
01070                                                 {
01071                                                         HostInit.Mode = HOST_MODE_SINGLE_PLAYER;
01072                                                         break;
01073                                                 }
01074 
01075                                                 case GMenu_SinglePlayerGame1:
01076                                                 {
01077                                                         strcpy(HostInit.LevelHack, "Levels\\GenVS.BSP");
01078                                                         HostInit.Mode = HOST_MODE_SINGLE_PLAYER;
01079                                                         break;
01080                                                 }
01081 
01082                                                 case GMenu_SinglePlayerGame2:
01083                                                 {
01084                                                         strcpy(HostInit.LevelHack, "Levels\\GenVS2.BSP");
01085                                                         HostInit.Mode = HOST_MODE_SINGLE_PLAYER;
01086                                                         break;
01087                                                 }
01088 
01089                                                 /*case GMenu_SinglePlayerGame3:
01090                                                 {
01091                                                         strcpy(HostInit.LevelHack, "Levels\\Gallery.BSP");
01092                                                         HostInit.Mode = HOST_MODE_SINGLE_PLAYER;
01093                                                         break;
01094                                                 }*/
01095 
01096                                                 case GMenu_StartGame:
01097                                                 {
01098                                                         HostInit.Mode = HOST_MODE_SERVER_CLIENT;
01099                                                         break;
01100                                                 }
01101 
01102                                                 case GMenu_StartGame1:
01103                                                 {
01104                                                         strcpy(HostInit.LevelHack, "Levels\\GenVS.BSP");
01105                                                         HostInit.Mode = HOST_MODE_SERVER_CLIENT;
01106                                                         break;
01107                                                 }
01108 
01109                                                 case GMenu_StartGame2:
01110                                                 {
01111                                                         strcpy(HostInit.LevelHack, "Levels\\GenVS2.BSP");
01112                                                         HostInit.Mode = HOST_MODE_SERVER_CLIENT;
01113                                                         break;
01114                                                 }
01115 
01116                                                 case GMenu_Connect:
01117                                                 {
01118                                                         HostInit.Mode = HOST_MODE_CLIENT;
01119                                                         break;
01120                                                 }
01121                                         }
01122 
01123                                         // Get the client name form the menu
01124                                         Menu_GetStringText( GMenu_GetMenu(GMenu_NameEntry), GMenu_NameEntry, TempString );
01125                                         strcpy(HostInit.ClientName, TempString);
01126 
01127                                         // Get the Ip address from the menu...
01128                                         Menu_GetStringText( GMenu_GetMenu(GMenu_IPEntry), GMenu_IPEntry, TempString );
01129                                         strcpy(HostInit.IPAddress, TempString);
01130 
01131                                         // create new host
01132                                         Host = Host_Create( Engine, &HostInit, GMgr, GameMgr_GetVidMode(GMgr));
01133                                         assert( Host != NULL );
01134                                         break;
01135                                 }
01136 
01137                                 case GMenu_QuitGame:
01138                                 {
01139                                         Running = 0;
01140                                         break;
01141                                 }
01142                         }
01143 
01144                         switch(wParam)
01145                         {
01146 
01147                                 case VK_F12:
01148                                 {
01149                                         int32           i;
01150                                         FILE            *f;
01151                                         char            Name[256];
01152 
01153                                         if (Engine)
01154                                         {
01155                                                 for (i=0 ;i<999; i++)           // Only 999 bmps, oh well...
01156                                                 {
01157                                                         sprintf(Name, "Bmp%i.Bmp", i);
01158 
01159                                                         f = fopen(Name, "rb");
01160 
01161                                                         if (f)
01162                                                         {
01163                                                                 fclose(f);
01164                                                                 continue;
01165                                                         }
01166                                                         
01167                                                         geEngine_ScreenShot(Engine, Name);
01168 
01169                                                         if (Console)
01170                                                                 Console_Printf(Console, "Writing Bmp: %s...\n", Name);
01171 
01172                                                         break;
01173                                                 }
01174                                         }
01175                                         break;
01176                                 }
01177 
01178                                 case 192:               // '~'
01179 
01180                                         if (Console)
01181                                                 Console_ToggleActive(Console);
01182                                         break;
01183 
01184                                 default:
01185                                         if (Console)
01186                                                 Console_KeyDown(Console, (char)wParam, GE_TRUE);
01187                                         break;
01188                         }
01189                         break;
01190                 }
01191                 
01192                 case (WM_KEYUP):
01193                 {
01194                         break;
01195                 }
01196 
01197                 case WM_DESTROY:
01198                         PostQuitMessage(0);
01199                         return  0;
01200 
01201 
01202                 default:
01203                         return DefWindowProc(hWnd, iMessage, wParam, lParam);
01204         }
01205         return 0;
01206 }
01207 
01208 
01209 geBoolean IsAMenuActive(void)
01210 {
01211         // intentionally falls through if required
01212         return(GMenu_IsAMenuActive());
01213 }
01214 
01215 
01216 //=====================================================================================
01217 //=====================================================================================
01218 static void GetMouseInput(HWND hWnd, int Width, int Height)
01219 {
01220         POINT   Point;
01221         float   dx, dy;
01222         int32   x, y;
01223         
01224         assert( Width > 0 );
01225         assert( Height > 0 );
01226         GetCursorPos(&Point);
01227   
01228         if (ScreenToClient( hWnd, &Point)==0)
01229                 {
01230                         geErrorLog_AddString(0,"GetMouseInput:  ScreenToClient failed",NULL); 
01231                         return;
01232                 }
01233         x = Point.x;
01234         y = Point.y;
01235 
01236         dx = ((float) (((float)Width/2.0f) - Point.x) / 350.0f);
01237         dy = ((float) (((float)Height/2.0f) - Point.y) / 350.0f);
01238 
01239         Point.x = Width/2;
01240         Point.y = Height/2;
01241 
01242         if (ClientToScreen( hWnd, &Point)==0)
01243                 {
01244                         geErrorLog_AddString(0,"GetMouseInput:  ClientToScreen failed",NULL); 
01245                         return;
01246                 }
01247         if(ResetMouse)
01248         {
01249                 SetCursorPos(Point.x,Point.y);
01250         }
01251 
01252         GlobalMouseSpeedX = dx;
01253         GlobalMouseSpeedY = dy;
01254 }
01255 
01256 //=====================================================================================
01257 //      GenVS_Error
01258 //=====================================================================================
01259 extern geBoolean        PopupD3DLog;
01260 static geBoolean        ErrorHandled = GE_FALSE;
01261 
01262 void GenVS_Error(const char *Msg, ...)
01263 {
01264         va_list         ArgPtr;
01265     char                TempStr[1024];
01266     char                TempStr2[1024];
01267         FILE            *f;
01268 
01269         if (ErrorHandled)
01270                 return;
01271 
01272         ErrorHandled = GE_TRUE;
01273 
01274         va_start (ArgPtr, Msg);
01275     vsprintf (TempStr, Msg, ArgPtr);
01276         va_end (ArgPtr);
01277 
01278         ShutdownAll();
01279 
01280         f = fopen("GTest.Log", "wt");
01281 
01282         if (f)
01283         {
01284                 int32           i, NumErrors;
01285 
01286                 NumErrors = geErrorLog_Count();
01287 
01288                 fprintf(f, "Error#:%3i, Code#:%3i, Info: %s\n", NumErrors, 0, TempStr);
01289 
01290                 for (i=0; i<NumErrors; i++)
01291                 {
01292                         geErrorLog_ErrorClassType       Error;
01293                         char                                            *String;
01294 
01295                         if (geErrorLog_Report(NumErrors-i-1, &Error, &String))
01296                         {
01297                                 fprintf(f, "Error#:%3i, Code#:%3i, Info:%s\n", NumErrors-i-1, Error, String);
01298                         }
01299                 }
01300 
01301                 fclose(f);
01302                 
01303                 sprintf(TempStr2, "%s\nPlease refer to GTest.Log for more info.", TempStr);
01304 
01305                 MessageBox(0, TempStr2, "** Genesis3D Virtual System Error **", MB_OK);
01306                 WinExec( "Notepad GTest.Log", SW_SHOW );
01307                 if (PopupD3DLog)
01308                         {
01309                                 WinExec( "Notepad d3ddrv.log", SW_SHOW);
01310                         }
01311         }
01312         else
01313         {
01314                 sprintf(TempStr2, "%s\nCould NOT output GTest.log!!!", TempStr);
01315 
01316                 MessageBox(0, TempStr2, "** Genesis3D Virtual System Error **", MB_OK);
01317         }
01318 
01319         _exit(1);
01320 }
01321 

Generated on Tue Sep 30 12:35:50 2003 for GTestAndEngine by doxygen 1.3.2