From 28baad8c5bbf69ef1392235653a1c11ee28b009e Mon Sep 17 00:00:00 2001 From: docentYT <63965954+docentYT@users.noreply.github.com> Date: Sat, 2 May 2026 00:35:50 +0200 Subject: [PATCH] Refactor clamp_power_of_two --- model/Texture.cpp | 2 +- model/Texture.h | 2 +- utilities/Globals.cpp | 8 ++++---- utilities/utilities.h | 18 +++++++++--------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/model/Texture.cpp b/model/Texture.cpp index 57b661c4..1adea69b 100644 --- a/model/Texture.cpp +++ b/model/Texture.cpp @@ -270,7 +270,7 @@ opengl_texture::load() { WriteLog( "Warning: dimensions of texture \"" + name + "\" aren't powers of 2", logtype::texture ); } } - if( ( quantize( data_width, 4 ) != data_width ) || ( quantize( data_height, 4 ) != data_height ) ) { + if( ( quantize( data_width, 4u ) != data_width ) || ( quantize( data_height, 4u ) != data_height ) ) { WriteLog( "Warning: dimensions of texture \"" + name + "\" aren't multiples of 4", logtype::texture ); } diff --git a/model/Texture.h b/model/Texture.h index b402d64c..cbcce4f6 100644 --- a/model/Texture.h +++ b/model/Texture.h @@ -119,7 +119,7 @@ public: bool is_texstub = false; // for make_from_memory internal_src: functionality std::vector data; // texture data (stored GL-style, bottom-left origin) resource_state data_state{ resource_state::none }; // current state of texture data - int data_width{ 0 }, + unsigned int data_width{ 0 }, data_height{ 0 }, data_mapcount{ 0 }; GLint data_format{ 0 }, diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index bd1f69d1..bdb5caf9 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -284,17 +284,17 @@ bool global_settings::ConfigParseGraphics(cParser& Parser, const std::string& to if (token == "maxtexturesize") { - int size = 0; + unsigned int size = 0; ParseOne(Parser, size, 1, false); - iMaxTextureSize = clamp_power_of_two(size, 64, 8192); + iMaxTextureSize = static_cast(clamp_power_of_two(size, 64u, 8192u)); return true; } if (token == "maxcabtexturesize") { - int size = 0; + unsigned int size = 0; ParseOne(Parser, size, 1, false); - iMaxCabTextureSize = clamp_power_of_two(size, 512, 8192); + iMaxCabTextureSize = static_cast(clamp_power_of_two(size, 512u, 8192u)); return true; } diff --git a/utilities/utilities.h b/utilities/utilities.h index d0646f96..15be1b01 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -269,17 +269,17 @@ template T clamp_circular(T Value, T const Range = T(360)) } // rounds down provided value to nearest power of two -template Type_ clamp_power_of_two(Type_ Value, Type_ const Min = static_cast(1), Type_ const Max = static_cast(16384)) +template T clamp_power_of_two(T Value, T const Min = T(1), T const Max = T(16384)) { + if (Value < Min) + return Min; - Type_ p2size{Min}; - Type_ size; - while ((p2size <= Max) && (p2size <= Value)) - { - size = p2size; - p2size = p2size << 1; - } - return size; + T p2 = std::bit_floor(Value); + + if (p2 > Max) + return Max; + + return p2; } template Type_ quantize(Type_ const Value, Type_ const Step)