Removed width/height parameters from LoadTexture()

You can directly access the texture width and height now.
This commit is contained in:
Sam Lantinga 2025-03-14 10:38:11 -07:00
parent dcb97a5f49
commit efe122be4d
14 changed files with 49 additions and 70 deletions

View file

@ -56,8 +56,7 @@ static struct
static SDL_AudioStream *stream;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
static void quit(int rc)
{
SDL_free(sprites);
SDL_DestroyAudioStream(stream);
@ -80,8 +79,7 @@ static int fillerup(void)
return 0;
}
void
UserLoggedIn(XUserHandle user)
static void UserLoggedIn(XUserHandle user)
{
HRESULT hr;
char gamertag[128];
@ -96,8 +94,7 @@ UserLoggedIn(XUserHandle user)
XUserCloseHandle(user);
}
void
AddUserUICallback(XAsyncBlock *asyncBlock)
static void AddUserUICallback(XAsyncBlock *asyncBlock)
{
HRESULT hr;
XUserHandle user = NULL;
@ -123,8 +120,7 @@ AddUserUICallback(XAsyncBlock *asyncBlock)
delete asyncBlock;
}
void
AddUserUI()
static void AddUserUI()
{
HRESULT hr;
XAsyncBlock *asyncBlock = new XAsyncBlock;
@ -141,8 +137,7 @@ AddUserUI()
}
}
void
AddUserSilentCallback(XAsyncBlock *asyncBlock)
static void AddUserSilentCallback(XAsyncBlock *asyncBlock)
{
HRESULT hr;
XUserHandle user = NULL;
@ -168,8 +163,7 @@ AddUserSilentCallback(XAsyncBlock *asyncBlock)
delete asyncBlock;
}
void
AddUserSilent()
static void AddUserSilent()
{
HRESULT hr;
XAsyncBlock *asyncBlock = new XAsyncBlock;
@ -186,30 +180,27 @@ AddUserSilent()
}
}
int
LoadSprite(const char *file)
static bool LoadSprite(const char *file)
{
int i;
for (i = 0; i < state->num_windows; ++i) {
/* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */
sprites[i] = LoadTexture(state->renderers[i], file, true, &sprite_w, &sprite_h);
sprites[i] = LoadTexture(state->renderers[i], file, true);
if (!sprites[i]) {
return -1;
}
if (!SDL_SetTextureBlendMode(sprites[i], blendMode)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s", SDL_GetError());
SDL_DestroyTexture(sprites[i]);
return -1;
return false;
}
sprite_w = sprites[i]->w;
sprite_h = sprites[i]->h;
SDL_SetTextureBlendMode(sprites[i], blendMode);
}
/* We're ready to roll. :) */
return 0;
return true;
}
void
DrawSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
static void DrawSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
{
SDL_Rect viewport;
SDL_FRect temp;
@ -300,8 +291,7 @@ DrawSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
SDL_RenderPresent(renderer);
}
void
loop()
static void loop()
{
int i;
SDL_Event event;
@ -329,8 +319,7 @@ loop()
fillerup();
}
int
main(int argc, char *argv[])
int main(int argc, char *argv[])
{
int i;
const char *icon = "icon.bmp";
@ -413,7 +402,7 @@ main(int argc, char *argv[])
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
}
if (LoadSprite(icon) < 0) {
if (!LoadSprite(icon)) {
quit(2);
}

View file

@ -718,16 +718,15 @@ static Texture *CreateTexture(const char *fname)
if (!tex) {
SDL_Log("Out of memory!");
} else {
int texw, texh;
tex->texture = LoadTexture(state->renderers[0], fname, true, &texw, &texh);
tex->texture = LoadTexture(state->renderers[0], fname, true);
if (!tex->texture) {
SDL_Log("Failed to load '%s': %s", fname, SDL_GetError());
SDL_free(tex);
return NULL;
}
SDL_SetTextureBlendMode(tex->texture, SDL_BLENDMODE_BLEND);
tex->w = (float) texw;
tex->h = (float) texh;
tex->w = (float)tex->texture->w;
tex->h = (float)tex->texture->h;
}
return tex;
}

View file

