summaryrefslogtreecommitdiff
path: root/themes/src
diff options
context:
space:
mode:
authorMatthew Kosarek <matthew@matthewkosarek.xyz>2023-09-27 08:03:04 -0400
committerMatthew Kosarek <matthew@matthewkosarek.xyz>2023-09-27 08:03:04 -0400
commit1da356d8a64c45c99871aca156ee9fdd3e10ec15 (patch)
tree00438481bc972c9606995af794be09189833510a /themes/src
parentec0b1d450a0f6219b3b0d352cd6625ae05f62618 (diff)
Simplification of the Theme system with a more OO model
Diffstat (limited to 'themes/src')
-rw-r--r--themes/src/autumn/AutumnTheme.cpp14
-rw-r--r--themes/src/autumn/AutumnTheme.hpp10
-rw-r--r--themes/src/main.cpp113
-rw-r--r--themes/src/spring/SpringTheme.cpp10
-rw-r--r--themes/src/spring/SpringTheme.hpp8
-rw-r--r--themes/src/summer/SummerTheme.cpp14
-rw-r--r--themes/src/summer/SummerTheme.h10
-rw-r--r--themes/src/theme.h22
-rw-r--r--themes/src/winter/WinterTheme.cpp14
-rw-r--r--themes/src/winter/WinterTheme.hpp12
10 files changed, 129 insertions, 98 deletions
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 <memory>
#include <vector>
@@ -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 <cstdio>
#include <emscripten/fetch.h>
-
-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 <vector>
+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 <vector>
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