Add change Frame time POG
This commit is contained in:
parent
010f6799ee
commit
b30e525d69
1 changed files with 91 additions and 12 deletions
103
main.c
103
main.c
|
@ -4,58 +4,137 @@
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3/SDL_main.h> // only include this one in the source file with main()!
|
#include <SDL3/SDL_main.h> // only include this one in the source file with main()!
|
||||||
|
|
||||||
|
#define INITIAL_FPS 60
|
||||||
|
|
||||||
SDL_Window* window = NULL;
|
SDL_Window* window = NULL;
|
||||||
SDL_Renderer* renderer = NULL;
|
SDL_Renderer* renderer = NULL;
|
||||||
|
|
||||||
|
static double FPS = INITIAL_FPS;
|
||||||
|
static double FrameTime = 1.0 / INITIAL_FPS;
|
||||||
|
|
||||||
|
static double PerformancePeriod;
|
||||||
|
|
||||||
static void panic ( const char *text ) __attribute__((noreturn));
|
static void panic ( const char *text ) __attribute__((noreturn));
|
||||||
|
|
||||||
|
static double GetTime()
|
||||||
|
{
|
||||||
|
return PerformancePeriod * SDL_GetPerformanceCounter();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SetTitle(SDL_Window * window)
|
||||||
|
{
|
||||||
|
char title[48] = {0};
|
||||||
|
SDL_snprintf(title, sizeof(title), "Crude frame limiter @ %6.3f Hz", FPS);
|
||||||
|
SDL_SetWindowTitle(window, title);
|
||||||
|
}
|
||||||
|
|
||||||
void panic ( const char *text )
|
void panic ( const char *text )
|
||||||
{
|
{
|
||||||
fprintf(stderr, "ERROR: %s. %s\n", text, SDL_GetError());
|
fprintf(stderr, "ERROR: %s. %s\n", text, SDL_GetError());
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( int argc, char* argv[] )
|
int main( int argc, char* argv[] )
|
||||||
{
|
{
|
||||||
const int WIDTH = 640;
|
const int WIDTH = 640;
|
||||||
const int HEIGHT = 480;
|
const int HEIGHT = 480;
|
||||||
|
int delay_threshold = 2;
|
||||||
|
double sleep_time;
|
||||||
|
double time_counter = 0;
|
||||||
bool loopShouldStop = false;
|
bool loopShouldStop = false;
|
||||||
|
|
||||||
|
SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0);
|
||||||
|
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||||
{
|
{
|
||||||
panic("SDL_Init failed");
|
panic("SDL_Init failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
window = SDL_CreateWindow("Hello SDL", WIDTH, HEIGHT, 0);
|
PerformancePeriod = 1.0 / SDL_GetPerformanceFrequency();
|
||||||
if (!window)
|
time_counter = GetTime();
|
||||||
|
|
||||||
|
window = SDL_CreateWindow("Hello SDL", WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
|
||||||
|
if (!window)
|
||||||
{
|
{
|
||||||
panic("SDL_CreateWindow");
|
panic("SDL_CreateWindow");
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer = SDL_CreateRenderer(window, "opengl");
|
renderer = SDL_CreateRenderer(window, "opengl");
|
||||||
if (!renderer)
|
if (!renderer)
|
||||||
{
|
{
|
||||||
panic("SDL_CreateRenderer");
|
panic("SDL_CreateRenderer");
|
||||||
}
|
}
|
||||||
|
|
||||||
int orange = 0;
|
//SDL_GetWindowSize(window, &width, &height);
|
||||||
|
SetTitle(window);
|
||||||
|
|
||||||
|
//if (!SDL_SetRenderVSync(renderer, 1))
|
||||||
|
//{
|
||||||
|
// panic("SDL_SetRenderVSync");
|
||||||
|
//}
|
||||||
|
|
||||||
|
SDL_HideCursor();
|
||||||
|
|
||||||
|
int red = 0;
|
||||||
|
int green = 0;
|
||||||
while (!loopShouldStop)
|
while (!loopShouldStop)
|
||||||
{
|
{
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
SDL_zero(e);
|
SDL_zero(e);
|
||||||
while (SDL_PollEvent(&e))
|
while (SDL_PollEvent(&e))
|
||||||
{
|
{
|
||||||
if (e.type == SDL_EVENT_KEY_DOWN && e.key.mod == SDL_KMOD_LCTRL && e.key.key == SDLK_Q)
|
if (e.type == SDL_EVENT_QUIT)
|
||||||
|
loopShouldStop = true;
|
||||||
|
else if (e.type == SDL_EVENT_KEY_DOWN)
|
||||||
|
{
|
||||||
|
if (e.key.key == SDLK_X)
|
||||||
|
{
|
||||||
|
FPS -= 0.1;
|
||||||
|
if (FPS < 1)
|
||||||
|
{
|
||||||
|
FPS = 1;
|
||||||
|
}
|
||||||
|
FrameTime = 1.0 / FPS;
|
||||||
|
SetTitle(window);
|
||||||
|
}
|
||||||
|
else if (e.key.key == SDLK_C)
|
||||||
|
{
|
||||||
|
FPS += 0.1;
|
||||||
|
FrameTime = 1.0 / FPS;
|
||||||
|
SetTitle(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (e.key.mod == SDL_KMOD_LCTRL && e.key.key == SDLK_Q)
|
||||||
loopShouldStop = true;
|
loopShouldStop = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer, 0, orange, 0, 255);
|
SDL_SetRenderDrawColor(renderer, red, green, 0, 255);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
|
sleep_time = time_counter - GetTime();
|
||||||
|
if (sleep_time * 1000 > delay_threshold)
|
||||||
|
{
|
||||||
|
Uint32 ms = (Uint32)(sleep_time * 1000) - delay_threshold;
|
||||||
|
SDL_Delay(ms);
|
||||||
|
if (time_counter < GetTime())
|
||||||
|
{
|
||||||
|
delay_threshold++;
|
||||||
|
SDL_Log("Slept too long. Increased threshold to %u ms.", delay_threshold);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (time_counter > GetTime()) {
|
||||||
|
/* Waiting for the right moment. */
|
||||||
|
}
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
orange = (orange + 1) % 256;
|
red = (red + 1) % 256;
|
||||||
|
green = (green + 1) % 64;
|
||||||
|
|
||||||
|
time_counter += FrameTime;
|
||||||
}
|
}
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
|
|
Loading…
Reference in a new issue