@ -28,7 +28,6 @@ static bool use_texture = false;
static SDL_Texture **sprites;
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
static float angle = 0.0f;
static int sprite_w, sprite_h;
static int translate_cx = 0;
static int translate_cy = 0;
@ -52,7 +51,7 @@ static int LoadSprite(const char *file)
for (i = 0; i < state->num_windows; ++i) {
/* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */
sprites[i] = LoadTexture(state->renderers[i], file, true, &sprite_w, &sprite_h);
sprites[i] = LoadTexture(state->renderers[i], file, true);
if (!sprites[i]) {
return -1;
}

View file

@ -263,13 +263,13 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
return SDL_APP_FAILURE;
}
background = LoadTexture(renderer, "sample.bmp", false, NULL, NULL);
background = LoadTexture(renderer, "sample.bmp", false);
if (!background) {
SDL_Log("Couldn't create background: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
sprite = LoadTexture(renderer, "icon.bmp", true, NULL, NULL);
sprite = LoadTexture(renderer, "icon.bmp", true);
if (!sprite) {
SDL_Log("Couldn't create sprite: %s", SDL_GetError());
return SDL_APP_FAILURE;

View file

@ -55,7 +55,7 @@ static GlyphInfo glyphs[128];
static bool LoadFontTexture(void)
{
font_texture = LoadTexture(renderer, "msdf_font.bmp", false, NULL, NULL);
font_texture = LoadTexture(renderer, "msdf_font.bmp", false);
if (!font_texture) {
SDL_Log("Failed to create font texture: %s", SDL_GetError());
return false;

View file

@ -987,18 +987,19 @@ int main(int argc, char *argv[])
WindowState *ctx = &windowstate[i];
SDL_Window *window = state->windows[i];
SDL_Renderer *renderer = state->renderers[i];
int icon_w = 0, icon_h = 0;
SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX);
ctx->window = window;
ctx->renderer = renderer;
ctx->rendererID = i;
ctx->settings_icon = LoadTexture(renderer, "icon.bmp", true, &icon_w, &icon_h);
ctx->settings_rect.x = (float)WINDOW_WIDTH - icon_w - MARGIN;
ctx->settings_icon = LoadTexture(renderer, "icon.bmp", true);
if (ctx->settings_icon) {
ctx->settings_rect.w = (float)ctx->settings_icon->w;
ctx->settings_rect.h = (float)ctx->settings_icon->h;
ctx->settings_rect.x = (float)WINDOW_WIDTH - ctx->settings_rect.w - MARGIN;
ctx->settings_rect.y = MARGIN;
ctx->settings_rect.w = (float)icon_w;
ctx->settings_rect.h = (float)icon_h;
}
InitInput(ctx);

View file

@ -168,7 +168,7 @@ int main(int argc, char *argv[])
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
sprite = LoadTexture(renderer, "icon.bmp", true, NULL, NULL);
sprite = LoadTexture(renderer, "icon.bmp", true);
if (!sprite) {
quit(6);
}

View file

@ -135,8 +135,8 @@ int main(int argc, char *argv[])
drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i];
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", true, NULL, NULL);
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", false, NULL, NULL);
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", true);
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", false);
if (!drawstate->sprite || !drawstate->background) {
quit(2);
}

View file

@ -253,11 +253,11 @@ int main(int argc, char *argv[])
drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i];
if (test_composite) {
drawstate->sprite = LoadTexture(drawstate->renderer, "icon-alpha.bmp", true, NULL, NULL);
drawstate->sprite = LoadTexture(drawstate->renderer, "icon-alpha.bmp", true);
} else {
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", true, NULL, NULL);
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", true);
}
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", false, NULL, NULL);
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", false);
if (!drawstate->sprite || !drawstate->background) {
quit(2);
}

View file

@ -126,8 +126,8 @@ int main(int argc, char *argv[])
drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i];
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", true, NULL, NULL);
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", false, NULL, NULL);
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", true);
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", false);
if (!drawstate->sprite || !drawstate->background) {
quit(2);
}

View file

@ -53,16 +53,18 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result)
static int LoadSprite(const char *file)
{
int i, w, h;
int i;
for (i = 0; i < state->num_windows; ++i) {
/* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */
if (sprites[i]) {
SDL_DestroyTexture(sprites[i]);
}
sprites[i] = LoadTexture(state->renderers[i], file, true, &w, &h);
sprite_w = (float)w;
sprite_h = (float)h;
sprites[i] = LoadTexture(state->renderers[i], file, true);
if (sprites[i]) {
sprite_w = (float)sprites[i]->w;
sprite_h = (float)sprites[i]->h;
}
if (!sprites[i]) {
return -1;
}

View file

@ -72,7 +72,7 @@ char *GetResourceFilename(const char *user_specified, const char *def)
*
* If height_out is non-NULL, set it to the texture height.
*/
SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent, int *width_out, int *height_out)
SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent)
{
SDL_Surface *temp = NULL;
SDL_Texture *texture = NULL;
@ -117,14 +117,6 @@ SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transpar
}
}
if (width_out) {
*width_out = temp->w;
}
if (height_out) {
*height_out = temp->h;
}
texture = SDL_CreateTextureFromSurface(renderer, temp);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError());

View file

@ -16,8 +16,7 @@
#include <SDL3/SDL.h>
SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent,
int *width_out, int *height_out);
SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent);
char *GetNearbyFilename(const char *file);
char *GetResourceFilename(const char *user_specified, const char *def);

View file

@ -31,7 +31,6 @@ static bool use_target = false;
static Uint32 wait_start;
#endif
static SDL_Texture *sprite;
static int sprite_w, sprite_h;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
@ -184,8 +183,7 @@ int main(int argc, char *argv[])
quit(2);
}
sprite = LoadTexture(state->renderers[0], "icon.bmp", true, &sprite_w, &sprite_h);
sprite = LoadTexture(state->renderers[0], "icon.bmp", true);
if (!sprite) {
quit(2);
}