Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sdl\SDL.h>
- #include <sdl\SDL_opengl.h>
- #include <GL\GLU.h>
- #include <iostream>
- #include <ctime>
- #include <chrono>
- #include <windows.h>
- SDL_Window* window;
- int window_width = 800;
- int window_height = 480;
- LARGE_INTEGER li;
- double ticks_per_second;
- __int64 last_time = 0;
- double get_delta() {
- QueryPerformanceCounter(&li);
- __int64 now = li.QuadPart;
- int diff = now - last_time;
- last_time = now;
- return double(diff) / ticks_per_second;
- }
- int main(int argc, char* argv[]) {
- QueryPerformanceFrequency(&li);
- ticks_per_second = double(li.QuadPart);
- SDL_Init(SDL_INIT_EVERYTHING);
- window = SDL_CreateWindow("SDL Game Loop", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width, window_height, SDL_WINDOW_OPENGL);
- //SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
- SDL_GL_CreateContext(window);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetSwapInterval(0);
- glClearColor(.2,.2,.2,1);
- double dt = get_delta();
- while (true) {
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_QUIT:
- return 0;
- break;
- }
- }
- glClearDepth(1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- get_delta();
- SDL_GL_SwapWindow(window);
- dt = get_delta();
- if (dt >= 0.001) std::cout << "SDL_GL_SwapWindow(window) took " << dt << "ms" << std::endl;
- if (dt >= 0.016) std::cout << "huge dt warning: " << dt << std::endl;
- //double freetime = (1.0 / 60.0) - dt - 0.001;
- //if (freetime > 0.001) SDL_Delay(freetime * 1000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement