Skip to content

Commit e46645b

Browse files
璀境石璀境石
authored andcommitted
修复了一些渲染失败不报错的bug
1 parent 67af4e1 commit e46645b

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

LuaSTG/LuaSTG/LuaBinding/LW_Renderer.cpp

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
inline Core::Graphics::IRenderer* LR2D() { return LAPP.GetAppModel()->getRenderer(); }
77
inline LuaSTGPlus::ResourceMgr& LRESMGR() { return LAPP.GetResourceMgr(); }
88

9-
#ifdef _DEBUG
9+
#ifndef NDEBUG
1010
#define check_rendertarget_usage(PTEXTURE) assert(!LuaSTGPlus::AppFrame::GetInstance().GetRenderTargetManager()->CheckRenderTargetInUse(PTEXTURE.get()));
1111
#else
1212
#define check_rendertarget_usage(PTEXTURE)
13-
#endif // _DEBUG
13+
#endif
1414

1515
#define validate_render_scope() if (!LR2D()->isBatchScope()) return luaL_error(L, "invalid render operation");
1616

17+
enum class RenderError
18+
{
19+
None,
20+
SpriteNotFound,
21+
SpriteSequenceNotFound,
22+
};
23+
1724
inline void rotate_float2(float& x, float& y, const float r)
1825
{
1926
float const sinv = std::sinf(r);
@@ -100,43 +107,45 @@ static Core::Graphics::IRenderer::BlendState translate_blend_3d(const LuaSTGPlus
100107
}
101108
}
102109

103-
static void api_drawSprite(LuaSTGPlus::IResourceSprite* pimg2dres, float const x, float const y, float const rot, float const hscale, float const vscale, float const z)
110+
static RenderError api_drawSprite(LuaSTGPlus::IResourceSprite* pimg2dres, float const x, float const y, float const rot, float const hscale, float const vscale, float const z)
104111
{
105112
Core::Graphics::ISprite* p_sprite = pimg2dres->GetSprite();
106113
auto* ctx = LR2D();
107114
translate_blend(ctx, pimg2dres->GetBlendMode());
108115
p_sprite->setZ(z);
109116
p_sprite->draw(Core::Vector2F(x, y), Core::Vector2F(hscale, vscale), rot);
117+
return RenderError::None;
110118
}
111-
static void api_drawSprite(char const* name, float const x, float const y, float const rot, float const hscale, float const vscale, float const z)
119+
static RenderError api_drawSprite(char const* name, float const x, float const y, float const rot, float const hscale, float const vscale, float const z)
112120
{
113121
Core::ScopeObject<LuaSTGPlus::IResourceSprite> pimg2dres = LRESMGR().FindSprite(name);
114122
if (!pimg2dres)
115123
{
116124
spdlog::error("[luastg] lstg.Renderer.drawSprite failed, can't find sprite '{}'", name);
117-
return;
125+
return RenderError::SpriteNotFound;
118126
}
119-
api_drawSprite(*pimg2dres, x, y, rot, hscale, vscale, z);
127+
return api_drawSprite(*pimg2dres, x, y, rot, hscale, vscale, z);
120128
}
121-
static void api_drawSpriteRect(LuaSTGPlus::IResourceSprite* pimg2dres, float const l, float const r, float const b, float const t, float const z)
129+
static RenderError api_drawSpriteRect(LuaSTGPlus::IResourceSprite* pimg2dres, float const l, float const r, float const b, float const t, float const z)
122130
{
123131
Core::Graphics::ISprite* p_sprite = pimg2dres->GetSprite();
124132
auto* ctx = LR2D();
125133
translate_blend(ctx, pimg2dres->GetBlendMode());
126134
p_sprite->setZ(z);
127135
p_sprite->draw(Core::RectF(l, t, r, b));
136+
return RenderError::None;
128137
}
129-
static void api_drawSpriteRect(char const* name, float const l, float const r, float const b, float const t, float const z)
138+
static RenderError api_drawSpriteRect(char const* name, float const l, float const r, float const b, float const t, float const z)
130139
{
131140
Core::ScopeObject<LuaSTGPlus::IResourceSprite> pimg2dres = LRESMGR().FindSprite(name);
132141
if (!pimg2dres)
133142
{
134143
spdlog::error("[luastg] lstg.Renderer.drawSpriteRect failed, can't find sprite '{}'", name);
135-
return;
144+
return RenderError::SpriteNotFound;
136145
}
137-
api_drawSpriteRect(*pimg2dres, l, r, b, t, z);
146+
return api_drawSpriteRect(*pimg2dres, l, r, b, t, z);
138147
}
139-
static void api_drawSprite4V(LuaSTGPlus::IResourceSprite* pimg2dres, float const x1, float const y1, float const z1, float const x2, float const y2, float const z2, float const x3, float const y3, float const z3, float const x4, float const y4, float const z4)
148+
static RenderError api_drawSprite4V(LuaSTGPlus::IResourceSprite* pimg2dres, float const x1, float const y1, float const z1, float const x2, float const y2, float const z2, float const x3, float const y3, float const z3, float const x4, float const y4, float const z4)
140149
{
141150
Core::Graphics::ISprite* p_sprite = pimg2dres->GetSprite();
142151
auto* ctx = LR2D();
@@ -147,35 +156,37 @@ static void api_drawSprite4V(LuaSTGPlus::IResourceSprite* pimg2dres, float const
147156
Core::Vector3F(x3, y3, z3),
148157
Core::Vector3F(x4, y4, z4)
149158
);
159+
return RenderError::None;
150160
}
151-
static void api_drawSprite4V(char const* name, float const x1, float const y1, float const z1, float const x2, float const y2, float const z2, float const x3, float const y3, float const z3, float const x4, float const y4, float const z4)
161+
static RenderError api_drawSprite4V(char const* name, float const x1, float const y1, float const z1, float const x2, float const y2, float const z2, float const x3, float const y3, float const z3, float const x4, float const y4, float const z4)
152162
{
153163
Core::ScopeObject<LuaSTGPlus::IResourceSprite> pimg2dres = LRESMGR().FindSprite(name);
154164
if (!pimg2dres)
155165
{
156166
spdlog::error("[luastg] lstg.Renderer.drawSprite4V failed, can't find sprite '{}'", name);
157-
return;
167+
return RenderError::SpriteNotFound;
158168
}
159-
api_drawSprite4V(*pimg2dres, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4);
169+
return api_drawSprite4V(*pimg2dres, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4);
160170
}
161171

