Skip to content

Commit

Permalink
Limit framerate
Browse files Browse the repository at this point in the history
 - Limit the application to 60 FPS
  • Loading branch information
yalue committed Feb 18, 2022
1 parent 5006321 commit 8cd5397
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
25 changes: 23 additions & 2 deletions l_system_3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <glad/glad.h>
#include <GLFW/glfw3.h>
Expand All @@ -14,19 +15,21 @@

#define DEFAULT_WINDOW_WIDTH (800)
#define DEFAULT_WINDOW_HEIGHT (600)
#define DEFAULT_FPS (60.0)

ApplicationState* AllocateApplicationState(void) {
static ApplicationState* AllocateApplicationState(void) {
ApplicationState *to_return = NULL;
to_return = calloc(1, sizeof(*to_return));
if (!to_return) return NULL;
to_return->window_width = DEFAULT_WINDOW_WIDTH;
to_return->window_height = DEFAULT_WINDOW_HEIGHT;
to_return->aspect_ratio = ((float) to_return->window_width) /
((float) to_return->window_height);
to_return->frame_duration = 1.0 / DEFAULT_FPS;
return to_return;
}

void FreeApplicationState(ApplicationState *s) {
static void FreeApplicationState(ApplicationState *s) {
if (!s) return;
if (s->mesh) DestroyLSystemMesh(s->mesh);
if (s->turtle) DestroyTurtle3D(s->turtle);
Expand Down Expand Up @@ -281,12 +284,29 @@ static void UpdateCamera(ApplicationState *s) {
glm_vec4(position, 0, s->shared_uniforms.camera_position);
}

// Sleeps for s number of seconds.
static void SleepSeconds(double s) {
struct timespec t;
t.tv_sec = (uint64_t) s;
t.tv_nsec = (s - ((double) t.tv_sec)) * 1e9;
nanosleep(&t, NULL);
}

static int WaitNextFrame(ApplicationState *s) {
double elapsed = glfwGetTime() - s->frame_start;
if (elapsed < s->frame_duration) {
SleepSeconds(s->frame_duration - elapsed);
}
return 1;
}

static int RunMainLoop(ApplicationState *s) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glClearColor(0, 0, 0, 1.0);
while (!glfwWindowShouldClose(s->window)) {
s->frame_start = glfwGetTime();
if (!ProcessInputs(s)) {
printf("Error processing inputs.\n");
return 0;
Expand All @@ -304,6 +324,7 @@ static int RunMainLoop(ApplicationState *s) {
printf("Error drawing window.\n");
return 0;
}
if (!WaitNextFrame(s)) return 0;
}
return 1;
}
Expand Down
10 changes: 2 additions & 8 deletions l_system_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ typedef struct {
int window_width;
int window_height;
float aspect_ratio;
double frame_start;
double frame_duration;
LSystemMesh *mesh;
LSystemConfig *config;
Turtle3D *turtle;
Expand All @@ -31,11 +33,3 @@ typedef struct {
uint8_t *l_system_string;
} ApplicationState;

// Allocates an ApplicationState struct and initializes its values to 0.
// Returns NULL on error.
ApplicationState* AllocateApplicationState(void);

// Frees an ApplicationState struct. The given pointer is no longer valid after
// this function returns.
void FreeApplicationState(ApplicationState *s);

0 comments on commit 8cd5397

Please sign in to comment.