Implementing a basic image caching system using SDL_Image can significantly improve performance, especially when you're frequently loading the same images. Here's a step-by-step guide to create an efficient image caching system:
First, let's define a structure to hold our cached images. We'll use an std::unordered_map
for fast lookups:
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <string>
#include <unordered_map>
struct ImageCache {
std::unordered_map<std::string, SDL_Surface*>
surfaces;
std::unordered_map<std::string, SDL_Texture*>
textures;
};
Now, let's create functions to load images into the cache and retrieve them:
SDL_Surface* getCachedSurface(
ImageCache& cache,
const std::string& filename) {
auto it = cache.surfaces.find(filename);
if (it != cache.surfaces.end()) {
return it->second;
}
SDL_Surface* surface = IMG_Load(
filename.c_str());
if (surface) {
cache.surfaces[filename] = surface;
} else {
std::cout << "Failed to load image: " <<
filename << '\n';
}
return surface;
}
SDL_Texture* getCachedTexture(
ImageCache& cache, SDL_Renderer* renderer,
const std::string& filename) {
auto it = cache.textures.find(filename);
if (it != cache.textures.end()) {
return it->second;
}
SDL_Surface* surface = getCachedSurface(
cache, filename);
if (!surface) { return nullptr; }
SDL_Texture* texture =
SDL_CreateTextureFromSurface(
renderer, surface);
if (texture) {
cache.textures[filename] = texture;
} else {
std::cout << "Failed to create texture: " <<
SDL_GetError() << '\n';
}
return texture;
}
To prevent memory leaks, we need a function to clean up our cache:
void clearImageCache(ImageCache& cache) {
for (auto& pair : cache.surfaces) {
SDL_FreeSurface(pair.second);
}
cache.surfaces.clear();
for (auto& pair : cache.textures) {
SDL_DestroyTexture(pair.second);
}
cache.textures.clear();
}
Now we can use our cache in our main program:
int main() {
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
SDL_Window* window =
SDL_CreateWindow("Image Cache Example",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640, 480, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(
window, -1, 0);
ImageCache cache;
// Load and render images
SDL_Texture* texture1 = getCachedTexture(
cache, renderer, "image1.png");
SDL_Texture* texture2 = getCachedTexture(
cache, renderer, "image2.png");
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture1, nullptr,
nullptr);
SDL_RenderPresent(renderer);
SDL_Delay(2000); // Display for 2 seconds
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture2, nullptr,
nullptr);
SDL_RenderPresent(renderer);
// Display for 2 seconds
SDL_Delay(2000);
// Cleanup
clearImageCache(cache);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;
}
Answers to questions are automatically generated and may not have been reviewed.
SDL_Image
Learn to load, manipulate, and save various image formats using SDL_Image
.