162-
static void api_drawSpriteSequence(LuaSTGPlus::IResourceAnimation* pani2dres, int const ani_timer, float const x, float const y, float const rot, float const hscale, float const vscale, float const z)
172+
static RenderError api_drawSpriteSequence(LuaSTGPlus::IResourceAnimation* pani2dres, int const ani_timer, float const x, float const y, float const rot, float const hscale, float const vscale, float const z)
163173
{
164174
Core::Graphics::ISprite* p_sprite = pani2dres->GetSpriteByTimer(ani_timer);
165175
auto* ctx = LR2D();
166176
translate_blend(ctx, pani2dres->GetBlendMode());
167177
p_sprite->setZ(z);
168178
p_sprite->draw(Core::Vector2F(x, y), Core::Vector2F(hscale, vscale), rot);
179+
return RenderError::None;
169180
}
170-
static void api_drawSpriteSequence(char const* name, int const ani_timer, float const x, float const y, float const rot, float const hscale, float const vscale, float const z)
181+
static RenderError api_drawSpriteSequence(char const* name, int const ani_timer, float const x, float const y, float const rot, float const hscale, float const vscale, float const z)
171182
{
172183
Core::ScopeObject<LuaSTGPlus::IResourceAnimation> pani2dres = LRESMGR().FindAnimation(name);
173184
if (!pani2dres)
174185
{
175186
spdlog::error("[luastg] lstg.Renderer.drawSpriteSequence failed, can't find sprite sequence '{}'", name);
176-
return;
187+
return RenderError::SpriteSequenceNotFound;
177188
}
178-
api_drawSpriteSequence(*pani2dres, ani_timer, x, y, rot, hscale, vscale, z);
189+
return api_drawSpriteSequence(*pani2dres, ani_timer, x, y, rot, hscale, vscale, z);
179190
}
180191

