Advertisement
Guest User

Untitled

a guest
Feb 17th, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <sdl\SDL.h>
  2. #include <sdl\SDL_opengl.h>
  3. #include <GL\GLU.h>
  4. #include <iostream>
  5. #include <ctime>
  6. #include <chrono>
  7. #include <windows.h>
  8.  
  9. SDL_Window* window;
  10. int window_width = 800;
  11. int window_height = 480;
  12.  
  13.  
  14. LARGE_INTEGER li;
  15. double ticks_per_second;
  16. __int64 last_time = 0;
  17. double get_delta() {
  18.     QueryPerformanceCounter(&li);
  19.     __int64 now = li.QuadPart;
  20.     int diff = now - last_time;
  21.     last_time = now;
  22.     return double(diff) / ticks_per_second;
  23. }
  24.  
  25. int main(int argc, char* argv[]) {
  26.     QueryPerformanceFrequency(&li);
  27.     ticks_per_second = double(li.QuadPart);
  28.  
  29.  
  30.  
  31.     SDL_Init(SDL_INIT_EVERYTHING);
  32.     window = SDL_CreateWindow("SDL Game Loop", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width, window_height, SDL_WINDOW_OPENGL);
  33.     //SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
  34.     SDL_GL_CreateContext(window);
  35.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  36.     SDL_GL_SetSwapInterval(0);
  37.     glClearColor(.2,.2,.2,1);
  38.  
  39.     double dt = get_delta();
  40.     while (true) {
  41.         SDL_Event event;
  42.         while (SDL_PollEvent(&event)) {
  43.             switch (event.type) {
  44.             case SDL_QUIT:
  45.                 return 0;
  46.                 break;
  47.             }
  48.         }
  49.        
  50.  
  51.         glClearDepth(1);
  52.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  53.  
  54.         get_delta();
  55.         SDL_GL_SwapWindow(window);
  56.         dt = get_delta();
  57.  
  58.         if (dt >= 0.001) std::cout << "SDL_GL_SwapWindow(window) took " << dt << "ms" << std::endl;
  59.         if (dt >= 0.016) std::cout << "huge dt warning: " << dt << std::endl;
  60.  
  61.         //double freetime = (1.0 / 60.0) - dt - 0.001;
  62.         //if (freetime > 0.001) SDL_Delay(freetime * 1000);
  63.     }
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement