From eedea7446d0aae8459a16b5cda6485bd3eac6813 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 22 Jan 2024 19:55:43 +1100 Subject: [PATCH] Only load top parts of gui.png and icons.png into GPU textures --- src/EnvRenderer.c | 8 ++++---- src/Game.c | 7 ++++++- src/Game.h | 14 ++++++++------ src/Gui.c | 11 +++++++---- src/Model.c | 5 +++-- src/Particle.c | 2 +- src/Screens.c | 4 ++-- src/Widgets.c | 15 +++++++++------ 8 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/EnvRenderer.c b/src/EnvRenderer.c index eb8067925..6bd16cc33 100644 --- a/src/EnvRenderer.c +++ b/src/EnvRenderer.c @@ -783,22 +783,22 @@ static void UpdateMapEdges(void) { *---------------------------------------------------------General---------------------------------------------------------* *#########################################################################################################################*/ static void CloudsPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&clouds_tex, stream, name, NULL); + Game_UpdateTexture(&clouds_tex, stream, name, NULL, NULL); } static struct TextureEntry clouds_entry = { "clouds.png", CloudsPngProcess }; static void SkyboxPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&skybox_tex, stream, name, NULL); + Game_UpdateTexture(&skybox_tex, stream, name, NULL, NULL); } static struct TextureEntry skybox_entry = { "skybox.png", SkyboxPngProcess }; static void SnowPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&snow_tex, stream, name, NULL); + Game_UpdateTexture(&snow_tex, stream, name, NULL, NULL); } static struct TextureEntry snow_entry = { "snow.png", SnowPngProcess }; static void RainPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&rain_tex, stream, name, NULL); + Game_UpdateTexture(&rain_tex, stream, name, NULL, NULL); } static struct TextureEntry rain_entry = { "rain.png", RainPngProcess }; diff --git a/src/Game.c b/src/Game.c index a0b497937..a95b731b6 100644 --- a/src/Game.c +++ b/src/Game.c @@ -213,13 +213,18 @@ cc_bool Game_CanPick(BlockID block) { return Blocks.Collide[block] != COLLIDE_LIQUID || Game_BreakableLiquids; } -cc_bool Game_UpdateTexture(GfxResourceID* texId, struct Stream* src, const cc_string* file, cc_uint8* skinType) { +cc_bool Game_UpdateTexture(GfxResourceID* texId, struct Stream* src, const cc_string* file, + cc_uint8* skinType, int* heightDivisor) { struct Bitmap bmp; cc_bool success; cc_result res; res = Png_Decode(&bmp, src); if (res) { Logger_SysWarn2(res, "decoding", file); } + + /* E.g. gui.png, icons.png only need top half of the texture loaded */ + if (heightDivisor && bmp.height >= *heightDivisor) + bmp.height /= *heightDivisor; success = !res && Game_ValidateBitmap(file, &bmp); if (success) { diff --git a/src/Game.h b/src/Game.h index c11763ea3..db22d993a 100644 --- a/src/Game.h +++ b/src/Game.h @@ -92,18 +92,20 @@ CC_API void Game_UpdateBlock(int x, int y, int z, BlockID block); CC_API void Game_ChangeBlock(int x, int y, int z, BlockID block); cc_bool Game_CanPick(BlockID block); -cc_bool Game_UpdateTexture(GfxResourceID* texId, struct Stream* src, const cc_string* file, cc_uint8* skinType); +/* Updates Game_Width and Game_Height. */ +void Game_UpdateDimensions(void); +/* Sets the strategy/method used to limit frames per second. */ +/* See FPS_LIMIT_ for valid strategies/methods */ +void Game_SetFpsLimit(int method); + +cc_bool Game_UpdateTexture(GfxResourceID* texId, struct Stream* src, const cc_string* file, + cc_uint8* skinType, int* heightDivisor); /* Checks that the given bitmap can be loaded into a native gfx texture. */ /* (must be power of two size and be <= Gfx_MaxTexWidth/Gfx_MaxHeight) */ cc_bool Game_ValidateBitmap(const cc_string* file, struct Bitmap* bmp); /* Checks that the given bitmap is a power of two size */ /* NOTE: Game_ValidateBitmap should nearly always be used instead of this */ cc_bool Game_ValidateBitmapPow2(const cc_string* file, struct Bitmap* bmp); -/* Updates Game_Width and Game_Height. */ -void Game_UpdateDimensions(void); -/* Sets the strategy/method used to limit frames per second. */ -/* See FPS_LIMIT_ for valid strategies/methods */ -void Game_SetFpsLimit(int method); /* Runs the main game loop until the window is closed. */ void Game_Run(int width, int height, const cc_string* title); diff --git a/src/Gui.c b/src/Gui.c index 350e9f974..9844dbfcd 100644 --- a/src/Gui.c +++ b/src/Gui.c @@ -546,22 +546,25 @@ void Screen_PointerUp(void* s, int id, int x, int y) { } *------------------------------------------------------Gui component------------------------------------------------------* *#########################################################################################################################*/ static void GuiPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&Gui.GuiTex, stream, name, NULL); + int heightDivisor = 2; /* only top half of gui png is used */ + Game_UpdateTexture(&Gui.GuiTex, stream, name, NULL, &heightDivisor); } static struct TextureEntry gui_entry = { "gui.png", GuiPngProcess }; static void GuiClassicPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&Gui.GuiClassicTex, stream, name, NULL); + int heightDivisor = 2; /* only top half of gui png is used */ + Game_UpdateTexture(&Gui.GuiClassicTex, stream, name, NULL, &heightDivisor); } static struct TextureEntry guiClassic_entry = { "gui_classic.png", GuiClassicPngProcess }; static void IconsPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&Gui.IconsTex, stream, name, NULL); + int heightDivisor = 4; /* only top quarter of icons png is used */ + Game_UpdateTexture(&Gui.IconsTex, stream, name, NULL, &heightDivisor); } static struct TextureEntry icons_entry = { "icons.png", IconsPngProcess }; static void TouchPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&Gui.TouchTex, stream, name, NULL); + Game_UpdateTexture(&Gui.TouchTex, stream, name, NULL, NULL); } static struct TextureEntry touch_entry = { "touch.png", TouchPngProcess }; diff --git a/src/Model.c b/src/Model.c index fed0298ac..7b4ad8c72 100644 --- a/src/Model.c +++ b/src/Model.c @@ -512,10 +512,11 @@ void Model_RegisterTexture(struct ModelTex* tex) { static void Models_TextureChanged(void* obj, struct Stream* stream, const cc_string* name) { struct ModelTex* tex; - for (tex = textures_head; tex; tex = tex->next) { + for (tex = textures_head; tex; tex = tex->next) + { if (!String_CaselessEqualsConst(name, tex->name)) continue; - Game_UpdateTexture(&tex->texID, stream, name, &tex->skinType); + Game_UpdateTexture(&tex->texID, stream, name, &tex->skinType, NULL); return; } } diff --git a/src/Particle.c b/src/Particle.c index 45ecc2068..4c3e3b81c 100644 --- a/src/Particle.c +++ b/src/Particle.c @@ -574,7 +574,7 @@ void Particles_CustomEffect(int effectID, float x, float y, float z, float origi *---------------------------------------------------Particles component---------------------------------------------------* *#########################################################################################################################*/ static void ParticlesPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&particles_TexId, stream, name, NULL); + Game_UpdateTexture(&particles_TexId, stream, name, NULL, NULL); } static struct TextureEntry particles_entry = { "particles.png", ParticlesPngProcess }; diff --git a/src/Screens.c b/src/Screens.c index 6e9e8c35f..0bc607ddb 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -334,11 +334,11 @@ static void HUDScreen_Update(void* screen, double delta) { #define CH_EXTENT 16 static void HUDScreen_BuildCrosshairsMesh(struct VertexTextured** ptr) { - static struct Texture tex = { 0, Tex_Rect(0,0,0,0), Tex_UV(0.0f,0.0f, 15/256.0f,15/256.0f) }; + /* Only top quarter of icons.png is used */ + static struct Texture tex = { 0, Tex_Rect(0,0,0,0), Tex_UV(0.0f,0.0f, 15/256.0f,15/64.0f) }; int extent; extent = (int)(CH_EXTENT * Gui_Scale(Window_Main.Height / 480.0f)); - tex.ID = Gui.IconsTex; tex.x = (Window_Main.Width / 2) - extent; tex.y = (Window_Main.Height / 2) - extent; diff --git a/src/Widgets.c b/src/Widgets.c index 97285407c..64766450d 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -17,7 +17,6 @@ #include "Block.h" #include "Input.h" -#define Widget_UV(u1,v1, u2,v2) Tex_UV(u1/256.0f,v1/256.0f, u2/256.0f,v2/256.0f) static void Widget_NullFunc(void* widget) { } static int Widget_Pointer(void* elem, int id, int x, int y) { return false; } static void Widget_InputUp(void* elem, int key) { } @@ -98,10 +97,12 @@ void TextWidget_SetConst(struct TextWidget* w, const char* text, struct FontDesc *------------------------------------------------------ButtonWidget-------------------------------------------------------* *#########################################################################################################################*/ #define BUTTON_uWIDTH (200.0f / 256.0f) +/* Only top half of gui.png is used */ +#define Button_UV(u1,v1, u2,v2) Tex_UV(u1/256.0f,v1/128.0f, u2/256.0f,v2/128.0f) -static struct Texture btnShadowTex = { 0, Tex_Rect(0,0, 0,0), Widget_UV(0,66, 200,86) }; -static struct Texture btnSelectedTex = { 0, Tex_Rect(0,0, 0,0), Widget_UV(0,86, 200,106) }; -static struct Texture btnDisabledTex = { 0, Tex_Rect(0,0, 0,0), Widget_UV(0,46, 200,66) }; +static struct Texture btnShadowTex = { 0, Tex_Rect(0,0, 0,0), Button_UV(0,66, 200,86) }; +static struct Texture btnSelectedTex = { 0, Tex_Rect(0,0, 0,0), Button_UV(0,86, 200,106) }; +static struct Texture btnDisabledTex = { 0, Tex_Rect(0,0, 0,0), Button_UV(0,46, 200,66) }; static void ButtonWidget_Free(void* widget) { struct ButtonWidget* w = (struct ButtonWidget*)widget; @@ -507,11 +508,13 @@ static void HotbarWidget_Reposition(void* widget) { w->slotWidth = 20.0f * scaleX; Tex_SetRect(w->backTex, w->x,w->y, w->width,w->height); - Tex_SetUV(w->backTex, 0,0, 182/256.0f,22/256.0f); + /* Only top half of gui png is used */ + Tex_SetUV(w->backTex, 0,0, 182/256.0f,22/128.0f); y = w->y + (w->height - (int)(23.0f * scaleY)); Tex_SetRect(w->selTex, 0,y, (int)w->selWidth,w->height); - Tex_SetUV(w->selTex, 0,22/256.0f, 24/256.0f,44/256.0f); + /* Only top half of gui png is used */ + Tex_SetUV(w->selTex, 0,22/128.0f, 24/256.0f,44/128.0f); } static int HotbarWidget_MapKey(int key) {