Skip to content
0.2.0-dev Download ZIP
106 lines ·3.7 KB Raw
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21// This is included in SDL_internal.h
22//#include "SDL_internal.h"
23
24#ifndef SDL_utils_h_
25#define SDL_utils_h_
26
27// Common utility functions that aren't in the public API
28
29// Return the smallest power of 2 greater than or equal to 'x'
30extern int SDL_powerof2(int x);
31
32extern Uint32 SDL_CalculateGCD(Uint32 a, Uint32 b);
33extern void SDL_CalculateFraction(float x, int *numerator, int *denominator);
34
35extern bool SDL_startswith(const char *string, const char *prefix);
36extern bool SDL_endswith(const char *string, const char *suffix);
37
38/** Convert URI to a local filename, stripping the "file://"
39 * preamble and hostname if present, and writes the result
40 * to the dst buffer. Since URI-encoded characters take
41 * three times the space of normal characters, src and dst
42 * can safely point to the same buffer for in situ conversion.
43 *
44 * Returns the number of decoded bytes that wound up in
45 * the destination buffer, excluding the terminating NULL byte.
46 *
47 * On error, -1 is returned.
48 */
49extern int SDL_URIToLocal(const char *src, char *dst);
50
51/// Determine if a URI is valid by validating the scheme.
52extern bool SDL_IsURI(const char *uri);
53
54typedef enum
55{
56 SDL_OBJECT_TYPE_UNKNOWN,
57 SDL_OBJECT_TYPE_WINDOW,
58 SDL_OBJECT_TYPE_RENDERER,
59 SDL_OBJECT_TYPE_TEXTURE,
60 SDL_OBJECT_TYPE_JOYSTICK,
61 SDL_OBJECT_TYPE_GAMEPAD,
62 SDL_OBJECT_TYPE_HAPTIC,
63 SDL_OBJECT_TYPE_SENSOR,
64 SDL_OBJECT_TYPE_HIDAPI_DEVICE,
65 SDL_OBJECT_TYPE_HIDAPI_JOYSTICK,
66 SDL_OBJECT_TYPE_THREAD,
67 SDL_OBJECT_TYPE_TRAY,
68
69} SDL_ObjectType;
70
71extern Uint32 SDL_GetNextObjectID(void);
72extern void SDL_SetObjectValid(void *object, SDL_ObjectType type, bool valid);
73extern bool SDL_FindObject(void *object, SDL_ObjectType type);
74extern int SDL_GetObjects(SDL_ObjectType type, void **objects, int count);
75extern void SDL_SetObjectsInvalid(void);
76
77extern bool SDL_object_validation;
78
79SDL_FORCE_INLINE bool SDL_ObjectValid(void *object, SDL_ObjectType type)
80{
81 if (!object) {
82 return false;
83 }
84
85 if (!SDL_object_validation) {
86 return true;
87 }
88
89 return SDL_FindObject(object, type);
90}
91
92extern const char *SDL_GetPersistentString(const char *string);
93
94extern char *SDL_CreateDeviceName(Uint16 vendor, Uint16 product, const char *vendor_name, const char *product_name, const char *default_name);
95
96// Log what backend a subsystem chose, if a hint was set to do so. Useful for debugging.
97extern void SDL_DebugLogBackend(const char *subsystem, const char *backend);
98
99#ifdef SDL_PLATFORM_EMSCRIPTEN
100// even though we reference the C runtime's free() in other places, it appears
101// to be inlined more aggressively in Emscripten 4, so we need a reference to
102// it here, too, so inlined Javascript doesn't fail to find it.
103extern void Emscripten_force_free(void *ptr);
104#endif
105
106#endif // SDL_utils_h_