| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 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 |
*/ |
| 23 |
char * |
| 24 |
GetNearbyFilename(const char *file) |
| 25 |
{ |
| 26 |
char *base; |
| 27 |
char *path; |
| 28 |
|
| 29 |
base = SDL_GetBasePath(); |
| 30 |
|
| 31 |
if (base) { |
| 32 |
SDL_RWops *rw; |
| 33 |
size_t len = SDL_strlen(base) + SDL_strlen(file) + 1; |
| 34 |
|
| 35 |
path = SDL_malloc(len); |
| 36 |
|
| 37 |
if (!path) { |
| 38 |
SDL_free(base); |
| 39 |
SDL_OutOfMemory(); |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
(void)SDL_snprintf(path, len, "%s%s", base, file); |
| 44 |
SDL_free(base); |
| 45 |
|
| 46 |
rw = SDL_RWFromFile(path, "rb"); |
| 47 |
if (rw) { |
| 48 |
SDL_RWclose(rw); |
| 49 |
return path; |
| 50 |
} |
| 51 |
|
| 52 |
/* Couldn't find the file in the base path */ |
| 53 |
SDL_free(path); |
| 54 |
} |
| 55 |
|
| 56 |
path = SDL_strdup(file); |
| 57 |
if (!path) { |
| 58 |
SDL_OutOfMemory(); |
| 59 |
} |
| 60 |
return path; |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* If user_specified is non-NULL, return a copy of it. Free with SDL_free. |
| 65 |
* |
| 66 |
* Otherwise, return the absolute path to def in the SDL_GetBasePath() if |
| 67 |
* possible, or the relative path to def on platforms that don't have a |
| 68 |
* working SDL_GetBasePath(). Free the result with SDL_free. |
| 69 |
* |
| 70 |
* Fails and returns NULL if out of memory. |
| 71 |
*/ |
| 72 |
char * |
| 73 |
GetResourceFilename(const char *user_specified, const char *def) |
| 74 |
{ |
| 75 |
if (user_specified) { |
| 76 |
char *ret = SDL_strdup(user_specified); |
| 77 |
|
| 78 |
if (!ret) { |
| 79 |
SDL_OutOfMemory(); |
| 80 |
} |
| 81 |
|
| 82 |
return ret; |
| 83 |
} else { |
| 84 |
return GetNearbyFilename(def); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* Load the .bmp file whose name is file, from the SDL_GetBasePath() if |
| 90 |
* possible or the current working directory if not. |
| 91 |
* |
| 92 |
* If transparent is true, set the transparent colour from the top left pixel. |
| 93 |
* |
| 94 |
* If width_out is non-NULL, set it to the texture width. |
| 95 |
* |
| 96 |
* If height_out is non-NULL, set it to the texture height. |
| 97 |
*/ |
| 98 |
SDL_Texture * |
| 99 |
LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent, |
| 100 |
int *width_out, int *height_out) |
| 101 |
{ |
| 102 |
SDL_Surface *temp = NULL; |
| 103 |
SDL_Texture *texture = NULL; |
| 104 |
char *path; |
| 105 |
|
| 106 |
path = GetNearbyFilename(file); |
| 107 |
|
| 108 |
if (path) { |
| 109 |
file = path; |
| 110 |
} |
| 111 |
|
| 112 |
temp = SDL_LoadBMP(file); |
| 113 |
if (!temp) { |
| 114 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError()); |
| 115 |
} else { |
| 116 |
/* Set transparent pixel as the pixel at (0,0) */ |
| 117 |
if (transparent) { |
| 118 |
if (temp->format->palette) { |
| 119 |
SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels); |
| 120 |
} else { |
| 121 |
switch (temp->format->BitsPerPixel) { |
| 122 |
case 15: |
| 123 |
SDL_SetColorKey(temp, SDL_TRUE, |
| 124 |
(*(Uint16 *)temp->pixels) & 0x00007FFF); |
| 125 |
break; |
| 126 |
case 16: |
| 127 |
SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *)temp->pixels); |
| 128 |
break; |
| 129 |
case 24: |
| 130 |
SDL_SetColorKey(temp, SDL_TRUE, |
| 131 |
(*(Uint32 *)temp->pixels) & 0x00FFFFFF); |
| 132 |
break; |
| 133 |
case 32: |
| 134 |
SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *)temp->pixels); |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (width_out) { |
| 141 |
*width_out = temp->w; |
| 142 |
} |
| 143 |
|
| 144 |
if (height_out) { |
| 145 |
*height_out = temp->h; |
| 146 |
} |
| 147 |
|
| 148 |
texture = SDL_CreateTextureFromSurface(renderer, temp); |
| 149 |
if (!texture) { |
| 150 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError()); |
| 151 |
} |
| 152 |
} |
| 153 |
SDL_FreeSurface(temp); |
| 154 |
if (path) { |
| 155 |
SDL_free(path); |
| 156 |
} |
| 157 |
return texture; |
| 158 |
} |