Kajetan Johannes Hammerle 3 years ago
parent
commit
5b3d8afaa1
6 changed files with 65 additions and 54 deletions
  1. 1 0
      Game.h
  2. 32 34
      Main.cpp
  3. 1 1
      gaming-core
  4. 18 4
      input/Controller.cpp
  5. 12 13
      input/Controller.h
  6. 1 2
      meson.build

+ 1 - 0
Game.h

@@ -6,6 +6,7 @@
 #include "input/Controller.h"
 #include "rendering/Renderer.h"
 #include "utils/Clock.h"
+#include "utils/Size.h"
 
 class Game final {
     const Controller& controller;

+ 32 - 34
Main.cpp

@@ -8,8 +8,6 @@
 #include "gaming-core/rendering/WindowOptions.h"
 #include "gaming-core/utils/Clock.h"
 #include "gaming-core/wrapper/GL.h"
-#include "gaming-core/wrapper/GLEW.h"
-#include "gaming-core/wrapper/GLFW.h"
 #include "rendering/Renderer.h"
 
 bool parseArgs(int argAmount, char* const* args, WindowOptions& options) {
@@ -26,13 +24,14 @@ bool parseArgs(int argAmount, char* const* args, WindowOptions& options) {
 int main(int argAmount, char* const* args) {
     Size size(800, 480);
     WindowOptions options(3, 0, size, true, "Pigine");
-    if(parseArgs(argAmount, args, options) || GLFW::init()) {
+    if(parseArgs(argAmount, args, options)) {
         return 0;
     }
 
     TextInput* input = nullptr;
-    Window window(input, options);
-    if(window.hasError() || GLEW::init()) {
+    Window w(input, options);
+    if(w.getError().has()) {
+        std::cout << w.getError().message << '\n';
         return 0;
     }
 
@@ -41,43 +40,42 @@ int main(int argAmount, char* const* args) {
         return 0;
     }
 
-    Renderer renderer(shader);
+    Controller controller(w.getButtons());
+    static Game game(controller, w.getFrameClock(), w.getTickClock(), size);
 
-    Clock fps;
-    Clock tps;
+    struct GameBase {
+        Window& window;
+        Game& game;
+        Shader& shader;
+        Renderer renderer;
 
-    Buttons buttons(window);
-    Controller controller(buttons);
-    static Game game(controller, fps, tps, size);
-
-    window.show();
-
-    const Clock::Nanos nanosPerTick = 10'000'000;
-    Clock::Nanos lag = 0;
-    while(!window.shouldClose() && game.isRunning()) {
-        GL::printError("GL-Error");
+        GameBase(Window& w, Game& g, Shader& s)
+            : window(w), game(g), shader(s), renderer(s) {
+        }
 
-        lag += fps.update();
-        while(lag >= nanosPerTick) {
-            lag -= nanosPerTick;
-            tps.update();
-            buttons.tick();
+        void tick() {
             game.tick();
         }
 
-        size = window.getSize();
-        GL::setViewport(size.width, size.height);
+        void render(float lag) {
+            GL::clear();
+            const Size& size = window.getSize();
+            GL::setViewport(size.width, size.height);
 
-        Matrix view;
-        view.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f));
-        view.translate(Vector3(-1.0f, 1.0f, 0.0f));
-        shader.setMatrix("view", view.getValues());
+            Matrix view;
+            view.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f));
+            view.translate(Vector3(-1.0f, 1.0f, 0.0f));
+            shader.setMatrix("view", view.getValues());
 
-        game.render(static_cast<float>(lag) / nanosPerTick, renderer);
-        window.swapBuffers();
-        GL::clear();
+            game.render(lag, renderer);
+            GL::printError("GL-Error");
+        }
 
-        glfwPollEvents();
-    }
+        bool isRunning() const {
+            return game.isRunning();
+        }
+    };
+    GameBase base(w, game, shader);
+    w.run(base, 10'000'000);
     return 0;
 }

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 18c11759be38328c95fea4e3ef067f4a0c390c4d
+Subproject commit f771b8a0406ce86272f32374811aca8b70dac424

+ 18 - 4
input/Controller.cpp

@@ -1,10 +1,24 @@
 #include "input/Controller.h"
 
 Controller::Controller(Buttons& bs)
-    : list(bs.get()), a(bs.add(GLFW_KEY_A, "A")), b(bs.add(GLFW_KEY_S, "B")), x(bs.add(GLFW_KEY_X, "X")),
-      y(bs.add(GLFW_KEY_Z, "Y")), l(bs.add(GLFW_KEY_Q, "L")), r(bs.add(GLFW_KEY_W, "R")),
-      start(bs.add(GLFW_KEY_E, "Start")), select(bs.add(GLFW_KEY_D, "Select")), left(bs.add(GLFW_KEY_LEFT, "Left")),
-      right(bs.add(GLFW_KEY_RIGHT, "Right")), up(bs.add(GLFW_KEY_UP, "Up")), down(bs.add(GLFW_KEY_DOWN, "Down")) {
+    : a(GLFW_KEY_A, "A"), b(GLFW_KEY_S, "B"), x(GLFW_KEY_X, "X"),
+      y(GLFW_KEY_Z, "Y"), l(GLFW_KEY_Q, "L"), r(GLFW_KEY_W, "R"),
+      start(GLFW_KEY_E, "Start"), select(GLFW_KEY_D, "Select"),
+      left(GLFW_KEY_LEFT, "Left"), right(GLFW_KEY_RIGHT, "Right"),
+      up(GLFW_KEY_UP, "Up"), down(GLFW_KEY_DOWN, "Down") {
+    bs.add(a);
+    bs.add(b);
+    bs.add(x);
+    bs.add(y);
+    bs.add(l);
+    bs.add(r);
+    bs.add(start);
+    bs.add(select);
+    bs.add(left);
+    bs.add(right);
+    bs.add(up);
+    bs.add(down);
+
     bs.mapGamepadButton(a, GLFW_GAMEPAD_BUTTON_A);
     bs.mapGamepadButton(b, GLFW_GAMEPAD_BUTTON_B);
     bs.mapGamepadButton(x, GLFW_GAMEPAD_BUTTON_X);

+ 12 - 13
input/Controller.h

@@ -4,19 +4,18 @@
 #include "gaming-core/input/Buttons.h"
 
 struct Controller {
-    const ButtonList& list;
-    const Button& a;
-    const Button& b;
-    const Button& x;
-    const Button& y;
-    const Button& l;
-    const Button& r;
-    const Button& start;
-    const Button& select;
-    const Button& left;
-    const Button& right;
-    const Button& up;
-    const Button& down;
+    Button a;
+    Button b;
+    Button x;
+    Button y;
+    Button l;
+    Button r;
+    Button start;
+    Button select;
+    Button left;
+    Button right;
+    Button up;
+    Button down;
 
     Controller(Buttons& buttons);
 };

+ 1 - 2
meson.build

@@ -2,9 +2,7 @@ project('pigine', 'cpp')
 
 sources = [
     'Main.cpp', 
-    'gaming-core/wrapper/GLFW.cpp', 
     'gaming-core/wrapper/GL.cpp', 
-    'gaming-core/wrapper/GLEW.cpp', 
     'gaming-core/rendering/Shader.cpp', 
     'gaming-core/rendering/Texture.cpp',
     'gaming-core/rendering/TextureFormat.cpp',
@@ -19,6 +17,7 @@ sources = [
     'gaming-core/utils/Size.cpp', 
     'gaming-core/utils/Clock.cpp', 
     'gaming-core/utils/Buffer.cpp', 
+    'gaming-core/utils/Error.cpp', 
     'gaming-core/images/PNGReader.cpp',
     'gaming-core/input/Buttons.cpp',
     'gaming-core/input/Button.cpp',