建档
This commit is contained in:
83
source/EngineFrame/Render/Texture.cpp
Normal file
83
source/EngineFrame/Render/Texture.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "EngineFrame/Render/Texture.h"
|
||||
#include "Asset/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);
|
||||
SDL_SetTextureScaleMode(m_texture, SDL_ScaleModeBest);
|
||||
|
||||
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;
|
||||
}
|
||||
29
source/EngineFrame/Render/Texture.h
Normal file
29
source/EngineFrame/Render/Texture.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include <string>
|
||||
#include "Tool/RefPtr.h"
|
||||
#include "Tool/RefObject.h"
|
||||
|
||||
class Texture : public RefObject
|
||||
{
|
||||
private:
|
||||
SDL_Texture *m_texture = nullptr;
|
||||
|
||||
public:
|
||||
VecSize TextureSize = {0, 0}; // 纹理大小
|
||||
VecPos TexturePos = {0, 0}; // 纹理位置
|
||||
VecSize TextureFramepos = {0, 0}; // 帧域宽高
|
||||
|
||||
public:
|
||||
Texture(/* args */);
|
||||
Texture(std::string PngPath);
|
||||
Texture(std::string imgPath, int Index);
|
||||
~Texture();
|
||||
|
||||
public:
|
||||
void SetBlendMode(SDL_BlendMode blendMode);
|
||||
// 获取混合模式
|
||||
SDL_BlendMode GetBlendMode();
|
||||
SDL_Texture *GetTexture(); // 获取纹理
|
||||
};
|
||||
Reference in New Issue
Block a user