2022-04-12 12:07:18 +00:00
|
|
|
/*
|
2024-01-01 21:15:26 +00:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2023-07-12 16:53:52 +00:00
|
|
|
Copyright 2022 Collabora Ltd.
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
2022-04-12 12:07:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testutils.h"
|
|
|
|
|
2023-02-01 23:21:53 +00:00
|
|
|
/**
|
2022-04-12 12:07:18 +00:00
|
|
|
* Return the absolute path to def in the SDL_GetBasePath() if possible, or
|
|
|
|
* the relative path to def on platforms that don't have a working
|
|
|
|
* SDL_GetBasePath(). Free the result with SDL_free.
|
|
|
|
*
|
|
|
|
* Fails and returns NULL if out of memory.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
GetNearbyFilename(const char *file)
|
|
|
|
{
|
2024-07-13 17:34:46 +00:00
|
|
|
const char *base = SDL_GetBasePath();
|
2022-04-12 12:07:18 +00:00
|
|
|
char *path;
|
|
|
|
|
2023-11-09 21:29:15 +00:00
|
|
|
if (base) {
|
2024-03-14 23:32:50 +00:00
|
|
|
SDL_IOStream *rw;
|
2022-05-19 21:54:23 +00:00
|
|
|
size_t len = SDL_strlen(base) + SDL_strlen(file) + 1;
|
2022-04-12 12:07:18 +00:00
|
|
|
|
|
|
|
path = SDL_malloc(len);
|
|
|
|
|
2023-11-09 21:29:15 +00:00
|
|
|
if (!path) {
|
2022-04-12 12:07:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)SDL_snprintf(path, len, "%s%s", base, file);
|
2022-05-18 16:59:12 +00:00
|
|
|
|
2024-03-14 23:32:50 +00:00
|
|
|
rw = SDL_IOFromFile(path, "rb");
|
2022-05-18 16:59:12 +00:00
|
|
|
if (rw) {
|
2024-03-14 23:32:50 +00:00
|
|
|
SDL_CloseIO(rw);
|
2022-05-18 16:59:12 +00:00
|
|
|
return path;
|
2022-05-18 15:23:26 +00:00
|
|
|
}
|
2022-05-18 16:59:12 +00:00
|
|
|
|
|
|
|
/* Couldn't find the file in the base path */
|
|
|
|
SDL_free(path);
|
2022-04-12 12:07:18 +00:00
|
|
|
}
|
|
|
|
|
error: SDL's allocators now call SDL_OutOfMemory on error.
This means the allocator's caller doesn't need to use SDL_OutOfMemory directly
if the allocation fails.
This applies to the usual allocators: SDL_malloc, SDL_calloc, SDL_realloc
(all of these regardless of if the app supplied a custom allocator or we're
using system malloc() or an internal copy of dlmalloc under the hood),
SDL_aligned_alloc, SDL_small_alloc, SDL_strdup, SDL_asprintf, SDL_wcsdup...
probably others. If it returns something you can pass to SDL_free, it should
work.
The caller might still need to use SDL_OutOfMemory if something that wasn't
SDL allocated the memory: operator new in C++ code, Objective-C's alloc
message, win32 GlobalAlloc, etc.
Fixes #8642.
2023-11-30 05:14:27 +00:00
|
|
|
return SDL_strdup(file);
|
2022-04-12 12:07:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:21:53 +00:00
|
|
|
/**
|
2022-04-12 12:07:18 +00:00
|
|
|
* If user_specified is non-NULL, return a copy of it. Free with SDL_free.
|
|
|
|
*
|
|
|
|
* Otherwise, return the absolute path to def in the SDL_GetBasePath() if
|
|
|
|
* possible, or the relative path to def on platforms that don't have a
|
|
|
|
* working SDL_GetBasePath(). Free the result with SDL_free.
|
|
|
|
*
|
|
|
|
* Fails and returns NULL if out of memory.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
GetResourceFilename(const char *user_specified, const char *def)
|
|
|
|
{
|
2023-11-09 21:29:15 +00:00
|
|
|
if (user_specified) {
|
error: SDL's allocators now call SDL_OutOfMemory on error.
This means the allocator's caller doesn't need to use SDL_OutOfMemory directly
if the allocation fails.
This applies to the usual allocators: SDL_malloc, SDL_calloc, SDL_realloc
(all of these regardless of if the app supplied a custom allocator or we're
using system malloc() or an internal copy of dlmalloc under the hood),
SDL_aligned_alloc, SDL_small_alloc, SDL_strdup, SDL_asprintf, SDL_wcsdup...
probably others. If it returns something you can pass to SDL_free, it should
work.
The caller might still need to use SDL_OutOfMemory if something that wasn't
SDL allocated the memory: operator new in C++ code, Objective-C's alloc
message, win32 GlobalAlloc, etc.
Fixes #8642.
2023-11-30 05:14:27 +00:00
|
|
|
return SDL_strdup(user_specified);
|
2022-04-12 12:07:18 +00:00
|
|
|
}
|
error: SDL's allocators now call SDL_OutOfMemory on error.
This means the allocator's caller doesn't need to use SDL_OutOfMemory directly
if the allocation fails.
This applies to the usual allocators: SDL_malloc, SDL_calloc, SDL_realloc
(all of these regardless of if the app supplied a custom allocator or we're
using system malloc() or an internal copy of dlmalloc under the hood),
SDL_aligned_alloc, SDL_small_alloc, SDL_strdup, SDL_asprintf, SDL_wcsdup...
probably others. If it returns something you can pass to SDL_free, it should
work.
The caller might still need to use SDL_OutOfMemory if something that wasn't
SDL allocated the memory: operator new in C++ code, Objective-C's alloc
message, win32 GlobalAlloc, etc.
Fixes #8642.
2023-11-30 05:14:27 +00:00
|
|
|
return GetNearbyFilename(def);
|
2022-04-12 12:07:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:21:53 +00:00
|
|
|
/**
|
2022-04-12 12:07:18 +00:00
|
|
|
* Load the .bmp file whose name is file, from the SDL_GetBasePath() if
|
|
|
|
* possible or the current working directory if not.
|
|
|
|
*
|
|
|
|
* If transparent is true, set the transparent colour from the top left pixel.
|
|
|
|
*
|
|
|
|
* If width_out is non-NULL, set it to the texture width.
|
|
|
|
*
|
|
|
|
* If height_out is non-NULL, set it to the texture height.
|
|
|
|
*/
|
|
|
|
SDL_Texture *
|
|
|
|
LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent,
|
|
|
|
int *width_out, int *height_out)
|
|
|
|
{
|
|
|
|
SDL_Surface *temp = NULL;
|
|
|
|
SDL_Texture *texture = NULL;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = GetNearbyFilename(file);
|
|
|
|
|
2023-11-09 21:29:15 +00:00
|
|
|
if (path) {
|
2022-04-12 12:07:18 +00:00
|
|
|
file = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = SDL_LoadBMP(file);
|
2023-11-09 21:29:15 +00:00
|
|
|
if (!temp) {
|
2022-04-12 12:07:18 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
|
|
|
|
} else {
|
|
|
|
/* Set transparent pixel as the pixel at (0,0) */
|
|
|
|
if (transparent) {
|
2024-07-08 21:59:18 +00:00
|
|
|
if (SDL_GetSurfacePalette(temp)) {
|
|
|
|
const Uint8 bpp = SDL_BITSPERPIXEL(temp->format);
|
2023-11-17 14:10:32 +00:00
|
|
|
const Uint8 mask = (1 << bpp) - 1;
|
2024-07-08 21:59:18 +00:00
|
|
|
if (SDL_PIXELORDER(temp->format) == SDL_BITMAPORDER_4321)
|
2023-11-17 14:10:32 +00:00
|
|
|
SDL_SetSurfaceColorKey(temp, SDL_TRUE, (*(Uint8 *)temp->pixels) & mask);
|
|
|
|
else
|
|
|
|
SDL_SetSurfaceColorKey(temp, SDL_TRUE, ((*(Uint8 *)temp->pixels) >> (8 - bpp)) & mask);
|
2022-04-12 12:07:18 +00:00
|
|
|
} else {
|
2024-07-08 21:59:18 +00:00
|
|
|
switch (SDL_BITSPERPIXEL(temp->format)) {
|
2022-04-12 12:07:18 +00:00
|
|
|
case 15:
|
2022-12-27 14:36:39 +00:00
|
|
|
SDL_SetSurfaceColorKey(temp, SDL_TRUE,
|
2022-11-30 20:51:59 +00:00
|
|
|
(*(Uint16 *)temp->pixels) & 0x00007FFF);
|
2022-04-12 12:07:18 +00:00
|
|
|
break;
|
|
|
|
case 16:
|
2022-12-27 14:36:39 +00:00
|
|
|
SDL_SetSurfaceColorKey(temp, SDL_TRUE, *(Uint16 *)temp->pixels);
|
2022-04-12 12:07:18 +00:00
|
|
|
break;
|
|
|
|
case 24:
|
2022-12-27 14:36:39 +00:00
|
|
|
SDL_SetSurfaceColorKey(temp, SDL_TRUE,
|
2022-11-30 20:51:59 +00:00
|
|
|
(*(Uint32 *)temp->pixels) & 0x00FFFFFF);
|
2022-04-12 12:07:18 +00:00
|
|
|
break;
|
|
|
|
case 32:
|
2022-12-27 14:36:39 +00:00
|
|
|
SDL_SetSurfaceColorKey(temp, SDL_TRUE, *(Uint32 *)temp->pixels);
|
2022-04-12 12:07:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 21:29:15 +00:00
|
|
|
if (width_out) {
|
2022-04-12 12:07:18 +00:00
|
|
|
*width_out = temp->w;
|
|
|
|
}
|
|
|
|
|
2023-11-09 21:29:15 +00:00
|
|
|
if (height_out) {
|
2022-04-12 12:07:18 +00:00
|
|
|
*height_out = temp->h;
|
|
|
|
}
|
|
|
|
|
|
|
|
texture = SDL_CreateTextureFromSurface(renderer, temp);
|
2023-11-09 21:29:15 +00:00
|
|
|
if (!texture) {
|
2022-04-12 12:07:18 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 14:36:39 +00:00
|
|
|
SDL_DestroySurface(temp);
|
2022-04-12 12:07:18 +00:00
|
|
|
if (path) {
|
|
|
|
SDL_free(path);
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|