181192
static void api_setFogState(float start, float end, Core::Color4B color)
@@ -374,7 +385,7 @@ static int lib_setTexture(lua_State* L)noexcept
374385
if (!p)
375386
{
376387
spdlog::error("[luastg] lstg.Renderer.setTexture failed: can't find texture '{}'", name);
377-
return false;
388+
return luaL_error(L, "can't find texture '%s'", name);
378389
}
379390
check_rendertarget_usage(p);
380391
LR2D()->setTexture(p->GetTexture());
@@ -502,47 +513,63 @@ static int lib_drawSprite(lua_State* L)
502513
{
503514
validate_render_scope();
504515
float const hscale = (float)luaL_optnumber(L, 5, 1.0);
505-
api_drawSprite(
516+
RenderError re = api_drawSprite(
506517
luaL_checkstring(L, 1),
507518
(float)luaL_checknumber(L, 2), (float)luaL_checknumber(L, 3),
508519
(float)(luaL_optnumber(L, 4, 0.0) * L_DEG_TO_RAD),
509520
hscale * LRESMGR().GetGlobalImageScaleFactor(), (float)luaL_optnumber(L, 6, hscale) * LRESMGR().GetGlobalImageScaleFactor(),
510521
(float)luaL_optnumber(L, 7, 0.5));
522+
if (re == RenderError::SpriteNotFound)
523+
{
524+
return luaL_error(L, "can't find sprite '%s'", luaL_checkstring(L, 1));
525+
}
511526
return 0;
512527
}
513528
static int lib_drawSpriteRect(lua_State* L)
514529
{
515530
validate_render_scope();
516-
api_drawSpriteRect(
531+
RenderError re = api_drawSpriteRect(
517532
luaL_checkstring(L, 1),
518533
(float)luaL_checknumber(L, 2), (float)luaL_checknumber(L, 3),
519534
(float)luaL_checknumber(L, 4), (float)luaL_checknumber(L, 5),
520535
(float)luaL_optnumber(L, 6, 0.5));
536+
if (re == RenderError::SpriteNotFound)
537+
{
538+
return luaL_error(L, "can't find sprite '%s'", luaL_checkstring(L, 1));
539+
}
521540
return 0;
522541
}
523542
static int lib_drawSprite4V(lua_State* L)
524543
{
525544
validate_render_scope();
526-
api_drawSprite4V(
545+
RenderError re = api_drawSprite4V(
527546
luaL_checkstring(L, 1),
528547
(float)luaL_checknumber(L, 2), (float)luaL_checknumber(L, 3), (float)luaL_checknumber(L, 4),
529548
(float)luaL_checknumber(L, 5), (float)luaL_checknumber(L, 6), (float)luaL_checknumber(L, 7),
530549
(float)luaL_checknumber(L, 8), (float)luaL_checknumber(L, 9), (float)luaL_checknumber(L, 10),
531550
(float)luaL_checknumber(L, 11), (float)luaL_checknumber(L, 12), (float)luaL_checknumber(L, 13));
551+
if (re == RenderError::SpriteNotFound)
552+
{
553+
return luaL_error(L, "can't find sprite '%s'", luaL_checkstring(L, 1));
554+
}
532555
return 0;
533556
}
534557

535558
static int lib_drawSpriteSequence(lua_State* L)
536559
{
537560
validate_render_scope();
538561
float const hscale = (float)luaL_optnumber(L, 6, 1.0);
539-
api_drawSpriteSequence(
562+
RenderError re = api_drawSpriteSequence(
540563
luaL_checkstring(L, 1),
541564
(int)luaL_checkinteger(L, 2),
542565
(float)luaL_checknumber(L, 3), (float)luaL_checknumber(L, 4),
543566
(float)(luaL_optnumber(L, 5, 0.0) * L_DEG_TO_RAD),
544567
hscale * LRESMGR().GetGlobalImageScaleFactor(), (float)luaL_optnumber(L, 7, hscale) * LRESMGR().GetGlobalImageScaleFactor(),
545568
(float)luaL_optnumber(L, 8, 0.5));
569+
if (re == RenderError::SpriteNotFound)
570+
{
571+
return luaL_error(L, "can't find animation '%s'", luaL_checkstring(L, 1));
572+
}
546573
return 0;
547574
}
548575

@@ -797,7 +824,7 @@ static int compat_PushRenderTarget(lua_State* L)noexcept
797824
if (!p)
798825
return luaL_error(L, "rendertarget '%s' not found.", luaL_checkstring(L, 1));
799826
if (!p->IsRenderTarget())
800-
return luaL_error(L, "'%s' is a texture.", luaL_checkstring(L, 1));
827+
return luaL_error(L, "'%s' is not a rendertarget.", luaL_checkstring(L, 1));
801828

802829
if (!LAPP.GetRenderTargetManager()->PushRenderTarget(p.get()))
803830
return luaL_error(L, "push rendertarget '%s' failed.", luaL_checkstring(L, 1));
@@ -832,7 +859,7 @@ static int compat_PostEffect(lua_State* L)
832859

833860
Core::ScopeObject<LuaSTGPlus::IResourcePostEffectShader> pfx = LRES.FindFX(ps_name);
834861
if (!pfx)
835-
return luaL_error(L, "effect '%s' not found.", ps_name);
862+
return luaL_error(L, "posteffect '%s' not found.", ps_name);
836863

837864
Core::ScopeObject<LuaSTGPlus::IResourceTexture> prt = LRES.FindTexture(rt_name);
838865
if (!prt)

0 commit comments

Comments
 (0)