Skip to content

Commit

Permalink
+Added an Application.useEffect(...) method.
Browse files Browse the repository at this point in the history
  • Loading branch information
paladin-t committed Apr 11, 2021
1 parent 06070d9 commit 634c26c
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 33 deletions.
6 changes: 6 additions & 0 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ class Application {
}
#if BITTY_EFFECTS_ENABLED
_effects->finish(_window, _renderer);
if (_workspace->effectCustomized()) {
const std::string &material = _workspace->effectConfig();
_effects->use(_workspace, material.empty() ? nullptr : material.c_str());
_workspace->effectCustomized(false);
_workspace->effectConfig().clear();
}
#else /* BITTY_EFFECTS_ENABLED */
_renderer->flush();
#endif /* BITTY_EFFECTS_ENABLED */
Expand Down
69 changes: 42 additions & 27 deletions src/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
** Macros and constants
*/

#if BITTY_EFFECTS_ENABLED
# pragma message("Effects enabled.")
#endif /* BITTY_EFFECTS_ENABLED */

#ifndef EFFECTS_DEFAULT_FILE
# define EFFECTS_DEFAULT_FILE "../effects/default.json"
#endif /* EFFECTS_DEFAULT_FILE */
Expand Down Expand Up @@ -267,31 +271,7 @@ class EffectsImpl : public Effects {
return true;
}
}
const GLchar* vertSrc =
"#version 150\n"
"uniform mat4 ProjMatrix;\n"
"in vec2 Position;\n"
"in vec2 UV;\n"
"in vec4 Color;\n"
"out vec2 Frag_UV;\n"
"out vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" Frag_UV = UV;\n"
" Frag_Color = Color;\n"
" gl_Position = ProjMatrix * vec4(Position.xy, 0, 1);\n"
"}\n";
const GLchar* fragSrc =
"#version 150\n"
"uniform sampler2D Texture;\n"
"in vec2 Frag_UV;\n"
"in vec4 Frag_Color;\n"
"out vec4 Out_Color;\n"
"void main()\n"
"{\n"
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
"}\n";
_material.open(vertSrc, fragSrc, workspace);
useDefault(workspace);

return true;
}
Expand Down Expand Up @@ -321,9 +301,15 @@ class EffectsImpl : public Effects {
return true;
}

