From 1da356d8a64c45c99871aca156ee9fdd3e10ec15 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Wed, 27 Sep 2023 08:03:04 -0400 Subject: Simplification of the Theme system with a more OO model --- themes/Makefile | 2 +- themes/compile_commands.json | 50 ++++++++--------- themes/compile_commands.sh | 2 +- themes/dist/output.wasm | Bin 153554 -> 157459 bytes themes/src/autumn/AutumnTheme.cpp | 14 ++++- themes/src/autumn/AutumnTheme.hpp | 10 +++- themes/src/main.cpp | 113 +++++++++----------------------------- themes/src/spring/SpringTheme.cpp | 10 ++++ themes/src/spring/SpringTheme.hpp | 8 ++- themes/src/summer/SummerTheme.cpp | 14 ++++- themes/src/summer/SummerTheme.h | 10 +++- themes/src/theme.h | 22 ++++++++ themes/src/winter/WinterTheme.cpp | 14 ++++- themes/src/winter/WinterTheme.hpp | 12 +++- 14 files changed, 156 insertions(+), 125 deletions(-) create mode 100644 themes/src/theme.h diff --git a/themes/Makefile b/themes/Makefile index bfe42a3..45967f5 100644 --- a/themes/Makefile +++ b/themes/Makefile @@ -4,7 +4,7 @@ BUILD_DIR ?= ./dist SRC_DIRS ?= ./src CC := emcc -CXX := emcc +CXX := em++ SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s) OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) DEPS := $(OBJS:.o=.d) diff --git a/themes/compile_commands.json b/themes/compile_commands.json index 1c3aee5..ef843ef 100644 --- a/themes/compile_commands.json +++ b/themes/compile_commands.json @@ -1,127 +1,127 @@ [ { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/MainLoop.cpp -o dist/./src/MainLoop.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/MainLoop.cpp -o dist/./src/MainLoop.cpp.o", "file": "dist/./src/MainLoop.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/spring/SpringTheme.cpp -o dist/./src/spring/SpringTheme.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/spring/SpringTheme.cpp -o dist/./src/spring/SpringTheme.cpp.o", "file": "dist/./src/spring/SpringTheme.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/spring/GrassRenderer.cpp -o dist/./src/spring/GrassRenderer.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/spring/GrassRenderer.cpp -o dist/./src/spring/GrassRenderer.cpp.o", "file": "dist/./src/spring/GrassRenderer.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/mathlib.cpp -o dist/./src/mathlib.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/mathlib.cpp -o dist/./src/mathlib.cpp.o", "file": "dist/./src/mathlib.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/renderer3d_frag.cpp -o dist/./src/shaders/renderer3d_frag.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/renderer3d_frag.cpp -o dist/./src/shaders/renderer3d_frag.cpp.o", "file": "dist/./src/shaders/renderer3d_frag.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/sun_frag.cpp -o dist/./src/shaders/sun_frag.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/sun_frag.cpp -o dist/./src/shaders/sun_frag.cpp.o", "file": "dist/./src/shaders/sun_frag.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/renderer3d_vert.cpp -o dist/./src/shaders/renderer3d_vert.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/renderer3d_vert.cpp -o dist/./src/shaders/renderer3d_vert.cpp.o", "file": "dist/./src/shaders/renderer3d_vert.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/renderer2d_frag.cpp -o dist/./src/shaders/renderer2d_frag.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/renderer2d_frag.cpp -o dist/./src/shaders/renderer2d_frag.cpp.o", "file": "dist/./src/shaders/renderer2d_frag.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/sun_vert.cpp -o dist/./src/shaders/sun_vert.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/sun_vert.cpp -o dist/./src/shaders/sun_vert.cpp.o", "file": "dist/./src/shaders/sun_vert.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/renderer2d_vert.cpp -o dist/./src/shaders/renderer2d_vert.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shaders/renderer2d_vert.cpp -o dist/./src/shaders/renderer2d_vert.cpp.o", "file": "dist/./src/shaders/renderer2d_vert.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shader_fetcher.cpp -o dist/./src/shader_fetcher.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shader_fetcher.cpp -o dist/./src/shader_fetcher.cpp.o", "file": "dist/./src/shader_fetcher.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/Logger.cpp -o dist/./src/Logger.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/Logger.cpp -o dist/./src/Logger.cpp.o", "file": "dist/./src/Logger.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/Renderer3d.cpp -o dist/./src/Renderer3d.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/Renderer3d.cpp -o dist/./src/Renderer3d.cpp.o", "file": "dist/./src/Renderer3d.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/Shader.cpp -o dist/./src/Shader.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/Shader.cpp -o dist/./src/Shader.cpp.o", "file": "dist/./src/Shader.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/summer/SummerTheme.cpp -o dist/./src/summer/SummerTheme.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/summer/SummerTheme.cpp -o dist/./src/summer/SummerTheme.cpp.o", "file": "dist/./src/summer/SummerTheme.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/winter/Windfield.cpp -o dist/./src/winter/Windfield.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/winter/Windfield.cpp -o dist/./src/winter/Windfield.cpp.o", "file": "dist/./src/winter/Windfield.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/winter/WinterTheme.cpp -o dist/./src/winter/WinterTheme.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/winter/WinterTheme.cpp -o dist/./src/winter/WinterTheme.cpp.o", "file": "dist/./src/winter/WinterTheme.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/winter/Snowflake.cpp -o dist/./src/winter/Snowflake.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/winter/Snowflake.cpp -o dist/./src/winter/Snowflake.cpp.o", "file": "dist/./src/winter/Snowflake.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/autumn/AutumnTheme.cpp -o dist/./src/autumn/AutumnTheme.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/autumn/AutumnTheme.cpp -o dist/./src/autumn/AutumnTheme.cpp.o", "file": "dist/./src/autumn/AutumnTheme.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/autumn/TreeShape.cpp -o dist/./src/autumn/TreeShape.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/autumn/TreeShape.cpp -o dist/./src/autumn/TreeShape.cpp.o", "file": "dist/./src/autumn/TreeShape.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/autumn/LeafParticleRender.cpp -o dist/./src/autumn/LeafParticleRender.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/autumn/LeafParticleRender.cpp -o dist/./src/autumn/LeafParticleRender.cpp.o", "file": "dist/./src/autumn/LeafParticleRender.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shapes_2d.cpp -o dist/./src/shapes_2d.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/shapes_2d.cpp -o dist/./src/shapes_2d.cpp.o", "file": "dist/./src/shapes_2d.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/Renderer2d.cpp -o dist/./src/Renderer2d.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/Renderer2d.cpp -o dist/./src/Renderer2d.cpp.o", "file": "dist/./src/Renderer2d.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/main.cpp -o dist/./src/main.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/main.cpp -o dist/./src/main.cpp.o", "file": "dist/./src/main.cpp.o" }, { "directory": ".", - "command": "emcc -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/WebglContext.cpp -o dist/./src/WebglContext.cpp.o", + "command": "em++ -I./src -I./src/spring -I./src/shaders -I./src/tools -I./src/summer -I./src/winter -I./src/autumn -I./src/_shaders -MMD -MP -c src/WebglContext.cpp -o dist/./src/WebglContext.cpp.o", "file": "dist/./src/WebglContext.cpp.o" } ] diff --git a/themes/compile_commands.sh b/themes/compile_commands.sh index 06ebba0..0252d04 100755 --- a/themes/compile_commands.sh +++ b/themes/compile_commands.sh @@ -1,5 +1,5 @@ make --always-make --dry-run \ - | grep -wE 'emcc|gcc|g\+\+|c\+\+' \ + | grep -wE 'em++|emcc|gcc|g\+\+|c\+\+' \ | grep -w '\-c' \ | jq -nR '[inputs|{directory:".", command:., file: match(" [^ ]+$").string[1:]}]' \ > compile_commands.json diff --git a/themes/dist/output.wasm b/themes/dist/output.wasm index 26dfb36..f4de713 100755 Binary files a/themes/dist/output.wasm and b/themes/dist/output.wasm differ diff --git a/themes/src/autumn/AutumnTheme.cpp b/themes/src/autumn/AutumnTheme.cpp index affcc00..c6431a1 100644 --- a/themes/src/autumn/AutumnTheme.cpp +++ b/themes/src/autumn/AutumnTheme.cpp @@ -8,6 +8,17 @@ namespace const int NUM_HILLS = 3; } +AutumnTheme::AutumnTheme(Renderer2d* renderer) + : renderer{renderer} +{ + load(renderer); +} + +AutumnTheme::~AutumnTheme() +{ + unload(); +} + void AutumnTheme::load(Renderer2d* renderer) { renderer->clearColor = Vector4(252, 210, 153, 255).toNormalizedColor(); auto lr = tree.load(renderer); @@ -45,7 +56,8 @@ void AutumnTheme::update(f32 dtSeconds) { leafParticles.update(dtSeconds); } -void AutumnTheme::render(Renderer2d* renderer) { +void AutumnTheme::render() { + renderer->render(); background->render(); background_hill->render(); tree.render(renderer); diff --git a/themes/src/autumn/AutumnTheme.hpp b/themes/src/autumn/AutumnTheme.hpp index 2d9958c..41df04c 100644 --- a/themes/src/autumn/AutumnTheme.hpp +++ b/themes/src/autumn/AutumnTheme.hpp @@ -4,6 +4,7 @@ #include "TreeShape.h" #include "LeafParticleRender.h" #include "../types.h" +#include "../theme.h" #include #include @@ -11,7 +12,10 @@ struct Renderer2d; class RectangularGradient; class Circleish; -struct AutumnTheme { +class AutumnTheme : public Theme { +public: + AutumnTheme(Renderer2d* renderer); + ~AutumnTheme(); TreeShape tree; LeafParticleRender leafParticles; RectangularGradient* background; @@ -20,8 +24,10 @@ struct AutumnTheme { void load(Renderer2d* renderer); void update(f32 dtSeconds); - void render(Renderer2d* renderer); + void render(); void unload(); +private: + Renderer2d* renderer; }; #endif diff --git a/themes/src/main.cpp b/themes/src/main.cpp index 297a498..384174b 100644 --- a/themes/src/main.cpp +++ b/themes/src/main.cpp @@ -2,6 +2,7 @@ #include "MainLoop.h" #include "Renderer2d.h" #include "mathlib.h" +#include "theme.h" #include "types.h" #include "summer/SummerTheme.h" #include "autumn/AutumnTheme.hpp" @@ -10,15 +11,6 @@ #include #include - -enum Theme { - Default = 0, - Autumn, - Winter, - Spring, - Summer -}; - void load(Theme theme); void unload(); void update(f32 dtSeconds, void* userData); @@ -31,11 +23,12 @@ EM_BOOL selectSummer(int eventType, const EmscriptenMouseEvent* mouseEvent, void WebglContext context; Renderer2d renderer2d; MainLoop mainLoop; -Theme activeTheme = Theme::Default; -AutumnTheme autumnTheme; -WinterTheme winterTheme; -SpringTheme springTheme; -SummerTheme summerTheme; +ThemeType type; +Theme* active_theme; +AutumnTheme* autumnTheme; +WinterTheme* winterTheme; +SpringTheme* springTheme; +SummerTheme* summerTheme; int main() { context.init("#theme_canvas"); @@ -49,32 +42,31 @@ int main() { } // -- Scene loading, updating, and unloading logic -void load(Theme theme) { - if (activeTheme == theme) { +void load(ThemeType theme) { + if (type == theme) { printf("This theme is already active.\n"); return; } unload(); // Try and unload before we load, so that we start fresh - activeTheme = theme; + type = theme; mainLoop.run(update); - switch (activeTheme) { - case Theme::Autumn: + switch (type) { + case ThemeType::Autumn: renderer2d.load(&context); - autumnTheme.load(&renderer2d); + active_theme = new AutumnTheme(&renderer2d); break; - case Theme::Winter: + case ThemeType::Winter: renderer2d.load(&context); - winterTheme.load(&renderer2d); + active_theme = new WinterTheme(&renderer2d); break; - case Theme::Spring: { - springTheme.load(&context); + case ThemeType::Spring: + active_theme = new SpringTheme(&context); break; - } - case Theme::Summer: - summerTheme.load(&renderer2d, &context); + case ThemeType::Summer: + active_theme = new SummerTheme(&renderer2d, &context); break; default: break; @@ -82,65 +74,14 @@ void load(Theme theme) { } void update(f32 dtSeconds, void* userData) { - // -- Update - switch (activeTheme) { - case Theme::Autumn: - autumnTheme.update(dtSeconds); - break; - case Theme::Winter: - winterTheme.update(dtSeconds); - break; - case Theme::Spring: - springTheme.update(dtSeconds); - break; - case Theme::Summer: - summerTheme.update(dtSeconds); - break; - default: - break; - } - - // -- Render - switch (activeTheme) { - case Theme::Autumn: - renderer2d.render(); - autumnTheme.render(&renderer2d); - break; - case Theme::Winter: - renderer2d.render(); - winterTheme.render(&renderer2d); - break; - case Theme::Spring: - springTheme.render(); - break; - case Theme::Summer: - renderer2d.render(); - summerTheme.render(&renderer2d); - break; - default: - break; - } + active_theme->update(dtSeconds); + active_theme->render(); } void unload() { - switch (activeTheme) { - case Theme::Autumn: - autumnTheme.unload(); - break; - case Theme::Winter: - winterTheme.unload(); - break; - case Theme::Spring: - springTheme.unload(); - break; - case Theme::Summer: - summerTheme.unload(); - break; - default: - break; - } + delete active_theme; - activeTheme = Theme::Default; + type = ThemeType::Default; if (mainLoop.isRunning) { mainLoop.stop(); renderer2d.unload(); @@ -156,24 +97,24 @@ EM_BOOL selectNone(int eventType, const EmscriptenMouseEvent* mouseEvent, void* EM_BOOL selectAutumn(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { printf("Autumn theme selected\n"); - load(Theme::Autumn); + load(ThemeType::Autumn); return true; } EM_BOOL selectWinter(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { printf("Winter theme selected\n"); - load(Theme::Winter); + load(ThemeType::Winter); return true; } EM_BOOL selectSpring(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { printf("Spring theme selected\n"); - load(Theme::Spring); + load(ThemeType::Spring); return true; } EM_BOOL selectSummer(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { printf("Summer theme selected\n"); - load(Theme::Summer); + load(ThemeType::Summer); return true; } diff --git a/themes/src/spring/SpringTheme.cpp b/themes/src/spring/SpringTheme.cpp index 39b6bad..e39c138 100644 --- a/themes/src/spring/SpringTheme.cpp +++ b/themes/src/spring/SpringTheme.cpp @@ -37,6 +37,16 @@ inline void on_shaders_loader(ShaderFetchResult* result) { fetch_bunny(theme); } +SpringTheme::SpringTheme(WebglContext* context) +{ + load(context); +} + +SpringTheme::~SpringTheme() +{ + unload(); +} + void SpringTheme::load(WebglContext* context) { state = SpringThemeState::Loading; renderer.context = context; diff --git a/themes/src/spring/SpringTheme.hpp b/themes/src/spring/SpringTheme.hpp index 0866921..64f9cb5 100644 --- a/themes/src/spring/SpringTheme.hpp +++ b/themes/src/spring/SpringTheme.hpp @@ -4,6 +4,7 @@ #include "../mathlib.h" #include "../types.h" #include "../Renderer3d.h" +#include "../theme.h" enum class SpringThemeState { @@ -15,7 +16,10 @@ enum class SpringThemeState { Idle }; -struct SpringTheme { +class SpringTheme : public Theme { +public: + SpringTheme(WebglContext*); + ~SpringTheme(); Renderer3d renderer; SpringThemeState state; f32 bunnySpeed = 5.f; @@ -38,4 +42,4 @@ struct SpringTheme { void unload(); }; -#endif \ No newline at end of file +#endif diff --git a/themes/src/summer/SummerTheme.cpp b/themes/src/summer/SummerTheme.cpp index 6106c89..522b44c 100644 --- a/themes/src/summer/SummerTheme.cpp +++ b/themes/src/summer/SummerTheme.cpp @@ -6,6 +6,17 @@ #include "../shaders/sun_vert.h" #include +SummerTheme::SummerTheme(Renderer2d* renderer, WebglContext* context) + : renderer{renderer} +{ + load(renderer, context); +} + +SummerTheme::~SummerTheme() +{ + unload(); +} + void SummerTheme::load(Renderer2d* renderer, WebglContext* context) { renderer->load(context, shader_sun_vert, shader_sun_frag); renderer->clearColor = Vector4(0, 181, 286, 255.f).toNormalizedColor(); @@ -18,7 +29,8 @@ void SummerTheme::update(f32 dtSeconds) { sun.update(dtSeconds); } -void SummerTheme::render(Renderer2d* renderer) { +void SummerTheme::render() { + renderer->render(); sun.render(renderer); } diff --git a/themes/src/summer/SummerTheme.h b/themes/src/summer/SummerTheme.h index dad4ac9..030f20a 100644 --- a/themes/src/summer/SummerTheme.h +++ b/themes/src/summer/SummerTheme.h @@ -1,6 +1,7 @@ #pragma once #include "../types.h" #include "../Renderer2d.h" +#include "../theme.h" #include struct Sun { @@ -14,10 +15,15 @@ struct Sun { void unload(); }; -struct SummerTheme { +class SummerTheme : public Theme { +public: + SummerTheme(Renderer2d*, WebglContext*); + ~SummerTheme(); Sun sun; void load(Renderer2d*, WebglContext*); void update(f32 dtSeconds); - void render(Renderer2d* renderer); + void render(); void unload(); +private: + Renderer2d* renderer; }; diff --git a/themes/src/theme.h b/themes/src/theme.h new file mode 100644 index 0000000..0872543 --- /dev/null +++ b/themes/src/theme.h @@ -0,0 +1,22 @@ +#ifndef THEME_H +#define THEME_H + +#include "types.h" + +enum class ThemeType { + Default = 0, + Autumn, + Winter, + Spring, + Summer +}; + +class Theme +{ +public: + virtual ~Theme() = default; + virtual void update(f32 dt) = 0; + virtual void render() = 0; +}; + +#endif diff --git a/themes/src/winter/WinterTheme.cpp b/themes/src/winter/WinterTheme.cpp index 2686988..69e7e69 100644 --- a/themes/src/winter/WinterTheme.cpp +++ b/themes/src/winter/WinterTheme.cpp @@ -1,6 +1,17 @@ #include "WinterTheme.hpp" #include "../Renderer2d.h" +WinterTheme::WinterTheme(Renderer2d* renderer) + : renderer{renderer} +{ + load(renderer); +} + +WinterTheme::~WinterTheme() +{ + unload(); +} + void WinterTheme::load(Renderer2d* renderer) { renderer->clearColor = Vector4(200, 229, 239, 255).toNormalizedColor(); SnowflakeLoadParameters lp; @@ -11,7 +22,8 @@ void WinterTheme::update(f32 dtSeconds) { spr.update(dtSeconds); } -void WinterTheme::render(Renderer2d* renderer) { +void WinterTheme::render() { + renderer->render(); spr.render(renderer); } diff --git a/themes/src/winter/WinterTheme.hpp b/themes/src/winter/WinterTheme.hpp index 5b8cc95..7e5f2ef 100644 --- a/themes/src/winter/WinterTheme.hpp +++ b/themes/src/winter/WinterTheme.hpp @@ -3,16 +3,22 @@ #include "Snowflake.h" #include "../types.h" +#include "../theme.h" struct Renderer2d; -struct WinterTheme { +struct WinterTheme : public Theme { +public: + WinterTheme(Renderer2d* renderer); + ~WinterTheme(); SnowflakeParticleRenderer spr; void load(Renderer2d* renderer); void update(f32 dtSeconds); - void render(Renderer2d* renderer); + void render(); void unload(); +private: + Renderer2d* renderer; }; -#endif \ No newline at end of file +#endif -- cgit v1.2.1