Yosin_Game/SpriteEx.cpp

188 lines
5.3 KiB
C++
Raw Normal View History

2024-05-04 18:30:32 +08:00
#include "SpriteEx.h"
#include "SquirrelClassEx.h"
//#include "kiwano/render/DirectX/D3DDeviceResources.h"
//#include "kiwano/render/DirectX/NativePtr.h"
//#include "d2d1effects_2.h"
std::unordered_map<int, TexturePtr>ImageRecShadowObject;
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dxguid.lib")
extern NPK_M* npk;
SpriteEx::SpriteEx(const std::string imgpath, const int frame)
{
SetFrame(SquirrelClassEx::GetTexturePtrByImg(imgpath, frame));
}
void SpriteEx::SetPosition(const Point& point)
{
TexturePtr t = GetTexture();
ImgInfo Img = *(ImgInfo*)t->GetUserData();
Actor::SetPosition(Point((point.x + Img.Xpos), (point.y + Img.Ypos) * GetScale().y));
}
void SpriteEx::SetScale(const Point& point)
{
//if (ShadowObject) {
// ShadowObject->GetTransform().skew = Vec2((-20.0) * (GetParent()->GetScale().x), 0.0);
//}
Sprite::SetScale(point);
}
void SpriteEx::OnRender(RenderContext& ctx)
{
//if (INTERPOLATION_MODE != D2D1_INTERPOLATION_MODE_FORCE_DWORD && COMPOSITE_MODE != D2D1_COMPOSITE_MODE_FORCE_DWORD)
//{
// auto bitmap = NativePtr::Get<ID2D1Bitmap>(GetFrame().GetTexture());
// auto d2d_res = graphics::directx::GetD2DDeviceResources();
// d2d_res->GetDeviceContext()->DrawImage(
// bitmap.Get(),
// INTERPOLATION_MODE,
// COMPOSITE_MODE
// );
// //auto bitmap = NativePtr::Get<ID2D1Bitmap>(GetFrame().GetTexture());
// //auto m_d2dContext = graphics::directx::GetD2DDeviceResources()->GetDeviceContext();
// //ComPtr<ID2D1Effect> chromakeyEffect;
// //m_d2dContext->CreateEffect(CLSID_D2D1Emboss, &chromakeyEffect);
// //
// //chromakeyEffect->SetInput(0, bitmap.Get());
// ////chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_COLOR, Color{ 1.0f, 1.0f, 1.0f, 1.0f });
// ////chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_TOLERANCE, 0.2f);
// ////chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_INVERT_ALPHA, true);
// //chromakeyEffect->SetValue(D2D1_EMBOSS_PROP_HEIGHT, 1.0f);
// //chromakeyEffect->SetValue(D2D1_EMBOSS_PROP_DIRECTION, 0.0f);
// //m_d2dContext->DrawImage(chromakeyEffect.Get());
//}
//else {
// this->Sprite::OnRender(ctx);
//}
switch (MyModel)
{
case -1: //-1ԭʼģʽ
ctx.SetBlendMode(BlendMode::SourceOver);
break;
case 0: //0<><30><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>
ctx.SetBlendMode(BlendMode::Add);
break;
default:
ctx.SetBlendMode(BlendMode::SourceOver);
break;
}
this->Sprite::OnRender(ctx);
}
//void SpriteEx::OnUpdate(Duration dt) {
// //<2F><><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>״̬
// if (is_Animotion_Play) {
// //<2F><>ǰ֡С<D6A1><D0A1><EFBFBD><EFBFBD>֡ʱִ<CAB1><D6B4><EFBFBD>߼<EFBFBD>
// if (NowFrameIndex < MaxFrameCount) {
// if (OffsetTime == 0)OffsetTime = clock();
// if ((clock() - OffsetTime) >= MyDelay[NowFrameIndex]) {
// OffsetTime = clock();
// NowFrameIndex++;
// SquirrelClassEx::RunUpdateScript("AnimotionUpdateCallBack", this->GetObjectID(), NowFrameIndex);
// }
// }
// //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> <20><><EFBFBD>õ<EFBFBD>ǰ֡
// else {
// OffsetTime = 0;
// NowFrameIndex = 0;
// MyLoop--;
// if (MyLoop != 0) {
// SquirrelClassEx::RunUpdateScript("AnimotionUpdateCallBack", this->GetObjectID(), NowFrameIndex);
// }
// else {
// is_Animotion_Play = false;
// }
// }
// }
//}
void SpriteEx::SetFrameInfo(std::vector<int> FrameDaley , const int Loop)
{
MyDelay = FrameDaley;
MaxFrameCount = FrameDaley.size();
MyLoop = Loop;
}
void SpriteEx::SetAnimotionPlay(bool Type)
{
is_Animotion_Play = Type;
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD> <20><>һ<EFBFBD><D2BB><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
SquirrelClassEx::RunUpdateScript("AnimotionUpdateCallBack", this->GetObjectID(), NowFrameIndex);
}
void SpriteEx::SetAnimotionFrame(const int gFrame)
{
this->NowFrameIndex = gFrame;
}
void SpriteEx::SetShadow()
{
TexturePtr t;
ImgInfo Img = *(ImgInfo*)GetTexture()->GetUserData();
if (ImageRecShadowObject.count(GetTexture()->GetObjectID())) {
t = ImageRecShadowObject[GetTexture()->GetObjectID()];
}
else {
BYTE* Data = Img.PNGdata;
BYTE* NewData = new BYTE[Img.Height * Img.Width * 4];
for (size_t i = 0; i < Img.Height * Img.Width * 4; i++)
{
if ((i + 1) % 4 == 0)
NewData[i] = Data[i];
else
NewData[i] = 0;
}
BinaryData data = { ((void*)NewData) ,(uint32_t)(Img.Height * Img.Width * 4) };
t = new Texture();
t->Load(PixelSize(Img.Width, Img.Height), data, PixelFormat::Bpp32BGRA);
t->SetUserData(&Img);
ImageRecShadowObject[GetTexture()->GetObjectID()] = t;
}
if (ShadowObject) {
ShadowObject->SetFrame(t);
}
else {
ShadowObject = new SpriteEx(t);
ShadowObject->SetAnchor(1, 1);
ShadowObject->SetOpacity(0.4);
ShadowObject->SetZOrder(-1);
AddChild(ShadowObject);
//GetParent()->GetParent()->AddChild(ShadowObject);
}
Transform B;
B.scale = Vec2(1.0, 0.29);
B.position = Point(GetWidth(), GetHeight() - 5);
B.skew = Vec2((-20.0) * (GetParent()->GetScale().x), 0.0);
ShadowObject->SetTransform(B);
}