Skip to content

Commit 634c26c

Browse files
committed
+Added an Application.useEffect(...) method.
1 parent 06070d9 commit 634c26c

File tree

9 files changed

+115
-33
lines changed

9 files changed

+115
-33
lines changed

src/application.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ class Application {
365365
}
366366
#if BITTY_EFFECTS_ENABLED
367367
_effects->finish(_window, _renderer);
368+
if (_workspace->effectCustomized()) {
369+
const std::string &material = _workspace->effectConfig();
370+
_effects->use(_workspace, material.empty() ? nullptr : material.c_str());
371+
_workspace->effectCustomized(false);
372+
_workspace->effectConfig().clear();
373+
}
368374
#else /* BITTY_EFFECTS_ENABLED */
369375
_renderer->flush();
370376
#endif /* BITTY_EFFECTS_ENABLED */

src/effects.cpp

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
** Macros and constants
4141
*/
4242

43+
#if BITTY_EFFECTS_ENABLED
44+
# pragma message("Effects enabled.")
45+
#endif /* BITTY_EFFECTS_ENABLED */
46+
4347
#ifndef EFFECTS_DEFAULT_FILE
4448
# define EFFECTS_DEFAULT_FILE "../effects/default.json"
4549
#endif /* EFFECTS_DEFAULT_FILE */
@@ -267,31 +271,7 @@ class EffectsImpl : public Effects {
267271
return true;
268272
}
269273
}
270-
const GLchar* vertSrc =
271-
"#version 150\n"
272-
"uniform mat4 ProjMatrix;\n"
273-
"in vec2 Position;\n"
274-
"in vec2 UV;\n"
275-
"in vec4 Color;\n"
276-
"out vec2 Frag_UV;\n"
277-
"out vec4 Frag_Color;\n"
278-
"void main()\n"
279-
"{\n"
280-
" Frag_UV = UV;\n"
281-
" Frag_Color = Color;\n"
282-
" gl_Position = ProjMatrix * vec4(Position.xy, 0, 1);\n"
283-
"}\n";
284-
const GLchar* fragSrc =
285-
"#version 150\n"
286-
"uniform sampler2D Texture;\n"
287-
"in vec2 Frag_UV;\n"
288-
"in vec4 Frag_Color;\n"
289-
"out vec4 Out_Color;\n"
290-
"void main()\n"
291-
"{\n"
292-
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
293-
"}\n";
294-
_material.open(vertSrc, fragSrc, workspace);
274+
useDefault(workspace);
295275

296276
return true;
297277
}
@@ -321,9 +301,15 @@ class EffectsImpl : public Effects {
321301
return true;
322302
}
323303

