SwitchGame/source/EngineFrame/Render/Texture.cpp

82 lines
2.1 KiB
C++
Raw Permalink Normal View History

2025-09-19 12:18:57 +08:00
#include "EngineFrame/Render/Texture.h"
2025-09-18 15:21:43 +08:00
#include "EngineCore/Asset_ImagePack.h"
#include "EngineCore/Game.h"
2025-09-19 12:18:57 +08:00
#include "Texture.h"
2025-09-18 15:21:43 +08:00
Texture::Texture()
{
}
2025-09-19 12:18:57 +08:00
Texture::Texture(std::string PngPath)
2025-09-18 15:21:43 +08:00
{
2025-09-19 12:18:57 +08:00
SDL_Surface *surface = IMG_Load(PngPath.c_str());
if (!surface)
{
SDL_Log("无法加载图片: %s", SDL_GetError());
return;
}
2025-09-18 15:21:43 +08:00
2025-09-19 12:18:57 +08:00
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);
2025-09-18 15:21:43 +08:00
}
2025-09-19 12:18:57 +08:00
Texture::Texture(std::string imgPath, int Index)
2025-09-18 15:21:43 +08:00
{
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;
2025-09-19 12:18:57 +08:00
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);
2025-09-18 15:21:43 +08:00
}
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;
}