Atlas - testutils.c

Home / ext / SDL / test Lines: 1 | Size: 4042 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 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 */ 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 .png 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) 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_LoadSurface(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 texture = SDL_CreateTextureFromSurface(renderer, temp); 121 if (!texture) { 122 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError()); 123 } 124 } 125 SDL_DestroySurface(temp); 126 SDL_free(path); 127 return texture; 128} 129
[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.