playground-sdk/gfx/character.cpp

128 lines
6.1 KiB
C++

#include "gfx/character.h"
#include "core/zxml.h"
#include <SDL3_image/SDL_image.h>
#include <charconv>
#include <cstring>
namespace playground {
namespace {
bool fail(std::string* error,const char* message) {
if(error)*error=message;
return false;
}
bool attribute(std::string_view tag,std::string_view key,int& value) {
const std::string needle=" "+std::string(key)+"=\"";
const auto start=tag.find(needle);
if(start==std::string_view::npos)return false;
const auto first=start+needle.size(),last=tag.find('"',first);
if(last==std::string_view::npos)return false;
const auto result=std::from_chars(tag.data()+first,tag.data()+last,value);
return result.ec==std::errc{} && result.ptr==tag.data()+last;
}
SDL_Surface* image(const Vfs& assets,const std::string& path) {
Bytes bytes;
if(!assets.read(path,bytes) || bytes.empty() || bytes.size()>16*1024*1024)return nullptr;
SDL_IOStream* io=SDL_IOFromConstMem(bytes.data(),bytes.size());
return io?IMG_Load_IO(io,true):nullptr;
}
}
bool parseCharacterFrames(std::string_view xml,std::vector<CharacterFrame>& frames,
std::string* error) {
std::vector<CharacterFrame> parsed;
size_t pos=xml.find("<framelist>");
if(pos==std::string_view::npos)return fail(error,"character framelist missing");
const size_t end=xml.find("</framelist>",pos);
if(end==std::string_view::npos)return fail(error,"character framelist unclosed");
while((pos=xml.find("<frame ",pos))!=std::string_view::npos && pos<end) {
const size_t close=xml.find('>',pos);
if(close==std::string_view::npos || close>=end || parsed.size()>=32)
return fail(error,"invalid character frame tag");
const auto tag=xml.substr(pos,close-pos+1);
CharacterFrame frame;
if(!attribute(tag,"x",frame.x) || !attribute(tag,"y",frame.y) ||
!attribute(tag,"w",frame.w) || !attribute(tag,"h",frame.h) ||
!attribute(tag,"regx",frame.regx) || !attribute(tag,"regy",frame.regy) ||
frame.x<0 || frame.y<0 || frame.w<=0 || frame.h<=0 ||
frame.w>4096 || frame.h>4096 || frame.regx<0 || frame.regy<0 ||
frame.regx>4096 || frame.regy>4096)
return fail(error,"invalid character frame bounds");
parsed.push_back(frame);
pos=close+1;
}
if(parsed.empty())return fail(error,"character frames missing");
frames=std::move(parsed);
return true;
}
CharacterAtlas::~CharacterAtlas(){if(texture_)SDL_DestroyTexture(texture_);}
bool CharacterAtlas::load(SDL_Renderer* renderer,const Vfs& assets,std::string_view name,
std::string* error) {
if(!renderer || name.empty() || name.size()>96 ||
name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_")!=std::string_view::npos)
return fail(error,"invalid character name");
const std::string stem="characters/"+std::string(name);
Bytes xmlBytes;
if(!assets.read(stem+".xml",xmlBytes))return fail(error,"character XML missing");
Bytes plain;
if(isZxml(xmlBytes)) {
if(!zxmlDecode(xmlBytes,plain,error))return false;
} else plain=std::move(xmlBytes);
if(plain.size()>128*1024)return fail(error,"character XML too large");
std::vector<CharacterFrame> frames;
if(!parseCharacterFrames({reinterpret_cast<const char*>(plain.data()),plain.size()},frames,error))return false;
SDL_Surface* photo=image(assets,stem+".jpg");
SDL_Surface* mask=image(assets,stem+".mask.png");
if(!photo || !mask) {
if(photo)SDL_DestroySurface(photo);
if(mask)SDL_DestroySurface(mask);
return fail(error,"character image or mask missing");
}
SDL_Surface* color=SDL_ConvertSurface(photo,SDL_PIXELFORMAT_RGBA32);
SDL_Surface* matte=SDL_ConvertSurface(mask,SDL_PIXELFORMAT_RGBA32);
SDL_DestroySurface(photo);SDL_DestroySurface(mask);
if(!color || !matte || color->w!=matte->w || color->h!=matte->h) {
if(color)SDL_DestroySurface(color);
if(matte)SDL_DestroySurface(matte);
return fail(error,"character image and mask dimensions differ");
}
for(const auto& frame:frames)
if(frame.x>color->w-frame.w || frame.y>color->h-frame.h) {
SDL_DestroySurface(color);SDL_DestroySurface(matte);
return fail(error,"character frame outside atlas");
}
if(!SDL_LockSurface(color)) {SDL_DestroySurface(color);SDL_DestroySurface(matte);return fail(error,"character image lock failed");}
if(!SDL_LockSurface(matte)) {SDL_UnlockSurface(color);SDL_DestroySurface(color);SDL_DestroySurface(matte);return fail(error,"character mask lock failed");}
for(int y=0;y<color->h;++y) for(int x=0;x<color->w;++x) {
auto* pixel=static_cast<Uint8*>(color->pixels)+y*color->pitch+x*4;
const auto* coverage=static_cast<const Uint8*>(matte->pixels)+y*matte->pitch+x*4;
pixel[3]=coverage[0];
}
SDL_UnlockSurface(matte);SDL_UnlockSurface(color);
SDL_Texture* texture=SDL_CreateTextureFromSurface(renderer,color);
SDL_DestroySurface(color);SDL_DestroySurface(matte);
if(!texture)return fail(error,"character texture creation failed");
SDL_SetTextureBlendMode(texture,SDL_BLENDMODE_BLEND);
if(texture_)SDL_DestroyTexture(texture_);
texture_=texture;
frames_=std::move(frames);
return true;
}
bool CharacterAtlas::draw(SDL_Renderer* renderer,float x,float y,size_t expression,Uint8 alpha) const {
if(!renderer || !texture_ || frames_.empty() || expression>=frames_.size())return false;
SDL_SetTextureAlphaMod(texture_,alpha);
const auto& base=frames_[0];
SDL_FRect src{float(base.x),float(base.y),float(base.w),float(base.h)};
SDL_FRect dst{x,y,float(base.w),float(base.h)};
if(!SDL_RenderTexture(renderer,texture_,&src,&dst))return false;
if(expression==0)return true;
const auto& mood=frames_[expression];
src={float(mood.x),float(mood.y),float(mood.w),float(mood.h)};
dst={x+float(base.regx-mood.regx),y+float(base.regy-mood.regy),
float(mood.w),float(mood.h)};
return SDL_RenderTexture(renderer,texture_,&src,&dst);
}
} // namespace playground