From bff491bbe9b756707e9a00716c50f7dcea58cce0 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Thu, 17 Oct 2024 09:15:37 +0900 Subject: [PATCH] gfx: Add frame rate counter It is exposed through StoatSpriteEngine functions, and is normally only visible when the game's debug mode is enabled. It's very useful for checking graphics performance. --- include/gfx/gfx.h | 1 + include/sact.h | 3 +++ src/hll/ChipmunkSpriteEngine.c | 4 +--- src/hll/StoatSpriteEngine.c | 24 ++++++++++++++++++------ src/video.c | 22 ++++++++++++++++++++++ 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/include/gfx/gfx.h b/include/gfx/gfx.h index 43781c1..5efd420 100644 --- a/include/gfx/gfx.h +++ b/include/gfx/gfx.h @@ -73,6 +73,7 @@ Texture *gfx_main_surface(void); void gfx_set_window_logical_size(int w, int h); void gfx_update_screen_scale(void); void gfx_set_wait_vsync(bool wait); +float gfx_get_frame_rate(void); void gfx_load_shader(struct shader *dst, const char *vertex_shader_path, const char *fragment_shader_path); GLuint gfx_load_shader_file(const char *path, GLenum type); diff --git a/include/sact.h b/include/sact.h index 11cde04..9756e80 100644 --- a/include/sact.h +++ b/include/sact.h @@ -114,6 +114,9 @@ void StoatSpriteEngine_SP_SetTextSpriteBoldWeight(float weight); void StoatSpriteEngine_SP_SetTextSpriteEdgeWeight(float weight); void StoatSpriteEngine_SP_SetTextSpriteEdgeColor(int r, int g, int b); bool StoatSpriteEngine_SP_SetDashTextSprite(int sp_no, int width, int height); +void StoatSpriteEngine_FPS_SetShow(bool show); +bool StoatSpriteEngine_FPS_GetShow(void); +float StoatSpriteEngine_FPS_Get(void); //int StoatSpriteEngine_SP_SetBrightness(int sp_no, int brightness); //int StoatSpriteEngine_SP_GetBrightness(int sp_no); int StoatSpriteEngine_SYSTEM_IsResetOnce(void); diff --git a/src/hll/ChipmunkSpriteEngine.c b/src/hll/ChipmunkSpriteEngine.c index 4e47860..b49535c 100644 --- a/src/hll/ChipmunkSpriteEngine.c +++ b/src/hll/ChipmunkSpriteEngine.c @@ -205,8 +205,6 @@ HLL_WARN_UNIMPLEMENTED(false, bool, ChipmunkSpriteEngine, SP_Suspend, int sp_no) //static void ChipmunkSpriteEngine_CombineTexture(void); //static void ChipmunkSpriteEngine_ReleaseCombinedTexture(void); -HLL_WARN_UNIMPLEMENTED( , void, StoatSpriteEngine, FPS_SetShow, bool bShow); -HLL_WARN_UNIMPLEMENTED(false, bool, StoatSpriteEngine, FPS_GetShow, void); HLL_LIBRARY(ChipmunkSpriteEngine, HLL_EXPORT(_PreLink, ChipmunkSpriteEngine_PreLink), @@ -277,7 +275,7 @@ HLL_LIBRARY(ChipmunkSpriteEngine, HLL_TODO_EXPORT(SP_GetAlphaClipperSpriteNumber, ChipmunkSpriteEngine_SP_GetAlphaClipperSpriteNumber), HLL_EXPORT(FPS_SetShow, StoatSpriteEngine_FPS_SetShow), HLL_EXPORT(FPS_GetShow, StoatSpriteEngine_FPS_GetShow), - HLL_TODO_EXPORT(FPS_Get, StoatSpriteEngine_FPS_Get), + HLL_EXPORT(FPS_Get, StoatSpriteEngine_FPS_Get), HLL_EXPORT(GAME_MSG_GetNumof, sact_GAME_MSG_GetNumOf), HLL_TODO_EXPORT(GAME_MSG_Get, SACT2_GAME_MSG_Get), HLL_EXPORT(IntToZenkaku, sact_IntToZenkaku), diff --git a/src/hll/StoatSpriteEngine.c b/src/hll/StoatSpriteEngine.c index 4478b06..6f8dec0 100644 --- a/src/hll/StoatSpriteEngine.c +++ b/src/hll/StoatSpriteEngine.c @@ -38,6 +38,8 @@ #include "AnteaterADVEngine.h" #include "CharSpriteManager.h" +static bool fps_show = false; + HLL_WARN_UNIMPLEMENTED( , void, StoatSpriteEngine, JoypadQuake_Set, int num, int type, int magnitude); HLL_WARN_UNIMPLEMENTED(0, int, StoatSpriteEngine, SP_SetBrightness, int sp_no, int brightness); HLL_WARN_UNIMPLEMENTED(0, int, StoatSpriteEngine, SP_GetBrightness, int sp_no); @@ -169,9 +171,19 @@ bool StoatSpriteEngine_SP_SetDashTextSprite(int sp_no, int width, int height) return true; } -//static void StoatSpriteEngine_FPS_SetShow(bool bShow); -//static bool StoatSpriteEngine_FPS_GetShow(void); -//static float StoatSpriteEngine_FPS_Get(void); +void StoatSpriteEngine_FPS_SetShow(bool show) +{ + fps_show = show; +} +bool StoatSpriteEngine_FPS_GetShow(void) +{ + return fps_show; +} + +float StoatSpriteEngine_FPS_Get(void) +{ + return gfx_get_frame_rate(); +} HLL_WARN_UNIMPLEMENTED( , void, StoatSpriteEngine, VIEW_SetOffsetPos, int x, int y); @@ -1003,9 +1015,9 @@ HLL_LIBRARY(StoatSpriteEngine, HLL_EXPORT(SP_SetTextSpriteEdgeWeight, StoatSpriteEngine_SP_SetTextSpriteEdgeWeight), HLL_EXPORT(SP_SetTextSpriteEdgeColor, StoatSpriteEngine_SP_SetTextSpriteEdgeColor), HLL_EXPORT(SP_SetDashTextSprite, StoatSpriteEngine_SP_SetDashTextSprite), - HLL_TODO_EXPORT(FPS_SetShow, StoatSpriteEngine_FPS_SetShow), - HLL_TODO_EXPORT(FPS_GetShow, StoatSpriteEngine_FPS_GetShow), - HLL_TODO_EXPORT(FPS_Get, StoatSpriteEngine_FPS_Get), + HLL_EXPORT(FPS_SetShow, StoatSpriteEngine_FPS_SetShow), + HLL_EXPORT(FPS_GetShow, StoatSpriteEngine_FPS_GetShow), + HLL_EXPORT(FPS_Get, StoatSpriteEngine_FPS_Get), HLL_EXPORT(GAME_MSG_GetNumof, sact_GAME_MSG_GetNumOf), HLL_TODO_EXPORT(GAME_MSG_Get, SACT2_GAME_MSG_Get), HLL_EXPORT(IntToZenkaku, sact_IntToZenkaku), diff --git a/src/video.c b/src/video.c index 2fe358d..76903f7 100644 --- a/src/video.c +++ b/src/video.c @@ -68,6 +68,7 @@ static struct texture main_surface; static struct texture *view = &main_surface; static GLint max_texture_size; static SDL_Color clear_color = { 0, 0, 0, 255 }; +static float frame_rate; static GLchar *read_shader_file(const char *path) { @@ -379,6 +380,25 @@ void gfx_set_view_offset(int x, int y) glm_translate(mw_transform, off); } +float gfx_get_frame_rate(void) +{ + return frame_rate; +} + +static void gfx_update_frame_rate_counter(void) +{ + static uint64_t timestamp; + static int frame_count; + + frame_count++; + uint64_t current_time = SDL_GetTicks64(); + if (current_time > timestamp + 1000) { + frame_rate = frame_count * 1000.f / (current_time - timestamp); + timestamp = current_time; + frame_count = 0; + } +} + void gfx_swap(void) { glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -403,6 +423,8 @@ void gfx_swap(void) SDL_GL_SwapWindow(sdl.window); glBindFramebuffer(GL_FRAMEBUFFER, main_surface_fb); glViewport(0, 0, sdl.w, sdl.h); + + gfx_update_frame_rate_counter(); } void gfx_set_view(struct texture *t)