virtual bool use(class Workspace* workspace, const char* config) override {
virtual bool use(class Workspace* workspace, const char* material) override {
if (!material) {
useDefault(workspace);

return true;
}

Json::Ptr json(Json::create());
if (!json->fromString(config))
if (!json->fromString(material))
return false;
rapidjson::Document doc;
if (!json->toJson(doc))
Expand Down Expand Up @@ -488,6 +474,35 @@ class EffectsImpl : public Effects {

SDL_GL_SwapWindow(window);
}

private:
void useDefault(Workspace* workspace) {
const GLchar* vertSrc =
"#version 150\n"
"uniform mat4 ProjMatrix;\n"
"in vec2 Position;\n"
"in vec2 UV;\n"
"in vec4 Color;\n"
"out vec2 Frag_UV;\n"
"out vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" Frag_UV = UV;\n"
" Frag_Color = Color;\n"
" gl_Position = ProjMatrix * vec4(Position.xy, 0, 1);\n"
"}\n";
const GLchar* fragSrc =
"#version 150\n"
"uniform sampler2D Texture;\n"
"in vec2 Frag_UV;\n"
"in vec4 Frag_Color;\n"
"out vec4 Out_Color;\n"
"void main()\n"
"{\n"
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
"}\n";
_material.open(vertSrc, fragSrc, workspace);
}
};
#else /* BITTY_EFFECTS_ENABLED && Platform macro. */
class EffectsImpl : public Effects {
Expand Down
2 changes: 1 addition & 1 deletion src/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Effects {
/**
* @brief Configures the effects.
*/
virtual bool use(class Workspace* workspace, const char* config) = 0;
virtual bool use(class Workspace* workspace, const char* material) = 0;

/**
* @brief Prepares the effects before rendering new frame.
Expand Down
4 changes: 4 additions & 0 deletions src/executable.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ class Executable {
* @brief Sets canvas size.
*/
virtual bool resize(const Math::Vec2i &size) = 0;
/**
* @brief Sets fullscreen effect.
*/
virtual void effect(const char* material) = 0;
};

typedef std::function<void(const char*, int)> BreakpointGetter;
Expand Down
7 changes: 5 additions & 2 deletions src/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2705,14 +2705,17 @@ class PrimitivesImpl : public Primitives {

commit(var, nullptr, true);
}
virtual void function(Function func, const Variant &arg) const override {
virtual void function(Function func, const Variant &arg, bool block) const override {
if (!func)
return;

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

commit(var, nullptr, true);
if (block)
commit(var, nullptr, block);
else
commit(var, nullptr);
}

virtual int newFrame(void) override {
Expand Down
2 changes: 1 addition & 1 deletion src/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ class Primitives {
/**
* @brief Schedules with a custom C++ function.
*/
virtual void function(Function func, const Variant &arg /* nullable */) const = 0;
virtual void function(Function func, const Variant &arg /* nullable */, bool block) const = 0;

/**
* @brief Begins a new frame.
Expand Down
39 changes: 37 additions & 2 deletions src/scripting_lua_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9377,15 +9377,17 @@ static int Application_resize(lua_State* L) {
wnd->size(Math::Vec2i(w, h));
wnd->centralize();
},
nullptr
nullptr,
true
);
} else if (s == "fullscreen") {
impl->primitives()->function(
[=] (const Variant &) -> void {
Window* wnd = impl->primitives()->window();
wnd->fullscreen(true);
},
nullptr
nullptr,
true
);
} else {
error(L, "Invalid size.");
Expand All @@ -9394,6 +9396,38 @@ static int Application_resize(lua_State* L) {
return 0;
}

static int Application_useEffect(lua_State* L) {
ScriptingLua* impl = ScriptingLua::instanceOf(L);

const char* material = nullptr;
read<>(L, material);

#if BITTY_EFFECTS_ENABLED
if (material) {
const std::string material_ = material;
impl->primitives()->function(
[=] (const Variant &) -> void {
impl->observer()->effect(material_.c_str());
},
nullptr,
true
);
} else {
impl->primitives()->function(
[=] (const Variant &) -> void {
impl->observer()->effect(nullptr);
},
nullptr,
true
);
}
#else /* BITTY_EFFECTS_ENABLED */
error(L, "Effects is not available.");
#endif /* BITTY_EFFECTS_ENABLED */

return 0;
}

static void open_Application(lua_State* L) {
req(
L,
Expand All @@ -9404,6 +9438,7 @@ static void open_Application(lua_State* L) {
array(
luaL_Reg{ "setCursor", Application_setCursor }, // Frame synchronized.
luaL_Reg{ "resize", Application_resize }, // Frame synchronized.
luaL_Reg{ "useEffect", Application_useEffect }, // Undocumented. Frame synchronized.
luaL_Reg{ nullptr, nullptr }
)
)
Expand Down
12 changes: 12 additions & 0 deletions src/workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ bool Workspace::open(class Window* wnd, class Renderer* rnd, const class Project

splashCustomized(false);

effectCustomized(false);

menuHeight(0.0f);
bannerHeight(0.0f);
bannerVisible(&settings()->bannerVisible);
Expand Down Expand Up @@ -697,6 +699,16 @@ bool Workspace::resize(const Math::Vec2i &size) {
return true;
}

void Workspace::effect(const char* material) {
if (material) {
effectCustomized(true);
effectConfig(material);
} else {
effectCustomized(true);
effectConfig().clear();
}
}

void Workspace::focusGained(class Window* /* wnd */, class Renderer* /* rnd */, const class Project* /* project */, Executable* /* exec */, class Primitives* /* primitives */) {
// Do nothing.
}
Expand Down
7 changes: 7 additions & 0 deletions src/workspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ class Workspace : public Executable::Observer, public Dispatchable {
BITTY_PROPERTY_PTR(Texture, splashBitty)
BITTY_PROPERTY_PTR(Texture, splashEngine)

BITTY_PROPERTY(bool, effectCustomized)
BITTY_PROPERTY(std::string, effectConfig)

BITTY_PROPERTY_READONLY(ImGui::PopupBox::Ptr, popupBox)
BITTY_PROPERTY(ImGui::Initializer, popupPromiseInit)
BITTY_PROPERTY(PopupPromiseTypes, popupPromiseType)
Expand Down Expand Up @@ -388,6 +391,10 @@ class Workspace : public Executable::Observer, public Dispatchable {
* @brief Implements `Executable::Observer`. Sets the size of the rendering canvas.
*/
virtual bool resize(const Math::Vec2i &size) override;
/**
* @brief Implements `Executable::Observer`. Sets fullscreen effect.
*/
virtual void effect(const char* material) override;

/**
* @brief Callback for focus gained.
Expand Down

0 comments on commit 634c26c

Please sign in to comment.