Refactor TexturePool class for improved readability and maintainability; reorganize method definitions and formatting for consistency.
This commit is contained in:
@@ -2,64 +2,68 @@
|
|||||||
#define YAZE_APP_GFX_TEXTURE_POOL_H
|
#define YAZE_APP_GFX_TEXTURE_POOL_H
|
||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <memory>
|
|
||||||
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
|
||||||
#include "app/core/platform/sdl_deleter.h"
|
|
||||||
|
|
||||||
namespace yaze {
|
namespace yaze {
|
||||||
namespace gfx {
|
namespace gfx {
|
||||||
|
|
||||||
class TexturePool {
|
class TexturePool {
|
||||||
public:
|
public:
|
||||||
static TexturePool& GetInstance() {
|
static TexturePool& GetInstance() {
|
||||||
static TexturePool instance;
|
static TexturePool instance;
|
||||||
return instance;
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Texture* GetTexture(SDL_Renderer* renderer, int width, int height,
|
||||||
|
Uint32 format) {
|
||||||
|
std::string key = GenerateKey(width, height, format);
|
||||||
|
|
||||||
|
// Check if we have a suitable texture in the pool
|
||||||
|
auto it = available_textures_.find(key);
|
||||||
|
if (it != available_textures_.end() && !it->second.empty()) {
|
||||||
|
SDL_Texture* texture = it->second.back();
|
||||||
|
it->second.pop_back();
|
||||||
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Texture* GetTexture(SDL_Renderer* renderer, int width, int height, Uint32 format) {
|
// Create a new texture
|
||||||
std::string key = GenerateKey(width, height, format);
|
return SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING,
|
||||||
|
width, height);
|
||||||
// Check if we have a suitable texture in the pool
|
}
|
||||||
auto it = available_textures_.find(key);
|
|
||||||
if (it != available_textures_.end() && !it->second.empty()) {
|
void ReturnTexture(SDL_Texture* texture, int width, int height,
|
||||||
SDL_Texture* texture = it->second.back();
|
Uint32 format) {
|
||||||
it->second.pop_back();
|
if (!texture) return;
|
||||||
return texture;
|
|
||||||
}
|
std::string key = GenerateKey(width, height, format);
|
||||||
|
available_textures_[key].push_back(texture);
|
||||||
// Create a new texture
|
}
|
||||||
return SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, width, height);
|
|
||||||
|
void Clear() {
|
||||||
|
for (auto& pair : available_textures_) {
|
||||||
|
for (SDL_Texture* texture : pair.second) {
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
available_textures_.clear();
|
||||||
void ReturnTexture(SDL_Texture* texture, int width, int height, Uint32 format) {
|
}
|
||||||
if (!texture) return;
|
|
||||||
|
private:
|
||||||
std::string key = GenerateKey(width, height, format);
|
TexturePool() = default;
|
||||||
available_textures_[key].push_back(texture);
|
|
||||||
}
|
std::string GenerateKey(int width, int height, Uint32 format) {
|
||||||
|
return std::to_string(width) + "x" + std::to_string(height) + "_" +
|
||||||
void Clear() {
|
std::to_string(format);
|
||||||
for (auto& pair : available_textures_) {
|
}
|
||||||
for (SDL_Texture* texture : pair.second) {
|
|
||||||
SDL_DestroyTexture(texture);
|
std::unordered_map<std::string, std::vector<SDL_Texture*>>
|
||||||
}
|
available_textures_;
|
||||||
}
|
|
||||||
available_textures_.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
TexturePool() = default;
|
|
||||||
|
|
||||||
std::string GenerateKey(int width, int height, Uint32 format) {
|
|
||||||
return std::to_string(width) + "x" + std::to_string(height) + "_" + std::to_string(format);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unordered_map<std::string, std::vector<SDL_Texture*>> available_textures_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace gfx
|
} // namespace gfx
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
|
|
||||||
#endif // YAZE_APP_GFX_TEXTURE_POOL_H
|
#endif // YAZE_APP_GFX_TEXTURE_POOL_H
|
||||||
|
|||||||
Reference in New Issue
Block a user