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

View file

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

View file

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

View file

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

View file

@ -55,7 +55,7 @@ static GlyphInfo glyphs[128];
static bool LoadFontTexture(void) 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) { if (!font_texture) {
SDL_Log("Failed to create font texture: %s", SDL_GetError()); SDL_Log("Failed to create font texture: %s", SDL_GetError());
return false; return false;

View file

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

View file

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

View file

@ -135,8 +135,8 @@ int main(int argc, char *argv[])
drawstate->window = state->windows[i]; drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i]; drawstate->renderer = state->renderers[i];
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) { if (!drawstate->sprite || !drawstate->background) {
quit(2); quit(2);
} }

View file

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

View file

@ -126,8 +126,8 @@ int main(int argc, char *argv[])
drawstate->window = state->windows[i]; drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i]; drawstate->renderer = state->renderers[i];
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) { if (!drawstate->sprite || !drawstate->background) {
quit(2); quit(2);
} }

View file

@ -53,16 +53,18 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result)
static int LoadSprite(const char *file) static int LoadSprite(const char *file)
{ {
int i, w, h; int i;
for (i = 0; i < state->num_windows; ++i) { for (i = 0; i < state->num_windows; ++i) {
/* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */ /* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */
if (sprites[i]) { if (sprites[i]) {
SDL_DestroyTexture(sprites[i]); SDL_DestroyTexture(sprites[i]);
} }
sprites[i] = LoadTexture(state->renderers[i], file, true, &w, &h); sprites[i] = LoadTexture(state->renderers[i], file, true);
sprite_w = (float)w; if (sprites[i]) {
sprite_h = (float)h; sprite_w = (float)sprites[i]->w;
sprite_h = (float)sprites[i]->h;
}
if (!sprites[i]) { if (!sprites[i]) {
return -1; 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. * 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_Surface *temp = NULL;
SDL_Texture *texture = 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); texture = SDL_CreateTextureFromSurface(renderer, temp);
if (!texture) { if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError());

View file

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

View file

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