Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ASys.cpp

Go to the documentation of this file.
00001 /*
00002 CS Senior Project 2003
00003 Team : Leftfield
00004 Project : ModernWarfare
00005 Members :
00006 - Russ Christensen              <rchriste@cs.utah.edu>
00007 - Todd Smith                    <tcsmith@cs.utah.edu>
00008 - Usit Duongsaa                 <duongsaa@cs.utah.edu>
00009 
00010 Copyright 2003 Russ Christensen, Usit Duongsaa, and Todd Smith. All rights reserved.
00011 
00012 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00013 
00014 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00015 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00016 THIS SOFTWARE IS PROVIDED BY RUSS CHRISTENSEN, USIT DUONGSAA, AND TODD SMITH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RUSS, USIT, TODD OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017 */
00018 
00019 
00024 #include "AEngine.h"
00025 #include "AState.h"
00026 #include "AConstants.h"
00027 #include "ADevice.h"
00028 #include "ASharedFunc.h"
00029 #include "float.h"
00030 
00031 const int UPDATE_RATE                   =       60;                                     // update 60 times/sec
00032 const int UPDATE_INTERVAL               =       1000/UPDATE_RATE;       // how many millisec between updates
00033 
00034 //------------------------------------------------------------------------------------
00035 void sys_init( HINSTANCE hInst, int width, int height, int colorDepth, bool fullScreen )
00036 {       
00037         AS.hInst = hInst;
00038         AS.windowWidth  = width;
00039         AS.windowHeight = height;
00040         AS.colorDepth   = colorDepth;
00041         AS.isFullScreen = fullScreen;
00042         AS.initFunc   = NULL;
00043         AS.updateFunc = NULL;
00044         AS.renderFunc = NULL;
00045         AS.shutdownFunc = NULL;
00046         AS.pConsoleLogFile = NULL;
00047         AS.vshader_terrain = NULL;
00048 
00049         AS.camera_pos    = Vec3D( 3.0f, 0.0f, 3.0f );
00050         AS.camera_target = Vec3D( 0.0f, 0.0f, 0.0f );
00051         D3DXMatrixIdentity(& (AS.camera_world2projMatrix) );
00052         AS.skipRate = 0;
00053 
00054         //unsigned int newFlags = _RC_DOWN | _PC_64;
00055         //unsigned int mask             = _MCW_RC | _MCW_PC;
00056         //_controlfp(newFlags,mask);
00057 }
00058 //------------------------------------------------------------------------------------
00059 void sys_setSkipRate( int rate )
00060 {
00061         AS.skipRate = rate;
00062 }
00063 //------------------------------------------------------------------------------------
00064 void sys_setInitFunc( AierraInitFunc f )
00065 {
00066         AS.initFunc = f;
00067 }
00068 //------------------------------------------------------------------------------------
00069 void sys_setUpdateFunc( AierraUpdateFunc f )
00070 {
00071         AS.updateFunc = f;
00072 }
00073 //------------------------------------------------------------------------------------
00074 void sys_setRenderFunc( AierraRenderFunc f )
00075 {
00076         AS.renderFunc = f;
00077 }
00078 //------------------------------------------------------------------------------------
00079 void sys_setShutdownFunc( AierraShutdownFunc f )
00080 {
00081         AS.shutdownFunc = f;
00082 }
00083 //------------------------------------------------------------------------------------
00084 // helper function for doGameLoop
00085 void handleSystemMessages()
00086 {
00087         MSG msg; 
00088         while(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))    
00089     {
00090                 TranslateMessage( &msg );
00091         DispatchMessage( &msg );
00092         }
00093 }
00094 //------------------------------------------------------------------------------------
00095 // helper for doGameLoop().  Latches input status from DirectInput
00096 void updateInput()
00097 {
00098         NDX_Input &N = AS.ndxInput;
00099 
00100         // update mouse
00101         DIMOUSESTATE mouseState;
00102         if( N.lpDIDMouse->GetDeviceState(sizeof(mouseState),&mouseState) != DI_OK )
00103                 N.lpDIDMouse->Acquire();
00104 
00105         N.MouseX=mouseState.lX;
00106         N.MouseY=mouseState.lY;
00107         N.MouseXPos+=N.MouseX;
00108         N.MouseYPos+=N.MouseY;
00109         if(N.MouseXPos<N.MouseLimit.left)               N.MouseXPos=N.MouseLimit.left;
00110         if(N.MouseXPos>N.MouseLimit.right-1)    N.MouseXPos=N.MouseLimit.right-1;
00111         if(N.MouseYPos<N.MouseLimit.top)                N.MouseYPos=N.MouseLimit.top;
00112         if(N.MouseYPos>N.MouseLimit.bottom-1)   N.MouseYPos=N.MouseLimit.bottom-1;
00113         AS.input_mouseWheel += ((FLOAT)(mouseState.lZ)) / 1500.0f;
00114         if( AS.input_mouseWheel > 1.0f ) AS.input_mouseWheel = 1.0f;
00115         if( AS.input_mouseWheel < 0.0f ) AS.input_mouseWheel = 0.0f;
00116 
00117                 N.MouseLBClick = !N.MouseLB && mouseState.rgbButtons[0];
00118                 N.MouseRBClick = !N.MouseRB && mouseState.rgbButtons[1];
00119                 N.MouseMBClick = !N.MouseMB && mouseState.rgbButtons[2];
00120         N.MouseLB = (mouseState.rgbButtons[0]) != 0;
00121         N.MouseRB = (mouseState.rgbButtons[1]) != 0;
00122         N.MouseMB = (mouseState.rgbButtons[2]) != 0;
00123         N.MouseLBDblClick = false;
00124         N.MouseMBDblClick = false;
00125         N.MouseRBDblClick = false;
00126         if(N.MouseLBClick)
00127         {
00128             if( (GetTickCount() - N.m_dwDoubleClickTimeLB) < N.m_dwDoubleClickDelay)         
00129                 N.MouseLBDblClick = true;
00130             else
00131                 N.m_dwDoubleClickTimeLB = GetTickCount();
00132         }
00133 
00134         if(N.MouseMBClick)
00135         {
00136             if( (GetTickCount() - N.m_dwDoubleClickTimeMB) < N.m_dwDoubleClickDelay)
00137                 N.MouseMBDblClick = true;
00138             else
00139                 N.m_dwDoubleClickTimeMB = GetTickCount();
00140         }
00141 
00142         if(N.MouseRBClick)
00143         {
00144             if( (GetTickCount() - N.m_dwDoubleClickTimeRB) < N.m_dwDoubleClickDelay)
00145                 N.MouseRBDblClick = true;
00146             else
00147                 N.m_dwDoubleClickTimeRB = GetTickCount();            
00148         }
00149         
00150         // update keyboard
00151         if( N.lpDIDKeys->GetDeviceState(256,&N.Keys) != DI_OK )
00152                 N.lpDIDKeys->Acquire();
00153         for( int i=0; i<MAX_KEY; i++ )
00154         {
00155                 AS.keyDownLastCycle[i] = AS.keyDownThisCycle[i];
00156                 AS.keyDownThisCycle[i] = N.Keys[i] != 0;
00157         }
00158 
00159         // mouse idle
00160         Vec2D thisMouse( input_getMouseX(), input_getMouseY() );
00161         if( thisMouse != AS.input_lastTickMousePos )
00162         {
00163                 AS.input_lastTickMousePos = thisMouse;
00164                 AS.input_mouseIdleTickCount = 0;
00165         }
00166         else
00167         {
00168                 AS.input_mouseIdleTickCount++;
00169         }
00170         
00171 }
00172 //------------------------------------------------------------------------------------
00173 void beginRendering()
00174 {
00175         if(AS.camera_screenSize==1.0f)
00176         {
00177                 AS.pDevice->Clear( 0, NULL, AS.clearFlags, D3DCOLOR_XRGB(0,200,250), 1.0f, 0L );
00178                 
00179                 D3DVIEWPORT8 viewport;
00180                 viewport.X = 0;
00181                 viewport.Y = 0;
00182                 viewport.Width  = AS.windowWidth;
00183                 viewport.Height = AS.windowHeight;
00184                 viewport.MinZ = 0.0f;
00185                 viewport.MaxZ = 1.0f;
00186                 AS.pDevice->SetViewport(&viewport);
00187         }
00188         else
00189         {
00190                 D3DRECT rect;
00191                 rect.x1 = (LONG) (AS.windowWidth /2 - AS.camera_screenSize*AS.windowWidth /2 );
00192                 rect.x2 = (LONG) (AS.windowWidth /2 + AS.camera_screenSize*AS.windowWidth /2 );
00193                 rect.y1 = (LONG) (AS.windowHeight/2 - AS.camera_screenSize*AS.windowHeight/2 );
00194                 rect.y2 = (LONG) (AS.windowHeight/2 + AS.camera_screenSize*AS.windowHeight/2 );
00195                 AS.pDevice->Clear( 1, &rect, AS.clearFlags, D3DCOLOR_XRGB(0,0,0), 1.0f, 0L );
00196 
00197                 D3DVIEWPORT8 viewport;
00198                 viewport.X = rect.x1;
00199                 viewport.Y = rect.y1;
00200                 viewport.Width  = rect.x2 - rect.x1;
00201                 viewport.Height = rect.y2 - rect.y1;
00202                 viewport.MinZ = 0.0f;
00203                 viewport.MaxZ = 1.0f;
00204                 AS.pDevice->SetViewport(&viewport);
00205         }
00206 
00207         AS.pDevice->BeginScene();
00208         device_flushSettings();
00209 }
00210 //------------------------------------------------------------------------------------
00211 void endRendering()
00212 {
00213         AS.pDevice->EndScene();
00214         AS.pDevice->Present(NULL,NULL,NULL,NULL);
00215 }
00216 //------------------------------------------------------------------------------------
00217 // helper for doGameLoop
00218 void drawConsole()
00219 {
00220         istringstream in(AS.pConsoleStream->str());
00221         string line;
00222         
00223         while(in)                                       // read stuffs from console stream and store in line array
00224         {
00225                 getline( in, line );
00226                 if(line!="" || in)
00227                 {
00228                         AS.consoleLineIndex = (AS.consoleLineIndex+1) % MAX_CONSOLE_LINES;
00229                         AS.consoleLines[ AS.consoleLineIndex ] = line;
00230                         if(AS.pConsoleLogFile) (*AS.pConsoleLogFile) << line << endl;
00231                 }
00232         }
00233 
00234         delete AS.pConsoleStream;
00235         AS.pConsoleStream = new ostringstream;
00236 
00237         // draw blue background box and red border
00238         if(!AS.showConsole) return;
00239         overlay_rect( Vec2D(0.0f,0.0f), Vec2D(0.5f,1.0f), true, Colors::blue, BlendModes::MIX, 0.4f );
00240         overlay_rect( Vec2D(0.0f,0.0f), Vec2D(0.5f,0.999f), false, Colors::red );
00241                 
00242         for( int i=0; i<MAX_CONSOLE_LINES; i++ )                // draw all lines
00243         {
00244                 int lineIndex = (AS.consoleLineIndex+1+i) % MAX_CONSOLE_LINES;
00245                 overlay_text() << AS.consoleLines[lineIndex];
00246                 overlay_textOut( Vec2D(0.005f, i*0.033f) );             
00247         }
00248 }
00249 //------------------------------------------------------------------------------------
00250 // helper for doGameLoop()
00251 // draw update rate, frame rate & excess time rate
00252 void drawFPS()
00253 {
00254         if(!AS.showFPS) return;
00255         char s[256];
00256         sprintf( s, "update %6.2f\nrender %6.2f\n excess %8.2f\n", AS.updateRate, AS.renderRate, AS.excessRate );
00257         overlay_text() << s;
00258         overlay_textOut( Vec2D(0.82f,0.0f) );
00259 }
00260 //------------------------------------------------------------------------------------
00261 // helper for doGameLoop()
00262 // checks&processes system keys (commands directly to the engine)
00263 void checkSysKeys()
00264 {       
00265         // LShift+LCtrl+END ==> quit
00266         if( input_isKeyDown(KeyCodes::key_LCONTROL) && input_isKeyDown(KeyCodes::key_LSHIFT)
00267                 && input_isKeyDown(KeyCodes::key_END) )
00268                 throw TerminateNormally();
00269 
00270         // LShift+LCtrl+C ==> toggle console visibility
00271         if( input_isKeyDown(KeyCodes::key_LCONTROL) && input_isKeyDown(KeyCodes::key_LSHIFT)
00272                 && input_isKeyPressed(KeyCodes::key_C) )        
00273                 AS.showConsole = !AS.showConsole;       
00274 
00275         // LShift+LCtrl+F ==> toggle FPS visibility
00276         if( input_isKeyDown(KeyCodes::key_LCONTROL) && input_isKeyDown(KeyCodes::key_LSHIFT)
00277                 && input_isKeyPressed(KeyCodes::key_F) )
00278                 AS.showFPS = !AS.showFPS;
00279 }
00280 //------------------------------------------------------------------------------------
00281 void flushRenderRequests()
00282 {
00283         overlay_flush();
00284         mesh_flush();
00285         terrain_flush();
00286         sprite_flush();
00287 }
00288 //------------------------------------------------------------------------------------
00289 void processRenderRequests()
00290 {
00291         mesh_processRenderRequests();
00292         terrain_processRenderRequest();
00293         sprite_process();
00294         flare_process();
00295         
00296         drawConsole();
00297         drawFPS();
00298         overlay_render();
00299 }
00300 //------------------------------------------------------------------------------------
00301 // helper function for sys_go.  this executes the gameloop forever (until an exception)
00302 void doGameLoop()
00303 {
00304         AS.idealMS = sys_getMilliSec();
00305         profiler_reset();
00306         updateInput();
00307         (AS.updateFunc)();
00308 
00309         while(true)
00310         {
00311                 profiler_begin("sys_overhead");
00312                 DWORD thisMS  = sys_getMilliSec();
00313                 DWORD elapsed = thisMS - AS.idealMS;
00314                 if( elapsed<1  ) elapsed = 1;
00315                 if( elapsed>100) elapsed = 100;
00316 
00317                 if( thisMS-AS.lastCountResetMS >= 1000 )                // calculate update/render/excess rate
00318                 {
00319                         FLOAT deltaT = (FLOAT) (thisMS-AS.lastCountResetMS);
00320                         AS.updateRate = AS.updateCount*1000.0f / deltaT;
00321                         AS.renderRate = AS.renderCount*1000.0f / deltaT;
00322                         AS.excessRate = AS.excessCount*1000.0f / deltaT;
00323                         AS.lastCountResetMS = thisMS;
00324                         AS.updateCount = 0;
00325                         AS.renderCount = 0;
00326                         AS.excessCount = 0;                     
00327                 }
00328 
00329                 if( thisMS-AS.profiler_MSlastReset >= 20000 )   // profiler
00330                 {
00331                         profiler_render();
00332                         profiler_reset();
00333                 }
00334                 profiler_end("sys_overhead");
00335                 
00336                 profiler_begin("sys_update");
00337 
00338                 if( AS.fastForwardAmount>0 )
00339                 {
00340                         for( int i=0; i<AS.fastForwardAmount; i++ )                             // fast forwarding
00341                         {
00342                                 updateInput();
00343                                 checkSysKeys();
00344                                 (AS.updateFunc)();
00345                                 AS.updateCount++;
00346                                 handleSystemMessages();
00347                         }
00348                         elapsed = 0;
00349                         AS.idealMS = sys_getMilliSec();         // attempt to make the system think we're just right on time
00350                         AS.fastForwardAmount = 0;
00351                 }
00352 
00353                 while( elapsed >= 16 )                                          // keep updating until we catch up
00354                 {
00355                         DWORD prevElapsed = elapsed;
00356                         updateInput();                  
00357 
00358                         checkSysKeys();
00359                         (AS.updateFunc)();
00360                         AS.updateCount++;
00361                         AS.idealMS += 16;
00362                         thisMS  = sys_getMilliSec();
00363                         elapsed = thisMS - AS.idealMS;
00364                         if(elapsed >= prevElapsed)
00365                         {
00366                                 sys_console() << "WARNING Update is slower than 62.5Hz" << endl;
00367                                 DWORD timeTaken = 16 + elapsed - prevElapsed;
00368                                 if(timeTaken) sys_console() << "WARNING Update() is taking " << timeTaken << " ms" << endl;
00369                         }                       
00370                         
00371                         handleSystemMessages();
00372                 }               
00373                 profiler_end("sys_update");
00374 
00375                 profiler_begin("sys_render");
00376                 if( AS.renderNumber++ % (1+AS.skipRate) == 0 )
00377                 {                                                                                       // rendering
00378                         AS.renderCount++;
00379                         flushRenderRequests();                  
00380                         (AS.renderFunc)();
00381                         beginRendering();
00382                         processRenderRequests();
00383                         endRendering();                 
00384                 }
00385                 profiler_end("sys_render");
00386 
00387                 thisMS  = sys_getMilliSec();
00388                 elapsed = thisMS - AS.idealMS;
00389                 handleSystemMessages();
00390 
00391                 profiler_begin("sys_sleep");
00392                 if(elapsed<UPDATE_INTERVAL)                                     // block if we are going too fast
00393                 {
00394                         AS.excessCount += 16-elapsed;
00395                         Sleep(16-elapsed);
00396                         while(elapsed<UPDATE_INTERVAL)
00397                         {
00398                                 thisMS = sys_getMilliSec();
00399                                 elapsed = thisMS - AS.idealMS;
00400                                 if( elapsed<1  ) elapsed = 1;
00401                                 if( elapsed>100) elapsed = 100;                                 
00402                         }                               
00403                 }
00404                 profiler_end("sys_sleep");
00405 
00406                 handleSystemMessages();
00407         }
00408 }
00409 //------------------------------------------------------------------------------------
00410 // window update function
00411 LRESULT WINAPI AierraWindowProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
00412 {
00413     if( msg==WM_DESTROY ) throw TerminateNormally();
00414     return DefWindowProc( hWnd, msg, wParam, lParam );
00415 }
00416 //------------------------------------------------------------------------------------
00417 void createWindow()
00418 {
00419         // Register the window class
00420     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, AierraWindowProc, 0L, 0L, GetModuleHandle(NULL),
00421                                           NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "Aierra Engine", NULL };
00422     RegisterClassEx( &wc );
00423 
00424     // Create the application's window  
00425     AS.hWnd = CreateWindow( "Aierra Engine", "Modern Warfare", WS_VISIBLE, 0,0, 
00426                                                         AS.windowWidth, AS.windowHeight,
00427                             GetDesktopWindow(), NULL, wc.hInstance, NULL );     
00428         if(AS.hWnd==NULL) throw Error("Can't Create New Window");
00429 }
00430 //------------------------------------------------------------------------------------
00431 // find a display mode that the graphics card can support
00432 void findCompatibleMode(D3DDISPLAYMODE &d3dDisplayMode)
00433 {
00434         int modeCount = AS.pD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT);
00435         bool foundGoodMode = false;
00436         for( int i=0; i<modeCount; i++ )
00437         {
00438                 AS.pD3D->EnumAdapterModes( D3DADAPTER_DEFAULT, i, &d3dDisplayMode );
00439                 D3DFORMAT format = d3dDisplayMode.Format;
00440                 if( d3dDisplayMode.Width==AS.windowWidth && d3dDisplayMode.Height==AS.windowHeight
00441                                                                  && d3dDisplayMode.RefreshRate>=40 )
00442                         if( format==D3DFMT_A8R8G8B8 || format==D3DFMT_A4R4G4B4 || format==D3DFMT_R5G6B5 || format==D3DFMT_R8G8B8 )
00443                         {
00444                                 foundGoodMode = true;
00445                                 i = modeCount;                  // Break the loop
00446                         }                       
00447         }
00448         if(!foundGoodMode) throw Error("Cant't Find Compatible Display Mode");
00449 }
00450 //------------------------------------------------------------------------------------
00451 void createRenderingDevice()
00452 {
00453         AS.pD3D = Direct3DCreate8(D3D_SDK_VERSION);
00454         if( AS.pD3D==NULL) throw Error("Can't create Direct3D");
00455 
00456         int rc;         // return code
00457         D3DDISPLAYMODE                  d3dDisplayMode;
00458         D3DPRESENT_PARAMETERS   d3dParam;
00459         ZeroMemory( &d3dParam, sizeof(d3dParam) );
00460     d3dParam.Windowed = !AS.isFullScreen;
00461     d3dParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
00462 
00463         if(AS.isFullScreen)
00464         {
00465                 AS.clearFlags = D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL ;
00466                 findCompatibleMode(d3dDisplayMode);
00467                 // Confirm Mode Compatibility
00468                 rc = AS.pD3D->CheckDeviceType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dDisplayMode.Format,                                                                               
00469                                                 d3dDisplayMode.Format, false );
00470                 if(FAILED(rc)) throw Error("Video Mode Not Compatible");                
00471                 d3dParam.BackBufferWidth  = d3dDisplayMode.Width;
00472                 d3dParam.BackBufferHeight = d3dDisplayMode.Height;
00473                 d3dParam.BackBufferFormat = d3dDisplayMode.Format;      
00474                 d3dParam.EnableAutoDepthStencil = TRUE;
00475                 d3dParam.AutoDepthStencilFormat = D3DFMT_D24S8;
00476                 d3dParam.FullScreen_RefreshRateInHz = d3dDisplayMode.RefreshRate;
00477                 d3dParam.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
00478         }
00479         else
00480         {
00481                 AS.clearFlags= D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER ;
00482                 // choose same display mode as current desktop
00483                 rc = AS.pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3dDisplayMode);
00484                 if(FAILED(rc)) throw Error("Can't Find Existing Display Mode");
00485                 d3dParam.BackBufferFormat = d3dDisplayMode.Format;
00486                 d3dParam.EnableAutoDepthStencil = TRUE; 
00487                 d3dParam.AutoDepthStencilFormat = D3DFMT_D16;
00488         }
00489 
00490         // create direct 3D rendering device
00491         rc = AS.pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, AS.hWnd,
00492                                                         D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dParam, &AS.pDevice );
00493         if( FAILED(rc) ) throw Error("Can't create Direct3D Device");
00494         
00495         ShowWindow(AS.hWnd, SW_SHOWDEFAULT);
00496     UpdateWindow(AS.hWnd);
00497 
00498         if(!AS.isFullScreen)            // get actual window size (minus the border,titlebar)
00499         {               
00500                 RECT rect;
00501                 GetClientRect(AS.hWnd,&rect);
00502                 AS.windowWidth  = rect.right  - rect.left;
00503                 AS.windowHeight = rect.bottom - rect.top;
00504         }
00505 }
00506 //------------------------------------------------------------------------------------
00508 void sys_shutdown()
00509 {
00510         static bool shutdownAlready = false;
00511         assert(!shutdownAlready);
00512         shutdownAlready = true;
00513 
00514         delete AS.pConsoleStream;
00515         
00516         for( UINT i=0; i<AS.res_fonts.size(); i++ )  delete AS.res_fonts[i];            // fonts
00517 
00518         delete AS.overlay_pTextStream;
00519 
00520         delete AS.pConsoleLogFile;
00521 
00522 }
00523 //------------------------------------------------------------------------------------
00524 void displayLoadingScreen()
00525 {
00526         overlay_flush();
00527         beginRendering();
00528         ATexture tex = load_texture("textures\\loading.tga");
00529         overlay_image( tex, Vec2D(0.5f,0.5f), Vec2D(1.0f,1.0f) );
00530         overlay_render();
00531         endRendering();
00532 }
00533 //------------------------------------------------------------------------------------
00534 void sys_go()
00535 {
00536         createWindow();
00537         createRenderingDevice();
00538 
00539         // init all major components
00540         input_init();
00541         overlay_init(); 
00542 
00543         // init all sub components
00544         // init console
00545         AS.showConsole = true;
00546         AS.showFPS     = false;
00547         AS.pConsoleStream = new ostringstream();
00548         AS.consoleLineIndex = 0;
00549 
00550         AS.camera_screenSize = 1.0f;
00551         AS.camera_vibratePos = 0.0f;
00552         AS.camera_vibrateSpeed = 0.0f;
00553 
00554         AS.input_mouseIdleTickCount = 0;
00555         AS.input_lastTickMousePos = Vec2D(0,0);
00556 
00557         // init game loop control counters
00558         AS.lastCountResetMS = 0;
00559         AS.updateCount = 0;
00560         AS.updateRate  = 0;
00561         AS.renderCount = 0;
00562         AS.renderRate  = 0;
00563         AS.excessCount = 0;
00564         AS.excessRate  = 0;
00565         AS.renderNumber = 0;
00566         AS.fastForwardAmount = 0;
00567 
00568         // specific to this game
00569         displayLoadingScreen();
00570 
00571         // networking
00572         AS.net_isInit = false;
00573 
00574         for(int i=0; i<MAX_LOCAL_RANDON_NUM; i++)                                       // local random number generator
00575                 AS.res_localRandomNums.push_back( rand()%256 );
00576         for(int i=0; i<MAX_LOCAL_RANDON_FLOAT; i++ )
00577                 AS.res_localRandomFloats.push_back( sys_randFloat() );
00578         AS.res_localRandomNumIndex = 0;
00579         AS.res_localRandomFloatIndex = 0;
00580 
00581         // user-defined initialization
00582         (AS.initFunc)();
00583 
00584         mesh_init();
00585         flare_init();
00586         sprite_init();
00587 
00588         // Let it all go
00589         try
00590         {
00591                 doGameLoop();
00592         }
00593         catch(TerminateNormally e)
00594         {
00595                 sys_shutdown();
00596                 if(AS.shutdownFunc) (AS.shutdownFunc)();
00597                 return;
00598         }
00599 }
00600 //------------------------------------------------------------------------------------
00601 void sys_terminate()
00602 {
00603         sys_flushConsole();
00604         throw TerminateNormally();
00605 }
00606 //------------------------------------------------------------------------------------
00607 void sys_showFPS(bool show)
00608 {
00609         AS.showFPS = show;
00610 }
00611 //------------------------------------------------------------------------------------
00612 void sys_showConsole(bool show)
00613 {
00614         AS.showConsole = show;  
00615 }
00616 //------------------------------------------------------------------------------------
00617 void sys_enableConsoleLog(string filename)
00618 {
00619         // make sure we wipe out all previous content in the file if it already exists
00620         AS.pConsoleLogFile = new ofstream(filename.c_str());
00621         (*AS.pConsoleLogFile) << "Clear Log" << endl;
00622         AS.pConsoleLogFile->close();
00623         delete AS.pConsoleLogFile;
00624 
00625         // open up the file for writting
00626         AS.pConsoleLogFile = new ofstream(filename.c_str());
00627 }
00628 //------------------------------------------------------------------------------------
00629 ostream& sys_console()
00630 {       
00631         assert(AS.pConsoleStream);
00632         return *(AS.pConsoleStream);
00633 }
00634 //------------------------------------------------------------------------------------
00635 void sys_flushConsole()
00636 {
00637         if(!AS.pConsoleLogFile) return;
00638 
00639         istringstream in(AS.pConsoleStream->str());
00640         string line;    
00641         while(in)
00642         {
00643                 getline( in, line );
00644                 if(line!="" || in)
00645                 {
00646                         AS.consoleLineIndex = (AS.consoleLineIndex+1) % MAX_CONSOLE_LINES;
00647                         AS.consoleLines[ AS.consoleLineIndex ] = line;
00648                         (*AS.pConsoleLogFile) << line << endl;
00649                 }
00650         }
00651 
00652         delete AS.pConsoleStream;
00653         AS.pConsoleStream = new ostringstream();
00654 }
00655 //------------------------------------------------------------------------------------
00656 void sys_fastForwardUpdate( int times )
00657 {
00658         assert(times>=0 && times<1000);
00659         AS.fastForwardAmount = times;   
00660 }
00661 //------------------------------------------------------------------------------------
00662 DWORD sys_getMilliSec()
00663 {
00664         LARGE_INTEGER                   tick;
00665         static LARGE_INTEGER    freq    = {0};  
00666         static bool                             inited  = false;
00667         if (!inited)
00668         {
00669                 inited = true;
00670                 QueryPerformanceFrequency( &freq );
00671         }
00672         QueryPerformanceCounter( &tick );
00673         return (DWORD)((tick.QuadPart * 1000) / freq.QuadPart);
00674 }
00675 //------------------------------------------------------------------------------------
00676 // currently, these are just direct wrappers of the C library equivalents
00677 // we will add log/profiling when we need to
00678 void  sys_setRandSeed(int seed)
00679 {
00680         srand( (unsigned int)seed );
00681         AS.res_localRandomNumIndex = seed % MAX_LOCAL_RANDON_NUM;
00682         AS.res_localRandomFloatIndex = seed % MAX_LOCAL_RANDON_FLOAT;
00683 }
00684 //------------------------------------------------------------------------------------
00685 int   sys_randInt()
00686 {
00687         return rand();
00688 }
00689 //------------------------------------------------------------------------------------
00690 FLOAT sys_randFloat()
00691 {
00692         return (rand()&0x0ff) / 128.0f - 1.0f;
00693 }
00694 //------------------------------------------------------------------------------------
00695 int sys_localRandInt()
00696 {
00697         AS.res_localRandomNumIndex = (AS.res_localRandomNumIndex + 1) % MAX_LOCAL_RANDON_NUM;
00698         return AS.res_localRandomNums[ AS.res_localRandomNumIndex ];
00699 }
00700 //------------------------------------------------------------------------------------
00701 FLOAT sys_localRandFloat()
00702 {
00703         AS.res_localRandomFloatIndex = (AS.res_localRandomFloatIndex + 1) % MAX_LOCAL_RANDON_FLOAT;
00704         return AS.res_localRandomFloats[ AS.res_localRandomFloatIndex ];
00705 }
00706 //------------------------------------------------------------------------------------
00707 HWND sys_getWindowHandle()
00708 {
00709         return AS.hWnd;
00710 }
00711 //------------------------------------------------------------------------------------
00712 

Generated on Wed Apr 23 05:50:14 2003 for Modern Warfare by doxygen1.3-rc2