Browse Source

animation tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
5c2a88509a

+ 47 - 4
Game.cpp

@@ -3,22 +3,65 @@
 #include "rendering/wrapper/GLFWWrapper.h"
 #include "utils/String.h"
 
+float x = 0.0f;
+float y = 0.0f;
+float lastX = 0.0f;
+float lastY = 0.0f;
+float playerWidth = 80.0f;
+float playerHeight = 80.0f;
+
+float width = 800.0f;
+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) {
 }
 
 void Game::tick() {
+    lastX = x;
+    lastY = y;
+    if(controller.isDown(Button::LEFT)) {
+        x -= 7;
+    }
+    if(controller.isDown(Button::RIGHT)) {
+        x += 7;
+    }
+    if(controller.isDown(Button::A) && onGround) {
+        motionY = -15.0f;
+        onGround = false;
+    }
+
+    motionY += 0.5f;
+    y += motionY;
+
+    if(y > height - playerHeight) {
+        y = height - playerHeight;
+        motionY = 0.0f;
+        onGround = true;
+    }
+    while(x > width) {
+        x -= width + playerWidth;
+        lastX -= width + playerWidth;
+    }
+    while(x < -playerWidth) {
+        x += width + playerWidth;
+        lastX += width + playerWidth;
+    }
 }
 
 void Game::render(float lag, Renderer& renderer) const {
-    (void) lag;
-    renderer.translateTo(0.0f, 0.0f).scale(4.0f).update();
+    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)).append("öüäÖÜÄÖß");
+        s.append(": ").append(controller.wasReleased(i)).append(" ").append(controller.getDownTime(i));
         renderer.drawString(0, 10 * i, s);
     }
+    renderer.translateTo(0.0f, 0.0f).update();
+    renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, 0x0000FFFF);
 }
 
 bool Game::isRunning() const {
-    return true;
+    return !controller.isDown(Button::SELECT);
 }

+ 1 - 1
Main.cpp

@@ -44,7 +44,7 @@ int main() {
     if(shader.hasError()) {
         return 0;
     }
-    
+       
     Renderer renderer(size, shader);
     
     Controller controller;

+ 1 - 1
meson.build

@@ -1,6 +1,6 @@
 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/FontRenderer.cpp', 'rendering/Mesh.cpp', 'utils/String.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']
 
 glewDep = dependency('glew')
 glfwDep = dependency('glfw3')

+ 17 - 0
rendering/ColorRenderer.cpp

@@ -0,0 +1,17 @@
+#include "rendering/ColorRenderer.h"
+
+ColorRenderer::ColorRenderer() : buffer(8 * 1024 * 1024, 2 * sizeof(float) + 4) {
+    vertexBuffer.bind();
+    uint step = 2 * sizeof(float) + 4;
+    vertexBuffer.setFloatAttribute(0, 2, 0, step);
+    vertexBuffer.setByteAttribute(2, 4, 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.draw();
+}

+ 24 - 0
rendering/ColorRenderer.h

@@ -0,0 +1,24 @@
+#ifndef COLORRENDERER_H
+#define COLORRENDERER_H
+
+#include "rendering/wrapper/VertexBuffer.h"
+#include "rendering/wrapper/StreamBuffer.h"
+
+class ColorRenderer final {
+public:
+    ColorRenderer();
+
+    struct Vertex {
+        float x;
+        float y;
+        u32 color;
+    };
+
+    void draw(const Vertex& v1, const Vertex& v2, const Vertex& v3);
+
+private:
+    VertexBuffer vertexBuffer;
+    StreamBuffer buffer;
+};
+
+#endif

+ 7 - 8
rendering/FontRenderer.cpp

@@ -1,12 +1,11 @@
-#include <iostream>
-
 #include "rendering/FontRenderer.h"
 
-FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024), tex("resources/font.png") {
+FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024, 8 * sizeof(float)), tex("resources/font.png") {
     vertexBuffer.bind();
-    vertexBuffer.setFloatAttribute(0, 2, 0, 8);
-    vertexBuffer.setFloatAttribute(1, 2, 2, 8);
-    vertexBuffer.setFloatAttribute(2, 4, 4, 8);
+    uint 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);
 }
 
 void FontRenderer::drawString(float x, float y, const char* text) {
@@ -52,5 +51,5 @@ void FontRenderer::drawString(float x, float y, const char* text) {
     }
 
     tex.bind();
-    buffer.draw(8);
-}
+    buffer.draw();
+}

+ 3 - 2
rendering/Mesh.cpp

