Added a utility function to calculate the next power of 2 for a value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
diff --git a/Makefile.os2 b/Makefile.os2
index 2f8d5ff..f68755f 100644
--- a/Makefile.os2
+++ b/Makefile.os2
@@ -67,7 +67,7 @@ CFLAGS_DLL+= -DSDL_BUILD_MAJOR_VERSION=$(MAJOR_VERSION)
CFLAGS_DLL+= -DSDL_BUILD_MINOR_VERSION=$(MINOR_VERSION)
CFLAGS_DLL+= -DSDL_BUILD_MICRO_VERSION=$(MICRO_VERSION)
-SRCS = SDL.c SDL_assert.c SDL_error.c SDL_guid.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
+SRCS = SDL.c SDL_assert.c SDL_error.c SDL_guid.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c SDL_utils.c
SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_memcpy.c SDL_memset.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
SRCS+= SDL_cpuinfo.c SDL_atomic.c SDL_spinlock.c SDL_thread.c SDL_timer.c
SRCS+= SDL_rwops.c SDL_power.c
diff --git a/Makefile.w32 b/Makefile.w32
index 978afab..feacca8 100644
--- a/Makefile.w32
+++ b/Makefile.w32
@@ -43,7 +43,7 @@ CFLAGS_DLL+= -DSDL_BUILD_MICRO_VERSION=$(MICRO_VERSION)
RCFLAGS = -q -r -bt=nt $(INCPATH)
-SRCS = SDL.c SDL_assert.c SDL_error.c SDL_guid.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
+SRCS = SDL.c SDL_assert.c SDL_error.c SDL_guid.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c SDL_utils.c
SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_memcpy.c SDL_memset.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
SRCS+= SDL_cpuinfo.c SDL_atomic.c SDL_spinlock.c SDL_thread.c SDL_timer.c
SRCS+= SDL_rwops.c SDL_power.c
diff --git a/VisualC-GDK/SDL/SDL.vcxproj b/VisualC-GDK/SDL/SDL.vcxproj
index c511fda..25478f3 100644
--- a/VisualC-GDK/SDL/SDL.vcxproj
+++ b/VisualC-GDK/SDL/SDL.vcxproj
@@ -689,6 +689,7 @@
<ClCompile Include="..\..\src\SDL_error.c" />
<ClCompile Include="..\..\src\SDL_hints.c" />
<ClCompile Include="..\..\src\SDL_log.c" />
+ <ClCompile Include="..\..\src\SDL_utils.c" />
<ClCompile Include="..\..\src\sensor\dummy\SDL_dummysensor.c" />
<ClCompile Include="..\..\src\sensor\SDL_sensor.c" />
<ClCompile Include="..\..\src\sensor\windows\SDL_windowssensor.c" />
@@ -756,4 +757,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/VisualC-GDK/SDL/SDL.vcxproj.filters b/VisualC-GDK/SDL/SDL.vcxproj.filters
index 59a3d72..dfbce65 100644
--- a/VisualC-GDK/SDL/SDL.vcxproj.filters
+++ b/VisualC-GDK/SDL/SDL.vcxproj.filters
@@ -852,6 +852,7 @@
<ClCompile Include="..\..\src\SDL_guid.c" />
<ClCompile Include="..\..\src\SDL_hints.c" />
<ClCompile Include="..\..\src\SDL_list.c" />
+ <ClCompile Include="..\..\src\SDL_utils.c" />
<ClCompile Include="..\..\src\audio\SDL_audio.c">
<Filter>audio</Filter>
</ClCompile>
@@ -1373,4 +1374,4 @@
<ItemGroup>
<ResourceCompile Include="..\..\src\main\windows\version.rc" />
</ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/VisualC-WinRT/SDL-UWP.vcxproj b/VisualC-WinRT/SDL-UWP.vcxproj
index aeaea7f..9ae6930 100644
--- a/VisualC-WinRT/SDL-UWP.vcxproj
+++ b/VisualC-WinRT/SDL-UWP.vcxproj
@@ -304,6 +304,7 @@
<ClCompile Include="..\src\SDL_guid.c" />
<ClCompile Include="..\src\SDL_hints.c" />
<ClCompile Include="..\src\SDL_log.c" />
+ <ClCompile Include="..\src\SDL_utils.c" />
<ClCompile Include="..\src\sensor\dummy\SDL_dummysensor.c" />
<ClCompile Include="..\src\sensor\SDL_sensor.c" />
<ClCompile Include="..\src\stdlib\SDL_crc32.c" />
diff --git a/VisualC-WinRT/SDL-UWP.vcxproj.filters b/VisualC-WinRT/SDL-UWP.vcxproj.filters
index 98b117d..879c07c 100644
--- a/VisualC-WinRT/SDL-UWP.vcxproj.filters
+++ b/VisualC-WinRT/SDL-UWP.vcxproj.filters
@@ -792,6 +792,9 @@
<ClCompile Include="..\src\SDL_list.c">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="..\src\SDL_utils.c">
+ <Filter>Source Files</Filter>
+ </ClCompile>
<ClCompile Include="..\src\haptic\windows\SDL_dinputhaptic.c">
<Filter>Source Files</Filter>
</ClCompile>
@@ -837,4 +840,4 @@
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj
index cb386bc..07e5042 100644
--- a/VisualC/SDL/SDL.vcxproj
+++ b/VisualC/SDL/SDL.vcxproj
@@ -557,6 +557,7 @@
<ClCompile Include="..\..\src\SDL_error.c" />
<ClCompile Include="..\..\src\SDL_hints.c" />
<ClCompile Include="..\..\src\SDL_log.c" />
+ <ClCompile Include="..\..\src\SDL_utils.c" />
<ClCompile Include="..\..\src\sensor\dummy\SDL_dummysensor.c" />
<ClCompile Include="..\..\src\sensor\SDL_sensor.c" />
<ClCompile Include="..\..\src\sensor\windows\SDL_windowssensor.c" />
@@ -624,4 +625,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters
index c6efccb..fc5318a 100644
--- a/VisualC/SDL/SDL.vcxproj.filters
+++ b/VisualC/SDL/SDL.vcxproj.filters
@@ -845,6 +845,7 @@
<ClCompile Include="..\..\src\SDL_guid.c" />
<ClCompile Include="..\..\src\SDL_hints.c" />
<ClCompile Include="..\..\src\SDL_list.c" />
+ <ClCompile Include="..\..\src\SDL_utils.c" />
<ClCompile Include="..\..\src\audio\SDL_audio.c">
<Filter>audio</Filter>
</ClCompile>
@@ -1348,4 +1349,4 @@
<ItemGroup>
<ResourceCompile Include="..\..\src\main\windows\version.rc" />
</ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/src/SDL_utils.c b/src/SDL_utils.c
new file mode 100644
index 0000000..e375d74
--- /dev/null
+++ b/src/SDL_utils.c
@@ -0,0 +1,50 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_internal.h"
+
+#include "SDL_utils_c.h"
+
+/* Common utility functions that aren't in the public API */
+
+int SDL_powerof2(int x)
+{
+ int value;
+
+ /* We could use this trick for 32-bit values:
+ * value = x;
+ * value -= 1;
+ * value |= value >> 1;
+ * value |= value >> 2;
+ * value |= value >> 4;
+ * value |= value >> 8;
+ * value |= value >> 16;
+ * value += 1;
+ *
+ * ... but this is more readable:
+ */
+ value = 1;
+ while (value < x) {
+ value <<= 1;
+ }
+ return value;
+}
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/SDL_utils_c.h b/src/SDL_utils_c.h
new file mode 100644
index 0000000..8f40515
--- /dev/null
+++ b/src/SDL_utils_c.h
@@ -0,0 +1,32 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef SDL_utils_h_
+#define SDL_utils_h_
+
+/* Common utility functions that aren't in the public API */
+
+/* Return the smallest power of 2 greater than or equal to 2 */
+int SDL_powerof2(int x);
+
+#endif /* SDL_utils_h_ */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index b9c38dc..9c47d40 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -27,6 +27,7 @@
#include "SDL_audio_c.h"
#include "SDL_sysaudio.h"
#include "../thread/SDL_systhread.h"
+#include "../SDL_utils_c.h"
#define _THIS SDL_AudioDevice *_this
@@ -1417,13 +1418,7 @@ open_audio_device(const char *devname, int iscapture,
* value we got from 'desired' and round up to the nearest value
*/
if (!current_audio.impl.SupportsNonPow2Samples && device->spec.samples > 0) {
- device->spec.samples -= 1;
- device->spec.samples |= device->spec.samples >> 1;
- device->spec.samples |= device->spec.samples >> 2;
- device->spec.samples |= device->spec.samples >> 4;
- device->spec.samples |= device->spec.samples >> 8;
- device->spec.samples |= device->spec.samples >> 16;
- device->spec.samples += 1;
+ device->spec.samples = SDL_powerof2(device->spec.samples);
}
if (current_audio.impl.OpenDevice(device, devname) < 0) {
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index 73a5694..202e17d 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -25,6 +25,7 @@
#include "SDL_opengl.h"
#include "../SDL_sysrender.h"
#include "SDL_shaders_gl.h"
+#include "../../SDL_utils_c.h"
#ifdef __MACOSX__
#include <OpenGL/OpenGL.h>
@@ -411,17 +412,6 @@ GL_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
return SDL_TRUE;
}
-SDL_FORCE_INLINE int
-power_of_2(int input)
-{
- int value = 1;
-
- while (value < input) {
- value <<= 1;
- }
- return value;
-}
-
SDL_FORCE_INLINE SDL_bool
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
GLint* internalFormat, GLenum* format, GLenum* type)
@@ -540,8 +530,8 @@ GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
data->texw = (GLfloat) texture_w;
data->texh = (GLfloat) texture_h;
} else {
- texture_w = power_of_2(texture->w);
- texture_h = power_of_2(texture->h);
+ texture_w = SDL_powerof2(texture->w);
+ texture_h = SDL_powerof2(texture->h);
data->texw = (GLfloat) (texture->w) / texture_w;
data->texh = (GLfloat) texture->h / texture_h;
}
diff --git a/src/render/opengles/SDL_render_gles.c b/src/render/opengles/SDL_render_gles.c
index 9afd8d1..5886449 100644
--- a/src/render/opengles/SDL_render_gles.c
+++ b/src/render/opengles/SDL_render_gles.c
@@ -25,6 +25,7 @@
#include "SDL_hints.h"
#include "SDL_opengles.h"
#include "../SDL_sysrender.h"
+#include "../../SDL_utils_c.h"
/* To prevent unnecessary window recreation,
* these should match the defaults selected in SDL_GL_ResetAttributes
@@ -303,17 +304,6 @@ GLES_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
return SDL_TRUE;
}
-static SDL_INLINE int
-power_of_2(int input)
-{
- int value = 1;
-
- while (value < input) {
- value <<= 1;
- }
- return value;
-}
-
static int
GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
@@ -374,8 +364,8 @@ GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
data->type = GL_TEXTURE_2D;
/* no NPOV textures allowed in OpenGL ES (yet) */
- texture_w = power_of_2(texture->w);
- texture_h = power_of_2(texture->h);
+ texture_w = SDL_powerof2(texture->w);
+ texture_h = SDL_powerof2(texture->h);
data->texw = (GLfloat) texture->w / texture_w;
data->texh = (GLfloat) texture->h / texture_h;