summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaditya Dhruv <[email protected]>2026-01-30 17:16:40 -0600
committerAaditya Dhruv <[email protected]>2026-01-30 17:16:40 -0600
commit4992ce0098cac8caef6c9315816b688d96259bce (patch)
tree862c389ec104838cbd11fc623747287b298558d0
parent0889db09c33aa96a31cc821effbf6dd42aa14471 (diff)
Add freetype for font rendering, display fps in stderr
-rw-r--r--CMakeLists.txt5
-rw-r--r--src/engine.c34
-rw-r--r--src/engine.h3
3 files changed, 37 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 126750d..1c953cd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,7 +8,8 @@ set(CMAKE_BUILD_TYPE Debug)
message("Compiler: ${CMAKE_CXX_COMPILER_ID}")
add_subdirectory(src)
-target_include_directories(junkcraft PRIVATE include)
find_package(SDL2)
+find_package(Freetype REQUIRED)
find_library(JUNK junk)
-target_link_libraries(junkcraft PRIVATE SDL2 ${JUNK} m pthread)
+target_include_directories(junkcraft PRIVATE include Freetype_INCLUDE_DIRS /usr/include/freetype2)
+target_link_libraries(junkcraft PRIVATE SDL2 ${JUNK} m pthread freetype)
diff --git a/src/engine.c b/src/engine.c
index 39f2511..ec1d9d3 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -6,7 +6,10 @@
#include "input.h"
#include "window.h"
#include "world.h"
+#include <SDL2/SDL_render.h>
+#include <bits/types/timer_t.h>
#include <junk/vector.h>
+#include <stdio.h>
#include <string.h>
#include <time.h>
@@ -73,7 +76,8 @@ void engine_update(struct engine* engine) {
int real_coord[2] = { i + CHUNK_DISTANCE, j + CHUNK_DISTANCE };
// engine->loaded_chunks[real_coord[0]][real_coord[1]] = chunk;
// Load chunk
- chunk_unload(chunk);
+ // TODO: Fix some VAO/VBO bug when negative y
+ // chunk_unload(chunk);
}
}
// Update the curr_chunk
@@ -82,15 +86,40 @@ void engine_update(struct engine* engine) {
}
}
+void engine_fps(struct engine* engine, float fps) {
+ // TTF_Font* font = TTF_OpenFont("fonts/PixelifySans-Regular.ttf", 8.0f);
+ // SDL_RenderClear(engine->window->renderer);
+ // SDL_Color black = {0, 0, 0};
+ // char fps_text[36];
+ // snprintf(fps_text, 36, "FPS: %.2f", fps);
+ // SDL_Surface* text = TTF_RenderText_Solid(font, fps_text, black);
+ // SDL_Texture* texture = SDL_CreateTextureFromSurface(engine->window->renderer, text);
+ // SDL_RenderCopy(engine->window->renderer, texture, NULL, &rect);
+}
+
void engine_start(struct engine* engine) {
+ float frames = 0;
+ time_t frame_last_time = time(NULL);
+ float fps = 0.0f;
while (engine->game_loop) {
+ time_t now = time(NULL);
+ time_t diff = now - frame_last_time;
+ // Calculate FPS every second
+ if (diff >= 1.0f) {
+ fps = frames / diff;
+ frames = 0;
+ frame_last_time = now;
+ fprintf(stderr, "FPS: %.2f\n", fps);
+ }
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.529f, 0.808f, 0.922f, 1.0f);
glEnable(GL_DEPTH_TEST);
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glUseProgram(engine->shader->program);
// Update engine managed objects
- engine_update(engine);//(1 + CHUNK_DISTANCE * 2) * (1 + CHUNK_DISTANCE * 2)
+ engine_fps(engine, fps);
+ engine_update(engine);
camera_update(engine->camera, engine->shader);
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
@@ -106,5 +135,6 @@ void engine_start(struct engine* engine) {
}
}
SDL_RenderPresent(engine->window->renderer);
+ frames += 1;
}
}
diff --git a/src/engine.h b/src/engine.h
index 89ee3cf..92689dd 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -1,5 +1,6 @@
#pragma once
#include "window.h"
+#include <ft2build.h>
#include "shader.h"
#include "junk/vector.h"
// CHUNK_DISTANCE is essentially render distance, it shows you how many chunks
@@ -8,7 +9,7 @@
// We want a square around curr_chunk, and a side of the square will be 1
// (center chunk) + 2 * CHUNK_DISTANCE (either side of center)
// loaded chunks = (1 + CHUNK_DISTANCE * 2)^2
-#define CHUNK_DISTANCE 5
+#define CHUNK_DISTANCE 2
struct engine {
struct window* window;