Use SDL_LoadTexture() in example and test code

This commit is contained in:
Cameron Cawley
2026-11-16 12:01:53 -08:00
committed by Sam Lantinga
parent 45b4d44e29
commit c6c538b120
21 changed files with 66 additions and 209 deletions
+1 -1
View File
@@ -187,7 +187,7 @@ 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. */
/* This does the SDL_LoadSurface step repeatedly, but that's OK for test code. */
sprites[i] = LoadTexture(state->renderers[i], file);
if (!sprites[i]) {
return false;
@@ -84,14 +84,11 @@ SDL_AppResult SDL_AppIterate(void *appstate)
}
if (i < SDL_arraysize(pngs)) { /* (just in case.) */
SDL_Surface *surface = SDL_LoadPNG_IO(SDL_IOFromConstMem(outcome.buffer, (size_t) outcome.bytes_transferred), true);
if (surface) { /* the renderer is not multithreaded, so create the texture here once the data loads. */
textures[i] = SDL_CreateTextureFromSurface(renderer, surface);
if (!textures[i]) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create texture!", SDL_GetError(), NULL);
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface);
/* the renderer is not multithreaded, so create the texture here once the data loads. */
textures[i] = SDL_LoadTexture_IO(renderer, SDL_IOFromConstMem(outcome.buffer, (size_t) outcome.bytes_transferred), true);
if (!textures[i]) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create texture!", SDL_GetError(), NULL);
return SDL_APP_FAILURE;
}
}
}
@@ -37,7 +37,6 @@ static SDL_Gamepad *gamepad = NULL;
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
char *png_path = NULL;
SDL_Surface *surface = NULL;
SDL_SetAppMetadata("Example Input Gamepad Polling", "1.0", "com.example.input-gamepad-polling");
@@ -59,25 +58,15 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%sgamepad_front.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
return SDL_APP_CONTINUE; /* carry on with the program! */
}
+4 -15
View File
@@ -22,7 +22,6 @@ static int texture_height = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Textures", "1.0", "com.example.renderer-textures");
@@ -42,27 +41,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a png file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load png: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture_width = surface->w;
texture_height = surface->h;
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
texture_width = texture->w;
texture_height = texture->h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
@@ -22,7 +22,6 @@ static int texture_height = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Rotating Textures", "1.0", "com.example.renderer-rotating-textures");
@@ -42,27 +41,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture_width = surface->w;
texture_height = surface->h;
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
texture_width = texture->w;
texture_height = texture->h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
@@ -22,7 +22,6 @@ static int texture_height = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Scaling Textures", "1.0", "com.example.renderer-scaling-textures");
@@ -42,27 +41,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture_width = surface->w;
texture_height = surface->h;
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
texture_width = texture->w;
texture_height = texture->h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
+4 -15
View File
@@ -22,7 +22,6 @@ static int texture_height = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Geometry", "1.0", "com.example.renderer-geometry");
@@ -42,27 +41,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture_width = surface->w;
texture_height = surface->h;
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
texture_width = texture->w;
texture_height = texture->h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
+4 -15
View File
@@ -22,7 +22,6 @@ static int texture_height = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Color Mods", "1.0", "com.example.renderer-color-mods");
@@ -42,27 +41,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture_width = surface->w;
texture_height = surface->h;
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
texture_width = texture->w;
texture_height = texture->h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
+4 -15
View File
@@ -22,7 +22,6 @@ static int texture_height = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Viewport", "1.0", "com.example.renderer-viewport");
@@ -42,27 +41,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture_width = surface->w;
texture_height = surface->h;
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
texture_width = texture->w;
texture_height = texture->h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
+2 -13
View File
@@ -29,7 +29,6 @@ static Uint64 last_time = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Clipping Rectangle", "1.0", "com.example.renderer-cliprect");
@@ -53,25 +52,15 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
return SDL_APP_CONTINUE; /* carry on with the program! */
}
+4 -15
View File
@@ -31,7 +31,6 @@ static int converted_texture_height = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Read Pixels", "1.0", "com.example.renderer-read-pixels");
@@ -51,27 +50,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture_width = surface->w;
texture_height = surface->h;
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
texture_width = texture->w;
texture_height = texture->h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
@@ -24,7 +24,6 @@ static int texture_height = 0;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_Surface *surface = NULL;
char *png_path = NULL;
SDL_SetAppMetadata("Example Renderer Affine Textures", "1.0", "com.example.renderer-affine-textures");
@@ -44,27 +43,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
times) with data from a bitmap file. */
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
Load a .png into a surface, move it to a texture from there. */
SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
surface = SDL_LoadPNG(png_path);
if (!surface) {
texture = SDL_LoadTexture(renderer, png_path);
if (!texture) {
SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_free(png_path); /* done with this, the file is loaded. */
texture_width = surface->w;
texture_height = surface->h;
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_Log("Couldn't create static texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
texture_width = texture->w;
texture_height = texture->h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
+1 -6
View File
@@ -420,14 +420,9 @@ struct GamepadImage
static SDL_Texture *CreateTexture(SDL_Renderer *renderer, unsigned char *data, unsigned int len)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface;
SDL_IOStream *src = SDL_IOFromConstMem(data, len);
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_DestroySurface(surface);
}
texture = SDL_LoadTexture_IO(renderer, src, true);
}
return texture;
}
+6 -10
View File
@@ -168,17 +168,13 @@ static float PrintClipboardImage(float x, float y, const char *mime_type)
bool rendered = false;
SDL_IOStream *stream = SDL_IOFromConstMem(data, size);
if (stream) {
SDL_Surface *surface = SDL_LoadSurface_IO(stream, false);
if (surface) {
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture) {
SDL_GetTextureSize(texture, &w, &h);
SDL_Texture *texture = SDL_LoadTexture_IO(renderer, stream, false);
if (texture) {
SDL_GetTextureSize(texture, &w, &h);
SDL_FRect dst = { x, y, w, h };
rendered = SDL_RenderTexture(renderer, texture, NULL, &dst);
SDL_DestroyTexture(texture);
}
SDL_DestroySurface(surface);
SDL_FRect dst = { x, y, w, h };
rendered = SDL_RenderTexture(renderer, texture, NULL, &dst);
SDL_DestroyTexture(texture);
}
SDL_CloseIO(stream);
}
+4 -7
View File
@@ -231,15 +231,12 @@ static bool CreateWindowAndRenderer(SDL_WindowFlags window_flags, const char *dr
static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface;
SDL_IOStream *src = SDL_IOFromConstMem(data, len);
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
texture = SDL_CreateTextureFromSurface(r, surface);
*w = surface->w;
*h = surface->h;
SDL_DestroySurface(surface);
texture = SDL_LoadTexture_IO(r, src, true);
if (texture) {
*w = texture->w;
*h = texture->h;
}
}
return texture;
+1 -1
View File
@@ -51,7 +51,7 @@ static int LoadSprite(const char *file)
int i;
for (i = 0; i < state->num_windows; ++i) {
/* This does the SDL_LoadPNG step repeatedly, but that's OK for test code. */
/* This does the SDL_LoadSurface step repeatedly, but that's OK for test code. */
sprites[i] = LoadTexture(state->renderers[i], file);
if (!sprites[i]) {
return -1;
+1 -6
View File
@@ -121,14 +121,9 @@ static void QuitSpriteOverlay(SpriteRenderState *rs)
static SDL_Texture *CreateSpriteTexture(SDL_Renderer *renderer, unsigned char *data, unsigned int len)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface;
SDL_IOStream *src = SDL_IOFromConstMem(data, len);
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_DestroySurface(surface);
}
texture = SDL_LoadTexture_IO(renderer, src, true);
}
return texture;
}
+1 -1
View File
@@ -56,7 +56,7 @@ static bool LoadSprite(const char *file)
int i;
for (i = 0; i < state->num_windows; ++i) {
/* This does the SDL_LoadPNG step repeatedly, but that's OK for test code. */
/* This does the SDL_LoadSurface step repeatedly, but that's OK for test code. */
if (sprites[i]) {
SDL_DestroyTexture(sprites[i]);
}
+4 -7
View File
@@ -36,15 +36,12 @@ static int done;
static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface;
SDL_IOStream *src = SDL_IOFromConstMem(data, len);
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
texture = SDL_CreateTextureFromSurface(r, surface);
*w = surface->w;
*h = surface->h;
SDL_DestroySurface(surface);
texture = SDL_LoadTexture_IO(r, src, true);
if (texture) {
*w = texture->w;
*h = texture->h;
}
}
return texture;
+2 -9
View File
@@ -129,7 +129,6 @@ char *GetResourceFilename(const char *user_specified, const char *def)
*/
SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file)
{
SDL_Surface *temp = NULL;
SDL_Texture *texture = NULL;
char *path;
@@ -139,16 +138,10 @@ SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file)
file = path;
}
temp = SDL_LoadSurface(file);
if (!temp) {
texture = SDL_LoadTexture(renderer, file);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
} else {
texture = SDL_CreateTextureFromSurface(renderer, temp);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError());
}
}
SDL_DestroySurface(temp);
SDL_free(path);
return texture;
}
+4 -7
View File
@@ -33,15 +33,12 @@ static int done;
static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface;
SDL_IOStream *src = SDL_IOFromConstMem(data, len);
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
texture = SDL_CreateTextureFromSurface(r, surface);
*w = surface->w;
*h = surface->h;
SDL_DestroySurface(surface);
texture = SDL_LoadTexture_IO(r, src, true);
if (texture) {
*w = texture->w;
*h = texture->h;
}
}
return texture;