// Stuff U don't have to worry about :) ///////////////////////////////////////////////////////// void CTimeManager::CalculateMS(void) // Calculates ms & msTotal { // Calculate ms past per frame ms=(double)TimerGetTime()-oldTime; oldTime=(double)TimerGetTime(); if (ms>100) ms = 100; // on met une limite pour la pondération des déplacements msTotal+=ms; // (absorbe les écarts ponctuels de vitesse trop importants) }
void TimerInit(void) // Initialize Our Timer (Get It Ready) { memset(&timer, 0, sizeof(timer)); // Clear Our Timer Structure // Check To See If A Performance Counter Is Available // If One Is Available The Timer Frequency Will Be Updated if (!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency)) { // No Performace Counter Available timer.performance_timer = FALSE; // Set Performance Timer To FALSE timer.mm_timer_start = (unsigned long)TimerGetTime(); // Use timeGetTime() To Get Current Time timer.resolution = 1.0f/1000.0f; // Set Our Timer Resolution To .001f timer.frequency = 1000; // Set Our Timer Frequency To 1000 timer.mm_timer_elapsed = timer.mm_timer_start; // Set The Elapsed Time To The Current Time } else { // Performance Counter Is Available, Use It Instead Of The Multimedia Timer // Get The Current Time And Store It In performance_timer_start QueryPerformanceCounter((LARGE_INTEGER *) &timer.performance_timer_start); timer.performance_timer = TRUE; // Set Performance Timer To TRUE // Calculate The Timer Resolution Using The Timer Frequency timer.resolution = (float) (((double)1.0f)/((double)timer.frequency)); // Set The Elapsed Time To The Current Time timer.performance_timer_elapsed = timer.performance_timer_start; } }
// Avoids the delay taken by the game loading. That does not affects the accounting void CTimeManager::FPSReset() { ms = 1; // make FPS believe that the last frame had taken 1 ms oldTime = TimerGetTime() -1; // make FPS believe that the last time recorded was 1 ms backwards int loop; for (loop=0;loop<10;loop++) // make FPS believe that the ten last records took 1 ms each { TimePerFrame[loop] = ms; } ms10 = 1; // same thing for ms10 }
float TimerGetTime() // Get Time In Milliseconds { __int64 time; // time Will Hold A 64 Bit Integer if (timer.performance_timer) // Are We Using The Performance Timer? { QueryPerformanceCounter((LARGE_INTEGER *) &time); // Grab The Current Performance Time // Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS) return ( (float) ( time - timer.performance_timer_start) * timer.resolution)*1000.0f; } else { // Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS) return( (float) ( TimerGetTime() - timer.mm_timer_start) * timer.resolution)*1000.0f; } }
int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure BOOL done=FALSE; // Bool Variable To Exit Loop // Ask The User Which Screen Mode They Prefer if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO) { fullscreen=FALSE; // Windowed Mode } // Create Our OpenGL Window if (!CreateGLWindow("NeHe's Line Tutorial",640,480,16,fullscreen)) { return 0; // Quit If Window Was Not Created } ResetObjects(); // Set Player / Enemy Starting Positions TimerInit(); while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; // If So done=TRUE } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { float start=TimerGetTime(); // Grab Timer Value Before We Draw // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received? { done=TRUE; // ESC or DrawGLScene Signalled A Quit } else // Not Time To Quit, Update Screen { SwapBuffers(hDC); // Swap Buffers (Double Buffering) } while(TimerGetTime()<start+float(steps[adjust]*2.0f)) {} // Waste Cycles On Fast Systems if (keys[VK_F1]) // Is F1 Being Pressed? { keys[VK_F1]=FALSE; // If So Make Key FALSE KillGLWindow(); // Kill Our Current Window fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode // Recreate Our OpenGL Window if (!CreateGLWindow("NeHe's Line Tutorial",640,480,16,fullscreen)) { return 0; // Quit If Window Was Not Created } } if (keys['A'] && !ap) // If 'A' Key Is Pressed And Not Held { ap=TRUE; // ap Becomes TRUE anti=!anti; // Toggle Antialiasing } if (!keys['A']) // If 'A' Key Has Been Released { ap=FALSE; // ap Becomes FALSE } if (!gameover && active) // If Game Isn't Over And Programs Active Move Objects { for (loop1=0; loop1<(stage*level); loop1++) // Loop Through The Different Stages { if ((enemy[loop1].x<player.x) && (enemy[loop1].fy==enemy[loop1].y*40)) { enemy[loop1].x++; // Move The Enemy Right } if ((enemy[loop1].x>player.x) && (enemy[loop1].fy==enemy[loop1].y*40)) { enemy[loop1].x--; // Move The Enemy Left } if ((enemy[loop1].y<player.y) && (enemy[loop1].fx==enemy[loop1].x*60)) { enemy[loop1].y++; // Move The Enemy Down } if ((enemy[loop1].y>player.y) && (enemy[loop1].fx==enemy[loop1].x*60)) { enemy[loop1].y--; // Move The Enemy Up } if (delay>(3-level) && (hourglass.fx!=2)) // If Our Delay Is Done And Player Doesn't Have Hourglass { delay=0; // Reset The Delay Counter Back To Zero for (loop2=0; loop2<(stage*level); loop2++) // Loop Through All The Enemies { if (enemy[loop2].fx<enemy[loop2].x*60) // Is Fine Position On X Axis Lower Than Intended Position? { enemy[loop2].fx+=steps[adjust]; // If So, Increase Fine Position On X Axis enemy[loop2].spin+=steps[adjust]; // Spin Enemy Clockwise } if (enemy[loop2].fx>enemy[loop2].x*60) // Is Fine Position On X Axis Higher Than Intended Position? { enemy[loop2].fx-=steps[adjust]; // If So, Decrease Fine Position On X Axis enemy[loop2].spin-=steps[adjust]; // Spin Enemy Counter Clockwise } if (enemy[loop2].fy<enemy[loop2].y*40) // Is Fine Position On Y Axis Lower Than Intended Position? { enemy[loop2].fy+=steps[adjust]; // If So, Increase Fine Position On Y Axis enemy[loop2].spin+=steps[adjust]; // Spin Enemy Clockwise } if (enemy[loop2].fy>enemy[loop2].y*40) // Is Fine Position On Y Axis Higher Than Intended Position? { enemy[loop2].fy-=steps[adjust]; // If So, Decrease Fine Position On Y Axis enemy[loop2].spin-=steps[adjust]; // Spin Enemy Counter Clockwise } } } // Are Any Of The Enemies On Top Of The Player? if ((enemy[loop1].fx==player.fx) && (enemy[loop1].fy==player.fy)) { lives--; // If So, Player Loses A Life if (lives==0) // Are We Out Of Lives? { gameover=TRUE; // If So, gameover Becomes TRUE } ResetObjects(); // Reset Player / Enemy Positions PlaySound("Data/Die.wav", NULL, SND_SYNC); // Play The Death Sound } } if (keys[VK_RIGHT] && (player.x<10) && (player.fx==player.x*60) && (player.fy==player.y*40)) { hline[player.x][player.y]=TRUE; // Mark The Current Horizontal Border As Filled player.x++; // Move The Player Right } if (keys[VK_LEFT] && (player.x>0) && (player.fx==player.x*60) && (player.fy==player.y*40)) { player.x--; // Move The Player Left hline[player.x][player.y]=TRUE; // Mark The Current Horizontal Border As Filled } if (keys[VK_DOWN] && (player.y<10) && (player.fx==player.x*60) && (player.fy==player.y*40)) { vline[player.x][player.y]=TRUE; // Mark The Current Verticle Border As Filled player.y++; // Move The Player Down } if (keys[VK_UP] && (player.y>0) && (player.fx==player.x*60) && (player.fy==player.y*40)) { player.y--; // Move The Player Up vline[player.x][player.y]=TRUE; // Mark The Current Verticle Border As Filled } if (player.fx<player.x*60) // Is Fine Position On X Axis Lower Than Intended Position? { player.fx+=steps[adjust]; // If So, Increase The Fine X Position } if (player.fx>player.x*60) // Is Fine Position On X Axis Greater Than Intended Position? { player.fx-=steps[adjust]; // If So, Decrease The Fine X Position } if (player.fy<player.y*40) // Is Fine Position On Y Axis Lower Than Intended Position? { player.fy+=steps[adjust]; // If So, Increase The Fine Y Position } if (player.fy>player.y*40) // Is Fine Position On Y Axis Lower Than Intended Position? { player.fy-=steps[adjust]; // If So, Decrease The Fine Y Position } } else // Otherwise { if (keys[' ']) // If Spacebar Is Being Pressed { gameover=FALSE; // gameover Becomes FALSE filled=TRUE; // filled Becomes TRUE level=1; // Starting Level Is Set Back To One level2=1; // Displayed Level Is Also Set To One stage=0; // Game Stage Is Set To Zero lives=5; // Lives Is Set To Five } } if (filled) // Is The Grid Filled In? { PlaySound("Data/Complete.wav", NULL, SND_SYNC); // If So, Play The Level Complete Sound stage++; // Increase The Stage if (stage>3) // Is The Stage Higher Than 3? { stage=1; // If So, Set The Stage To One level++; // Increase The Level level2++; // Increase The Displayed Level if (level>3) // Is The Level Greater Than 3? { level=3; // If So, Set The Level To 3 lives++; // Give The Player A Free Life if (lives>5) // Does The Player Have More Than 5 Lives? { lives=5; // If So, Set Lives To Five } } } ResetObjects(); // Reset Player / Enemy Positions for (loop1=0; loop1<11; loop1++) // Loop Through The Grid X Coordinates { for (loop2=0; loop2<11; loop2++) // Loop Through The Grid Y Coordinates { if (loop1<10) // If X Coordinate Is Less Than 10 { hline[loop1][loop2]=FALSE; // Set The Current Horizontal Value To FALSE } if (loop2<10) // If Y Coordinate Is Less Than 10 { vline[loop1][loop2]=FALSE; // Set The Current Vertical Value To FALSE } } } } // If The Player Hits The Hourglass While It's Being Displayed On The Screen if ((player.fx==hourglass.x*60) && (player.fy==hourglass.y*40) && (hourglass.fx==1)) { // Play Freeze Enemy Sound PlaySound("Data/freeze.wav", NULL, SND_ASYNC | SND_LOOP); hourglass.fx=2; // Set The hourglass fx Variable To Two hourglass.fy=0; // Set The hourglass fy Variable To Zero } player.spin+=0.5f*steps[adjust]; // Spin The Player Clockwise if (player.spin>360.0f) // Is The spin Value Greater Than 360? { player.spin-=360; // If So, Subtract 360 } hourglass.spin-=0.25f*steps[adjust]; // Spin The Hourglass Counter Clockwise if (hourglass.spin<0.0f) // Is The spin Value Less Than 0? { hourglass.spin+=360.0f; // If So, Add 360 } hourglass.fy+=steps[adjust]; // Increase The hourglass fy Variable if ((hourglass.fx==0) && (hourglass.fy>6000/level)) // Is The hourglass fx Variable Equal To 0 And The fy { // Variable Greater Than 6000 Divided By The Current Level? PlaySound("Data/hourglass.wav", NULL, SND_ASYNC); // If So, Play The Hourglass Appears Sound hourglass.x=rand()%10+1; // Give The Hourglass A Random X Value hourglass.y=rand()%11; // Give The Hourglass A Random Y Value hourglass.fx=1; // Set hourglass fx Variable To One (Hourglass Stage) hourglass.fy=0; // Set hourglass fy Variable To Zero (Counter) } if ((hourglass.fx==1) && (hourglass.fy>6000/level)) // Is The hourglass fx Variable Equal To 1 And The fy { // Variable Greater Than 6000 Divided By The Current Level? hourglass.fx=0; // If So, Set fx To Zero (Hourglass Will Vanish) hourglass.fy=0; // Set fy to Zero (Counter Is Reset) } if ((hourglass.fx==2) && (hourglass.fy>500+(500*level))) // Is The hourglass fx Variable Equal To 2 And The fy { // Variable Greater Than 500 Plus 500 Times The Current Level? PlaySound(NULL, NULL, 0); // If So, Kill The Freeze Sound hourglass.fx=0; // Set hourglass fx Variable To Zero hourglass.fy=0; // Set hourglass fy Variable To Zero } delay++; // Increase The Enemy Delay Counter } } // Shutdown KillGLWindow(); // Kill The Window return (msg.wParam); // Exit The Program }
// Fps_Init /////////////////////////////////////////////////////////////////////////////////////// // Initalizes the fps variables (Must be called once on startup) void CTimeManager::Fps_Init() { TimerInit(); fpsoldtime=(long)TimerGetTime(); msTotal=0; }
int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure s1.readInitFile(); // Create Our OpenGL Window if (!b1.CreateGLWindow(winname,resset[0] [res],resset[1] [res],resset[2] [res],(bool)!fullres)) return 0; // Quit If Window Was Not Created TimerInit(); glClearColor(0.0f,0.0f,0.0f,0.5f); BOOL escpress=FALSE; BOOL inmenu=FALSE; bool vspace=FALSE; while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; // If So done=TRUE } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer if(intro) { runintro(); SwapBuffers(hDC); } if(!intro) { float start=TimerGetTime(); if(keys[VK_ESCAPE] && !inmenu && !escpress) { inmenu=escpress=TRUE; shot.LoadSong("beep.mp3"); oldres=res; oldfullres=fullres; } if(keys[VK_ESCAPE] && inmenu && !escpress) { inmenu=FALSE; shot.LoadSong("shot.mp3"); escpress=TRUE; } if(!keys[VK_ESCAPE]) escpress=FALSE; DrawGLScene(); if(inmenu) { m1.testmovement(); drawmenu(); } if(!inmenu) { if(keys[VK_SPACE] && !vspace) { w1.shoot(); vspace=TRUE; } if(!keys[VK_SPACE]) vspace=FALSE; w1.testweapons(); m1.testmovement(); e1.testenemies(); checkstuff(); //remember to do all drawing BEFORE swap buffers!!!!! if(character.hurtcounter>0) character.hurtcounter--; if(character.life<0) character.life=0; if(keys[VK_DOWN]) keys[VK_UP]=false; if(keys[VK_UP]) { if(character.yvel<character.termvel) character.yvel+=.5; character.ypos-=character.yvel; } else character.yvel=0; if(!uppress && character.ypos<600) { character.ypos+=character.grav; character.grav+=.5; } else character.grav=2; if(character.ypos>600) character.ypos=600; if(character.ypos<98) character.ypos=98; if(leftpress && !rightpress) { xpos1+=3; xpos+=3; xpos3++; xpos2+=2; if(keys[VK_UP]) { xpos1-=character.xvel; xpos-=character.xvel; xpos2-=character.xvel/2; if(character.xvel>-4) character.xvel-=.5; } else character.xvel=0; character.look=-1; } if(rightpress && !leftpress) { xpos-=3; xpos1-=3; xpos2-=2; xpos3--; if(keys[VK_UP]) { xpos1-=character.xvel; xpos-=character.xvel; xpos2-=character.xvel/2; if(character.xvel<4) character.xvel+=.5; } else character.xvel=0; character.look=1; } } SwapBuffers(hDC); // Swap Buffers (Double Buffering) while(TimerGetTime()<start+float(steps[adjust]*2.0f)) {} } } // Shutdown s1.updateInitFile(); b1.KillGLWindow(); // Kill The Window return (msg.wParam); // Exit The Program }