@@ -1,6 +1,7 @@
 #include "rendering/Mesh.h"
 
 Mesh::Mesh() {
+    vertexBuffer.bind();
     vertexBuffer.bind();
     vertexBuffer.setFloatAttribute(0, 3, 0, 8);
     vertexBuffer.setFloatAttribute(1, 2, 3, 8);
@@ -16,11 +17,11 @@ void Mesh::clear() {
 }
 
 void Mesh::build() {
-    vertexBuffer.bindBuffer();
+    vertexBuffer.bind();
     vertexBuffer.setData(sizeof (VertexData) * buffer.getLength(), buffer.getData());
 }
 
 void Mesh::draw() const {
-    vertexBuffer.bindArray();
+    vertexBuffer.bind();
     vertexBuffer.draw(buffer.getLength());
 }

+ 15 - 0
rendering/Renderer.cpp

@@ -73,4 +73,19 @@ void Renderer::drawString(float x, float y, const char* text) {
     setTextureMode(true);
     setColorMode(true);
     fontRenderer.drawString(x, y, text);
+}
+
+void Renderer::drawTriangle(const ColorRenderer::Vertex& v1, const ColorRenderer::Vertex& v2, const ColorRenderer::Vertex& v3) {
+    setTextureMode(false);
+    setColorMode(true);
+    colorRenderer.draw(v1, v2, v3);
+}
+
+void Renderer::drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, u32 color) {
+    drawTriangle({x1, y1, color}, {x2, y2, color}, {x3, y3, color});
+}
+
+void Renderer::drawRectangle(float x, float y, float width, float height, u32 color) {
+    drawTriangle(x, y, x + width, y, x, y + height, color);
+    drawTriangle(x + width, y, x, y + height, x + width, y + height, color);
 }

+ 5 - 0
rendering/Renderer.h

@@ -5,6 +5,7 @@
 #include "rendering/WindowSize.h"
 #include "math/MatrixStack.h"
 #include "rendering/FontRenderer.h"
+#include "rendering/ColorRenderer.h"
 
 class Renderer final {
 public:
@@ -26,6 +27,9 @@ public:
     Renderer& rotate(float degrees);
 
     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);
 
 private:
     void setTextureMode(bool b);
@@ -37,6 +41,7 @@ private:
     bool color;
     MatrixStack stack;
     FontRenderer fontRenderer;
+    ColorRenderer colorRenderer;
 };
 
 #endif

+ 18 - 7
rendering/wrapper/StreamBuffer.cpp

@@ -1,28 +1,39 @@
 #include <GL/glew.h>
 #include <cassert>
+#include <cstring>
 
 #include "rendering/wrapper/StreamBuffer.h"
 
-StreamBuffer::StreamBuffer(u64 size) : bufferSize(size), offset(size), index(0), buffer(nullptr) {
+StreamBuffer::StreamBuffer(uint size, uint bytesPerVertex) :
+bufferSize(size), offset(size), bytesPerVertex(bytesPerVertex), index(0), buffer(nullptr) {
 }
 
-void StreamBuffer::reset(u64 size) {
+void StreamBuffer::reset(uint size) {
     if(offset + size >= bufferSize) {
         offset = 0;
         glBufferData(GL_ARRAY_BUFFER, bufferSize, nullptr, GL_STREAM_DRAW);
     }
-    buffer = static_cast<float*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
+    buffer = static_cast<u8*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
     assert(buffer != nullptr);
     index = 0;
 }
 
 StreamBuffer& StreamBuffer::add(float f) {
-    buffer[index++] = f;
+    memcpy(buffer + index, &f, sizeof (f));
+    index += sizeof (f);
     return *this;
 }
 
-void StreamBuffer::draw(uint floatPerVertex) {
+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 / (sizeof (float) * floatPerVertex), index / floatPerVertex);
-    offset += index * sizeof(float);
+    glDrawArrays(GL_TRIANGLE_STRIP, offset / bytesPerVertex, index / bytesPerVertex);
+    offset += index;
 }

+ 10 - 8
rendering/wrapper/StreamBuffer.h

@@ -5,22 +5,24 @@
 
 class StreamBuffer final {
 public:
-    StreamBuffer(u64 size);
+    StreamBuffer(uint size, uint bytesPerVertex);
     StreamBuffer(const StreamBuffer& other) = delete;
     StreamBuffer(StreamBuffer&& other) = delete;
     StreamBuffer& operator=(const StreamBuffer& other) = delete;
     StreamBuffer& operator=(StreamBuffer&& other) = delete;
 
-    void reset(u64 size);
+    void reset(uint size);
     StreamBuffer& add(float f);
-    void draw(uint floatPerVertex);
+    StreamBuffer& addReversed(u32 i);
+    void draw();
 
 private:
-    u64 bufferSize;
-    u64 offset;
-
-    u64 index;
-    float* buffer;
+    uint bufferSize;
+    uint offset;
+    uint bytesPerVertex;
+    
+    uint index;
+    u8* buffer;
 };
 
 #endif

+ 7 - 2
rendering/wrapper/VertexBuffer.cpp

@@ -11,7 +11,12 @@ VertexBuffer::~VertexBuffer() {
 }
 
 void VertexBuffer::setFloatAttribute(uint index, uint length, uint offset, uint step) {
-    glVertexAttribPointer(index, length, GL_FLOAT, false, sizeof (float) * step, static_cast<float*> (0) + offset);
+    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);
     glEnableVertexAttribArray(index);
 }
 
@@ -34,4 +39,4 @@ void VertexBuffer::bind() const {
 
 void VertexBuffer::draw(uint vertices) const {
     glDrawArrays(GL_TRIANGLES, 0, vertices);
-}
+}

+ 1 - 0
rendering/wrapper/VertexBuffer.h

@@ -15,6 +15,7 @@ public:
     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 bindArray() const;

+ 0 - 4
utils/Types.h

@@ -13,8 +13,6 @@ typedef short s16;
 typedef int s32;
 typedef long long s64;
 
-typedef u32 size;
-
 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");
@@ -25,6 +23,4 @@ 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");
 
-static_assert(sizeof(size) == sizeof(nullptr), "size has not the same size as a nullpointer");
-
 #endif