Browse Source

replaced png reader with the png reader from the gaming core

Kajetan Johannes Hammerle 3 years ago
parent
commit
29c4b6ce83

+ 1 - 1
Game.cpp

@@ -65,7 +65,7 @@ void Game::tick() {
 
 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.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, Color4(255, 0, 0, 255));
     renderer.translateTo(0.0f, 0.0f).update();
     StringBuffer<100> s;
     s.append("FPS: ").append(fps.getUpdatesPerSecond());

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 375ceefe325e4b3394b613f6a8952eef20410f3d
+Subproject commit bff2959facb9b6d78684bc188f5b9e326dbc95d6

+ 1 - 1
meson.build

@@ -15,7 +15,7 @@ sources = [
     'gaming-core/math/Matrix.cpp',
     'gaming-core/math/Quaternion.cpp',
     'gaming-core/math/Vector.cpp',
-    'utils/PNGReader.cpp',
+    'gaming-core/images/PNGReader.cpp',
     'rendering/wrapper/StreamBuffer.cpp',
     'rendering/wrapper/Texture.cpp',
     'rendering/wrapper/VertexBuffer.cpp',

+ 2 - 2
rendering/ColorRenderer.h

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

+ 15 - 11
rendering/FileTexture.cpp

@@ -1,20 +1,24 @@
-#include <iostream>
-
 #include "rendering/FileTexture.h"
-#include "utils/PNGReader.h"
 
 FileTexture::FileTexture(const char* path) {
-    PNGReader reader(path);
-    if(reader.hasError()) {
+    PNGReader png(path);
+    if(png.hasError()) {
         return;
     }
-    char* buffer = new char[reader.getBufferSize()];
-    if(reader.readData(buffer)) {
-        delete[] buffer;
-        return;
+    switch(png.getChannels()) {
+        case 1:
+            read<1>(png);
+            break;
+        case 2:
+            read<2>(png);
+            break;
+        case 3:
+            read<3>(png);
+            break;
+        case 4:
+            read<4>(png);
+            break;
     }
-    texture.setColors(reader.getWidth(), reader.getHeight(), buffer, reader.getChannels());
-    delete[] buffer;
 }
 
 void FileTexture::bind() const {

+ 13 - 1
rendering/FileTexture.h

@@ -2,15 +2,27 @@
 #define FILE_TEXTURE_H
 
 #include "rendering/wrapper/Texture.h"
+#include "gaming-core/images/PNGReader.h"
 
 class FileTexture final {
+    Texture texture;
+
 public:
     FileTexture(const char* path);
 
     void bind() const;
 
 private:
-    Texture texture;
+
+    template<int N>
+    void read(PNGReader& png) {
+        Color<N>* buffer = new Color<N>[png.getBufferSize()];
+        if(png.readData(buffer)) {
+            return;
+        }
+        texture.setColorData(png.getWidth(), png.getHeight(), buffer);
+        delete[] buffer;
+    }
 };
 
 #endif

+ 2 - 2
rendering/Renderer.cpp

@@ -85,14 +85,14 @@ 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, Color color) {
+void Renderer::drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, Color4 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) {
+void Renderer::drawRectangle(float x, float y, float width, float height, Color4 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

@@ -29,8 +29,8 @@ public:
     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);
+    void drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, Color4 color);
+    void drawRectangle(float x, float y, float width, float height, Color4 color);
 
 private:
     void setTextureMode(bool b);

+ 14 - 16
rendering/wrapper/Texture.cpp

@@ -14,22 +14,20 @@ Texture::~Texture() {
     glDeleteTextures(1, &texture);
 }
 
-void Texture::setColors(int width, int height, const char* data, int channels) {
-    glBindTexture(GL_TEXTURE_2D, texture);
-    switch(channels) {
-        case 1:
-            glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
-            break;
-        case 2:
-            glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, width, height, 0, GL_RG, GL_UNSIGNED_BYTE, data);
-            break;
-        case 3:
-            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, 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::setColorData(int width, int height, const Color4* data) {
+    setColorData(width, height, GL_RGBA, data);
+}
+
+void Texture::setColorData(int width, int height, const Color3* data) {
+    setColorData(width, height, GL_RGB, data);
+}
+
+void Texture::setColorData(int width, int height, const Color2* data) {
+    setColorData(width, height, GL_RG, data);
+}
+
+void Texture::setColorData(int width, int height, const Color1* data) {
+    setColorData(width, height, GL_RED, data);
 }
 
 void Texture::bind() const {

+ 15 - 5
rendering/wrapper/Texture.h

@@ -3,9 +3,11 @@
 
 #include <GL/glew.h>
 
-#include "utils/PNGReader.h"
+#include "gaming-core/utils/Color.h"
 
 class Texture final {
+    GLuint texture;
+
 public:
     Texture();
     ~Texture();
@@ -13,13 +15,21 @@ public:
     Texture(Texture&& other) = delete;
     Texture& operator=(const Texture& other) = delete;
     Texture& operator=(Texture&& other) = delete;
-    
-    void setColors(int width, int height, const char* data, int channels);
-    
+
+    void setColorData(int width, int height, const Color4* data);
+    void setColorData(int width, int height, const Color3* data);
+    void setColorData(int width, int height, const Color2* data);
+    void setColorData(int width, int height, const Color1* data);
+
     void bind() const;
 
 private:
-    GLuint texture;
+
+    template<int N>
+    void setColorData(int width, int height, int mode, const Color<N>* data) {
+        glBindTexture(GL_TEXTURE_2D, texture);
+        glTexImage2D(GL_TEXTURE_2D, 0, mode, width, height, 0, mode, GL_UNSIGNED_BYTE, data);
+    }
 };
 
 #endif

+ 0 - 103
utils/PNGReader.cpp

@@ -1,103 +0,0 @@
-#include <iostream>
-#include <png.h>
-#include <cstring>
-
-#include "utils/PNGReader.h"
-
-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(checkSignature()) {
-        return;
-    }
-    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);
-}
-
-PNGReader::~PNGReader() {
-    if(file != nullptr) {
-        fclose(file);
-    }
-    if(rowPointers != nullptr) {
-        png_free(read, rowPointers);
-    }
-    png_destroy_read_struct(&read, &info, nullptr);
-}
-
-int PNGReader::getWidth() const {
-    return width;
-}
-
-int PNGReader::getHeight() const {
-    return height;
-}
-
-int PNGReader::getChannels() const {
-    return channels;
-}
-
-int PNGReader::getBufferSize() const {
-    return width * height * channels;
-}
-
-bool PNGReader::hasError() const {
-    if(channels < 1 || channels > 4) {
-        std::cout << "'" << path << "' has unsupported number of channels: " << channels << "\n";
-        return true;
-    } else if(width != height) {
-        std::cout << "width and height of '" << path << "' are not equal\n";
-        return true;
-    } else if(width < 1 || width > 2048) {
-        std::cout << "width and height of '" << path << "' are too big\n";
-        return true;
-    }
-    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;
-    }
-    if(png_sig_cmp(buffer, 0, 8)) {
-        std::cout << "file '" << path << "' is not a png\n";
-        return true;
-    }
-    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);
-    }
-    png_set_rows(read, info, reinterpret_cast<png_bytepp> (rowPointers));
-    png_read_image(read, reinterpret_cast<png_bytepp> (rowPointers));
-    return false;
-}

+ 0 - 37
utils/PNGReader.h

@@ -1,37 +0,0 @@
-#ifndef PNGREADER_H
-#define PNGREADER_H
-
-#include <png.h>
-
-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