From 9c677cc32f645fe2ccce8bbcc2d89ee1a6c35868 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Wed, 31 Jan 2024 00:23:44 +1100 Subject: [PATCH] added improvements --- README.md | 1 + src/graphics/camera.cpp | 6 +-- src/graphics/camera.hpp | 2 +- src/graphics/input/focus.cpp | 15 ++++--- src/graphics/input/focus.hpp | 6 +-- src/graphics/input/mouse.cpp | 9 ++-- src/graphics/monitor/core.cpp | 80 +++++++++++++++++---------------- src/graphics/monitor/core.hpp | 4 +- src/graphics/monitor/vessel.cpp | 12 ++++- src/graphics/monitor/vessel.hpp | 2 +- src/graphics/window.cpp | 13 +++--- src/graphics/window.hpp | 4 +- src/main.cpp | 11 +++-- src/reactor/reactor.cpp | 30 ++++++++----- src/reactor/reactor.hpp | 5 ++- src/reactor/rod.cpp | 24 ---------- src/reactor/rod.hpp | 10 +---- src/system.cpp | 2 + src/system.hpp | 2 + 19 files changed, 118 insertions(+), 120 deletions(-) diff --git a/README.md b/README.md index dbf7293..7ce45d2 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ # How to build This is built using CMake. You will also need the required libraries to build. +This project currently only works on Linux. ``` mkdir build diff --git a/src/graphics/camera.cpp b/src/graphics/camera.cpp index f8e2d21..750d637 100644 --- a/src/graphics/camera.cpp +++ b/src/graphics/camera.cpp @@ -47,7 +47,7 @@ glm::vec<3, double> camera::get_pos() return pos; } -void camera::update(const system& sys, double dt) +void camera::update(double dt) { glm::vec<2, double> off(0, 0); double m = 30; @@ -89,8 +89,8 @@ void camera::update(const system& sys, double dt) glm::vec<3, double> normal_last(0); glm::vec<3, double> velocity2; - velocity2 = sys.scene.calc_intersect(pos, velocity * dt, normal_last); - velocity2 = sys.scene.calc_intersect(pos + glm::vec<3, double>(0, 0, -1.5), velocity2, normal_last) / dt; + velocity2 = system::active.scene.calc_intersect(pos, velocity * dt, normal_last); + velocity2 = system::active.scene.calc_intersect(pos + glm::vec<3, double>(0, 0, -1.5), velocity2, normal_last) / dt; pos += velocity2 * dt; on_ground = ((velocity * dt / dt).z != velocity2.z); diff --git a/src/graphics/camera.hpp b/src/graphics/camera.hpp index 50b809a..fb9a1a4 100644 --- a/src/graphics/camera.hpp +++ b/src/graphics/camera.hpp @@ -14,7 +14,7 @@ glm::vec<3, double> get_pos(); void rotate(double pitch, double yaw); void move(double x, double y, double z); -void update(const system& sys, double dt); +void update(double dt); }; diff --git a/src/graphics/input/focus.cpp b/src/graphics/input/focus.cpp index cd0293c..334ab2a 100644 --- a/src/graphics/input/focus.cpp +++ b/src/graphics/input/focus.cpp @@ -55,6 +55,16 @@ void focus::on_charcode(unsigned int c) } } +void focus::update() +{ + triggered = false; + + if(state) + { + state->update(); + } +} + bool focus::is_focused() { return (state != nullptr); @@ -75,8 +85,3 @@ bool focus::is_triggered() return triggered; } -void focus::clear_trigger() -{ - triggered = false; -} - diff --git a/src/graphics/input/focus.hpp b/src/graphics/input/focus.hpp index 1bd4af7..f5ce407 100644 --- a/src/graphics/input/focus.hpp +++ b/src/graphics/input/focus.hpp @@ -13,18 +13,18 @@ struct focus_t virtual void on_mouse_button(int button, int action, int mods) { } virtual void on_cursor_pos(double x, double y) { } virtual void on_charcode(unsigned int c) { } + virtual void update() { } }; -bool is_triggered(); -void clear_trigger(); - void clear(); bool is_focused(); +bool is_triggered(); void set(std::unique_ptr f); void on_keypress(int key, int sc, int action, int mods); void on_mouse_button(int button, int action, int mods); void on_cursor_pos(double x, double y); void on_charcode(unsigned int c); +void update(); }; diff --git a/src/graphics/input/mouse.cpp b/src/graphics/input/mouse.cpp index a850103..2c7059b 100644 --- a/src/graphics/input/mouse.cpp +++ b/src/graphics/input/mouse.cpp @@ -13,14 +13,15 @@ static double xpos = 0, ypos = 0; static void cb_cursor_pos(GLFWwindow* win, double x, double y) { - focus::on_cursor_pos(x, y); - if(focus::is_focused()) { - return; + focus::on_cursor_pos(x - xpos, y - ypos); } - camera::rotate(x - xpos, y - ypos); + else + { + camera::rotate(x - xpos, y - ypos); + } xpos = x; ypos = y; diff --git a/src/graphics/monitor/core.cpp b/src/graphics/monitor/core.cpp index 04c9105..f119f83 100644 --- a/src/graphics/monitor/core.cpp +++ b/src/graphics/monitor/core.cpp @@ -17,37 +17,16 @@ using namespace sim::graphics::monitor; struct core_focus_t : public focus::focus_t { - sim::system* sys; - - core_focus_t(sim::system& sys) + virtual void on_cursor_pos(double x, double y) { - this->sys = &sys; - } - - void on_keypress(int key, int sc, int action, int mods) - { - if(action != GLFW_PRESS) - { - return; - } - - } - - void on_mouse_button(int button, int action, int mods) - { - - } - - void on_cursor_pos(double x, double y) - { - + sim::system::active.reactor->rod_speed -= y * 1e-6; } void set_all(bool state) { - for(int i = 0; i < sys->reactor->rods.size(); i++) + for(int i = 0; i < sim::system::active.reactor->rods.size(); i++) { - sim::reactor::rod* r = sys->reactor->rods[i].get(); + sim::reactor::rod* r = sim::system::active.reactor->rods[i].get(); if(r->should_select() && (r->is_selected() != state)) { @@ -61,28 +40,51 @@ struct core_focus_t : public focus::focus_t //TODO } - void on_charcode(unsigned int c) + virtual void on_charcode(unsigned int c) { + sim::system& sys = sim::system::active; + switch(c) { case 'a': case 'A': - sys->reactor->move_cursor(-1); + sys.reactor->move_cursor(-1); break; case 'd': case 'D': - sys->reactor->move_cursor(1); - break; - case 's': case 'S': - sys->reactor->toggle_selected(); + sys.reactor->move_cursor(1); break; case 'w': case 'W': + sys.reactor->move_cursor(-sim::system::active.reactor->height); + break; + case 's': case 'S': + sys.reactor->move_cursor(sim::system::active.reactor->height); + break; + } + } + + virtual void on_keypress(int key, int sc, int action, int mods) + { + if(action != GLFW_PRESS) + { + return; + } + + switch(key) + { + case GLFW_KEY_Z: + sim::system::active.reactor->toggle_selected(); + break; + case GLFW_KEY_C: toggle_auto(); break; - case 'q': case 'Q': - set_all(false); + case GLFW_KEY_X: + sim::system::active.reactor->rod_speed = 0; break; - case 'e': case 'E': + case GLFW_KEY_Q: set_all(true); break; + case GLFW_KEY_E: + set_all(false); + break; } } }; @@ -112,16 +114,18 @@ void core::init() mesh_click.load_model("../assets/model/", "reactor_core_input.stl"); } -void core::update(sim::system& sys) +void core::update() { if(mesh_click.check_focus(2)) { - focus::set(std::make_unique(sys)); + focus::set(std::make_unique()); } } -void core::render(sim::system& sys) +void core::render() { + sim::system& sys = sim::system::active; + double step = 1 / (sys.vessel->diameter / sys.reactor->cell_width * 0.8); double sx = 0.5 - (sys.reactor->width - 1) * step / 2.0; double sy = 0.5 - (sys.reactor->height - 1) * step / 2.0; @@ -157,7 +161,7 @@ void core::render(sim::system& sys) mesh2.uniform(); mesh2.render(); - if(sys.reactor->cursor == i && r->should_select()) + if(sys.reactor->cursor == i) { mesh2.model_matrix = mat * mat_cursor; mesh2.colour_matrix = arrays::colour({1, 0, 0, 1}); diff --git a/src/graphics/monitor/core.hpp b/src/graphics/monitor/core.hpp index 44ca65f..ccd71a3 100644 --- a/src/graphics/monitor/core.hpp +++ b/src/graphics/monitor/core.hpp @@ -16,8 +16,8 @@ public: core(); void init(); - void update(sim::system& sys); - void render(sim::system& sys); + void update(); + void render(); }; }; diff --git a/src/graphics/monitor/vessel.cpp b/src/graphics/monitor/vessel.cpp index 8c985fe..d443e44 100644 --- a/src/graphics/monitor/vessel.cpp +++ b/src/graphics/monitor/vessel.cpp @@ -41,20 +41,27 @@ void vessel::init() ss << "Reactor Core\n\n"; ss << "Temperature\nMin\nMax\n\n"; ss << "Neutron Flux\nSlow\nFast\n\n"; - ss << "Control Rods\nMin\nMax\n\n"; + ss << "Control Rods\nMin\nMax\nSpeed\n"; rmesh.load_text(ss.str().c_str(), 0.04); mesh1.bind(); mesh1.set(rmesh, GL_STATIC_DRAW); } +static double show(double v, double m) +{ + return std::round(v * m) / m; +} + static double show(double v) { return std::round(v * 1e3) * 1e-3; } -void vessel::update(sim::system& sys) +void vessel::update() { + sim::system& sys = sim::system::active; + std::stringstream ss; sim::graphics::mesh rmesh; @@ -98,6 +105,7 @@ void vessel::update(sim::system& sys) ss << sys.reactor->get_total(sim::reactor::rod::val_t::N_FAST) << " mol\n\n\n"; ss << show( crod_min * 100 ) << " %\n"; ss << show( crod_max * 100 ) << " %\n"; + ss << show( sys.reactor->rod_speed * 100, 1e-5 ) << " %/s\n"; rmesh.load_text(ss.str().c_str(), 0.04); mesh2.bind(); diff --git a/src/graphics/monitor/vessel.hpp b/src/graphics/monitor/vessel.hpp index 016bd08..fb5e3a4 100644 --- a/src/graphics/monitor/vessel.hpp +++ b/src/graphics/monitor/vessel.hpp @@ -15,7 +15,7 @@ public: vessel(); void init(); - void update(sim::system& sys); + void update(); void render(); }; diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index 325815e..9a5c4b5 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -18,7 +18,6 @@ #include "shader.hpp" #include "mesh/font.hpp" #include "locations.hpp" -#include "../system.hpp" #include "monitor/vessel.hpp" #include "monitor/core.hpp" #include "mesh/texture.hpp" @@ -40,7 +39,7 @@ void GLAPIENTRY cb_debug_message(GLenum source, GLenum type, GLuint id, GLenum s } } -void window::create(system& sys) +void window::create() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); @@ -97,6 +96,8 @@ void window::create(system& sys) shader::init_program(); + sim::system& sys = sim::system::active; + sys.scene.load_model("../assets", "scene-baked.glb"); MeshScene.bind(); MeshScene.set(sys.scene, GL_STATIC_DRAW); @@ -116,12 +117,12 @@ void window::create(system& sys) glViewport(0, 0, 800, 600); } -void window::loop(sim::system& sys) +void window::loop() { glfwPollEvents(); - MonitorCore.update(sys); - MonitorVessel.update(sys); + MonitorCore.update(); + MonitorVessel.update(); glm::mat4 mat_projection = glm::perspective(glm::radians(80.0f), resize::get_aspect(), 0.01f, 20.f); glUniformMatrix4fv(shader::gl_projection, 1, false, &mat_projection[0][0]); @@ -137,7 +138,7 @@ void window::loop(sim::system& sys) MeshScene.uniform(); MeshScene.render(); - MonitorCore.render(sys); + MonitorCore.render(); MonitorVessel.render(); glfwSwapBuffers(win); diff --git a/src/graphics/window.hpp b/src/graphics/window.hpp index f9222d8..58c934d 100644 --- a/src/graphics/window.hpp +++ b/src/graphics/window.hpp @@ -9,8 +9,8 @@ namespace sim::graphics::window { bool should_close(); -void create(sim::system& sys); -void loop(sim::system& sys); +void create(); +void loop(); void destroy(); void close(); diff --git a/src/main.cpp b/src/main.cpp index bec1ecd..7665d81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,8 +29,7 @@ unsigned long get_now() int main() { - sim::system sys; - graphics::window::create(sys); + graphics::window::create(); long clock = get_now(); @@ -41,10 +40,10 @@ int main() double dt = (double)passed / 1e6; clock += passed; - sys.update(dt); - graphics::camera::update(sys, dt); - graphics::window::loop(sys); - graphics::focus::clear_trigger(); + sim::system::active.update(dt); + graphics::camera::update(dt); + graphics::window::loop(); + graphics::focus::update(); } graphics::window::destroy(); diff --git a/src/reactor/reactor.cpp b/src/reactor/reactor.cpp index 00def97..ca7410e 100644 --- a/src/reactor/reactor.cpp +++ b/src/reactor/reactor.cpp @@ -9,6 +9,7 @@ using namespace sim::reactor; reactor::reactor(std::unique_ptr* rods, int w, int h, double cw, double ch) : cell_width(cw), cell_height(ch), width(w), height(h), size(w * h) { this->rods = std::vector>(w * h); + this->cursor = w * h; for(int i = 0; i < size; i++) { @@ -55,9 +56,14 @@ void reactor::update(double secs) } update_interactions(rods_lookup, secs / 2); + + if(rod_speed != 0) + { + update_selected(secs); + } } -void reactor::update_selected(int v) +void reactor::update_selected(double dt) { for(int i = 0; i < size; i++) { @@ -65,34 +71,34 @@ void reactor::update_selected(int v) if(r->is_selected()) { - r->update_rod_selected(v); + r->update_selected(rod_speed * dt); } } } int reactor::move_cursor(int d) { - for(int i = 0; i < size; i++) + goto logic; + + while(cursor == size || !rods[cursor]->should_display()) { - cursor = (cursor + d) % size; - +logic: cursor = (cursor + d) % (size + 1); + if(cursor < 0) { - cursor += size; + cursor += size + 1; } - if(rods[cursor]->should_select()) - { - return cursor; - } + if(d > 1) d = 1; + if(d < -1) d = -1; } - return 0; + return cursor; } void reactor::toggle_selected() { - if(rods[cursor]->should_select()) + if(cursor < size && rods[cursor]->should_select()) { rods[cursor]->toggle_selected(); } diff --git a/src/reactor/reactor.hpp b/src/reactor/reactor.hpp index 7ebc894..00c36a3 100644 --- a/src/reactor/reactor.hpp +++ b/src/reactor/reactor.hpp @@ -19,14 +19,14 @@ struct reactor const int size; std::vector> rods; - int cursor = 0; + double rod_speed = 0; + int cursor; reactor(std::unique_ptr* rods, int width, int height, double cell_width, double cell_height); reactor(const reactor& r); reactor(reactor&& r); void update(double secs); - void update_selected(int v); void get_stats(rod::val_t type, double& min, double& max); void get_rod_stats(int type, double& min, double& max); double get_total(rod::val_t type); @@ -37,6 +37,7 @@ private: void update_tile(double secs, int i, int x, int y); void update_interactions(int* rods_lookup, double secs); + void update_selected(double dt); }; } diff --git a/src/reactor/rod.cpp b/src/reactor/rod.cpp index d744e5d..29ae14d 100644 --- a/src/reactor/rod.cpp +++ b/src/reactor/rod.cpp @@ -43,12 +43,6 @@ void rod::interact(rod* o, double secs) } } -double rod::get_speed() const -{ - int m = motion < 0 ? -1 : 1; - return motion == 0 ? 0 : (std::pow(10, std::abs(motion)) * 1e-6 * m); -} - double rod::get_volume() const { auto r = (sim::reactor::reactor*)reactor; @@ -61,23 +55,5 @@ void rod::update_rod(double secs) double m = std::pow(0.5, secs / 879.4); vals[val_t::N_FAST] *= m; vals[val_t::N_SLOW] *= m; - - if(motion != 0 && !is_selected()) - { - motion = 0; - } - - if(motion != 0) - { - update_selected(get_speed() * secs); - } -} - -void rod::update_rod_selected(int m) -{ - motion += m; - - if(motion > 5) motion = 5; - if(motion < -5) motion = -5; } diff --git a/src/reactor/rod.hpp b/src/reactor/rod.hpp index 284da37..db714d7 100644 --- a/src/reactor/rod.hpp +++ b/src/reactor/rod.hpp @@ -35,7 +35,7 @@ public: virtual bool has_sensors(val_t t) const { return false; } virtual bool should_display() const { return false; } virtual bool should_select() const { return false; } - void update_rod_selected(int m); + virtual void update_selected(double a) { } double get_volume() const; constexpr void toggle_selected() { selected = !selected; } @@ -47,11 +47,6 @@ public: o << r.get_name() << "\n"; - if(r.is_selected()) - { - o << "Speed: " << r.get_speed() << "\n"; - } - r.display(o); o << "Heat: " << r.get(val_t::HEAT) << " C\n"; @@ -65,15 +60,12 @@ protected: double vals[VAL_N] = {0}; bool selected = false; - int motion = 0; virtual void display(std::ostream& o) const { }; virtual double get_k(val_t type) const { return 0; } virtual const char* get_name() const { return "Empty"; } - virtual void update_selected(double a) { } void update_rod(double secs); - double get_speed() const; }; } diff --git a/src/system.cpp b/src/system.cpp index 64b5797..e110c26 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -9,6 +9,8 @@ using namespace sim; +sim::system system::active; + system::system() { const char* layout[] = { diff --git a/src/system.hpp b/src/system.hpp index 148ea54..058a91f 100644 --- a/src/system.hpp +++ b/src/system.hpp @@ -14,6 +14,8 @@ namespace sim struct system { + static system active; + std::unique_ptr reactor; std::unique_ptr vessel; std::unique_ptr> valve;