Browse Source

different font sizes, more work on controller

Kajetan Johannes Hammerle 3 years ago
parent
commit
8b6e288191

+ 30 - 12
Game.cpp

@@ -16,19 +16,31 @@ float height = 480.0f;
 float motionY = 0.0f;
 bool onGround = false;
 
-Game::Game(const Controller& c, const Clock& fps, const Clock& tps) : controller(c), fps(fps), tps(tps) {
+Game::Game(Controller& c, const Clock& fps, const Clock& tps) : controller(c), fps(fps), tps(tps) {
+    c.mapKey(GLFW_KEY_A, Controller::A);
+    c.mapKey(GLFW_KEY_S, Controller::B);
+    c.mapKey(GLFW_KEY_X, Controller::X);
+    c.mapKey(GLFW_KEY_Z, Controller::Y);
+    c.mapKey(GLFW_KEY_Q, Controller::L);
+    c.mapKey(GLFW_KEY_W, Controller::R);
+    c.mapKey(GLFW_KEY_E, Controller::START);
+    c.mapKey(GLFW_KEY_D, Controller::SELECT);
+    c.mapKey(GLFW_KEY_LEFT, Controller::LEFT);
+    c.mapKey(GLFW_KEY_RIGHT, Controller::RIGHT);
+    c.mapKey(GLFW_KEY_UP, Controller::UP);
+    c.mapKey(GLFW_KEY_DOWN, Controller::DOWN);
 }
 
 void Game::tick() {
-    /*lastX = x;
+    lastX = x;
     lastY = y;
-    if(controller.isDown(Button::LEFT)) {
+    if(controller.isDown(Controller::LEFT)) {
         x -= 7;
     }
-    if(controller.isDown(Button::RIGHT)) {
+    if(controller.isDown(Controller::RIGHT)) {
         x += 7;
     }
-    if(controller.isDown(Button::A) && onGround) {
+    if(controller.isDown(Controller::A) && onGround) {
         motionY = -15.0f;
         onGround = false;
     }
@@ -48,18 +60,24 @@ void Game::tick() {
     while(x < -playerWidth) {
         x += width + playerWidth;
         lastX += width + playerWidth;
-    }*/
+    }
 }
 
-void Game::render(float lag, Renderer& renderer) const { 
+void Game::render(float lag, Renderer& renderer) const {
     renderer.translateTo(0.0f, 0.0f).update();
     renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, 0xFF0000FF);
-    renderer.translateTo(0.0f, 0.0f).scale(4).update();
-    String s("A");
-    s.append(": ").append(controller.getDownTime(0)).append("     ");
-    renderer.drawString(0, 10, s);
+    renderer.translateTo(0.0f, 0.0f).update();
+    String s;
+    s.append("FPS: ").append(fps.getUpdatesPerSecond());
+    float y = 0.0f;
+    renderer.setStringSize(2);
+    y = renderer.drawString(0.0f, y, s);
+    for(int i = 0; i < controller.getButtonAmount(); i++) {
+        s.clear().append(controller.getName(i)).append(": ").append(controller.getDownTime(i)).append(" ").append(controller.getUpTime(i));
+        y = renderer.drawString(0.0f, y, s);
+    }
 }
 
 bool Game::isRunning() const {
-    return true;//!controller.isDown(Button::SELECT);
+    return !controller.isDown(Controller::SELECT);
 }

+ 1 - 1
Game.h

