Various changes

- add release build
- control framerate
- fix bug that caused camera to shake
- enable double-buffering and VSync
This commit is contained in:
var
2026-04-26 22:15:58 -05:00
parent 58289aa6b2
commit 839a9dd5c2
5 changed files with 35 additions and 3 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
obj/*
Game
Release
*.xcf

View File

@@ -2,17 +2,19 @@ INC = include
OBJ = obj
SRC = src
EXE = Game
REL = Release
CC = gcc
CFLAGS = -w -ggdb -I$(INC)
LIBS = -lGLEW -lGLU -lGL -lSDL2 -lcglm -lz -lm
RELFLAGS = -O3 -s
OBJFILES := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(shell find $(SRC) -name '*.c'))
OBJDIRS := $(patsubst $(SRC)%, $(OBJ)%, $(shell find $(SRC) -type d))
CLEANDIRS := $(addsuffix /.clean, $(OBJDIRS));
# Explicit targets
.PHONY: all clean
.PHONY: all release clean
.DEFAULT_GOAL := all
# Build executable
@@ -28,8 +30,12 @@ $(OBJ)/%.o: $(SRC)/%.c | $(OBJDIRS)
$(OBJDIRS):
mkdir -p $@
release: $(REL)
$(REL): $(OBJFILES) | $(OBJDIRS)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS) $(RELFLAGS)
# Delete previously built files
clean: $(CLEANDIRS)
rm -f $(EXE)
rm -f $(EXE) $(REL)
%.clean:
rm -f $**.o

View File

@@ -18,6 +18,26 @@ GameState *Game_New()
void Game_Update(GameState *gs)
{
const int fps = 60;
uint64_t currentTicks = SDL_GetTicks64();
uint64_t targetTicks = gs->previousTicks + (1000 / fps);
int32_t waitTicks = targetTicks - currentTicks;
if (waitTicks > 0)
{
SDL_Delay(waitTicks);
currentTicks = SDL_GetTicks64();
}
else if (waitTicks <= -5)
{
printf("frame is late by %d ms\n", -waitTicks);
}
uint32_t delta = currentTicks - gs->previousTicks;
gs->previousTicks = currentTicks;
Camera_UpdateVectors(&gs->camera);
const float speed = 0.3f;
vec3 move, front, right;
glm_vec3_zero(move);
@@ -47,7 +67,6 @@ void Game_Update(GameState *gs)
glm_vec3_add(target->position, displacement, gs->camera.position);
Camera_UpdateVectors(&gs->camera);
SDL_Delay(5);
}
void Game_Destroy(GameState *gs)

View File

@@ -1,6 +1,7 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "SDL2/SDL.h"
#include "cglm/cglm.h"
#include "camera.h"
@@ -20,6 +21,7 @@ typedef struct
typedef struct
{
bool running;
uint64_t previousTicks;
InputState input;
SDL_Window *window;
SDL_GLContext *glContext;

View File

@@ -216,11 +216,15 @@ bool Render_Init(GameState *gs)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
gs->window = SDL_CreateWindow("Sandbox", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);;
gs->glContext = SDL_GL_CreateContext(gs->window);
printf("Created OpenGL window.\n");
if (SDL_GL_SetSwapInterval(1) == 0) printf("Enabled VSync.\n");
else printf("Failed to enable VSync. SDL Error: %s\n", SDL_GetError());
glewInit();
gs->shaderProgramId = LoadShaders("./res/glsl/vertex.glsl", "./res/glsl/fragment.glsl");
gs->modelShaderProgramId = LoadShaders("./res/glsl/model_vertex.glsl", "./res/glsl/fragment.glsl");