summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testutils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testutils.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testutils.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testutils.c b/src/contrib/SDL-3.2.20/test/testutils.c
new file mode 100644
index 0000000..ab044bf
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testutils.c
@@ -0,0 +1,138 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3 Copyright 2022 Collabora Ltd.
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.
12*/
13
14#include "testutils.h"
15
16/**
17 * Return the absolute path to def in the SDL_GetBasePath() if possible, or
18 * the relative path to def on platforms that don't have a working
19 * SDL_GetBasePath(). Free the result with SDL_free.
20 *
21 * Fails and returns NULL if out of memory.
22 */
23char *GetNearbyFilename(const char *file)
24{
25 const char *base = SDL_GetBasePath();
26 char *path;
27
28 if (base) {
29 SDL_IOStream *rw;
30
31 if (SDL_asprintf(&path, "%s%s", base, file) < 0) {
32 return NULL;
33 }
34
35 rw = SDL_IOFromFile(path, "rb");
36 if (rw) {
37 SDL_CloseIO(rw);
38 return path;
39 }
40
41 /* Couldn't find the file in the base path */
42 SDL_free(path);
43 }
44
45 return SDL_strdup(file);
46}
47
48/**
49 * If user_specified is non-NULL, return a copy of it. Free with SDL_free.
50 *
51 * Otherwise, return the absolute path to def in the SDL_GetBasePath() if
52 * possible, or the relative path to def on platforms that don't have a
53 * working SDL_GetBasePath(). Free the result with SDL_free.
54 *
55 * Fails and returns NULL if out of memory.
56 */
57char *GetResourceFilename(const char *user_specified, const char *def)
58{
59 if (user_specified) {
60 return SDL_strdup(user_specified);
61 }
62 return GetNearbyFilename(def);
63}
64
65/**
66 * Load the .bmp file whose name is file, from the SDL_GetBasePath() if
67 * possible or the current working directory if not.
68 *
69 * If transparent is true, set the transparent colour from the top left pixel.
70 *
71 * If width_out is non-NULL, set it to the texture width.
72 *
73 * If height_out is non-NULL, set it to the texture height.
74 */
75SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent, int *width_out, int *height_out)
76{
77 SDL_Surface *temp = NULL;
78 SDL_Texture *texture = NULL;
79 char *path;
80
81 path = GetNearbyFilename(file);
82
83 if (path) {
84 file = path;
85 }
86
87 temp = SDL_LoadBMP(file);
88 if (!temp) {
89 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
90 } else {
91 /* Set transparent pixel as the pixel at (0,0) */
92 if (transparent) {
93 if (SDL_GetSurfacePalette(temp)) {
94 const Uint8 bpp = SDL_BITSPERPIXEL(temp->format);
95 const Uint8 mask = (1 << bpp) - 1;
96 if (SDL_PIXELORDER(temp->format) == SDL_BITMAPORDER_4321)
97 SDL_SetSurfaceColorKey(temp, true, (*(Uint8 *)temp->pixels) & mask);
98 else
99 SDL_SetSurfaceColorKey(temp, true, ((*(Uint8 *)temp->pixels) >> (8 - bpp)) & mask);
100 } else {
101 switch (SDL_BITSPERPIXEL(temp->format)) {
102 case 15:
103 SDL_SetSurfaceColorKey(temp, true,
104 (*(Uint16 *)temp->pixels) & 0x00007FFF);
105 break;
106 case 16:
107 SDL_SetSurfaceColorKey(temp, true, *(Uint16 *)temp->pixels);
108 break;
109 case 24:
110 SDL_SetSurfaceColorKey(temp, true,
111 (*(Uint32 *)temp->pixels) & 0x00FFFFFF);
112 break;
113 case 32:
114 SDL_SetSurfaceColorKey(temp, true, *(Uint32 *)temp->pixels);
115 break;
116 }
117 }
118 }
119
120 if (width_out) {
121 *width_out = temp->w;
122 }
123
124 if (height_out) {
125 *height_out = temp->h;
126 }
127
128 texture = SDL_CreateTextureFromSurface(renderer, temp);
129 if (!texture) {
130 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError());
131 }
132 }
133 SDL_DestroySurface(temp);
134 if (path) {
135 SDL_free(path);
136 }
137 return texture;
138}