DBGCFLAGS=-g
DBGLDFLAGS=
CFLAGS=-Wall -Wextra -Werror -pedantic $(DBGCFLAGS) -std=c89
-LIBS=-lSDL3
+LIBS=-lSDL3 -lSDL3_ttf
LDFLAGS=$(DBGLDFLAGS) $(LIBS)
INSTALL_DIR=/usr/local
state->frame_count = 0;
state->tick_count = 0;
+#ifdef LRTS_DEBUG
+ state->debug = LRTS_TRUE;
+#endif
}
int lrts_main(int argc, char **argv) {
#include "p_pc/u_assert.c"
#include "p_posix/p_init.c"
#include "p_posix/p_getopt.c"
+#include "p_pc/u_string.c"
#else
#error "No platform is provided"
#endif
--- /dev/null
+#include "../u_string.h"
+#include <string.h>
+
+int u_vsprintf(char *buffer, const char *fmt, va_list args) {
+ return vsprintf(buffer, fmt, args);
+}
+
+
+int u_sprintf(char *buffer, const char *fmt, ...) {
+ int res = 0;
+ va_list args;
+
+ va_start(args, fmt);
+ res = vsprintf(buffer, fmt, args);
+ va_end(args);
+
+ return res;
+}
*/
void p_delay(u64 time);
+/**
+ * Drwas debug text to the screen
+ */
+void p_draw_debug_text(u32 x, u32 y, const char *fmt, ...);
+
/**
* polls input events
#include "../p_draw.h"
#include "p_window.h"
#include "../u_assert.h"
-
+#include <stdarg.h>
+#include "p_r_sdl.h"
+#include "../u_string.h"
u64 p_get_ticks(void) {
return SDL_GetTicksNS();
}
void p_draw_begin(void) {
- SDL_LockSurface(r_target);
+ SDL_FillSurfaceRect(r_target, NULL, 0x000000);
}
void p_draw_end(void) {
- SDL_UnlockSurface(r_target);
}
void p_draw_present(void) {
SDL_UpdateWindowSurface(p_main_window);
}
+
+void p_draw_debug_text(u32 x, u32 y, const char *fmt, ...) {
+ char buffer[2048];
+ va_list args;
+ TTF_Text *t;
+
+ va_start(args, fmt);
+ vsprintf(buffer, fmt, args);
+ va_end(args);
+
+ /* if there is no font just print the info...
+ * this might spam a lot but at least we can still read the data
+ */
+ if (p_r_sdl_debug_font == LRTS_NULL) {
+ u_log(U_LOG_DEBUG, "%s\n", buffer);
+ return;
+ }
+
+ t = TTF_CreateText(p_r_sdl_text_engine, p_r_sdl_debug_font, buffer, strlen(buffer));
+ TTF_DrawSurfaceText(t, x, y, r_target);
+}
#include "../p_platform.h"
#include <SDL3/SDL.h>
+#include <SDL3_ttf/SDL_ttf.h>
#include "p_window.h"
#include "../u_defs.h"
#include "../u_log.h"
#include "p_window.c"
+TTF_TextEngine *p_r_sdl_text_engine;
+TTF_Font *p_r_sdl_debug_font;
+
#define DEFAULT_COLOR 0x222222FF
int p_render_init(void) {
exit(-1);
}
+ if (!TTF_Init()) {
+ u_log(U_LOG_ERR, "Failed to init ttf: %s\n", SDL_GetError());
+ exit(-1);
+ }
+
+ p_r_sdl_text_engine = TTF_CreateSurfaceTextEngine();
+ if (p_r_sdl_text_engine == LRTS_NULL) {
+ u_log(U_LOG_ERR, "Failed to init ttf text engine: %s\n", SDL_GetError());
+ exit(-1);
+ }
+
+ /* TODO: find a way to get a default font on any platform... */
+ p_r_sdl_debug_font = TTF_OpenFont("", 14);
+ if (p_r_sdl_debug_font == LRTS_NULL) {
+ u_log(U_LOG_WARN, "Failed to open debug font\n", SDL_GetError());
+ }
+
SDL_SetSurfaceColorKey(SDL_GetWindowSurface(p_main_window), LRTS_TRUE, R_COLOR_TRANSPARENT);
u_log(U_LOG_DEBUG, "Init successful\n");
int p_renderer_finish(void) {
SDL_DestroySurface(r_target);
SDL_DestroyWindow(p_main_window);
+ TTF_CloseFont(p_r_sdl_debug_font);
+ TTF_DestroySurfaceTextEngine(p_r_sdl_text_engine);
SDL_Quit();
u_log(U_LOG_DEBUG, "Exiting...\n");
return 0;
--- /dev/null
+#ifndef P_R_SDL_H__
+#define P_R_SDL_H__
+
+#include <SDL3_ttf/SDL_ttf.h>
+
+extern TTF_Font *p_r_sdl_debug_font;
+extern TTF_TextEngine *p_r_sdl_text_engine;
+
+#endif
*pixel = color;
}
-void r_render_frame() {
+void r_render_debug(void) {
+ struct lrts_state *state = lrts_state();
+
+ p_draw_debug_text(0, 0, "FPS: %d", state->fps);
+}
+
+void r_render_frame(void) {
struct lrts_state *state = lrts_state();
u_timer_start(&u_fps_timer);
p_draw_begin();
-
- /* t_map_draw(&t_map); */
+ t_map_draw(&t_map);
+ if (state->debug) {
+ r_render_debug();
+ }
p_draw_end();
+
+
p_draw_present();
u_timer_end(&u_fps_timer);
state->frame_count++;
- u_cap_fps();
+ state->fps = u_cap_fps();
}
struct lrts_state {
u32 frame_count;
u32 tick_count;
+ lrts_bool debug;
+ u32 fps;
};
#define LRTS_UNUSED(x) (void)(x)
+
+
#endif
--- /dev/null
+#ifndef U_STRING_H__
+#define U_STRING_H__
+
+#include <stdarg.h>
+
+int u_vsprintf(char *buffer, const char *fmt, va_list args);
+int u_sprintf(char *buffer, const char *fmt, ...);
+
+#endif