#include #include namespace Game::Renderer { Texture::Texture(std::string id) : mTex(nullptr), mId(std::move(id)) {} Texture::Texture(const std::string& path, SDL_Renderer* renderer, std::string id) : mTex(nullptr), mId(id) { mPath = path; mIsFromFile = true; SDL_Surface* surf = IMG_Load(path.c_str()); if (!surf) { ERROR("Failed to load image at " << path); // Load Missing Texture surf = IMG_Load("../resources/missing_texture.png"); if (!surf) { ERROR("Failed to load missing texture image"); return; } } mTex = SDL_CreateTextureFromSurface(renderer, surf); SDL_DestroySurface(surf); // Apply a more pixelated scaling mode, since this is a pixel art game and the default linear filtering causes unwanted blurring when textures are scaled up if (mTex && !SDL_SetTextureScaleMode(mTex, SDL_SCALEMODE_NEAREST)) { WARN("Failed to set texture scale mode to NEAREST for '" << mId << "': " << SDL_GetError()); } } Texture::Texture(const Texture& other) { // Copy the references, since copying memory would require re-initing a bunch of things - for now this->mTex = other.mTex; } Texture& Texture::operator=(const Texture& other) { // Same reasoning this->mTex = other.mTex; return *this; } Texture::~Texture() { if (mTex) SDL_DestroyTexture(mTex); LOG("Tekstura '" << mId << "' uničena") } bool Texture::reload(SDL_Renderer* renderer) { if (!mIsFromFile || mPath.empty()) return false; if (mTex) { SDL_DestroyTexture(mTex); mTex = nullptr; } SDL_Surface* surf = IMG_Load(mPath.c_str()); if (!surf) { ERROR("Failed to reload image at " << mPath); return false; } mTex = SDL_CreateTextureFromSurface(renderer, surf); SDL_DestroySurface(surf); if (!mTex) { ERROR("Failed to create texture from surface when reloading " << mPath << ": " << SDL_GetError()); return false; } // Restore scale mode if (!SDL_SetTextureScaleMode(mTex, SDL_SCALEMODE_NEAREST)) { WARN("Failed to set texture scale mode to NEAREST for '" << mId << "' during reload: " << SDL_GetError()); } return true; } SDL_Texture* Texture::getSDLTexture() { return mTex; } std::string Texture::getId() { return mId; } float Texture::getWidth() { if (!mTex) return 0.f; float width; SDL_GetTextureSize(mTex, &width, nullptr); return width; } float Texture::getHeight() { if (!mTex) return 0.f; float height; SDL_GetTextureSize(mTex, nullptr, &height); return height; } }