#include "EngineFrame/Render/Texture.h" #include "EngineCore/Asset_ImagePack.h" #include "EngineCore/Game.h" #include "Texture.h" Texture::Texture() { } Texture::Texture(std::string PngPath) { SDL_Surface *surface = IMG_Load(PngPath.c_str()); if (!surface) { SDL_Log("无法加载图片: %s", SDL_GetError()); return; } m_texture = SDL_CreateTextureFromSurface(Game::GetInstance().GetRenderer(), surface); if (!m_texture) { SDL_Log("无法创建纹理: %s", SDL_GetError()); SDL_FreeSurface(surface); // 记得释放surface return; } this->TextureSize.width = surface->w; this->TextureSize.height = surface->h; this->TextureFramepos.width = surface->w; this->TextureFramepos.height = surface->h; SDL_FreeSurface(surface); } Texture::Texture(std::string imgPath, int Index) { Asset_ImagePack::IMG *Info = Asset_ImagePack::GetInstance().GetIMG(imgPath); if (Info->lpImgName == "sprite/interface/base.img") return; Asset_ImagePack::ImgInfo &Buf = Info->lp_lplist[Index]; m_texture = SDL_CreateTexture( Game::GetInstance().GetRenderer(), SDL_PIXELFORMAT_ARGB8888, // 匹配RGBA数据格式 SDL_TEXTUREACCESS_STREAMING, Buf.Width, Buf.Height); if (!m_texture) { SDL_Log("纹理创建失败: %s", SDL_GetError()); } int pitch = Buf.Width * 4; SDL_UpdateTexture(m_texture, NULL, Buf.PNGdata, pitch); SDL_SetTextureBlendMode(m_texture, SDL_BLENDMODE_BLEND); this->TexturePos.x = Buf.Xpos; this->TexturePos.y = Buf.Ypos; this->TextureSize.width = Buf.Width; this->TextureSize.height = Buf.Height; this->TextureFramepos.width = Buf.FrameXpos; this->TextureFramepos.height = Buf.FrameYpos; } Texture::~Texture() { SDL_DestroyTexture(m_texture); } SDL_Texture *Texture::GetTexture() { return m_texture; } void Texture::SetBlendMode(SDL_BlendMode blendMode) { SDL_SetTextureBlendMode(m_texture, blendMode); } SDL_BlendMode Texture::GetBlendMode() { SDL_BlendMode blendMode; SDL_GetTextureBlendMode(m_texture, &blendMode); return blendMode; }