Created
December 14, 2022 22:31
-
-
Save hkva/a97fad27ce9a4b7e38d7eea16a0f863d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../SDL" SDL) | |
add_executable(vsync "${CMAKE_CURRENT_LIST_DIR}/vsync.cc") | |
target_link_libraries(vsync PRIVATE SDL3::SDL3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SDL3/SDL.h" | |
#include <cassert> | |
int main() { | |
SDL_Window* w = SDL_CreateWindow("vsync", 0, 0, 1280, 720, 0); assert(w); | |
SDL_Renderer* r = SDL_CreateRenderer(w, "opengl", 0); assert(r); | |
SDL_Log("Available render drivers:\n"); | |
for (int i = 0; i < SDL_GetNumRenderDrivers(); ++i) { | |
SDL_Log("[%d] => %s\n", i, SDL_GetRenderDriver(i)); | |
} | |
float then = 0.0f; | |
bool vsync = false; | |
bool running = true; | |
do { | |
SDL_Event evt = { }; | |
while (SDL_PollEvent(&evt)) { | |
if (evt.type == SDL_QUIT) { running = false; } | |
if (evt.type == SDL_KEYDOWN && evt.key.keysym.sym == SDLK_ESCAPE) { | |
running = false; | |
} | |
if (evt.type == SDL_KEYDOWN && evt.key.keysym.sym == SDLK_v) { | |
vsync = !vsync; | |
if (SDL_RenderSetVSync(r, vsync) != 0) { | |
SDL_Log("SDL_RenderSetVSync failed: %s\n", SDL_GetError()); | |
running = false; | |
} | |
SDL_Log("Set VSync to %d\n", vsync); | |
} | |
} | |
SDL_RenderClear(r); | |
SDL_RenderPresent(r); | |
float now = SDL_GetTicks() / 1e3f; | |
float dt = now - then; | |
then = now; | |
SDL_Log("Frame now=%f dt=%f fps=%.0f\n", now, dt, 1.0f / dt); | |
SDL_Log("SDL_RenderGetVSync(): %d\n", SDL_RenderGetVSync(r)); | |
SDL_Delay(1000/100); | |
} while (running); | |
SDL_DestroyRenderer(r); | |
SDL_DestroyWindow(w); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment