Browse Source

start on uint to int refactoring

Kajetan Johannes Hammerle 3 years ago
parent
commit
c8c08a4f4a

+ 10 - 12
Game.cpp

@@ -1,6 +1,6 @@
 #include "Game.h"
 #include "rendering/Renderer.h"
-#include "rendering/wrapper/GLFWWrapper.h"
+#include "rendering/wrapper/GLFW.h"
 #include "utils/String.h"
 
 float x = 0.0f;
@@ -20,7 +20,7 @@ Game::Game(const Controller& c, const Clock& fps, const Clock& tps) : controller
 }
 
 void Game::tick() {
-    lastX = x;
+    /*lastX = x;
     lastY = y;
     if(controller.isDown(Button::LEFT)) {
         x -= 7;
@@ -48,20 +48,18 @@ void Game::tick() {
     while(x < -playerWidth) {
         x += width + playerWidth;
         lastX += width + playerWidth;
-    }
+    }*/
 }
 
-void Game::render(float lag, Renderer& renderer) const {
-    renderer.translateTo(0.0f, 0.0f).scale(4).update();
-    for(uint i = 0; i < controller.getButtonAmount(); i++) {
-        String s(controller.getName(i));
-        s.append(": ").append(controller.wasReleased(i)).append(" ").append(controller.getDownTime(i));
-        renderer.drawString(0, 10 * i, s);
-    }
+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, 0x0000FFFF);
+    renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, 0xFF0000FF);
+    renderer.translateTo(0.0f, 0.0f).scale(4).update();
+    String s("&900A");
+    s.append(": ").append(controller.getDownTime(0)).append("     ");
+    renderer.drawString(0, 10, s);
 }
 
 bool Game::isRunning() const {
-    return !controller.isDown(Button::SELECT);
+    return true;//!controller.isDown(Button::SELECT);
 }

+ 1 - 1
Game.h

@@ -11,7 +11,7 @@ public:
 
     void tick();
     void render(float lag, Renderer& renderer) const;
-    
+
     bool isRunning() const;
 
 private:

+ 35 - 24
Main.cpp

@@ -1,9 +1,11 @@
 #include <iostream>
+#include <bits/getopt_core.h>
 
-#include "rendering/wrapper/GLFWWrapper.h"
+#include "rendering/wrapper/GLFW.h"
 #include "rendering/wrapper/GLWrapper.h"
 #include "rendering/WindowSize.h"
 #include "rendering/wrapper/Window.h"
+#include "rendering/Options.h"
 #include "utils/Clock.h"
 #include "rendering/wrapper/Shader.h"
 #include "input/Controller.h"
@@ -20,44 +22,53 @@ bool initGLEW() {
     return false;
 }
 
