Bläddra i källkod

file textures use stack allocators, simplified file texture and png
reader

Kajetan Johannes Hammerle 3 år sedan
förälder
incheckning
1afdb90909

+ 1 - 1
images/PNGReader.cpp

@@ -99,7 +99,7 @@ bool PNGReader::checkSignature() {
     return false;
 }
 
-bool PNGReader::readData(unsigned char* buffer) {
+bool PNGReader::readData(ColorChannel* buffer) {
     if(setjmp(png_jmpbuf(read))) {
         std::cout << "png file '" << path << "' has used error callback\n";
         return true;

+ 2 - 9
images/PNGReader.h

@@ -31,16 +31,9 @@ public:
     int getBufferSize() const;
     bool hasError() const;
 
-    template<int N>
-    bool readData(Color<N>* buffer) {
-        if(channels != N) {
-            return true;
-        }
-        return readData(reinterpret_cast<ColorChannel*> (buffer));
-    }
-
-private:
     bool readData(ColorChannel* buffer);
+    
+private:
     bool checkSignature();
 };
 

+ 1 - 0
meson.build

@@ -43,6 +43,7 @@ sources = ['Main.cpp',
     'input/Button.cpp',
     'input/Buttons.cpp',
     'wrapper/TextureFormat.cpp',
+    'rendering/FileTexture.cpp',
     'memory/StackAllocator.cpp',
     'tests/StackAllocatorTests.cpp',
     'utils/Buffer.cpp']

+ 19 - 0
rendering/FileTexture.cpp

@@ -0,0 +1,19 @@
+#include "rendering/FileTexture.h"
+#include "images/PNGReader.h"
+#include "memory/StackAllocator.h"
+
+FileTexture::FileTexture(const char* path) {
+    PNGReader png(path);
+    if(png.hasError()) {
+        return;
+    }
+    StackAllocator::Array<ColorChannel> buffer(png.getBufferSize());
+    if(buffer.getLength() == png.getBufferSize() && !png.readData(buffer)) {
+        texture.setFormat(TextureFormat::color8(png.getChannels()));
+        texture.setData(png.getWidth(), png.getHeight(), buffer);
+    }
+}
+
+void FileTexture::bindTo(int index) const {
+    texture.bindTo(index);
+}

+ 3 - 24
rendering/FileTexture.h

@@ -1,36 +1,15 @@
 #ifndef FILETEXTURE_H
 #define FILETEXTURE_H
 
-#include <iostream>
-
 #include "wrapper/Texture.h"
-#include "images/PNGReader.h"
 
-template<int N>
 class FileTexture final {
     Texture texture;
 
 public:
-
-    FileTexture(const char* path) : texture(TextureFormat::color8(N)) {
-        PNGReader png(path);
-        if(png.hasError()) {
-            return;
-        }
-        if(png.getChannels() != N) {
-            std::cout << "expected " << N << " from '" << path << "' but got " << png.getChannels() << "\n";
-            return;
-        }
-        Color<N>* buffer = new Color<N>[png.getBufferSize()];
-        if(!png.readData(buffer)) {
-            texture.setData(png.getWidth(), png.getHeight(), buffer);
-        }
-        delete[] buffer;
-    }
-
-    void bindTo(int index) const {
-        texture.bindTo(index);
-    }
+    FileTexture(const char* path);
+    
+    void bindTo(int index) const;
 };
 
 #endif

+ 16 - 20
tests/PNGReaderTests.cpp

@@ -2,19 +2,15 @@
 #include "tests/Test.h"
 #include "images/PNGReader.h"
 #include "utils/StringBuffer.h"
+#include "memory/StackAllocator.h"
 
-template<int N>
-static void testRead(int correct, Test& test, PNGReader& png, const char* text) {
-    Color<N>* buffer = new Color<N>[png.getBufferSize()];
-    test.checkEqual(N != correct, png.readData(buffer), text);
-    delete[] buffer;
-}
-
-static void testFullRead(int correct, Test& test, PNGReader& png, const char* text) {
-    testRead<1>(correct, test, png, text);
-    testRead<2>(correct, test, png, text);
-    testRead<3>(correct, test, png, text);
-    testRead<4>(correct, test, png, text);
+static void testRead(Test& test, PNGReader& png, const char* text) {
+    StackAllocator::Array<ColorChannel> buffer(png.getBufferSize());
+    test.checkEqual(true, png.getBufferSize() > 0, text);
+    test.checkEqual(png.getBufferSize(), buffer.getLength(), text);
+    if(png.getBufferSize() == buffer.getLength()) {
+        test.checkEqual(false, png.readData(buffer), text);
+    }
 }
 
 static void testReadRGB8(Test& test, const char* path) {
@@ -26,7 +22,7 @@ static void testReadRGB8(Test& test, const char* path) {
     test.checkEqual(32, png.getWidth(), "rgb8 width");
     test.checkEqual(64, png.getHeight(), "rgb8 height");
     test.checkEqual(3, png.getChannels(), "rgb8 channels");
-    testFullRead(3, test, png, "rgb8 read");
+    testRead(test, png, "rgb8 read");
 }
 
 static void testReadRGB16(Test& test, const char* path) {
@@ -39,7 +35,7 @@ static void testReadRGB16(Test& test, const char* path) {
     test.checkEqual(64, png.getHeight(), "rgb16 height");
     test.checkEqual(3, png.getChannels(), "rgb16 channels");
     test.checkEqual(32 * 64, png.getBufferSize(), "rgb16 channels");
-    testFullRead(3, test, png, "rgb16 read");
+    testRead(test, png, "rgb16 read");
 }
 
 static void testReadRGBA8(Test& test, const char* path) {
@@ -52,7 +48,7 @@ static void testReadRGBA8(Test& test, const char* path) {
     test.checkEqual(64, png.getHeight(), "rgba8 height");
     test.checkEqual(4, png.getChannels(), "rgba8 channels");
     test.checkEqual(32 * 64, png.getBufferSize(), "rgba8 channels");
-    testFullRead(4, test, png, "rgba8 read");
+    testRead(test, png, "rgba8 read");
 }
 
 static void testReadRGBA16(Test& test, const char* path) {
@@ -65,7 +61,7 @@ static void testReadRGBA16(Test& test, const char* path) {
     test.checkEqual(64, png.getHeight(), "rgba16 height");
     test.checkEqual(4, png.getChannels(), "rgba16 channels");
     test.checkEqual(32 * 64, png.getBufferSize(), "rgba16 channels");
-    testFullRead(4, test, png, "rgba16 read");
+    testRead(test, png, "rgba16 read");
 }
 
 static void testReadGray8(Test& test, const char* path) {
@@ -78,7 +74,7 @@ static void testReadGray8(Test& test, const char* path) {
     test.checkEqual(64, png.getHeight(), "gray8 height");
     test.checkEqual(1, png.getChannels(), "gray8 channels");
     test.checkEqual(32 * 64, png.getBufferSize(), "gray8 channels");
-    testFullRead(1, test, png, "gray8 read");
+    testRead(test, png, "gray8 read");
 }
 
 static void testReadGray16(Test& test, const char* path) {
@@ -91,7 +87,7 @@ static void testReadGray16(Test& test, const char* path) {
     test.checkEqual(64, png.getHeight(), "gray16 height");
     test.checkEqual(1, png.getChannels(), "gray16 channels");
     test.checkEqual(32 * 64, png.getBufferSize(), "gray16 channels");
-    testFullRead(1, test, png, "gray16 read");
+    testRead(test, png, "gray16 read");
 }
 
 static void testReadGrayA8(Test& test, const char* path) {
@@ -104,7 +100,7 @@ static void testReadGrayA8(Test& test, const char* path) {
     test.checkEqual(64, png.getHeight(), "graya8 height");
     test.checkEqual(2, png.getChannels(), "graya8 channels");
     test.checkEqual(32 * 64, png.getBufferSize(), "graya8 channels");
-    testFullRead(2, test, png, "graya8 read");
+    testRead(test, png, "graya8 read");
 }
 
 static void testReadGrayA16(Test& test, const char* path) {
@@ -117,7 +113,7 @@ static void testReadGrayA16(Test& test, const char* path) {
     test.checkEqual(64, png.getHeight(), "graya16 height");
     test.checkEqual(2, png.getChannels(), "graya16 channels");
     test.checkEqual(32 * 64, png.getBufferSize(), "graya16 channels");
-    testFullRead(2, test, png, "graya16 read");
+    testRead(test, png, "graya16 read");
 }
 
 void PNGReaderTests::test(const char* path) {

+ 7 - 0
wrapper/Texture.cpp

@@ -6,10 +6,17 @@ Texture::Texture(const TextureFormat& format) : format(format), texture(0) {
     setRepeatWrap();
 }
 
+Texture::Texture() : Texture(TextureFormat::unknown()) {
+}
+
 Texture::~Texture() {
     glDeleteTextures(1, &texture);
 }
 
+void Texture::setFormat(const TextureFormat& tf) {
+    format = tf;
+}
+
 void Texture::setFilter(GLint param) {
     bind();
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, param);

+ 3 - 1
wrapper/Texture.h

@@ -6,7 +6,7 @@
 #include "wrapper/TextureFormat.h"
 
 class Texture final {
-    const TextureFormat format;
+    TextureFormat format;
     GLuint texture;
     
     template<int N>
@@ -14,12 +14,14 @@ class Texture final {
 
 public:
     Texture(const TextureFormat& format);
+    Texture();
     ~Texture();
     Texture(const Texture& other) = delete;
     Texture(Texture&& other) = delete;
     Texture& operator=(const Texture& other) = delete;
     Texture& operator=(Texture&& other) = delete;
 
+    void setFormat(const TextureFormat& format);
     void setNearestFilter();
     void setLinearFilter();
     void setRepeatWrap();

+ 4 - 0
wrapper/TextureFormat.cpp

@@ -45,4 +45,8 @@ TextureFormat TextureFormat::depth16() {
 
 TextureFormat TextureFormat::depth32() {
     return TextureFormat(GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT, true);
+}
+
+TextureFormat TextureFormat::unknown() {
+    return TextureFormat(-1, -1, -1, false);
 }

+ 5 - 4
wrapper/TextureFormat.h

@@ -4,16 +4,17 @@
 #include <GL/glew.h>
 
 struct TextureFormat final {
-    const GLint internalformat;
-    const GLenum format;
-    const GLenum type;
-    const bool depth;
+    GLint internalformat;
+    GLenum format;
+    GLenum type;
+    bool depth;
 
     static TextureFormat color8(int channels);
     static TextureFormat float16(int channels);
     static TextureFormat float32(int channels);
     static TextureFormat depth16();
     static TextureFormat depth32();
+    static TextureFormat unknown();
 
 private:
     TextureFormat(GLint internalformat, GLenum format, GLenum type, bool depth = false);