Add change Frame time POG
This commit is contained in:
parent
010f6799ee
commit
b30e525d69
1 changed files with 91 additions and 12 deletions
89
main.c
89
main.c
|
@ -4,11 +4,30 @@
|
||||||
#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());
|
||||||
|
@ -22,14 +41,22 @@ 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();
|
||||||
|
time_counter = GetTime();
|
||||||
|
|
||||||
|
window = SDL_CreateWindow("Hello SDL", WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
|
||||||
if (!window)
|
if (!window)
|
||||||
{
|
{
|
||||||
panic("SDL_CreateWindow");
|
panic("SDL_CreateWindow");
|
||||||
|
@ -41,21 +68,73 @@ int main( int argc, char* argv[] )
|
||||||
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