@@ -7,7 +7,7 @@
 
 class Game final {
 public:
-    Game(const Controller& c, const Clock& fps, const Clock& tps);
+    Game(Controller& c, const Clock& fps, const Clock& tps);
 
     void tick();
     void render(float lag, Renderer& renderer) const;

+ 6 - 4
Main.cpp

@@ -8,7 +8,6 @@
 #include "rendering/Options.h"
 #include "utils/Clock.h"
 #include "rendering/wrapper/Shader.h"
-#include "input/Controller.h"
 #include "rendering/Renderer.h"
 #include "Game.h"
 
@@ -24,12 +23,15 @@ bool initGLEW() {
 
 bool parseArgs(int argAmount, char* const* args, Options& options) {
     while(true) {
-        switch(getopt(argAmount, args, "f")) {
+        switch(getopt(argAmount, args, "fv")) {
             case '?':
                 return true;
             case 'f':
                 options.fullscreen = true;
                 break;
+            case 'v':
+                options.vsync = true;
+                break;
             case -1:
                 return false;
         }
@@ -47,7 +49,8 @@ int main(int argAmount, char* const* args) {
     }
 
     WindowSize size(800, 480);
-    Window window(size, options);
+    Controller controller;
+    Window window(size, controller, options);
     if(window.hasError() || initGLEW()) {
         return 0;
     }
@@ -59,7 +62,6 @@ int main(int argAmount, char* const* args) {
 
     Renderer renderer(size, shader);
 
-    Controller controller;
     Clock fps;
     Clock tps;
 

+ 1 - 1
input/Button.cpp

@@ -1,6 +1,6 @@
 #include "input/Button.h"
 
-Button::Button() : downTime(0), upTime(0) {
+Button::Button() : downTime(0), upTime(1) {
 }
 
 void Button::press() {

+ 72 - 28
input/Controller.cpp

@@ -1,15 +1,72 @@
-/*
-#include <iostream>*/
+#include <iostream>
 
 #include "input/Controller.h"
 
-/*Controller::ButtonState::ButtonState() : downTime(0), justReleased(false) {
-}*/
+Controller::Axis::Axis() : positive(0), negative(0) {
+}
+
+Controller::Controller() : keyToButton(0), gamepadToButton(0)/*activeController(GLFW_JOYSTICK_1)*/ {
+}
+
+bool Controller::mapKey(int key, int button) {
+    if(key < 0 || key >= keyToButton.getLength()) {
+        return true;
+    }
+    button++;
+    if(button < 0 || button >= buttons.getLength()) {
+        return true;
+    }
+    keyToButton[key] = button;
+    return false;
+}
+
+bool Controller::mapGamepad(int gamepad, int button) {
+    if(gamepad < 0 || gamepad >= gamepadToButton.getLength()) {
+        return true;
+    }
+    button++;
+    if(button < 0 || button >= buttons.getLength()) {
+        return true;
+    }
+    gamepadToButton[gamepad] = button;
+    return false;
+}
+
+bool Controller::mapAxis(int axis, int positiveButton, int negativeButton) {
+    if(axis < 0 || axis >= axisToButton.getLength()) {
+        return true;
+    }
+    positiveButton++;
+    if(positiveButton < 0 || positiveButton >= buttons.getLength()) {
+        return true;
+    }
+    negativeButton++;
+    if(negativeButton < 0 || negativeButton >= buttons.getLength()) {
+        return true;
+    }
+    axisToButton[axis].positive = positiveButton;
+    axisToButton[axis].negative = negativeButton;
+    return false;
+}
+
+void Controller::press(int key) {
+    if(key < 0 || key >= keyToButton.getLength()) {
+        return;
+    }
+    buttons[keyToButton[key]].press();
+}
 
-Controller::Controller() /*activeController(GLFW_JOYSTICK_1)*/ {
+void Controller::release(int key) {
+    if(key < 0 || key >= keyToButton.getLength()) {
+        return;
+    }
+    buttons[keyToButton[key]].release();
 }
 
 void Controller::tick() {
+    for(Button& b : buttons) {
+        b.tick();
+    }
     /*if(!glfwJoystickPresent(activeController) && findController()) {
         std::cout << "cannot find any controller - resetting buttons\n";
         reset();
@@ -64,46 +121,33 @@ void Controller::increment(uint button, bool notReleased) {
     buttons[button].justReleased = (buttons[button].downTime > 0 && !notReleased && !buttons[button].justReleased);
     bool b = notReleased || buttons[button].justReleased;
     buttons[button].downTime = buttons[button].downTime * b + b;
-}
+}*/
 
-uint Controller::getButtonAmount() const {
+int Controller::getButtonAmount() const {
     return buttons.getLength();
 }
 
-const char* Controller::getName(uint button) const {
-    static constexpr const char* buttonNames[] = {
-        "A", "B", "X", "Y", "L", "R", "Select", "Start", "Left", "Right", "Up", "Down"
+const char* Controller::getName(int button) const {
+    static constexpr const char* buttonNames[BUTTON_AMOUNT] = {
+        "Unknown", "A", "B", "X", "Y", "L", "R", "Start", "Select", "Left", "Right", "Up", "Down"
     };
-    return buttonNames[button];
-}
-
-uint Controller::getDownTime(uint button) const {
-    return buttons[button].downTime;
-}
-
-bool Controller::isDown(uint button) const {
-    return buttons[button].downTime > 0;
-}
-
-bool Controller::wasReleased(uint button) const {
-    return buttons[button].justReleased;
+    return buttonNames[(button + 1) & getRangeMask(button + 1)];
 }
- */
 
 bool Controller::isDown(int button) const {
-    return buttons[(button + 1) & getRangeMask(button)].isDown();
+    return buttons[(button + 1) & getRangeMask(button + 1)].isDown();
 }
 
 int Controller::getDownTime(int button) const {
-    return buttons[(button + 1) & getRangeMask(button)].getDownTime();
+    return buttons[(button + 1) & getRangeMask(button + 1)].getDownTime();
 }
 
 bool Controller::isUp(int button) const {
-    return buttons[(button + 1) & getRangeMask(button)].isUp();
+    return buttons[(button + 1) & getRangeMask(button + 1)].isUp();
 }
 
 int Controller::getUpTime(int button) const {
-    return buttons[(button + 1) & getRangeMask(button)].getUpTime();
+    return buttons[(button + 1) & getRangeMask(button + 1)].getUpTime();
 }
 
 int Controller::getRangeMask(int button) const {

+ 31 - 9
input/Controller.h

@@ -8,14 +8,43 @@
 #include "input/Button.h"
 
 class Controller final {
-    int index;
-    Array<Button, 2 + GLFW_JOYSTICK_LAST> buttons;
+    static constexpr int BUTTON_AMOUNT = 13;
+    Array<Button, BUTTON_AMOUNT> buttons;
+    Array<int, 1 + GLFW_KEY_LAST> keyToButton;
+    Array<int, 1 + GLFW_JOYSTICK_LAST> gamepadToButton;
+
+    struct Axis {
+        int positive;
+        int negative;
+        Axis();
+    };
+    Array<Axis, 1 + GLFW_GAMEPAD_AXIS_LAST> axisToButton;
 
 public:
+    static constexpr int A = 0;
+    static constexpr int B = 1;
+    static constexpr int X = 2;
+    static constexpr int Y = 3;
+    static constexpr int L = 4;
+    static constexpr int R = 5;
+    static constexpr int START = 6;
+    static constexpr int SELECT = 7;
+    static constexpr int LEFT = 8;
+    static constexpr int RIGHT = 9;
+    static constexpr int UP = 10;
+    static constexpr int DOWN = 11;
+
     Controller();
+    bool mapKey(int key, int button);
+    bool mapGamepad(int gamepad, int button);
+    bool mapAxis(int axis, int positiveButton, int negativeButton);
 
+    void press(int key);
+    void release(int key);
     void tick();
 
+    int getButtonAmount() const;
+    const char* getName(int button) const;
     bool isDown(int button) const;
     int getDownTime(int button) const;
     bool isUp(int button) const;
@@ -23,13 +52,6 @@ public:
 
 private:
     int getRangeMask(int button) const;
-
-    /*bool findController();
-    void reset();
-    void increment(uint button, const u8* mapping, uint index);
-    void increment(uint button, bool notReleased);
-
-    uint activeController;*/
 };
 
 #endif

+ 28 - 10
rendering/FontRenderer.cpp

@@ -1,14 +1,15 @@
 #include "rendering/FontRenderer.h"
 
-FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024, 8 * sizeof(float)), tex("resources/font.png") {
+FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024, 8 * sizeof (float)), tex1("resources/font8x8.png"),
+tex2("resources/font16x16.png"), tex3("resources/font24x24.png"), activeTex(&tex1), scale(1) {
     vertexBuffer.bind();
-    int step = 8 * sizeof(float);
+    int step = 8 * sizeof (float);
     vertexBuffer.setFloatAttribute(0, 2, 0, step);
-    vertexBuffer.setFloatAttribute(1, 2, 2 * sizeof(float), step);
-    vertexBuffer.setFloatAttribute(2, 4, 4 * sizeof(float), step);
+    vertexBuffer.setFloatAttribute(1, 2, 2 * sizeof (float), step);
+    vertexBuffer.setFloatAttribute(2, 4, 4 * sizeof (float), step);
 }
 
-void FontRenderer::drawString(float x, float y, const char* text) {
+float FontRenderer::drawString(float x, float y, const char* text) {
     vertexBuffer.bind();
 
     const int maxIndex = 256;
@@ -18,6 +19,9 @@ void FontRenderer::drawString(float x, float y, const char* text) {
     float r = 1.0f;
     float g = 1.0f;
     float b = 1.0f;
+    
+    float addX = 6.0f * scale;
+    float addY = 8.0f * scale;
 
     while(text[index] != '\0' && index < maxIndex) {
         char32_t c = text[index];
@@ -42,14 +46,28 @@ void FontRenderer::drawString(float x, float y, const char* text) {
         float maxY = minY + (1.0f / 16.0f);
 
         buffer.add(x).add(y).add(minX).add(minY).add(r).add(g).add(b).add(1.0f);
-        buffer.add(x).add(y + 8).add(minX).add(maxY).add(r).add(g).add(b).add(1.0f);
-        buffer.add(x + 6).add(y).add(maxX).add(minY).add(r).add(g).add(b).add(1.0f);
-        buffer.add(x + 6).add(y + 8).add(maxX).add(maxY).add(r).add(g).add(b).add(1.0f);
+        buffer.add(x).add(y + addY).add(minX).add(maxY).add(r).add(g).add(b).add(1.0f);
+        buffer.add(x + addX).add(y).add(maxX).add(minY).add(r).add(g).add(b).add(1.0f);
+        buffer.add(x + addX).add(y + addY).add(maxX).add(maxY).add(r).add(g).add(b).add(1.0f);
 
-        x += 6;
+        x += addX;
         index++;
     }
 
-    tex.bind();
+    activeTex->bind();
     buffer.draw();
+    return y + addY;
+}
+
+void FontRenderer::setSize(int size) {
+    if(size == 1) {
+        activeTex = &tex2;
+        scale = 2;
+    } else if(size == 2) {
+        activeTex = &tex3;
+        scale = 3;
+    } else {
+        activeTex = &tex1;
+        scale = 1;
+    }
 }

+ 9 - 3
rendering/FontRenderer.h

@@ -4,15 +4,21 @@
 #include "rendering/wrapper/VertexBuffer.h"
 #include "rendering/wrapper/StreamBuffer.h"
 #include "rendering/FileTexture.h"
+#include "utils/Array.h"
 
 class FontRenderer final {
     VertexBuffer vertexBuffer;
     StreamBuffer buffer;
-    FileTexture tex;
-    
+    FileTexture tex1;
+    FileTexture tex2;
+    FileTexture tex3;
+    FileTexture* activeTex;
+    int scale;
+
 public:
     FontRenderer();
-    void drawString(float x, float y, const char* text);
+    float drawString(float x, float y, const char* text);
+    void setSize(int size);
 };
 
 #endif

+ 1 - 1
rendering/Options.cpp

@@ -1,4 +1,4 @@
 #include "rendering/Options.h"
 
-Options::Options(const char* name) : name(name), fullscreen(false) {
+Options::Options(const char* name) : name(name), fullscreen(false), vsync(false) {
 }

+ 1 - 0
rendering/Options.h

@@ -4,6 +4,7 @@
 struct Options final {
     const char* name;
     bool fullscreen;
+    bool vsync;
 
     Options(const char* name);
 };

+ 10 - 3
rendering/Renderer.cpp

@@ -69,10 +69,14 @@ void Renderer::setColorMode(bool b) {
     }
 }
 
-void Renderer::drawString(float x, float y, const char* text) {
+void Renderer::setStringSize(int size) {
+    fontRenderer.setSize(size);
+}
+
+float Renderer::drawString(float x, float y, const char* text) {
     setTextureMode(true);
     setColorMode(true);
-    fontRenderer.drawString(x, y, text);
+    return fontRenderer.drawString(x, y, text);
 }
 
 void Renderer::drawTriangle(const ColorRenderer::Vertex& v1, const ColorRenderer::Vertex& v2, const ColorRenderer::Vertex& v3) {
@@ -82,7 +86,10 @@ void Renderer::drawTriangle(const ColorRenderer::Vertex& v1, const ColorRenderer
 }
 
 void Renderer::drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, Color color) {
-    drawTriangle({x1, y1, color}, {x2, y2, color}, {x3, y3, color});
+    ColorRenderer::Vertex v1 = {x1, y1, color};
+    ColorRenderer::Vertex v2 = {x2, y2, color};
+    ColorRenderer::Vertex v3 = {x3, y3, color};
+    drawTriangle(v1, v2, v3);
 }
 
 void Renderer::drawRectangle(float x, float y, float width, float height, Color color) {

+ 2 - 1
rendering/Renderer.h

@@ -26,7 +26,8 @@ public:
 
     Renderer& rotate(float degrees);
 
-    void drawString(float x, float y, const char* text);
+    void setStringSize(int size);
+    float drawString(float x, float y, const char* text);
     void drawTriangle(const ColorRenderer::Vertex& v1, const ColorRenderer::Vertex& v2, const ColorRenderer::Vertex& v3);
     void drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, Color color);
     void drawRectangle(float x, float y, float width, float height, Color color);

+ 17 - 9
rendering/wrapper/Window.cpp

@@ -2,7 +2,8 @@
 
 #include "rendering/wrapper/Window.h"
 
-Window::Window(WindowSize& size, const Options& options) : window(nullptr), size(size) {
+Window::Window(WindowSize& size, Controller& controller, const Options& options) : window(nullptr), size(size),
+controller(controller) {
     glfwDefaultWindowHints();
     glfwWindowHint(GLFW_VISIBLE, 0);
     glfwWindowHint(GLFW_RESIZABLE, 1);
@@ -21,20 +22,31 @@ Window::Window(WindowSize& size, const Options& options) : window(nullptr), size
     }
     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
     glfwMakeContextCurrent(window);
-    glfwSwapInterval(0);
+    glfwSwapInterval(options.vsync);
 
     glfwSetWindowUserPointer(window, this);
-    
+
     glfwSetFramebufferSizeCallback(window, [](GLFWwindow* glfwWindow, int newWidth, int newHeight) {
         glViewport(0, 0, newWidth, newHeight);
         void* data = glfwGetWindowUserPointer(glfwWindow);
         if(data == nullptr) {
             return;
         }
-        Window& w = *static_cast<Window*>(data);
+        Window& w = *static_cast<Window*> (data);
         w.size.width = newWidth;
         w.size.height = newHeight;
-        std::cout << "WUSI\n";
+    });
+    glfwSetKeyCallback(window, [](GLFWwindow* glfwWindow, int key, int, int action, int) {
+        void* data = glfwGetWindowUserPointer(glfwWindow);
+        if(data == nullptr) {
+            return;
+        }
+        Window& w = *static_cast<Window*> (data);
+        if(action == GLFW_PRESS) {
+            w.controller.press(key);
+        } else if(action == GLFW_RELEASE) {
+            w.controller.release(key);
+        }
     });
 }
 
@@ -59,8 +71,4 @@ bool Window::shouldClose() const {
 void Window::swapBuffers() {
     glfwSwapBuffers(window);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-}
-
-void Window::setKeyCallback(GLFWkeyfun f) {
-    glfwSetKeyCallback(window, f);
 }

+ 3 - 3
rendering/wrapper/Window.h

@@ -5,14 +5,16 @@
 #include <GLFW/glfw3.h>
 
 #include "rendering/WindowSize.h"
+#include "input/Controller.h"
 #include "rendering/Options.h"
 
 class Window final {
     GLFWwindow* window;
     WindowSize& size;
+    Controller& controller;
 
 public:
-    Window(WindowSize& size, const Options& options);
+    Window(WindowSize& size, Controller& controller, const Options& options);
     ~Window();
 
     Window(const Window&) = delete;
@@ -24,8 +26,6 @@ public:
     void show();
     bool shouldClose() const;
     void swapBuffers();
-
-    void setKeyCallback(GLFWkeyfun f);
 };
 
 #endif

BIN
resources/font.png


BIN
resources/font16x16.png


BIN
resources/font24x24.png


BIN
resources/font8x8.png


+ 1 - 1
utils/Array.h

@@ -46,7 +46,7 @@ public:
         return data + N;
     }
 
-    int getLength() const {
+    constexpr int getLength() const {
         return N;
     }
 };

+ 1 - 1
utils/List.h

@@ -28,7 +28,7 @@ public:
         return entries;
     }
 
-    int getCapacity() const {
+    constexpr int getCapacity() const {
         return L;
     }
 

+ 0 - 3
utils/PNGReader.cpp

@@ -68,9 +68,6 @@ bool PNGReader::hasError() const {
     } else if(width != height) {
         std::cout << "width and height of '" << path << "' are not equal\n";
         return true;
-    } else if(((width - 1) & width) != 0) {
-        std::cout << "width and height of '" << path << "' are not a multiple of 2\n";
-        return true;
     } else if(width < 1 || width > 2048) {
         std::cout << "width and height of '" << path << "' are too big\n";
         return true;

+ 6 - 0
utils/String.cpp

@@ -34,6 +34,12 @@ int String::getLength() const {
     return length;
 }
 
+String& String::clear() {
+    length = 0;
+    data[0] = '\0';
+    return *this;
+}
+
 String& String::append(char c) {
     if(length < MAX_LENGTH - 1) {
         data[length++] = c;

+ 2 - 1
utils/String.h

@@ -11,6 +11,7 @@ public:
     operator const char*() const;
     char operator[](int index) const;
     int getLength() const;
+    String& clear();
 
     template<typename T>
     String& append(const char* format, const T& t) {
@@ -32,8 +33,8 @@ public:
 
 private:
     static constexpr int MAX_LENGTH = 256 - sizeof(int);
-    char data[MAX_LENGTH];
     int length;
+    char data[MAX_LENGTH];
 };
 
 #endif