2022-04-12 20:07:18 +08:00
|
|
|
/*
|
2024-01-02 05:15:26 +08:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2023-07-13 00:53:52 +08: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 20:07:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testutils.h"
|
|
|
|
|
2023-02-02 07:21:53 +08:00
|
|
|
/**
|
2022-04-12 20:07:18 +08: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.
|
|
|
|
*/
|
2024-07-27 23:41:06 +08:00
|
|
|
char *GetNearbyFilename(const char *file)
|
2022-04-12 20:07:18 +08:00
|
|
|
{
|
2024-07-14 01:34:46 +08:00
|
|
|
const char *base = SDL_GetBasePath();
|
2022-04-12 20:07:18 +08:00
|
|
|
char *path;
|
|
|
|
|
2023-11-10 05:29:15 +08:00
|
|
|
if (base) {
|
2024-03-15 07:32:50 +08:00
|
|
|
SDL_IOStream *rw;
|
2022-04-12 20:07:18 +08:00
|
|
|
|
2024-07-27 23:41:06 +08:00
|
|
|
if (SDL_asprintf(&path, "%s%s", base, file) < 0) {
|
2022-04-12 20:07:18 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-03-15 07:32:50 +08:00
|
|
|
rw = SDL_IOFromFile(path, "rb");
|
2022-05-19 00:59:12 +08:00
|
|
|
if (rw) {
|
2024-03-15 07:32:50 +08:00
|
|
|
SDL_CloseIO(rw);
|
2022-05-19 00:59:12 +08:00
|
|
|
return path;
|
2022-05-18 23:23:26 +08:00
|
|
|
}
|
2022-05-19 00:59:12 +08:00
|
|
|
|
|
|
|
/* Couldn't find the file in the base path */
|
|
|
|
SDL_free(path);
|
2022-04-12 20:07:18 +08: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 13:14:27 +08:00
|
|
|
return SDL_strdup(file);
|
2022-04-12 20:07:18 +08:00
|
|
|
}
|
|
|
|
|
2023-02-02 07:21:53 +08:00
|
|
|
/**
|
2022-04-12 20:07:18 +08: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.
|
|
|
|
*/
|
2024-07-27 23:41:06 +08:00
|
|
|
char *GetResourceFilename(const char *user_specified, const char *def)
|
2022-04-12 20:07:18 +08:00
|
|
|
{
|
2023-11-10 05:29:15 +08: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 13:14:27 +08:00
|
|
|
return SDL_strdup(user_specified);
|
2022-04-12 20:07:18 +08: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 13:14:27 +08:00
|
|
|
return GetNearbyFilename(def);
|
2022-04-12 20:07:18 +08:00
|
|
|
}
|
|
|
|
|
2023-02-02 07:21:53 +08:00
|
|
|
/**
|
2022-04-12 20:07:18 +08: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.
|
|
|
|
*/
|
2024-09-18 22:52:28 +08:00
|
|
|
SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent, int *width_out, int *height_out)
|
2022-04-12 20:07:18 +08:00
|
|
|
{
|
|
|
|
SDL_Surface *temp = NULL;
|
|
|
|
SDL_Texture *texture = NULL;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = GetNearbyFilename(file);
|
|
|
|
|
2023-11-10 05:29:15 +08:00
|
|
|
if (path) {
|
2022-04-12 20:07:18 +08:00
|
|
|
file = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = SDL_LoadBMP(file);
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!temp) {
|
2022-04-12 20:07:18 +08: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-09 05:59:18 +08:00
|
|
|
if (SDL_GetSurfacePalette(temp)) {
|
|
|
|
const Uint8 bpp = SDL_BITSPERPIXEL(temp->format);
|
2023-11-17 22:10:32 +08:00
|
|
|
const Uint8 mask = (1 << bpp) - 1;
|
2024-07-09 05:59:18 +08:00
|
|
|
if (SDL_PIXELORDER(temp->format) == SDL_BITMAPORDER_4321)
|
2024-09-18 22:52:28 +08:00
|
|
|
SDL_SetSurfaceColorKey(temp, true, (*(Uint8 *)temp->pixels) & mask);
|
2023-11-17 22:10:32 +08:00
|
|
|
else
|
2024-09-18 22:52:28 +08:00
|
|
|
SDL_SetSurfaceColorKey(temp, true, ((*(Uint8 *)temp->pixels) >> (8 - bpp)) & mask);
|
2022-04-12 20:07:18 +08:00
|
|
|
} else {
|
2024-07-09 05:59:18 +08:00
|
|
|
switch (SDL_BITSPERPIXEL(temp->format)) {
|
2022-04-12 20:07:18 +08:00
|
|
|
case 15:
|
2024-09-18 22:52:28 +08:00
|
|
|
SDL_SetSurfaceColorKey(temp, true,
|
2022-12-01 04:51:59 +08:00
|
|
|
(*(Uint16 *)temp->pixels) & 0x00007FFF);
|
2022-04-12 20:07:18 +08:00
|
|
|
break;
|
|
|
|
case 16:
|
2024-09-18 22:52:28 +08:00
|
|
|
SDL_SetSurfaceColorKey(temp, true, *(Uint16 *)temp->pixels);
|
2022-04-12 20:07:18 +08:00
|
|
|
break;
|
|
|
|
case 24:
|
2024-09-18 22:52:28 +08:00
|
|
|
SDL_SetSurfaceColorKey(temp, true,
|
2022-12-01 04:51:59 +08:00
|
|
|
(*(Uint32 *)temp->pixels) & 0x00FFFFFF);
|
2022-04-12 20:07:18 +08:00
|
|
|
break;
|
|
|
|
case 32:
|
2024-09-18 22:52:28 +08:00
|
|
|
SDL_SetSurfaceColorKey(temp, true, *(Uint32 *)temp->pixels);
|
2022-04-12 20:07:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 05:29:15 +08:00
|
|
|
if (width_out) {
|
2022-04-12 20:07:18 +08:00
|
|
|
*width_out = temp->w;
|
|
|
|
}
|
|
|
|
|
2023-11-10 05:29:15 +08:00
|
|
|
if (height_out) {
|
2022-04-12 20:07:18 +08:00
|
|
|
*height_out = temp->h;
|
|
|
|
}
|
|
|
|
|
|
|
|
texture = SDL_CreateTextureFromSurface(renderer, temp);
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!texture) {
|
2022-04-12 20:07:18 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 22:36:39 +08:00
|
|
|
SDL_DestroySurface(temp);
|
2022-04-12 20:07:18 +08:00
|
|
|
if (path) {
|
|
|
|
SDL_free(path);
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|