Atlas - testutils.c
Home / ext / SDL / test Lines: 1 | Size: 5882 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]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#ifdef SDL_PLATFORM_DOS 17static const struct 18{ 19 const char *longname; 20 const char *shortname; 21} names83_map[] = { 22 { "unifont-15.1.05.hex", "UNIFONT.HEX" }, 23 { "unifont-15.1.05-license.txt", "UNIFONTL.TXT" }, 24 { "physaudiodev.png", "PHYSADEV.PNG" }, 25 { "logaudiodev.png", "LOGADEV.PNG" }, 26 { "audiofile.png", "AUDIOFIL.PNG" }, 27 { "soundboard.png", "SNDBRD.PNG" }, 28 { "soundboard_levels.png", "SNDLVL.PNG" }, 29 { "trashcan.png", "TRASHCAN.PNG" }, 30 { "msdf_font.png", "MSDFFONT.PNG" }, 31 { "msdf_font.csv", "MSDFFONT.CSV" }, 32 { "gamepad_front.png", "GP_FRONT.PNG" }, 33 { "gamepad_back.png", "GP_BACK.PNG" }, 34 { "gamepad_face_abxy.png", "GP_FABXY.PNG" }, 35 { "gamepad_face_axby.png", "GP_FAXBY.PNG" }, 36 { "gamepad_face_bayx.png", "GP_FBAYX.PNG" }, 37 { "gamepad_face_sony.png", "GP_FSONY.PNG" }, 38 { "gamepad_battery.png", "GP_BATT.PNG" }, 39 { "gamepad_battery_unknown.png", "GP_BATTX.PNG" }, 40 { "gamepad_battery_wired.png", "GP_BATTW.PNG" }, 41 { "gamepad_touchpad.png", "GP_TOUCH.PNG" }, 42 { "gamepad_button.png", "GP_BTN.PNG" }, 43 { "gamepad_button_small.png", "GP_BTNSM.PNG" }, 44 { "gamepad_button_background.png", "GP_BTNBG.PNG" }, 45 { "gamepad_axis.png", "GP_AXIS.PNG" }, 46 { "gamepad_axis_arrow.png", "GP_AXARW.PNG" }, 47 { "gamepad_wired.png", "GP_WIRED.PNG" }, 48 { "gamepad_wireless.png", "GP_WLESS.PNG" }, 49 { "sdl-test_round.png", "SDLROUND.PNG" }, 50 { NULL, NULL } 51}; 52 53static const char *Map83Filename(const char *file) 54{ 55 int i; 56 for (i = 0; names83_map[i].longname; i++) { 57 if (SDL_strcasecmp(file, names83_map[i].longname) == 0) { 58 return names83_map[i].shortname; 59 } 60 } 61 return file; 62} 63#endif 64 65/** 66 * Return the absolute path to def in the SDL_GetBasePath() if possible, or 67 * the relative path to def on platforms that don't have a working 68 * SDL_GetBasePath(). Free the result with SDL_free. 69 * 70 * Fails and returns NULL if out of memory. 71 */ 72char *GetNearbyFilename(const char *file) 73{ 74 const char *base = SDL_GetBasePath(); 75#ifdef SDL_PLATFORM_DOS 76 file = Map83Filename(file); 77#endif 78 char *path; 79 80 if (base) { 81 SDL_IOStream *rw; 82 83 if (SDL_asprintf(&path, "%s%s", base, file) < 0) { 84 return NULL; 85 } 86 87 rw = SDL_IOFromFile(path, "rb"); 88 if (rw) { 89 SDL_CloseIO(rw); 90 return path; 91 } 92 93 /* Couldn't find the file in the base path */ 94 SDL_free(path); 95 } 96 97 return SDL_strdup(file); 98} 99 100/** 101 * If user_specified is non-NULL, return a copy of it. Free with SDL_free. 102 * 103 * Otherwise, return the absolute path to def in the SDL_GetBasePath() if 104 * possible, or the relative path to def on platforms that don't have a 105 * working SDL_GetBasePath(). Free the result with SDL_free. 106 * 107 * Fails and returns NULL if out of memory. 108 */ 109char *GetResourceFilename(const char *user_specified, const char *def) 110{ 111 if (user_specified) { 112 return SDL_strdup(user_specified); 113 } 114#ifdef SDL_PLATFORM_DOS 115 def = Map83Filename(def); 116#endif 117 return GetNearbyFilename(def); 118} 119 120/** 121 * Load the .png file whose name is file, from the SDL_GetBasePath() if 122 * possible or the current working directory if not. 123 * 124 * If transparent is true, set the transparent colour from the top left pixel. 125 * 126 * If width_out is non-NULL, set it to the texture width. 127 * 128 * If height_out is non-NULL, set it to the texture height. 129 */ 130SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent) 131{ 132 SDL_Surface *temp = NULL; 133 SDL_Texture *texture = NULL; 134 char *path; 135 136 path = GetNearbyFilename(file); 137 138 if (path) { 139 file = path; 140 } 141 142 temp = SDL_LoadSurface(file); 143 if (!temp) { 144 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError()); 145 } else { 146 /* Set transparent pixel as the pixel at (0,0) */ 147 if (transparent) { 148 if (SDL_GetSurfacePalette(temp)) { 149 const Uint8 bpp = SDL_BITSPERPIXEL(temp->format); 150 const Uint8 mask = (1 << bpp) - 1; 151 if (SDL_PIXELORDER(temp->format) == SDL_BITMAPORDER_4321) 152 SDL_SetSurfaceColorKey(temp, true, (*(Uint8 *)temp->pixels) & mask); 153 else 154 SDL_SetSurfaceColorKey(temp, true, ((*(Uint8 *)temp->pixels) >> (8 - bpp)) & mask); 155 } else { 156 switch (SDL_BITSPERPIXEL(temp->format)) { 157 case 15: 158 SDL_SetSurfaceColorKey(temp, true, 159 (*(Uint16 *)temp->pixels) & 0x00007FFF); 160 break; 161 case 16: 162 SDL_SetSurfaceColorKey(temp, true, *(Uint16 *)temp->pixels); 163 break; 164 case 24: 165 SDL_SetSurfaceColorKey(temp, true, 166 (*(Uint32 *)temp->pixels) & 0x00FFFFFF); 167 break; 168 case 32: 169 SDL_SetSurfaceColorKey(temp, true, *(Uint32 *)temp->pixels); 170 break; 171 } 172 } 173 } 174 175 texture = SDL_CreateTextureFromSurface(renderer, temp); 176 if (!texture) { 177 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError()); 178 } 179 } 180 SDL_DestroySurface(temp); 181 SDL_free(path); 182 return texture; 183} 184[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.