Created
July 8, 2026 03:08
-
-
Save jweinst1/c0575db0c5c3f2117311eb6323db2636 to your computer and use it in GitHub Desktop.
empty SDL3 time accum loop
This file contains hidden or 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 <iostream> | |
| #include <cstdint> | |
| #include <cassert> | |
| #include <cmath> | |
| #include <cstdlib> | |
| #include <cstring> | |
| #include <vector> | |
| #include <utility> | |
| #include <algorithm> | |
| #include <string> | |
| int main(int argc, char const *argv[]) | |
| { | |
| if (!SDL_Init(SDL_INIT_VIDEO)) { | |
| std::cerr << "Initialization failed: " << SDL_GetError() << "\n"; | |
| return -1; | |
| } | |
| SDL_Window* window = nullptr; | |
| SDL_Renderer* renderer = nullptr; | |
| if (!SDL_CreateWindowAndRenderer("The Modern Black Swordsman", SQ_CAM_X_PX, SQ_CAM_Y_PX, 0, &window, &renderer)) { | |
| std::cerr << "Failed to create window/renderer: " << SDL_GetError() << "\n"; | |
| return -1; | |
| } | |
| bool running = true; | |
| // Fixed timestep setup (60 updates per second) | |
| const Uint64 FIXED_DELTA_TIME_NS = 1000000000 / 60; | |
| Uint64 current_time_ns = SDL_GetTicksNS(); | |
| Uint64 previous_time_ns = current_time_ns; | |
| Uint64 accumulator_ns = 0; | |
| while (running) { | |
| current_time_ns = SDL_GetTicksNS(); | |
| Uint64 elapsed_ns = current_time_ns - previous_time_ns; | |
| previous_time_ns = current_time_ns; | |
| if (elapsed_ns > 250000000) elapsed_ns = 250000000; | |
| accumulator_ns += elapsed_ns; | |
| // ========================================== | |
| // 1. INPUT DETECTION (OS Events & Mouse) | |
| // ========================================== | |
| SDL_Event event; | |
| while (SDL_PollEvent(&event)) { | |
| if (event.type == SDL_EVENT_QUIT) { | |
| running = false; | |
| } | |
| // Detect Mouse Clicks in SDL3 | |
| else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { | |
| if (event.button.button == SDL_BUTTON_LEFT) { | |
| // SDL3 provides mouse coordinates as clean floats | |
| float mousepre_x = event.button.x; | |
| float mousepre_y = event.button.y; | |
| std::cout << "Clicked x=" << mousepre_x << ", y=" << mousepre_y << "\n"; | |
| } | |
| } | |
| } | |
| // ========================================== | |
| // 2. FIXED PHYSICS / LOGIC TIMESTEP | |
| // ========================================== | |
| while (accumulator_ns >= FIXED_DELTA_TIME_NS) { | |
| // Update item animations, physics, and skilling timers here | |
| //playerPathMover.moveSprite(); | |
| // based on path, change target. | |
| accumulator_ns -= FIXED_DELTA_TIME_NS; | |
| } | |
| // ========================================== | |
| // 3. PAINT / RENDERING THE WINDOW | |
| // ========================================== | |
| // A. Wipe the canvas clean with background color (Dark Berserk Charcoal) | |
| SDL_SetRenderDrawColor(renderer, 20, 20, 20, 255); | |
| SDL_RenderClear(renderer); | |
| //SDL_RenderFillRect(renderer, &specrect); | |
| // C. Command the GPU to push the backbuffer into the visible monitor frame | |
| SDL_RenderPresent(renderer); | |
| } | |
| SDL_DestroyRenderer(renderer); | |
| SDL_DestroyWindow(window); | |
| SDL_Quit(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment