aboutsummaryrefslogtreecommitdiff
path: root/Simulation/SimBase_Execute.cpp
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2024-02-29 19:27:35 +0100
committerPaul Oliver <contact@pauloliver.dev>2024-02-29 19:27:49 +0100
commit17909d029c6a8872b2fddf4e171d7925bbbe9c5c (patch)
treecbb08af84cd68d24acc362d593a2048b0fa79689 /Simulation/SimBase_Execute.cpp
Initial commitHEADmaster
Diffstat (limited to 'Simulation/SimBase_Execute.cpp')
-rw-r--r--Simulation/SimBase_Execute.cpp152
1 files changed, 152 insertions, 0 deletions
diff --git a/Simulation/SimBase_Execute.cpp b/Simulation/SimBase_Execute.cpp
new file mode 100644
index 0000000..eb3166a
--- /dev/null
+++ b/Simulation/SimBase_Execute.cpp
@@ -0,0 +1,152 @@
+#include "SimBase.hpp"
+
+void SimBase::execute()
+{
+ while (window.isOpen())
+ {
+ update();
+
+ sf::Event event;
+ while (window.pollEvent(event))
+ {
+ if (event.type == sf::Event::Closed)
+ {
+ window.close();
+ }
+
+ if (event.type == sf::Event::Resized)
+ {
+ camera.onResize();
+ }
+
+ if (event.type == sf::Event::MouseButtonPressed)
+ {
+ if (event.mouseButton.button == sf::Mouse::Button::Left)
+ {
+ camera.onClick();
+ }
+ }
+
+ if (event.type == sf::Event::MouseWheelMoved)
+ {
+ sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
+ float f1 = 2.f / 10.f;
+ float f2 = 1.f - f1;
+
+ if (event.mouseWheel.delta > 0)
+ {
+ sf::Vector2f target = camera.getCrds() * f2 + mousePos * f1;
+ camera.shift(target, 1);
+ }
+ else
+ {
+ mousePos = camera.getCrds() * 2.f - mousePos;
+ sf::Vector2f target = camera.getCrds() * f2 + mousePos * f1;
+ camera.shift(target, -1);
+ }
+ }
+
+ if (event.type == sf::Event::KeyPressed)
+ {
+ if (event.key.code == sf::Keyboard::Escape)
+ {
+ window.close();
+ }
+
+ if (event.key.code == sf::Keyboard::Space)
+ {
+ paused = !paused;
+ text.simState.setString("Sim. state: " + std::string(paused ? "PAUSED" : "RUNNING"));
+ }
+
+ if (event.key.code == sf::Keyboard::G)
+ {
+ gfx = !gfx;
+ text.gfx.setString("Graphics: " + std::string(gfx ? "ON" : "OFF"));
+ }
+
+ if (event.key.code == sf::Keyboard::T)
+ {
+ displayText = !displayText;
+ }
+
+ if (event.key.code == sf::Keyboard::V)
+ {
+ vSync = !vSync;
+ window.setVerticalSyncEnabled(vSync);
+ text.vSync.setString("V. Sync: " + std::string(vSync ? "ON" : "OFF"));
+ }
+
+ if (event.key.code == sf::Keyboard::Z)
+ {
+ camera.shift(sf::Vector2f(), -2);
+ }
+
+ if (event.key.code == sf::Keyboard::F11)
+ {
+ sf::ContextSettings ctx = window.getSettings();
+ std::string title = "Neural Guppies - 0.1 beta";
+
+ if (fullscreen)
+ {
+ fullscreen = false;
+ window.create(sf::VideoMode(prms.WIN_WIDTH, prms.WIN_HEIGHT), title, sf::Style::Default, ctx);
+ sf::Image icon;
+ icon.loadFromFile("gfx/icon.png");
+ window.setIcon(32, 32, icon.getPixelsPtr());
+ }
+ else
+ {
+ fullscreen = true;
+ window.create(sf::VideoMode::getDesktopMode(), title, sf::Style::Fullscreen, ctx);
+ }
+
+ window.setVerticalSyncEnabled(vSync);
+ camera.onResize();
+ }
+
+ if (event.key.code == sf::Keyboard::I)
+ {
+ camera.shift(camera.getCrds(), 1, false);
+ }
+
+ if (event.key.code == sf::Keyboard::O)
+ {
+ camera.shift(camera.getCrds(), -1, false);
+ }
+
+ if ( event.key.code == sf::Keyboard::W ||
+ event.key.code == sf::Keyboard::A ||
+ event.key.code == sf::Keyboard::S ||
+ event.key.code == sf::Keyboard::D )
+ {
+ float shift = 30.f * camera.getZoom();
+ sf::Vector2f shiftVec;
+
+ if (event.key.code == sf::Keyboard::W)
+ {
+ shiftVec.y = -shift;
+ }
+ else if (event.key.code == sf::Keyboard::A)
+ {
+ shiftVec.x = -shift;
+ }
+ else if (event.key.code == sf::Keyboard::S)
+ {
+ shiftVec.y = shift;
+ }
+ else if (event.key.code == sf::Keyboard::D)
+ {
+ shiftVec.x = shift;
+ }
+
+ camera.shift(camera.getCrds() + shiftVec);
+ }
+ }
+ }
+
+ draw();
+ }
+
+ //onClose();
+}