Browse Source

core update

Kajetan Johannes Hammerle 2 years ago
parent
commit
6ffac4818d

+ 13 - 13
client/Game.cpp

@@ -2,13 +2,13 @@
 #include "client/GameClient.h"
 #include "client/World.h"
 #include "client/gui/StartGUI.h"
+#include "client/input/Controller.h"
 #include "client/rendering/Engine.h"
 #include "common/network/Packets.h"
 #include "common/network/toserver/PlayerUpdatePacket.h"
 #include "utils/Logger.h"
 #include "utils/Utils.h"
 
-Controller Game::controller;
 Entity Game::player;
 
 typedef void (*State)();
@@ -33,16 +33,16 @@ static void tickConnectedState() {
 
     constexpr float rotationSpeed = 4.0f;
     Vector3 force;
-    if(Game::controller.down.isDown()) {
+    if(Controller::isDown(Controller::down)) {
         force += back;
     }
-    if(Game::controller.up.isDown()) {
+    if(Controller::isDown(Controller::up)) {
         force -= back;
     }
-    if(Game::controller.left.isDown()) {
+    if(Controller::isDown(Controller::left)) {
         force -= right;
     }
-    if(Game::controller.right.isDown()) {
+    if(Controller::isDown(Controller::right)) {
         force += right;
     }
     if(force.squareLength() > 0.0f) {
@@ -50,19 +50,19 @@ static void tickConnectedState() {
     }
     Game::player.addForce(force * Game::player.speed);
 
-    if(Game::controller.jump.isDown() && Game::player.isOnGround()) {
+    if(Controller::isDown(Controller::jump) && Game::player.isOnGround()) {
         Game::player.jump();
     }
-    if(Game::controller.camLeft.isDown()) {
+    if(Controller::isDown(Controller::camLeft)) {
         Game::player.addLengthAngle(-rotationSpeed);
     }
-    if(Game::controller.camRight.isDown()) {
+    if(Controller::isDown(Controller::camRight)) {
         Game::player.addLengthAngle(rotationSpeed);
     }
-    if(Game::controller.camUp.isDown()) {
+    if(Controller::isDown(Controller::camUp)) {
         Game::player.addWidthAngle(-rotationSpeed * 0.5f);
     }
-    if(Game::controller.camDown.isDown()) {
+    if(Controller::isDown(Controller::camDown)) {
         Game::player.addWidthAngle(rotationSpeed * 0.5f);
     }
 
@@ -99,7 +99,7 @@ bool Game::init() {
     if(World::init()) {
         return true;
     }
-    controller.init();
+    Controller::init();
     tickState = tickConnectState;
     renderState = renderConnectState;
 
@@ -123,9 +123,9 @@ void Game::renderOverlay() {
     Engine::matrix.identity().scale(2.0f).update();
     StringBuffer<100> s;
     s.append("FPS: &074")
-        .append(Engine::getFrameClock().getUpdatesPerSecond())
+        .append(Window::getFramesPerSecond())
         .append(" &999TPS: &722")
-        .append(Engine::getTickClock().getUpdatesPerSecond());
+        .append(Window::getTicksPerSecond());
     Engine::renderer.renderString(Vector2(10.0f, 10.0f), s);
     s.clear().append("a = ").append(player.acceleration);
     Engine::renderer.renderString(Vector2(10.0f, 20.0f), s);

+ 0 - 2
client/Game.h

@@ -1,12 +1,10 @@
 #ifndef GAME_H
 #define GAME_H
 
-#include "client/input/Controller.h"
 #include "common/entities/Entity.h"
 #include "common/network/toclient/EntityUpdatePacket.h"
 
 namespace Game {
-    extern Controller controller;
     extern Entity player;
 
     bool init();

+ 26 - 8
client/gui/BaseGUI.cpp

@@ -1,6 +1,8 @@
 #include "client/gui/BaseGUI.h"
 #include "client/Game.h"
+#include "client/input/Controller.h"
 #include "client/rendering/Engine.h"
+#include "rendering/Window.h"
 
 static const Color4 INPUT_BACKGROUND(0x00, 0x00, 0x00, 0xA0);
 static const Color4 INPUT_BACKGROUND_2(0x00, 0x00, 0x00, 0x80);
@@ -10,19 +12,32 @@ BaseGUI::Base::Base() : hovered(false), pressed(false) {
 }
 
 void BaseGUI::tickBase(Base& b) {
-    b.hovered = isIn(b.pos, b.size, Game::controller.getMouse() / scale);
-    b.pressed = b.hovered && Game::controller.leftClick.wasReleased();
+    b.hovered =
+        isIn(b.pos, b.size, Window::Controls::getMousePosition() / scale);
+    b.pressed = b.hovered && Controller::wasReleased(Controller::leftClick);
 }
 
 void BaseGUI::tick() {
+    if(Controller::wasReleased(Controller::leftClick)) {
+        activeInput = -1;
+    }
     for(Label& label : labels) {
         tickBase(label.base);
     }
+    Window::Input::disable();
+    int i = 0;
     for(Input& input : inputs) {
         tickBase(input.base);
         if(input.base.pressed) {
-            Engine::setTextInput(&input.text);
+            Window::Input::fill(input.text);
+            activeInput = i;
+        }
+        if(i == activeInput) {
+            Window::Input::enable();
+            input.text.clear();
+            Window::Input::toString(input.text);
         }
+        i++;
     }
     for(Button& button : buttons) {
         tickBase(button.base);
@@ -30,7 +45,7 @@ void BaseGUI::tick() {
 }
 
 void BaseGUI::updateScale() {
-    const Size& size = Engine::getSize();
+    const Size& size = Window::getSize();
     scale = static_cast<int>(
         std::min(size.width / FIXED_SIZE[0], size.height / FIXED_SIZE[1]));
     scale = scale < 1.0f ? 1.0f : scale;
@@ -51,17 +66,20 @@ void BaseGUI::renderLabels() {
 }
 
 void BaseGUI::renderInputs() {
+    int index = 0;
     for(Input& i : inputs) {
-        i.text.setLimit(Engine::renderer.charsInSpace(i.base.size[0] - 20.0f));
+        Window::Input::setLimit(
+            Engine::renderer.charsInSpace(i.base.size[0] - 20.0f));
         renderBase(i.base);
         StringBuffer<256> text(i.text);
         Vector2 pos = renderCenteredString(i.base, text);
-        if(Engine::isActiveTextInput(&i.text)) {
-            Vector2 c =
-                Engine::renderer.getStringSize(text, i.text.getCursor());
+        if(activeInput == index) {
+            Vector2 c = Engine::renderer.getStringSize(
+                text, Window::Input::getCursor());
             renderString(Vector2(pos[0] + c[0], pos[1] + 2.0f), "&292_");
             renderString(Vector2(pos[0] + c[0], pos[1] + 3.0f), "&292_");
         }
+        index++;
     }
 }
 

+ 2 - 2
client/gui/BaseGUI.h

@@ -1,7 +1,6 @@
 #ifndef BASE_GUI_H
 #define BASE_GUI_H
 
-#include "input/TextInput.h"
 #include "math/Vector.h"
 #include "utils/List.h"
 #include "utils/Size.h"
@@ -28,9 +27,10 @@ struct BaseGUI final {
 
     struct Input {
         Base base;
-        TextInput text;
+        StringBuffer<50> text;
     };
     List<Input> inputs;
+    int activeInput = -1;
 
     struct Button {
         Base base;

+ 1 - 2
client/gui/StartGUI.cpp

@@ -4,8 +4,7 @@
 StartGUI::StartGUI()
     : info(base.addLabel("Connect to server ...")), address(base.addInput()),
       connect(base.addButton("Connect ...")) {
-    address.text.setActive(true);
-    address.text.fill("127.0.0.1");
+    address.text.clear().append("127.0.0.1");
 }
 
 void StartGUI::tick() {

+ 42 - 40
client/input/Controller.cpp

@@ -1,51 +1,53 @@
+#include "GLFW/glfw3.h"
+
 #include "client/input/Controller.h"
 #include "client/rendering/Engine.h"
 
-Controller::Controller()
-    : left(GLFW_KEY_A, "Left"), right(GLFW_KEY_D, "Right"), up(GLFW_KEY_W, "X"),
-      down(GLFW_KEY_S, "Y"), jump(GLFW_KEY_SPACE, "L"),
-      sneak(GLFW_KEY_LEFT_SHIFT, "R"), camLeft(GLFW_KEY_LEFT, "Start"),
-      camRight(GLFW_KEY_RIGHT, "Select"), camUp(GLFW_KEY_UP, "Left"),
-      camDown(GLFW_KEY_DOWN, "Right"),
-      leftClick(GLFW_MOUSE_BUTTON_LEFT, "Left Click"),
-      rightClick(GLFW_MOUSE_BUTTON_RIGHT, "Right Click") {
-}
+Window::Controls::ButtonId Controller::left = 0;
+Window::Controls::ButtonId Controller::right = 0;
+Window::Controls::ButtonId Controller::up = 0;
+Window::Controls::ButtonId Controller::down = 0;
+Window::Controls::ButtonId Controller::jump = 0;
+Window::Controls::ButtonId Controller::sneak = 0;
+Window::Controls::ButtonId Controller::camLeft = 0;
+Window::Controls::ButtonId Controller::camRight = 0;
+Window::Controls::ButtonId Controller::camUp = 0;
+Window::Controls::ButtonId Controller::camDown = 0;
+Window::Controls::ButtonId Controller::leftClick = 0;
+Window::Controls::ButtonId Controller::rightClick = 0;
 
 void Controller::init() {
-    Buttons& b = Engine::getButtons();
-    b.add(left);
-    b.add(right);
-    b.add(up);
-    b.add(down);
-    b.add(jump);
-    b.add(sneak);
-    b.add(camLeft);
-    b.add(camRight);
-    b.add(camUp);
-    b.add(camDown);
-
-    b.addMouse(leftClick);
-    b.addMouse(rightClick);
-
-    b.mapGamepadAxis(left, -0.5f, GLFW_GAMEPAD_AXIS_LEFT_X);
-    b.mapGamepadAxis(right, 0.5f, GLFW_GAMEPAD_AXIS_LEFT_X);
-    b.mapGamepadAxis(up, -0.5f, GLFW_GAMEPAD_AXIS_LEFT_Y);
-    b.mapGamepadAxis(down, 0.5f, GLFW_GAMEPAD_AXIS_LEFT_Y);
+    left = Window::Controls::add("Left");
+    Window::Controls::bindKey(left, GLFW_KEY_A);
+    right = Window::Controls::add("Right");
+    Window::Controls::bindKey(right, GLFW_KEY_D);
+    up = Window::Controls::add("Up");
+    Window::Controls::bindKey(up, GLFW_KEY_W);
+    down = Window::Controls::add("Down");
+    Window::Controls::bindKey(down, GLFW_KEY_S);
+    jump = Window::Controls::add("Jump");
+    Window::Controls::bindKey(jump, GLFW_KEY_SPACE);
+    sneak = Window::Controls::add("Sneak");
+    Window::Controls::bindKey(sneak, GLFW_KEY_LEFT_SHIFT);
+    camLeft = Window::Controls::add("Camera Left");
+    Window::Controls::bindKey(camLeft, GLFW_KEY_LEFT);
+    camRight = Window::Controls::add("Camera Right");
+    Window::Controls::bindKey(camRight, GLFW_KEY_RIGHT);
+    camUp = Window::Controls::add("Camera Up");
+    Window::Controls::bindKey(camUp, GLFW_KEY_UP);
+    camDown = Window::Controls::add("Camera Down");
+    Window::Controls::bindKey(camDown, GLFW_KEY_DOWN);
 
-    b.mapGamepadAxis(camUp, -0.5f, GLFW_GAMEPAD_AXIS_RIGHT_Y);
-    b.mapGamepadAxis(camDown, 0.5f, GLFW_GAMEPAD_AXIS_RIGHT_Y);
-    b.mapGamepadAxis(camLeft, -0.5f, GLFW_GAMEPAD_AXIS_RIGHT_X);
-    b.mapGamepadAxis(camRight, 0.5f, GLFW_GAMEPAD_AXIS_RIGHT_X);
+    leftClick = Window::Controls::add("Left Click");
+    Window::Controls::bindMouse(leftClick, GLFW_MOUSE_BUTTON_LEFT);
+    rightClick = Window::Controls::add("Right Click");
+    Window::Controls::bindMouse(leftClick, GLFW_MOUSE_BUTTON_RIGHT);
 }
 
-Vector2 Controller::getMouse() const {
-    Buttons& b = Engine::getButtons();
-    return Vector2(static_cast<float>(b.getMouseX()),
-                   static_cast<float>(b.getMouseY()));
+bool Controller::wasReleased(Window::Controls::ButtonId id) {
+    return Window::Controls::wasReleased(id);
 }
 
-Vector2 Controller::getMouseChange() const {
-    Buttons& b = Engine::getButtons();
-    return Vector2(static_cast<float>(b.getMouseX() - b.getLastMouseX()),
-                   static_cast<float>(b.getMouseY() - b.getLastMouseY()));
+bool Controller::isDown(Window::Controls::ButtonId id) {
+    return Window::Controls::isDown(id);
 }

+ 17 - 19
client/input/Controller.h

@@ -1,29 +1,27 @@
 #ifndef CONTROLLER_H
 #define CONTROLLER_H
 
-#include "input/Button.h"
 #include "math/Vector.h"
+#include "rendering/Window.h"
 
-struct Controller {
-    Button left;
-    Button right;
-    Button up;
-    Button down;
-    Button jump;
-    Button sneak;
-    Button camLeft;
-    Button camRight;
-    Button camUp;
-    Button camDown;
+namespace Controller {
+    extern Window::Controls::ButtonId left;
+    extern Window::Controls::ButtonId right;
+    extern Window::Controls::ButtonId up;
+    extern Window::Controls::ButtonId down;
+    extern Window::Controls::ButtonId jump;
+    extern Window::Controls::ButtonId sneak;
+    extern Window::Controls::ButtonId camLeft;
+    extern Window::Controls::ButtonId camRight;
+    extern Window::Controls::ButtonId camUp;
+    extern Window::Controls::ButtonId camDown;
+    extern Window::Controls::ButtonId leftClick;
+    extern Window::Controls::ButtonId rightClick;
 
-    Button leftClick;
-    Button rightClick;
-
-    Controller();
     void init();
 
-    Vector2 getMouse() const;
-    Vector2 getMouseChange() const;
-};
+    bool wasReleased(Window::Controls::ButtonId id);
+    bool isDown(Window::Controls::ButtonId id);
+}
 
 #endif

+ 22 - 57
client/rendering/Engine.cpp

@@ -8,7 +8,6 @@
 #include "utils/Random.h"
 #include "wrapper/GL.h"
 
-static Window window;
 static Shader worldShader;
 static Shader ssaoShader;
 static Shader ssaoBlurShader;
@@ -19,8 +18,7 @@ static Framebuffer<5> worldBuffer;
 static Framebuffer<1> ssaoBuffer;
 static Framebuffer<1> ssaoBlurBuffer;
 static Framebuffer<1> shadowBuffer;
-static Size lastSize{0, 0};
-static Frustum frustum{60.0f, 0.1f, 1000.0f, window.getSize()};
+static Frustum frustum{60.0f, 0.1f, 1000.0f, Window::getSize()};
 static MatrixStack<16> model;
 Renderer Engine::renderer;
 ShaderMatrix Engine::matrix{nullptr, model, nullptr};
@@ -120,18 +118,18 @@ static void initNoise() {
 
 bool Engine::init() {
     WindowOptions options(4, 0, {1024, 620}, false, "test");
-    Error error = window.open(options);
+    Error error = Window::open(options);
     if(error.has()) {
         LOG_ERROR(error.message);
         return true;
     }
-    lastSize = window.getSize();
+    Window::show();
     error = initShaders();
     if(error.has()) {
         LOG_ERROR(error.message);
         return true;
     }
-    error = initFramebuffers(window.getSize());
+    error = initFramebuffers(Window::getSize());
     if(error.has()) {
         LOG_ERROR(error.message);
         return true;
@@ -187,9 +185,8 @@ static void renderSSAO() {
     rProj *= worldProj;
 
     ssaoShader.setMatrix("proj", rProj.getValues());
-    const Size& size = window.getSize();
-    ssaoShader.setInt("width", size.width);
-    ssaoShader.setInt("height", size.height);
+    ssaoShader.setInt("width", Window::getSize().width);
+    ssaoShader.setInt("height", Window::getSize().height);
     worldBuffer.bindTextureTo(0, 0);
     worldBuffer.bindTextureTo(4, 1);
     ssaoNoise.bindTo(2);
@@ -219,9 +216,9 @@ static void renderOverlay() {
     GL::disableDepthTesting();
     overlayShader.use();
 
-    const Size& size = window.getSize();
     Matrix m;
-    m.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f))
+    m.scale(Vector3(2.0f / Window::getSize().width,
+                    -2.0f / Window::getSize().height, 1.0f))
         .translate(Vector3(-1.0f, 1.0f, 0.0f));
     overlayShader.setMatrix("view", m.getValues());
     model.clear();
@@ -265,11 +262,9 @@ static void updateWorldView() {
 }
 
 static void startRender() {
-    const Size& size = window.getSize();
-    if(size.width != lastSize.width || size.height != lastSize.height) {
-        GL::setViewport(size.width, size.height);
-        resizeFramebuffers(size);
-        lastSize = size;
+    if(Window::hasSizeChanged()) {
+        GL::setViewport(Window::getSize().width, Window::getSize().height);
+        resizeFramebuffers(Window::getSize());
     }
     GL::printError("loop error");
 
@@ -287,53 +282,23 @@ static void startRender() {
     renderOverlay();
 }
 
-struct Loop final {
-    void render(float lag) {
-        Engine::lag = lag;
-        startRender();
-    }
-
-    void tick() {
-        Game::tick();
-    }
-
-    bool isRunning() const {
-        return running;
-    }
-};
-
-void Engine::run() {
-    Loop loop;
-    window.run(loop, 50'000'000);
+static void render(float lag) {
+    Engine::lag = lag;
+    startRender();
 }
 
-void Engine::stop() {
-    running = false;
+static void tick() {
+    Game::tick();
 }
 
-void Engine::setTextInput(TextInput* input) {
-    window.textInput = input;
-    if(input != nullptr) {
-        input->setActive(true);
-    }
+static bool isRunning() {
+    return running && !Window::shouldClose();
 }
 
-bool Engine::isActiveTextInput(TextInput* input) {
-    return window.textInput == input;
-}
-
-Buttons& Engine::getButtons() {
-    return window.buttons;
-}
-
-const Size& Engine::getSize() {
-    return window.getSize();
-}
-
-const Clock& Engine::getFrameClock() {
-    return window.getFrameClock();
+void Engine::run() {
+    Window::run<isRunning, tick, render>(50'000'000);
 }
 
-const Clock& Engine::getTickClock() {
-    return window.getTickClock();
+void Engine::stop() {
+    running = false;
 }

+ 0 - 11
client/rendering/Engine.h

@@ -3,9 +3,6 @@
 
 #include "client/rendering/Renderer.h"
 #include "client/rendering/ShaderMatrix.h"
-#include "input/Buttons.h"
-#include "input/TextInput.h"
-#include "utils/Clock.h"
 #include "utils/Size.h"
 #include "utils/Utils.h"
 
@@ -17,14 +14,6 @@ namespace Engine {
     bool init();
     void run();
     void stop();
-
-    void setTextInput(TextInput* input);
-    bool isActiveTextInput(TextInput* input);
-
-    Buttons& getButtons();
-    const Size& getSize();
-    const Clock& getFrameClock();
-    const Clock& getTickClock();
 }
 
 #endif

+ 1 - 1
subprojects/gaming-core

@@ -1 +1 @@
-Subproject commit bf440d867ea41cbea5356e8531e68d3207b4806f
+Subproject commit 25066d5c6adcd5a13e318ce99e3065b38cdea019