diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-12-13 23:47:30 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-12-13 23:47:30 +0000 |
commit | 1686a01b6ae7467e71eac247078248de4a3b3423 (patch) | |
tree | 53716ce767b1b775dc06f658a41a647bddbbeac1 | |
parent | Move TickDuration to its own files (diff) | |
download | ilt-1686a01b6ae7467e71eac247078248de4a3b3423.tar.bz2 ilt-1686a01b6ae7467e71eac247078248de4a3b3423.tar.xz ilt-1686a01b6ae7467e71eac247078248de4a3b3423.zip |
Refactor to start splitting out UI components
-rw-r--r-- | application/main.cpp | 75 | ||||
-rw-r--r-- | game/network/rail.cpp | 1 | ||||
-rw-r--r-- | game/vehicles/railVehicleClass.cpp | 1 | ||||
-rw-r--r-- | game/vehicles/vehicle.cpp | 2 | ||||
-rw-r--r-- | gfx/camera_controller.h | 8 | ||||
-rw-r--r-- | gfx/followCameraController.h | 3 | ||||
-rw-r--r-- | gfx/gl/glSource.cpp | 2 | ||||
-rw-r--r-- | gfx/gl/glSource.h | 1 | ||||
-rw-r--r-- | gfx/gl/shader.cpp | 3 | ||||
-rw-r--r-- | gfx/gl/shader.h | 2 | ||||
-rw-r--r-- | gfx/gl/uiShader.cpp | 4 | ||||
-rw-r--r-- | gfx/gl/uiShader.h | 3 | ||||
-rw-r--r-- | iwyu.json | 240 | ||||
-rw-r--r-- | lib/jsonParse.ll | 1 | ||||
-rw-r--r-- | ui/gameMainWindow.cpp | 43 | ||||
-rw-r--r-- | ui/gameMainWindow.h | 27 | ||||
-rw-r--r-- | ui/inputHandler.h (renamed from gfx/inputHandler.h) | 2 | ||||
-rw-r--r-- | ui/manualCameraController.cpp (renamed from gfx/manualCameraController.cpp) | 2 | ||||
-rw-r--r-- | ui/manualCameraController.h (renamed from gfx/manualCameraController.h) | 5 | ||||
-rw-r--r-- | ui/window.cpp (renamed from gfx/window.cpp) | 12 | ||||
-rw-r--r-- | ui/window.h (renamed from gfx/window.h) | 13 |
21 files changed, 261 insertions, 189 deletions
diff --git a/application/main.cpp b/application/main.cpp index 07a593b..9992ec0 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -16,22 +16,19 @@ #include <game/vehicles/railVehicleClass.h> #include <game/vehicles/train.h> #include <game/worldobject.h> -#include <gfx/camera_controller.h> -#include <gfx/gl/camera.h> -#include <gfx/gl/shader.h> -#include <gfx/inputHandler.h> -#include <gfx/manualCameraController.h> -#include <gfx/renderable.h> -#include <gfx/window.h> #include <glm/glm.hpp> +#include <glm/gtx/transform.hpp> // IWYU pragma: keep #include <memory> #include <special_members.hpp> +#include <ui/gameMainWindow.h> +#include <ui/inputHandler.h> +#include <ui/window.h> #include <vector> static const int DISPLAY_WIDTH = 1280; static const int DISPLAY_HEIGHT = 1024; -class SDL_Application : public InputHandler, public std::enable_shared_from_this<SDL_Application>, GameState { +class SDL_Application : public GameState { public: SDL_Application() { @@ -46,7 +43,7 @@ public: SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); } - ~SDL_Application() override + ~SDL_Application() { SDL_Quit(); } @@ -54,35 +51,12 @@ public: NO_COPY(SDL_Application); NO_MOVE(SDL_Application); - std::shared_ptr<Train> train; - bool - handleInput(SDL_Event & e) override - { - switch (e.type) { - case SDL_QUIT: - isRunning = false; - return true; - case SDL_KEYUP: - switch (e.key.keysym.scancode) { - case SDL_SCANCODE_G: - train->currentActivity = std::make_unique<Go>(); - break; - case SDL_SCANCODE_I: - train->currentActivity = std::make_unique<Idle>(); - break; - default: - return false; - } - return true; - } - return false; - } - + using Windows = Collection<Window>; int run() { - Collection<Window> windows; - windows.create(DISPLAY_WIDTH, DISPLAY_HEIGHT, "OpenGL"); + Windows windows; + windows.create<GameMainWindow>(DISPLAY_WIDTH, DISPLAY_HEIGHT); world.create<Terrain>(); @@ -109,7 +83,7 @@ public: rl->addLinksBetween(l, s); rl->addLinksBetween(t, u); rl->addLinksBetween(u, m); - train = world.create<Train>(l3); + std::shared_ptr<Train> train = world.create<Train>(l3); auto b47 = std::make_shared<RailVehicleClass>("brush47"); for (int N = 0; N < 6; N++) { train->create<RailVehicle>(b47); @@ -119,49 +93,38 @@ public: train->currentActivity = train->orders.current()->createActivity(); } - Shader shader; - Camera camera({-1250.0F, -1250.0F, 35.0F}, 70.0F, rdiv(DISPLAY_WIDTH, DISPLAY_HEIGHT), 0.1F, 10000.0F); - shader.setView(camera.GetViewProjection()); - shader.setUniform("lightDirection", glm::normalize(glm::vec3 {1, 0, -1})); - shader.setUniform("lightColor", {.6, .6, .6}); - shader.setUniform("ambientColor", {0.5, 0.5, 0.5}); - auto t_start = std::chrono::high_resolution_clock::now(); - - inputStack.objects.push_back(shared_from_this()); - inputStack.objects.insert( - inputStack.objects.begin(), world.create<ManualCameraController>(glm::vec2 {-1150, -1150})); - while (isRunning) { - processInputs(); + processInputs(windows); const auto t_end = std::chrono::high_resolution_clock::now(); const auto t_passed = std::chrono::duration_cast<TickDuration>(t_end - t_start); world.apply(&WorldObject::tick, t_passed); - world.apply<CameraController>(&CameraController::updateCamera, &camera); - shader.setView(camera.GetViewProjection()); + windows.apply(&Window::tick, t_passed); windows.apply(&Window::Clear, 0.0F, 0.0F, 0.0F, 1.0F); - world.apply<Renderable>(&Renderable::render, shader); + windows.apply(&Window::Refresh, this); windows.apply(&Window::SwapBuffers); t_start = t_end; } - inputStack.removeAll(); return 0; } private: void - processInputs() + processInputs(const Windows & windows) { for (SDL_Event e; SDL_PollEvent(&e);) { - inputStack.applyOne(&InputHandler::handleInput, e); + if (e.type == SDL_QUIT) { + isRunning = false; + return; + } + windows.applyOne(&Window::handleInput, e); } } bool isRunning {true}; - Collection<InputHandler> inputStack; }; int diff --git a/game/network/rail.cpp b/game/network/rail.cpp index d7f95a8..73ab3bf 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -2,6 +2,7 @@ #include "network.h" #include <GL/glew.h> #include <array> +#include <cmath> #include <collection.hpp> #include <cstddef> #include <game/network/link.h> diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 8fd504f..689e1e6 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -6,6 +6,7 @@ #include <algorithm> #include <array> #include <cache.h> +#include <cmath> #include <cstddef> #include <filesystem> #include <glm/glm.hpp> diff --git a/game/vehicles/vehicle.cpp b/game/vehicles/vehicle.cpp index 8730d6d..53450f6 100644 --- a/game/vehicles/vehicle.cpp +++ b/game/vehicles/vehicle.cpp @@ -7,7 +7,7 @@ #include "game/vehicles/linkHistory.h" #include <algorithm> #include <array> -#include <game/worldobject.h> +#include <cmath> #include <glm/glm.hpp> #include <iterator> #include <location.hpp> diff --git a/gfx/camera_controller.h b/gfx/camera_controller.h index 926c93b..e6fc16d 100644 --- a/gfx/camera_controller.h +++ b/gfx/camera_controller.h @@ -1,12 +1,16 @@ #ifndef CAMERA_CONTROLLER_H #define CAMERA_CONTROLLER_H -#include <game/worldobject.h> +#include <special_members.hpp> class Camera; -class CameraController : public WorldObject { +class CameraController { public: + CameraController() = default; + virtual ~CameraController() = default; + DEFAULT_MOVE_COPY(CameraController); + virtual void updateCamera(Camera *) const = 0; }; diff --git a/gfx/followCameraController.h b/gfx/followCameraController.h index ef2000d..cfe70c2 100644 --- a/gfx/followCameraController.h +++ b/gfx/followCameraController.h @@ -1,7 +1,6 @@ #ifndef FOLLOW_CAMERA_CONTROLLER_H #define FOLLOW_CAMERA_CONTROLLER_H -#include "game/worldobject.h" #include <game/vehicles/vehicle.h> #include <gfx/camera_controller.h> @@ -12,8 +11,6 @@ public: enum class Mode { Pan, Ride, ISO }; explicit FollowCameraController(VehicleWPtr, Mode = Mode::Pan); - void tick(TickDuration) override { } - void updateCamera(Camera * camera) const override; private: diff --git a/gfx/gl/glSource.cpp b/gfx/gl/glSource.cpp index 13686ae..49773d0 100644 --- a/gfx/gl/glSource.cpp +++ b/gfx/gl/glSource.cpp @@ -1,5 +1,7 @@ #include "glSource.h" #include <array> +#include <stdexcept> +#include <string> GLsource::ShaderRef GLsource::compile() const diff --git a/gfx/gl/glSource.h b/gfx/gl/glSource.h index 53c221b..e44c5a2 100644 --- a/gfx/gl/glSource.h +++ b/gfx/gl/glSource.h @@ -3,6 +3,7 @@ #include <GL/glew.h> #include <glRef.hpp> +#include <string_view> struct GLsource { using ShaderRef = glRef<GLuint, __glewCreateShader, __glewDeleteShader>; diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 03e7899..a196e07 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -2,6 +2,7 @@ #include "gfx/gl/glSource.h"
#include <array>
#include <cstddef>
+#include <gfx/gl/programHandle.h>
#include <gfx/gl/shaders/fs-basicShader.h>
#include <gfx/gl/shaders/fs-landmassShader.h>
#include <gfx/gl/shaders/fs-waterShader.h>
@@ -13,8 +14,6 @@ #include <glm/gtx/transform.hpp>
#include <location.hpp>
#include <maths.h>
-#include <stdexcept>
-#include <string>
Shader::ProgramHandle::ProgramHandle(GLuint vs, GLuint fs) : ProgramHandleBase {vs, fs}
{
diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index 249b2f4..7df7a07 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -4,9 +4,7 @@ #include "programHandle.h"
#include <GL/glew.h>
#include <array>
-#include <glRef.hpp>
#include <glm/glm.hpp>
-#include <string_view>
class Location;
diff --git a/gfx/gl/uiShader.cpp b/gfx/gl/uiShader.cpp index 2fbb0d4..b3fee14 100644 --- a/gfx/gl/uiShader.cpp +++ b/gfx/gl/uiShader.cpp @@ -1,8 +1,10 @@ #include "uiShader.h" +#include <gfx/gl/glSource.h> +#include <gfx/gl/programHandle.h> #include <gfx/gl/shaders/fs-uiShader.h> #include <gfx/gl/shaders/vs-uiShader.h> +#include <glm/glm.hpp> #include <glm/gtc/type_ptr.hpp> -#include <glm/gtx/transform.hpp> UIShader::UIShader(size_t width, size_t height) : program {uiShader_vs.compile(), uiShader_fs.compile()} { diff --git a/gfx/gl/uiShader.h b/gfx/gl/uiShader.h index ff0e96d..b251136 100644 --- a/gfx/gl/uiShader.h +++ b/gfx/gl/uiShader.h @@ -2,7 +2,8 @@ #define UISHADER_H #include "programHandle.h" -#include <cstdint> +#include <GL/glew.h> +#include <cstddef> class UIShader { public: @@ -1,114 +1,130 @@ [ - { - "include": [ - "<ext/alloc_traits.h>", - "private", - "<memory>", - "public" - ] - }, - { - "include": [ - "@\"SDL.*.h\"", - "private", - "<SDL2/SDL.h>", - "public" - ] - }, - { - "include": [ - "@<glm/detail/.*>", - "private", - "<glm/glm.hpp>", - "public" - ] - }, - { - "include": [ - "@<glm/ext/.*>", - "private", - "<glm/glm.hpp>", - "public" - ] - }, - { - "include": [ - "@<glm/gtc/constants.inl>", - "private", - "<glm/gtc/constants.hpp>", - "public" - ] - }, - { - "include": [ - "@<glm/gtx/vector_angle.inl>", - "private", - "<glm/gtx/vector_angle.hpp>", - "public" - ] - }, - { - "include": [ - "@<glm/gtx/rotate_vector.inl>", - "private", - "<glm/gtx/rotate_vector.hpp>", - "public" - ] - }, - { - "include": [ - "@<glm/gtx/transform.inl>", - "private", - "<glm/gtx/transform.hpp>", - "public" - ] - }, - { - "include": [ - "<boost/test/unit_test_suite.hpp>", - "private", - "<boost/test/unit_test.hpp>", - "public" - ] - }, - { - "include": [ - "@<boost/test/utils/.*>", - "private", - "<boost/test/unit_test.hpp>", - "public" - ] - }, - { - "include": [ - "@<boost/test/data/.*>", - "private", - "<boost/test/data/test_case.hpp>", - "public" - ] - }, - { - "include": [ - "@<boost/test/tools/.*>", - "private", - "<boost/test/unit_test.hpp>", - "public" - ] - }, - { - "include": [ - "@<boost/preprocessor/.*>", - "private", - "<boost/test/unit_test.hpp>", - "public" - ] - }, - { - "symbol": [ - "std::__atomic_wait", - "private", - "<mutex>", - "public" - ] - } + { + "include": [ + "<ext/alloc_traits.h>", + "private", + "<memory>", + "public" + ] + }, + { + "include": [ + "@\"SDL.*.h\"", + "private", + "<SDL2/SDL.h>", + "public" + ] + }, + { + "include": [ + "@<glm/detail/.*>", + "private", + "<glm/glm.hpp>", + "public" + ] + }, + { + "include": [ + "@<glm/ext/.*>", + "private", + "<glm/glm.hpp>", + "public" + ] + }, + { + "include": [ + "@<glm/gtc/constants.inl>", + "private", + "<glm/gtc/constants.hpp>", + "public" + ] + }, + { + "include": [ + "@<glm/gtx/vector_angle.inl>", + "private", + "<glm/gtx/vector_angle.hpp>", + "public" + ] + }, + { + "include": [ + "@<glm/gtx/rotate_vector.inl>", + "private", + "<glm/gtx/rotate_vector.hpp>", + "public" + ] + }, + { + "include": [ + "@<glm/gtx/transform.inl>", + "private", + "<glm/gtx/transform.hpp>", + "public" + ] + }, + { + "include": [ + "@<glm/gtc/type_ptr.inl>", + "private", + "<glm/gtc/type_ptr.hpp>", + "public" + ] + }, + { + "include": [ + "<boost/test/unit_test_suite.hpp>", + "private", + "<boost/test/unit_test.hpp>", + "public" + ] + }, + { + "include": [ + "@<boost/test/utils/.*>", + "private", + "<boost/test/unit_test.hpp>", + "public" + ] + }, + { + "include": [ + "@<boost/test/data/.*>", + "private", + "<boost/test/data/test_case.hpp>", + "public" + ] + }, + { + "include": [ + "@<boost/test/tools/.*>", + "private", + "<boost/test/unit_test.hpp>", + "public" + ] + }, + { + "include": [ + "@<boost/preprocessor/.*>", + "private", + "<boost/test/unit_test.hpp>", + "public" + ] + }, + { + "symbol": [ + "std::abs", + "private", + "<cmath>", + "public" + ] + }, + { + "symbol": [ + "std::__atomic_wait", + "private", + "<mutex>", + "public" + ] + } ] diff --git a/lib/jsonParse.ll b/lib/jsonParse.ll index c646044..8029830 100644 --- a/lib/jsonParse.ll +++ b/lib/jsonParse.ll @@ -151,4 +151,5 @@ text [^\\\"]* LexerError("Unexpected input"); // Make iwyu think unistd.h is required [[maybe_unused]]static constexpr auto x=getpid; + [[maybe_unused]]static constexpr auto y=printf; } diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp new file mode 100644 index 0000000..4421098 --- /dev/null +++ b/ui/gameMainWindow.cpp @@ -0,0 +1,43 @@ +#include "gameMainWindow.h" +#include "collection.hpp" +#include "gfx/camera_controller.h" +#include "manualCameraController.h" +#include "ui/window.h" +#include <GL/glew.h> +#include <SDL2/SDL.h> +#include <game/gamestate.h> +#include <game/worldobject.h> // IWYU pragma: keep +#include <gfx/renderable.h> +#include <glm/glm.hpp> +#include <maths.h> +#include <memory> + +GameMainWindow::GameMainWindow(size_t w, size_t h) : + Window {static_cast<int>(w), static_cast<int>(h), "I Like Trains"}, uiShader {w, h}, camera {{-1250.0F, -1250.0F, + 35.0F}, + 70.0F, rdiv(w, h), + 0.1F, 10000.0F} +{ + inputStack.create<ManualCameraController>(glm::vec2 {-1150, -1150}); + + shader.setUniform("lightDirection", glm::normalize(glm::vec3 {1, 0, -1})); + shader.setUniform("lightColor", {.6, .6, .6}); + shader.setUniform("ambientColor", {0.5, 0.5, 0.5}); +} + +void +GameMainWindow::tick(TickDuration) +{ + inputStack.apply<CameraController>(&CameraController::updateCamera, &camera); + shader.setView(camera.GetViewProjection()); +} + +void +GameMainWindow::Refresh(const GameState * gameState) const +{ + SDL_GL_MakeCurrent(m_window, m_glContext); + glEnable(GL_DEPTH_TEST); + gameState->world.apply<Renderable>(&Renderable::render, shader); + glDisable(GL_DEPTH_TEST); + // do ui bits +} diff --git a/ui/gameMainWindow.h b/ui/gameMainWindow.h new file mode 100644 index 0000000..affb389 --- /dev/null +++ b/ui/gameMainWindow.h @@ -0,0 +1,27 @@ +#ifndef UI_GAMEMAINWINDOW_H +#define UI_GAMEMAINWINDOW_H + +#include "chronology.hpp" +#include "gfx/gl/camera.h" +#include "gfx/gl/shader.h" +#include "gfx/gl/uiShader.h" +#include "window.h" +#include <cstddef> + +class GameState; + +class GameMainWindow : public Window { +public: + GameMainWindow(size_t w, size_t h); + + void tick(TickDuration) override; + + void Refresh(const GameState * gameState) const override; + +private: + UIShader uiShader; + Shader shader; + Camera camera; +}; + +#endif diff --git a/gfx/inputHandler.h b/ui/inputHandler.h index 5b426c7..aac323b 100644 --- a/gfx/inputHandler.h +++ b/ui/inputHandler.h @@ -12,7 +12,7 @@ public: DEFAULT_MOVE_COPY(InputHandler); - virtual bool handleInput(SDL_Event &) = 0; + virtual bool handleInput(const SDL_Event &) = 0; }; #endif diff --git a/gfx/manualCameraController.cpp b/ui/manualCameraController.cpp index cacf6ac..1d3a067 100644 --- a/gfx/manualCameraController.cpp +++ b/ui/manualCameraController.cpp @@ -5,7 +5,7 @@ #include <maths.h> bool -ManualCameraController::handleInput(SDL_Event & e) +ManualCameraController::handleInput(const SDL_Event & e) { switch (e.type) { case SDL_KEYDOWN: diff --git a/gfx/manualCameraController.h b/ui/manualCameraController.h index f2141a4..ceff9ac 100644 --- a/gfx/manualCameraController.h +++ b/ui/manualCameraController.h @@ -1,7 +1,6 @@ #ifndef MANUAL_CAMERA_CONTROLLER_H #define MANUAL_CAMERA_CONTROLLER_H -#include "game/worldobject.h" #include "inputHandler.h" #include <SDL2/SDL.h> #include <gfx/camera_controller.h> @@ -14,9 +13,7 @@ class ManualCameraController : public CameraController, public InputHandler { public: explicit ManualCameraController(glm::vec2 f) : focus {f} { } - bool handleInput(SDL_Event & e) override; - - void tick(TickDuration) override { } + bool handleInput(const SDL_Event & e) override; void updateCamera(Camera * camera) const override; diff --git a/gfx/window.cpp b/ui/window.cpp index dd300a7..c451367 100644 --- a/gfx/window.cpp +++ b/ui/window.cpp @@ -1,4 +1,5 @@ #include "window.h"
+#include "ui/inputHandler.h"
#include <GL/glew.h>
#include <stdexcept>
@@ -11,7 +12,6 @@ Window::Window(int width, int height, const std::string & title) : throw std::runtime_error {"Glew failed to initialize!"};
}
- glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
@@ -30,3 +30,13 @@ Window::SwapBuffers() const {
SDL_GL_SwapWindow(m_window);
}
+
+bool
+Window::handleInput(const SDL_Event & e)
+{
+ if (SDL_GetWindowID(m_window) == e.window.windowID) {
+ inputStack.applyOne(&InputHandler::handleInput, e);
+ return true;
+ }
+ return false;
+}
diff --git a/gfx/window.h b/ui/window.h index 252ccaa..f6279e2 100644 --- a/gfx/window.h +++ b/ui/window.h @@ -1,29 +1,38 @@ #ifndef DISPLAY_INCLUDED_H
#define DISPLAY_INCLUDED_H
+#include "chronology.hpp"
+#include "collection.hpp"
+#include "inputHandler.h" // IWYU pragma: keep
#include "ptr.hpp"
#include <SDL2/SDL.h>
#include <special_members.hpp>
#include <string>
#include <type_traits>
+class GameState;
+
class Window {
public:
Window(int width, int height, const std::string & title);
- ~Window() = default;
+ virtual ~Window() = default;
NO_COPY(Window);
NO_MOVE(Window);
void Clear(float r, float g, float b, float a) const;
void SwapBuffers() const;
+ virtual void tick(TickDuration elapsed) = 0;
+ virtual void Refresh(const GameState *) const = 0;
+ bool handleInput(const SDL_Event & e);
-private:
+protected:
using GL_Context = std::remove_pointer_t<SDL_GLContext>;
using SDL_WindowPtr = wrapped_ptrt<SDL_Window, SDL_CreateWindow, SDL_DestroyWindow>;
using SDL_GLContextPtr = wrapped_ptrt<GL_Context, SDL_GL_CreateContext, SDL_GL_DeleteContext>;
SDL_WindowPtr m_window;
SDL_GLContextPtr m_glContext;
+ Collection<InputHandler> inputStack;
};
#endif
|