324-
virtual bool use(class Workspace* workspace, const char* config) override {
304+
virtual bool use(class Workspace* workspace, const char* material) override {
305+
if (!material) {
306+
useDefault(workspace);
307+
308+
return true;
309+
}
310+
325311
Json::Ptr json(Json::create());
326-
if (!json->fromString(config))
312+
if (!json->fromString(material))
327313
return false;
328314
rapidjson::Document doc;
329315
if (!json->toJson(doc))
@@ -488,6 +474,35 @@ class EffectsImpl : public Effects {
488474

489475
SDL_GL_SwapWindow(window);
490476
}
477+
478+
private:
479+
void useDefault(Workspace* workspace) {
480+
const GLchar* vertSrc =
481+
"#version 150\n"
482+
"uniform mat4 ProjMatrix;\n"
483+
"in vec2 Position;\n"
484+
"in vec2 UV;\n"
485+
"in vec4 Color;\n"
486+
"out vec2 Frag_UV;\n"
487+
"out vec4 Frag_Color;\n"
488+
"void main()\n"
489+
"{\n"
490+
" Frag_UV = UV;\n"
491+
" Frag_Color = Color;\n"
492+
" gl_Position = ProjMatrix * vec4(Position.xy, 0, 1);\n"
493+
"}\n";
494+
const GLchar* fragSrc =
495+
"#version 150\n"
496+
"uniform sampler2D Texture;\n"
497+
"in vec2 Frag_UV;\n"
498+
"in vec4 Frag_Color;\n"
499+
"out vec4 Out_Color;\n"
500+
"void main()\n"
501+
"{\n"
502+
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
503+
"}\n";
504+
_material.open(vertSrc, fragSrc, workspace);
505+
}
491506
};
492507
#else /* BITTY_EFFECTS_ENABLED && Platform macro. */
493508
class EffectsImpl : public Effects {

src/effects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Effects {
3535
/**
3636
* @brief Configures the effects.
3737
*/
38-
virtual bool use(class Workspace* workspace, const char* config) = 0;
38+
virtual bool use(class Workspace* workspace, const char* material) = 0;
3939

4040
/**
4141
* @brief Prepares the effects before rendering new frame.

src/executable.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class Executable {
107107
* @brief Sets canvas size.
108108
*/
109109
virtual bool resize(const Math::Vec2i &size) = 0;
110+
/**
111+
* @brief Sets fullscreen effect.
112+
*/
113+
virtual void effect(const char* material) = 0;
110114
};
111115

112116
typedef std::function<void(const char*, int)> BreakpointGetter;

src/primitives.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,14 +2705,17 @@ class PrimitivesImpl : public Primitives {
27052705

27062706
commit(var, nullptr, true);
27072707
}
2708-
virtual void function(Function func, const Variant &arg) const override {
2708+
virtual void function(Function func, const Variant &arg, bool block) const override {
27092709
if (!func)
27102710
return;
27112711

27122712
CmdVariant var;
27132713
new (&var.function) CmdFunction(func, arg);
27142714

2715-
commit(var, nullptr, true);
2715+
if (block)
2716+
commit(var, nullptr, block);
2717+
else
2718+
commit(var, nullptr);
27162719
}
27172720

27182721
virtual int newFrame(void) override {

src/primitives.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ class Primitives {
357357
/**
358358
* @brief Schedules with a custom C++ function.
359359
*/
360-
virtual void function(Function func, const Variant &arg /* nullable */) const = 0;
360+
virtual void function(Function func, const Variant &arg /* nullable */, bool block) const = 0;
361361

362362
/**
363363
* @brief Begins a new frame.

src/scripting_lua_api.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9377,15 +9377,17 @@ static int Application_resize(lua_State* L) {
93779377
wnd->size(Math::Vec2i(w, h));
93789378
wnd->centralize();
93799379
},
9380-
nullptr
9380+
nullptr,
9381+
true
93819382
);
93829383
} else if (s == "fullscreen") {
93839384
impl->primitives()->function(
93849385
[=] (const Variant &) -> void {
93859386
Window* wnd = impl->primitives()->window();
93869387
wnd->fullscreen(true);
93879388
},
9388-
nullptr
9389+
nullptr,
9390+
true
93899391
);
93909392
} else {
93919393
error(L, "Invalid size.");
@@ -9394,6 +9396,38 @@ static int Application_resize(lua_State* L) {
93949396
return 0;
93959397
}
93969398

9399+
static int Application_useEffect(lua_State* L) {
9400+
ScriptingLua* impl = ScriptingLua::instanceOf(L);
9401+
9402+
const char* material = nullptr;
9403+
read<>(L, material);
9404+
9405+
#if BITTY_EFFECTS_ENABLED
9406+
if (material) {
9407+
const std::string material_ = material;
9408+
impl->primitives()->function(
9409+
[=] (const Variant &) -> void {
9410+
impl->observer()->effect(material_.c_str());
9411+
},
9412+
nullptr,
9413+
true
9414+
);
9415+
} else {
9416+
impl->primitives()->function(
9417+
[=] (const Variant &) -> void {
9418+
impl->observer()->effect(nullptr);
9419+
},
9420+
nullptr,
9421+
true
9422+
);
9423+
}
9424+
#else /* BITTY_EFFECTS_ENABLED */
9425+
error(L, "Effects is not available.");
9426+
#endif /* BITTY_EFFECTS_ENABLED */
9427+
9428+
return 0;
9429+
}
9430+
93979431
static void open_Application(lua_State* L) {
93989432
req(
93999433
L,
@@ -9404,6 +9438,7 @@ static void open_Application(lua_State* L) {
94049438
array(
94059439
luaL_Reg{ "setCursor", Application_setCursor }, // Frame synchronized.
94069440
luaL_Reg{ "resize", Application_resize }, // Frame synchronized.
9441+
luaL_Reg{ "useEffect", Application_useEffect }, // Undocumented. Frame synchronized.
94079442
luaL_Reg{ nullptr, nullptr }
94089443
)
94099444
)

src/workspace.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ bool Workspace::open(class Window* wnd, class Renderer* rnd, const class Project
319319

320320
splashCustomized(false);
321321

322+
effectCustomized(false);
323+
322324
menuHeight(0.0f);
323325
bannerHeight(0.0f);
324326
bannerVisible(&settings()->bannerVisible);
@@ -697,6 +699,16 @@ bool Workspace::resize(const Math::Vec2i &size) {
697699
return true;
698700
}
699701

702+
void Workspace::effect(const char* material) {
703+
if (material) {
704+
effectCustomized(true);
705+
effectConfig(material);
706+
} else {
707+
effectCustomized(true);
708+
effectConfig().clear();
709+
}
710+
}
711+
700712
void Workspace::focusGained(class Window* /* wnd */, class Renderer* /* rnd */, const class Project* /* project */, Executable* /* exec */, class Primitives* /* primitives */) {
701713
// Do nothing.
702714
}

src/workspace.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ class Workspace : public Executable::Observer, public Dispatchable {
221221
BITTY_PROPERTY_PTR(Texture, splashBitty)
222222
BITTY_PROPERTY_PTR(Texture, splashEngine)
223223

224+
BITTY_PROPERTY(bool, effectCustomized)
225+
BITTY_PROPERTY(std::string, effectConfig)
226+
224227
BITTY_PROPERTY_READONLY(ImGui::PopupBox::Ptr, popupBox)
225228
BITTY_PROPERTY(ImGui::Initializer, popupPromiseInit)
226229
BITTY_PROPERTY(PopupPromiseTypes, popupPromiseType)
@@ -388,6 +391,10 @@ class Workspace : public Executable::Observer, public Dispatchable {
388391
* @brief Implements `Executable::Observer`. Sets the size of the rendering canvas.
389392
*/
390393
virtual bool resize(const Math::Vec2i &size) override;
394+
/**
395+
* @brief Implements `Executable::Observer`. Sets fullscreen effect.
396+
*/
397+
virtual void effect(const char* material) override;
391398

392399
/**
393400
* @brief Callback for focus gained.

0 commit comments

Comments
 (0)