-void initCallbacks(Window& w, WindowSize& size) {
-    static WindowSize& cSize = size;
-    w.setFramebufferSizeCallback([](GLFWwindow*, int newWidth, int newHeight) {
-        glViewport(0, 0, newWidth, newHeight);
-        cSize.width = newWidth;
-        cSize.height = newHeight;
-    });
+bool parseArgs(int argAmount, char* const* args, Options& options) {
+    while(true) {
+        switch(getopt(argAmount, args, "f")) {
+            case '?':
+                return true;
+            case 'f':
+                options.fullscreen = true;
+                break;
+            case -1:
+                return false;
+        }
+    }
 }
 
-int main() {
-    if(GLFWWrapper::hasError()) {
+int main(int argAmount, char* const* args) {
+    Options options("Pigine");
+    if(parseArgs(argAmount, args, options)) {
+        return 0;
+    }
+
+    if(GLFW::init()) {
         return 0;
     }
-    
+
     WindowSize size(800, 480);
-    Window window(size, "Test");
+    Window window(size, options);
     if(window.hasError() || initGLEW()) {
         return 0;
     }
-    
+
     Shader shader("resources/shader/vertex.vs", "resources/shader/fragment.fs");
     if(shader.hasError()) {
         return 0;
     }
-       
+
     Renderer renderer(size, shader);
-    
+
     Controller controller;
     Clock fps;
     Clock tps;
-    
+
     static Game game(controller, fps, tps);
-    
-    initCallbacks(window, size);
+
     window.show();
-    
-    const u64 nanosPerTick = 10'000'000;
-    u64 lag = 0;
+
+    const Nanos nanosPerTick = 10'000'000;
+    Nanos lag = 0;
     while(!window.shouldClose() && game.isRunning()) {
         GLWrapper::checkAndPrintError("GL-Error");
 
@@ -68,15 +79,15 @@ int main() {
             controller.tick();
             game.tick();
         }
-        
+
         Matrix view;
         view.translate(-1.0f, 1.0f);
         view.scale(2.0f / size.width, -2.0f / size.height);
         shader.setMatrix("view", view.getValues());
-        
+
         game.render((float) lag / nanosPerTick, renderer);
         window.swapBuffers();
-        
+
         glfwPollEvents();
     }
     return 0;

+ 35 - 0
input/Button.cpp

@@ -0,0 +1,35 @@
+#include "input/Button.h"
+
+Button::Button() : downTime(0), upTime(0) {
+}
+
+void Button::press() {
+    downTime = 1;
+    upTime = 0;
+}
+
+void Button::release() {
+    downTime = 0;
+    upTime = 1;
+}
+
+void Button::tick() {
+    downTime += isDown();
+    upTime += isUp();
+}
+
+bool Button::isDown() const {
+    return downTime > 0;
+}
+
+int Button::getDownTime() const {
+    return downTime;
+}
+
+bool Button::isUp() const {
+    return upTime > 0;
+}
+
+int Button::getUpTime() const {
+    return upTime;
+}

+ 19 - 0
input/Button.h

@@ -0,0 +1,19 @@
+#ifndef BUTTON_H
+#define BUTTON_H
+
+class Button final {
+    int downTime;
+    int upTime;
+    
+public:
+    Button();
+    void press();
+    void release();
+    void tick();
+    bool isDown() const;
+    int getDownTime() const;
+    bool isUp() const;
+    int getUpTime() const;
+};
+
+#endif

+ 29 - 9
input/Controller.cpp

@@ -1,17 +1,16 @@
-#include <GL/glew.h>
-#include <GLFW/glfw3.h>
-#include <iostream>
+/*
+#include <iostream>*/
 
 #include "input/Controller.h"
 
-Controller::ButtonState::ButtonState() : downTime(0), justReleased(false) {
-}
+/*Controller::ButtonState::ButtonState() : downTime(0), justReleased(false) {
+}*/
 
-Controller::Controller() : activeController(GLFW_JOYSTICK_1) {
+Controller::Controller() /*activeController(GLFW_JOYSTICK_1)*/ {
 }
 
 void Controller::tick() {
-    if(!glfwJoystickPresent(activeController) && findController()) {
+    /*if(!glfwJoystickPresent(activeController) && findController()) {
         std::cout << "cannot find any controller - resetting buttons\n";
         reset();
         return;
@@ -38,10 +37,10 @@ void Controller::tick() {
     increment(Button::LEFT, axisMap[0] < -0.5f);
     increment(Button::RIGHT, axisMap[0] > 0.5f);
     increment(Button::UP, axisMap[1] < -0.5f);
-    increment(Button::DOWN, axisMap[1] > 0.5f);
+    increment(Button::DOWN, axisMap[1] > 0.5f);*/
 }
 
-bool Controller::findController() {
+/*bool Controller::findController() {
     for(uint i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
         if(glfwJoystickPresent(i)) {
             activeController = i;
@@ -89,3 +88,24 @@ bool Controller::isDown(uint button) const {
 bool Controller::wasReleased(uint button) const {
     return buttons[button].justReleased;
 }
+ */
+
+bool Controller::isDown(int button) const {
+    return buttons[(button + 1) & getRangeMask(button)].isDown();
+}
+
+int Controller::getDownTime(int button) const {
+    return buttons[(button + 1) & getRangeMask(button)].getDownTime();
+}
+
+bool Controller::isUp(int button) const {
+    return buttons[(button + 1) & getRangeMask(button)].isUp();
+}
+
+int Controller::getUpTime(int button) const {
+    return buttons[(button + 1) & getRangeMask(button)].getUpTime();
+}
+
+int Controller::getRangeMask(int button) const {
+    return -(button >= 0 && button < buttons.getLength());
+}

+ 19 - 86
input/Controller.h

@@ -1,102 +1,35 @@
 #ifndef CONTROLLER_H
 #define CONTROLLER_H
 
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
 #include "utils/Array.h"
+#include "input/Button.h"
 
-enum Button {
-    A, B, X, Y, L, R, SELECT, START, LEFT, RIGHT, UP, DOWN
-};
+class Controller final {
+    int index;
+    Array<Button, 2 + GLFW_JOYSTICK_LAST> buttons;
 
-struct Controller final {
+public:
     Controller();
+
     void tick();
-    uint getButtonAmount() const;
-    const char* getName(uint button) const;
-    uint getDownTime(uint button) const;
-    bool isDown(uint button) const;
-    bool wasReleased(uint button) const;
+
+    bool isDown(int button) const;
+    int getDownTime(int button) const;
+    bool isUp(int button) const;
+    int getUpTime(int button) const;
 
 private:
-    bool findController();
+    int getRangeMask(int button) const;
+
+    /*bool findController();
     void reset();
     void increment(uint button, const u8* mapping, uint index);
     void increment(uint button, bool notReleased);
 
-    struct ButtonState final {
-        ButtonState();
-        uint downTime;
-        bool justReleased;
-    };
-    
-    Array<ButtonState, 12> buttons;
-    uint activeController;
+    uint activeController;*/
 };
 
-#endif
-/*#ifndef KEYS_H
-#define KEYS_H
-
-#include <iostream>
-#include <array>
-
-#include "common/utils/Types.h"
-
-class Keys final {
-public:
-
-    class Key final {
-    public:
-        friend class Keys;
-
-        bool isDown() const;
-        bool isReleased() const;
-        u32 getDownTime() const;
-
-    private:
-        Key();
-        Key(const Key&) = delete;
-        Key(Key&&) = delete;
-        Key& operator=(const Key&) = delete;
-        Key& operator=(Key&&) = delete;
-
-        int glfwKey;
-        bool down;
-        bool shouldRelease;
-        u32 downTime;
-    };
-
-private:
-    Key keys[15];
-
-public:
-    Keys();
-    const Key& left;
-    const Key& right;
-    const Key& up;
-    const Key& down;
-    const Key& jump;
-    const Key& sneak;
-    const Key& camLeft;
-    const Key& camRight;
-    const Key& camUp;
-    const Key& camDown;
-    const Key& test;
-    const Key& test2;
-    const Key& test3;
-    const Key& test4;
-    const Key& test5;
-
-    void release(int key);
-    void press(int key);
-    void tick();
-
-private:
-    Keys(const Keys&) = delete;
-    Keys& operator=(const Keys&) = delete;
-    Keys(Key&&) = delete;
-    Keys& operator=(Keys&&) = delete;
-};
-
-std::ostream& operator<<(std::ostream& os, const Keys::Key& k);
-
-#endif*/
+#endif

+ 27 - 1
meson.build

@@ -1,6 +1,32 @@
 project('pigine', 'cpp')
 
-sources = ['Main.cpp', 'rendering/wrapper/GLFWWrapper.cpp', 'rendering/WindowSize.cpp', 'rendering/wrapper/Window.cpp', 'utils/Clock.cpp', 'rendering/wrapper/Shader.cpp', 'rendering/wrapper/GLWrapper.cpp', 'Game.cpp', 'input/Controller.cpp', 'rendering/Renderer.cpp', 'math/Vector.cpp', 'math/Matrix.cpp', 'math/MatrixStack.cpp', 'utils/PNGReader.cpp', 'utils/Utils.cpp', 'rendering/wrapper/StreamBuffer.cpp', 'rendering/wrapper/Texture.cpp', 'rendering/wrapper/VertexBuffer.cpp', 'rendering/FileTexture.cpp', 'rendering/ColorRenderer.cpp', 'rendering/FontRenderer.cpp', 'rendering/Mesh.cpp', 'utils/String.cpp']
+sources = [
+    'Main.cpp', 
+    'rendering/wrapper/GLFW.cpp', 
+    'rendering/WindowSize.cpp', 
+    'rendering/wrapper/Window.cpp', 
+    'utils/Clock.cpp', 
+    'rendering/wrapper/Shader.cpp', 
+    'rendering/wrapper/GLWrapper.cpp',
+    'Game.cpp',
+    'input/Controller.cpp',
+    'input/Button.cpp',
+    'rendering/Renderer.cpp',
+    'math/Vector.cpp',
+    'math/Matrix.cpp',
+    'math/MatrixStack.cpp',
+    'utils/PNGReader.cpp',
+    'utils/Utils.cpp',
+    'rendering/wrapper/StreamBuffer.cpp',
+    'rendering/wrapper/Texture.cpp',
+    'rendering/wrapper/VertexBuffer.cpp',
+    'rendering/FileTexture.cpp',
+    'rendering/ColorRenderer.cpp',
+    'rendering/FontRenderer.cpp',
+    'rendering/Mesh.cpp',
+    'utils/String.cpp',
+    'rendering/Options.cpp'
+]
 
 glewDep = dependency('glew')
 glfwDep = dependency('glfw3')

+ 5 - 5
rendering/ColorRenderer.cpp

@@ -2,16 +2,16 @@
 
 ColorRenderer::ColorRenderer() : buffer(8 * 1024 * 1024, 2 * sizeof(float) + 4) {
     vertexBuffer.bind();
-    uint step = 2 * sizeof(float) + 4;
+    int step = 2 * sizeof(float) + 4;
     vertexBuffer.setFloatAttribute(0, 2, 0, step);
-    vertexBuffer.setByteAttribute(2, 4, 2 * sizeof(float), step);
+    vertexBuffer.setColorAttribute(2, 2 * sizeof(float), step);
 }
 
 void ColorRenderer::draw(const Vertex& v1, const Vertex& v2, const Vertex& v3) {
     vertexBuffer.bind();
     buffer.reset(3 * sizeof (float) * 6);
-    buffer.add(v1.x).add(v1.y).addReversed(v1.color);
-    buffer.add(v2.x).add(v2.y).addReversed(v2.color);
-    buffer.add(v3.x).add(v3.y).addReversed(v3.color);
+    buffer.add(v1.x).add(v1.y).add(v1.color);
+    buffer.add(v2.x).add(v2.y).add(v2.color);
+    buffer.add(v3.x).add(v3.y).add(v3.color);
     buffer.draw();
 }

+ 2 - 1
rendering/ColorRenderer.h

@@ -3,6 +3,7 @@
 
 #include "rendering/wrapper/VertexBuffer.h"
 #include "rendering/wrapper/StreamBuffer.h"
+#include "utils/PNGReader.h"
 
 class ColorRenderer final {
 public:
@@ -11,7 +12,7 @@ public:
     struct Vertex {
         float x;
         float y;
-        u32 color;
+        Color color;
     };
 
     void draw(const Vertex& v1, const Vertex& v2, const Vertex& v3);

+ 16 - 5
rendering/FileTexture.cpp

@@ -1,13 +1,24 @@
+#include <iostream>
+
 #include "rendering/FileTexture.h"
 #include "utils/PNGReader.h"
 
 FileTexture::FileTexture(const char* path) {
-    u32 maxWidth = 128;
-    u32 maxHeight = 128;
-    u32 buffer[128 * 128];
-    if(PNGReader::load(path, buffer, maxWidth, maxHeight)) {
-        texture.setRGBAData(maxWidth, maxHeight, buffer);
+    PNGReader reader(path);
+    if(reader.hasError()) {
+        return;
+    }
+    std::cout << reader.getChannels() << "\n";
+    char* buffer = new char[reader.getBufferSize()];
+    if(reader.readData(buffer)) {
+        delete[] buffer;
+        return;
+    }
+    texture.setColors(reader.getWidth(), reader.getHeight(), buffer, reader.getChannels());
+    for(int i = 0; i < reader.getWidth() * reader.getHeight(); i++) {
+        std::cout << (int) buffer[i] << "\n";
     }
+    delete[] buffer;
 }
 
 void FileTexture::bind() const {

+ 3 - 3
rendering/FontRenderer.cpp

@@ -2,7 +2,7 @@
 
 FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024, 8 * sizeof(float)), tex("resources/font.png") {
     vertexBuffer.bind();
-    uint 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);
@@ -11,10 +11,10 @@ FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024, 8 * sizeof(float)), tex("
 void FontRenderer::drawString(float x, float y, const char* text) {
     vertexBuffer.bind();
 
-    const u64 maxIndex = 256;
+    const int maxIndex = 256;
     buffer.reset(maxIndex * 4 * sizeof (float) * 8);
 
-    u64 index = 0;
+    int index = 0;
     float r = 1.0f;
     float g = 1.0f;
     float b = 1.0f;

+ 4 - 6
rendering/FontRenderer.h

@@ -6,15 +6,13 @@
 #include "rendering/FileTexture.h"
 
 class FontRenderer final {
-public:
-    FontRenderer();
-
-    void drawString(float x, float y, const char* text);
-
-private:
     VertexBuffer vertexBuffer;
     StreamBuffer buffer;
     FileTexture tex;
+    
+public:
+    FontRenderer();
+    void drawString(float x, float y, const char* text);
 };
 
 #endif

+ 4 - 0
rendering/Options.cpp

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

+ 11 - 0
rendering/Options.h

@@ -0,0 +1,11 @@
+#ifndef OPTIONS_H
+#define OPTIONS_H
+
+struct Options final {
+    const char* name;
+    bool fullscreen;
+
+    Options(const char* name);
+};
+
+#endif

+ 2 - 2
rendering/Renderer.cpp

@@ -81,11 +81,11 @@ void Renderer::drawTriangle(const ColorRenderer::Vertex& v1, const ColorRenderer
     colorRenderer.draw(v1, v2, v3);
 }
 
-void Renderer::drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, u32 color) {
+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});
 }
 
-void Renderer::drawRectangle(float x, float y, float width, float height, u32 color) {
+void Renderer::drawRectangle(float x, float y, float width, float height, Color color) {
     drawTriangle(x, y, x + width, y, x, y + height, color);
     drawTriangle(x + width, y, x, y + height, x + width, y + height, color);
 }

+ 2 - 2
rendering/Renderer.h

@@ -28,8 +28,8 @@ public:
 
     void 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, u32 color);
-    void drawRectangle(float x, float y, float width, float height, u32 color);
+    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);
 
 private:
     void setTextureMode(bool b);

+ 18 - 0
rendering/wrapper/GLFW.cpp

@@ -0,0 +1,18 @@
+#include <iostream>
+
+#include "rendering/wrapper/GLFW.h"
+
+bool GLFW::init() {
+    if(glfwInit()) {
+        std::atexit([]() {
+            glfwTerminate();
+        });
+        return false;
+    }
+    std::cout << "could not initialize GLFW\n";
+    return true;
+}
+
+Nanos GLFW::getNanos() {
+    return glfwGetTimerValue() * (1000000000 / glfwGetTimerFrequency());
+}

+ 14 - 0
rendering/wrapper/GLFW.h

@@ -0,0 +1,14 @@
+#ifndef GLFW_H
+#define GLFW_H
+
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+typedef uint64_t Nanos;
+
+namespace GLFW {
+    bool init();
+    Nanos getNanos();
+}
+
+#endif

+ 0 - 31
rendering/wrapper/GLFWWrapper.cpp

@@ -1,31 +0,0 @@
-#include <iostream>
-#include <GL/glew.h>
-#include <GLFW/glfw3.h>
-
-#include "rendering/wrapper/GLFWWrapper.h"
-
-struct GLFWInit final {
-    bool error;
-
-    GLFWInit() : error(glfwInit() == GLFW_FALSE) {
-        if(error) {
-            std::cout << "could not initialize GLFW\n";
-        }
-    }
-
-    ~GLFWInit() {
-        if(!error) {
-            glfwTerminate();
-        }
-    }
-};
-
-static GLFWInit init;
-
-bool GLFWWrapper::hasError() {
-    return init.error;
-}
-
-u64 GLFWWrapper::getTimeNanos() {
-    return glfwGetTimerValue() * (1000000000 / glfwGetTimerFrequency());
-}

+ 0 - 11
rendering/wrapper/GLFWWrapper.h

@@ -1,11 +0,0 @@
-#ifndef GLFWWRAPPER_H
-#define GLFWWRAPPER_H
-
-#include "utils/Types.h"
-
-namespace GLFWWrapper {
-    bool hasError();
-    u64 getTimeNanos();
-}
-
-#endif

+ 3 - 18
rendering/wrapper/StreamBuffer.cpp

@@ -1,37 +1,22 @@
 #include <GL/glew.h>
 #include <cassert>
-#include <cstring>
 
 #include "rendering/wrapper/StreamBuffer.h"
 
-StreamBuffer::StreamBuffer(uint size, uint bytesPerVertex) :
+StreamBuffer::StreamBuffer(int size, int bytesPerVertex) :
 bufferSize(size), offset(size), bytesPerVertex(bytesPerVertex), index(0), buffer(nullptr) {
 }
 
-void StreamBuffer::reset(uint size) {
+void StreamBuffer::reset(int size) {
     if(offset + size >= bufferSize) {
         offset = 0;
         glBufferData(GL_ARRAY_BUFFER, bufferSize, nullptr, GL_STREAM_DRAW);
     }
-    buffer = static_cast<u8*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
+    buffer = static_cast<char*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
     assert(buffer != nullptr);
     index = 0;
 }
 
-StreamBuffer& StreamBuffer::add(float f) {
-    memcpy(buffer + index, &f, sizeof (f));
-    index += sizeof (f);
-    return *this;
-}
-
-StreamBuffer& StreamBuffer::addReversed(u32 i) {
-    buffer[index++] = i >> 24;
-    buffer[index++] = i >> 16;
-    buffer[index++] = i >> 8;
-    buffer[index++] = i >> 0;
-    return *this;
-}
-
 void StreamBuffer::draw() {
     glUnmapBuffer(GL_ARRAY_BUFFER);
     glDrawArrays(GL_TRIANGLE_STRIP, offset / bytesPerVertex, index / bytesPerVertex);

+ 17 - 11
rendering/wrapper/StreamBuffer.h

@@ -1,28 +1,34 @@
 #ifndef STREAMBUFFER_H
 #define STREAMBUFFER_H
 
-#include "utils/Types.h"
+#include <cstring>
 
 class StreamBuffer final {
 public:
-    StreamBuffer(uint size, uint bytesPerVertex);
+    StreamBuffer(int size, int bytesPerVertex);
     StreamBuffer(const StreamBuffer& other) = delete;
     StreamBuffer(StreamBuffer&& other) = delete;
     StreamBuffer& operator=(const StreamBuffer& other) = delete;
     StreamBuffer& operator=(StreamBuffer&& other) = delete;
 
-    void reset(uint size);
-    StreamBuffer& add(float f);
-    StreamBuffer& addReversed(u32 i);
+    void reset(int size);
+
+    template<typename T>
+    StreamBuffer& add(const T& t) {
+        std::memcpy(buffer + index, &t, sizeof (t));
+        index += sizeof (t);
+        return *this;
+    }
+    
     void draw();
 
 private:
-    uint bufferSize;
-    uint offset;
-    uint bytesPerVertex;
-    
-    uint index;
-    u8* buffer;
+    int bufferSize;
+    int offset;
+    int bytesPerVertex;
+
+    int index;
+    char* buffer;
 };
 
 #endif

+ 9 - 2
rendering/wrapper/Texture.cpp

@@ -14,9 +14,16 @@ Texture::~Texture() {
     glDeleteTextures(1, &texture);
 }
 
-void Texture::setRGBAData(int width, int height, const u32* data) {
+void Texture::setColors(int width, int height, const char* data, int channels) {
     glBindTexture(GL_TEXTURE_2D, texture);
-    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+    switch(channels) {
+        case 1:
+            glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
+            break;
+        case 4:
+            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+            break;
+    }
 }
 
 void Texture::setRGBFloatData(int width, int height, const float* data) {

+ 2 - 2
rendering/wrapper/Texture.h

@@ -3,7 +3,7 @@
 
 #include <GL/glew.h>
 
-#include "utils/Types.h"
+#include "utils/PNGReader.h"
 
 class Texture final {
 public:
@@ -14,7 +14,7 @@ public:
     Texture& operator=(const Texture& other) = delete;
     Texture& operator=(Texture&& other) = delete;
     
-    void setRGBAData(int width, int height, const u32* data);
+    void setColors(int width, int height, const char* data, int channels);
     void setRGBFloatData(int width, int height, const float* data);
     
     void bind() const;

+ 5 - 5
rendering/wrapper/VertexBuffer.cpp

@@ -10,17 +10,17 @@ VertexBuffer::~VertexBuffer() {
     glDeleteVertexArrays(1, &vertexArray);
 }
 
-void VertexBuffer::setFloatAttribute(uint index, uint length, uint offset, uint step) {
+void VertexBuffer::setFloatAttribute(int index, int length, int offset, int step) {
     glVertexAttribPointer(index, length, GL_FLOAT, false, step, static_cast<char*> (0) + offset);
     glEnableVertexAttribArray(index);
 }
 
-void VertexBuffer::setByteAttribute(uint index, uint length, uint offset, uint step) {
-    glVertexAttribPointer(index, length, GL_UNSIGNED_BYTE, true, step, static_cast<char*> (0) + offset);
+void VertexBuffer::setColorAttribute(int index, int offset, int step) {
+    glVertexAttribPointer(index, GL_BGRA, GL_UNSIGNED_BYTE, true, step, static_cast<char*> (0) + offset);
     glEnableVertexAttribArray(index);
 }
 
-void VertexBuffer::setData(u64 size, const void* data) {
+void VertexBuffer::setData(int size, const void* data) {
     glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
 }
 
@@ -37,6 +37,6 @@ void VertexBuffer::bind() const {
     bindBuffer();
 }
 
-void VertexBuffer::draw(uint vertices) const {
+void VertexBuffer::draw(int vertices) const {
     glDrawArrays(GL_TRIANGLES, 0, vertices);
 }

+ 4 - 6
rendering/wrapper/VertexBuffer.h

@@ -3,8 +3,6 @@
 
 #include <GL/glew.h>
 
-#include "utils/Types.h"
-
 class VertexBuffer final {
 public:
     VertexBuffer();
@@ -14,15 +12,15 @@ public:
     VertexBuffer& operator=(const VertexBuffer& other) = delete;
     VertexBuffer& operator=(VertexBuffer&& other) = delete;
     
-    void setFloatAttribute(uint index, uint length, uint offset, uint step);
-    void setByteAttribute(uint index, uint length, uint offset, uint step);
-    void setData(u64 size, const void* data);
+    void setFloatAttribute(int index, int length, int offset, int step);
+    void setColorAttribute(int index, int offset, int step);
+    void setData(int size, const void* data);
     
     void bindArray() const;
     void bindBuffer() const;
     void bind() const;
     
-    void draw(uint vertices) const;
+    void draw(int vertices) const;
 
 private:
     GLuint vertexArray;

+ 17 - 16
rendering/wrapper/Window.cpp

@@ -2,18 +2,19 @@
 
 #include "rendering/wrapper/Window.h"
 
-Window::Window(const WindowSize& size, const char* windowName) : window(nullptr) {
+Window::Window(WindowSize& size, const Options& options) : window(nullptr), size(size) {
     glfwDefaultWindowHints();
     glfwWindowHint(GLFW_VISIBLE, 0);
     glfwWindowHint(GLFW_RESIZABLE, 1);
-    glfwWindowHint(GLFW_DECORATED, 0);
+    glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
     glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
-    
+
     glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
 
-    window = glfwCreateWindow(size.width, size.height, windowName, glfwGetPrimaryMonitor(), nullptr);
+    GLFWmonitor* monitor = options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
+    window = glfwCreateWindow(size.width, size.height, options.name, monitor, nullptr);
     if(window == nullptr) {
         std::cout << "could not create window\n";
         return;
@@ -23,6 +24,18 @@ Window::Window(const WindowSize& size, const char* windowName) : window(nullptr)
     glfwSwapInterval(0);
 
     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);
+        w.size.width = newWidth;
+        w.size.height = newHeight;
+        std::cout << "WUSI\n";
+    });
 }
 
 Window::~Window() {
@@ -48,18 +61,6 @@ void Window::swapBuffers() {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
-void Window::setFramebufferSizeCallback(GLFWframebuffersizefun f) {
-    glfwSetFramebufferSizeCallback(window, f);
-}
-
 void Window::setKeyCallback(GLFWkeyfun f) {
     glfwSetKeyCallback(window, f);
-}
-
-void Window::setMouseButtonCallback(GLFWmousebuttonfun f) {
-    glfwSetMouseButtonCallback(window, f);
-}
-
-void Window::setCursorPosCallback(GLFWcursorposfun f) {
-    glfwSetCursorPosCallback(window, f);
 }

+ 5 - 7
rendering/wrapper/Window.h

@@ -5,10 +5,14 @@
 #include <GLFW/glfw3.h>
 
 #include "rendering/WindowSize.h"
+#include "rendering/Options.h"
 
 class Window final {
+    GLFWwindow* window;
+    WindowSize& size;
+
 public:
-    Window(const WindowSize& size, const char* windowName);
+    Window(WindowSize& size, const Options& options);
     ~Window();
 
     Window(const Window&) = delete;
@@ -21,13 +25,7 @@ public:
     bool shouldClose() const;
     void swapBuffers();
 
-    void setFramebufferSizeCallback(GLFWframebuffersizefun f);
     void setKeyCallback(GLFWkeyfun f);
-    void setMouseButtonCallback(GLFWmousebuttonfun f);
-    void setCursorPosCallback(GLFWcursorposfun f);
-
-private:
-    GLFWwindow* window;
 };
 
 #endif

BIN
resources/font.png


+ 1 - 1
resources/shader/fragment.fs

@@ -15,7 +15,7 @@ void main() {
     if(useTexture) {
         color = texture2D(samp, varTex);
         if(useColor) {
-            color = vec4(varColor.xyz, color.w);
+            color = vec4(varColor.xyz, color.r);
         }
     } else {
         color = varColor;

+ 10 - 15
utils/Array.h

@@ -1,34 +1,32 @@
 #ifndef ARRAY_H
 #define ARRAY_H
 
-#include "utils/Types.h"
-
-template<typename T, uint N>
+template<typename T, int N>
 class Array final {
-public:
+    T data[N];
 
-    Array() {
-    }
+public:
+    Array() = default;
 
     Array(const T& t) {
-        for(uint i = 0; i < N; i++) {
+        for(int i = 0; i < N; i++) {
             data[i] = t;
         }
     }
 
-    const T& operator[](uint index) const {
+    const T& operator[](int index) const {
         return data[index];
     }
 
-    T& operator[](uint index) {
+    T& operator[](int index) {
         return data[index];
     }
 
-    const T* operator+(uint index) const {
+    const T* operator+(int index) const {
         return data + index;
     }
 
-    T* operator+(uint index) {
+    T* operator+(int index) {
         return data + index;
     }
 
@@ -48,12 +46,9 @@ public:
         return data + N;
     }
 
-    uint getLength() const {
+    int getLength() const {
         return N;
     }
-
-private:
-    T data[N];
 };
 
 #endif

+ 4 - 5
utils/Clock.cpp

@@ -1,12 +1,11 @@
 #include "utils/Clock.h"
-#include "rendering/wrapper/GLFWWrapper.h"
 
-Clock::Clock() : last(GLFWWrapper::getTimeNanos()), index(0), sum(0), time(0) {
+Clock::Clock() : index(0), last(GLFW::getNanos()), sum(0), time(0) {
 }
 
-u64 Clock::update() {
+Nanos Clock::update() {
     index = (index + 1) & (length - 1);
-    u64 current = GLFWWrapper::getTimeNanos();
+    Nanos current = GLFW::getNanos();
     sum -= time[index];
     time[index] = current - last;
     sum += time[index];
@@ -14,7 +13,7 @@ u64 Clock::update() {
     return time[index];
 }
 
-u64 Clock::getLength() const {
+Nanos Clock::getLength() const {
     return length;
 }
 

+ 12 - 11
utils/Clock.h

@@ -1,23 +1,24 @@
 #ifndef CLOCK_H
 #define CLOCK_H
 
-#include "utils/Types.h"
 #include "utils/Array.h"
 
+#include "rendering/wrapper/GLFW.h"
+
 class Clock final {
+    static constexpr int bits = 7;
+    static constexpr int length = 1 << bits;
+    
+    int index;
+    Nanos last;
+    Nanos sum;
+    Array<Nanos, length> time;
+    
 public:
     Clock();
-    u64 update();
-    u64 getLength() const;
+    Nanos update();
+    Nanos getLength() const;
     float getUpdatesPerSecond() const;
-
-private:
-    static constexpr u64 bits = 7;
-    static constexpr u64 length = 1 << bits;
-    u64 last;
-    u64 index;
-    s64 sum;
-    Array<u64, length> time;
 };
 
 #endif

+ 8 - 11
utils/List.h

@@ -1,10 +1,11 @@
 #ifndef LIST_H
 #define LIST_H
 
-#include "utils/Types.h"
-
-template<typename T, uint L>
+template<typename T, int L>
 class List final {
+    int entries;
+    T data[L];
+
 public:
 
     List() : entries(0) {
@@ -23,19 +24,19 @@ public:
         return *this;
     }
 
-    uint getLength() const {
+    int getLength() const {
         return entries;
     }
 
-    uint getCapacity() const {
+    int getCapacity() const {
         return L;
     }
 
-    T& operator[](uint index) {
+    T& operator[](int index) {
         return data[index];
     }
 
-    const T& operator[](uint index) const {
+    const T& operator[](int index) const {
         return data[index];
     }
 
@@ -58,10 +59,6 @@ public:
     const T* end() const {
         return data + entries;
     }
-
-private:
-    uint entries;
-    T data[L];
 };
 
 #endif

+ 70 - 62
utils/PNGReader.cpp

@@ -4,82 +4,90 @@
 
 #include "utils/PNGReader.h"
 
-static bool checkSignature(const char* path, FILE* file) {
-    unsigned char buffer[8];
-    if(fread(buffer, sizeof (char), 8, file) != 8) {
-        std::cout << "cannot read signature of texture '" << path << "'\n";
-        return true;
+PNGReader::PNGReader(const char* path) : path(path), width(0), height(0), channels(0),
+file(fopen(path, "r")), read(nullptr), info(nullptr), rowPointers(nullptr) {
+    if(file == nullptr) {
+        std::cout << "file '" << path << "' cannot be read: " << strerror(errno) << "\n";
+        return;
     }
-    if(png_sig_cmp(buffer, 0, 8)) {
-        std::cout << "file '" << path << "' is not a texture\n";
-        return true;
+    if(checkSignature()) {
+        return;
     }
-    return false;
+    read = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
+    if(read == nullptr) {
+        std::cout << "cannot create png read data structure\n";
+        return;
+    }
+    info = png_create_info_struct(read);
+    if(info == nullptr) {
+        std::cout << "cannot create png info structure\n";
+        return;
+    }
+    if(setjmp(png_jmpbuf(read))) {
+        std::cout << "png file '" << path << "' has used error callback\n";
+        return;
+    }
+    png_init_io(read, file);
+    png_set_sig_bytes(read, 8);
+    png_read_info(read, info);
+    width = png_get_image_width(read, info);
+    height = png_get_image_height(read, info);
+    channels = png_get_channels(read, info);
 }
 
-static bool load(const char* path, FILE* file, u32* buffer, u32& maxWidth, u32& maxHeight) {
-    if(checkSignature(path, file)) {
-        return false;
+PNGReader::~PNGReader() {
+    if(file != nullptr) {
+        fclose(file);
     }
-
-    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
-    if(png == nullptr) {
-        std::cout << "cannot create texture data structure\n";
-        return false;
+    if(rowPointers != nullptr) {
+        png_free(read, rowPointers);
     }
+    png_destroy_read_struct(&read, &info, nullptr);
+}
 
-    png_infop info = png_create_info_struct(png);
-    if(info == nullptr) {
-        std::cout << "cannot create image info structure\n";
-        return false;
-    }
+int PNGReader::getWidth() const {
+    return width;
+}
+
+int PNGReader::getHeight() const {
+    return height;
+}
 
-    u32** rowPointers = nullptr;
+int PNGReader::getChannels() const {
+    return channels;
+}
 
-    if(setjmp(png_jmpbuf(png))) { // set callback for errors
-        if(rowPointers != nullptr) {
-            png_free(png, rowPointers);
-        }
-        png_destroy_read_struct(&png, &info, nullptr);
-        std::cout << "texture '" << path << "' has used error callback\n";
-        return false;
-    }
+int PNGReader::getBufferSize() const {
+    return width * height * channels;
+}
 
-    png_init_io(png, file); // set reading function
-    png_set_sig_bytes(png, 8); // notify about already used signature bytes
+bool PNGReader::hasError() const {
+    return width == 0 || height == 0 || channels == 0;
+}
 
-    png_read_info(png, info); // read info data
-    
-    u32 width = png_get_image_width(png, info);
-    u32 height = png_get_image_height(png, info);
-    if(width > maxWidth || height > maxHeight) {
-        std::cout << "texture '" << path << "' is too big: " << maxWidth << " x " << maxHeight << " allowed\n";
-        return false;
+bool PNGReader::checkSignature() {
+    png_byte buffer[8];
+    if(fread(buffer, sizeof (png_byte), 8, file) != 8) {
+        std::cout << "cannot read signature of file '" << path << "'\n";
+        return true;
     }
-    maxWidth = width;
-    maxHeight = height;
-    
-    // allocate and set row pointer to correct places in block
-    rowPointers = static_cast<u32**> (png_malloc(png, height * (sizeof (u32*))));
-    for(uint i = 0; i < height; i++) {
-        rowPointers[i] = (buffer + i * width);
+    if(png_sig_cmp(buffer, 0, 8)) {
+        std::cout << "file '" << path << "' is not a png\n";
+        return true;
     }
-    png_set_rows(png, info, reinterpret_cast<png_bytepp> (rowPointers));
-
-    png_read_image(png, reinterpret_cast<png_bytepp> (rowPointers));
-
-    png_free(png, rowPointers);
-    png_destroy_read_struct(&png, &info, nullptr);
-    return true;
+    return false;
 }
 
-bool PNGReader::load(const char* path, u32* buffer, u32& maxWidth, u32& maxHeight) {
-    FILE* file = fopen(path, "r");
-    if(file == nullptr) {
-        std::cout << "texture '" << path << "' cannot be read: " << strerror(errno) << "\n";
-        return false;
+bool PNGReader::readData(char* buffer) {
+    if(setjmp(png_jmpbuf(read))) {
+        std::cout << "png file '" << path << "' has used error callback\n";
+        return true;
+    }
+    rowPointers = static_cast<char**> (png_malloc(read, height * (sizeof (char*))));
+    for(int i = 0; i < height; i++) {
+        rowPointers[i] = (buffer + i * width * channels);
     }
-    bool result = load(path, file, buffer, maxWidth, maxHeight);
-    fclose(file);
-    return result;
+    png_set_rows(read, info, reinterpret_cast<png_bytepp> (rowPointers));
+    png_read_image(read, reinterpret_cast<png_bytepp> (rowPointers));
+    return false;
 }

+ 31 - 4
utils/PNGReader.h

@@ -1,10 +1,37 @@
 #ifndef PNGREADER_H
 #define PNGREADER_H
 
-#include "utils/Types.h"
+#include <png.h>
 
-namespace PNGReader {
-    bool load(const char* path, u32* buffer, u32& maxWidth, u32& maxHeight);
-}
+typedef png_int_32 Color;
+
+class PNGReader final {
+    const char* path;
+    int width;
+    int height;
+    int channels;
+    FILE* file;
+    png_structp read;
+    png_infop info;
+    char** rowPointers;
+    
+    bool checkSignature();
+    
+public:
+    PNGReader(const char* path);
+    ~PNGReader();
+    PNGReader(const PNGReader& other) = delete;
+    PNGReader(PNGReader&& other) = delete;
+    PNGReader& operator=(const PNGReader& other) = delete;
+    PNGReader& operator=(PNGReader&& other) = delete;
+    
+    int getWidth() const;
+    int getHeight() const;
+    int getChannels() const;
+    int getBufferSize() const;
+    bool hasError() const;
+    
+    bool readData(char* buffer);
+};
 
 #endif

+ 3 - 7
utils/String.cpp

@@ -26,11 +26,11 @@ String::operator const char*() const {
     return data;
 }
 
-char String::operator[](uint index) const {
+char String::operator[](int index) const {
     return data[index];
 }
 
-uint String::getLength() const {
+int String::getLength() const {
     return length;
 }
 
@@ -43,17 +43,13 @@ String& String::append(char c) {
 }
 
 String& String::append(const char* str) {
-    for(uint i = 0; length < MAX_LENGTH - 1 && str[i] != '\0'; length++, i++) {
+    for(int i = 0; length < MAX_LENGTH - 1 && str[i] != '\0'; length++, i++) {
         data[length] = str[i];
     }
     data[length] = '\0';
     return *this;
 }
 
-String& String::append(uint i) {
-    return append("%u", i);
-}
-
 String& String::append(int i) {
     return append("%d", i);
 }

+ 6 - 9
utils/String.h

@@ -1,8 +1,6 @@
 #ifndef STRING_H
 #define STRING_H
 
-#include "utils/Types.h"
-
 class String final {
 public:
     String();
@@ -11,13 +9,13 @@ public:
     bool operator==(const String& other) const;
     bool operator!=(const String& other) const;
     operator const char*() const;
-    char operator[](uint index) const;
-    uint getLength() const;
+    char operator[](int index) const;
+    int getLength() const;
 
     template<typename T>
     String& append(const char* format, const T& t) {
-        uint left = MAX_LENGTH - length;
-        uint written = snprintf(data + length, left, format, t);
+        int left = MAX_LENGTH - length;
+        int written = snprintf(data + length, left, format, t);
         if(written < left) {
             length += written;
         } else {
@@ -28,15 +26,14 @@ public:
 
     String& append(char c);
     String& append(const char* str);
-    String& append(uint i);
     String& append(int i);
     String& append(float f);
     String& append(bool b);
 
 private:
-    static constexpr uint MAX_LENGTH = 255;
+    static constexpr int MAX_LENGTH = 256 - sizeof(int);
     char data[MAX_LENGTH];
-    u8 length;
+    int length;
 };
 
 #endif

+ 0 - 26
utils/Types.h

@@ -1,26 +0,0 @@
-#ifndef TYPES_H
-#define TYPES_H
-
-typedef unsigned int uint;
-
-typedef unsigned char u8;
-typedef unsigned short u16;
-typedef unsigned int u32;
-typedef unsigned long long u64;
-
-typedef char s8;
-typedef short s16;
-typedef int s32;
-typedef long long s64;
-
-static_assert(sizeof(u8) == 1, "u8 is not 8 bit");
-static_assert(sizeof(u16) == 2, "u16 is not 16 bit");
-static_assert(sizeof(u32) == 4, "u32 is not 32 bit");
-static_assert(sizeof(u64) == 8, "u64 is not 64 bit");
-
-static_assert(sizeof(s8) == 1, "s8 is not 8 bit");
-static_assert(sizeof(s16) == 2, "s16 is not 16 bit");
-static_assert(sizeof(s32) == 4, "s32 is not 64 bit");
-static_assert(sizeof(s64) == 8, "s64 is not 64 bit");
-
-#endif