summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 02:56:33 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 02:56:33 +0100
commitc44e7537d027cba66abe564b247549040426ebfe (patch)
treeda1861ea38a619be11cb3befcc4c08a931203835 /gfx
parentNo need to pass size around, we can get it back from the texture (diff)
downloadilt-c44e7537d027cba66abe564b247549040426ebfe.tar.bz2
ilt-c44e7537d027cba66abe564b247549040426ebfe.tar.xz
ilt-c44e7537d027cba66abe564b247549040426ebfe.zip
Externalise a neater definition of TGAHead
Diffstat (limited to 'gfx')
-rw-r--r--gfx/models/texture.cpp14
-rw-r--r--gfx/models/texture.h2
-rw-r--r--gfx/models/tga.h14
3 files changed, 23 insertions, 7 deletions
diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp
index ab256d5..3b27e77 100644
--- a/gfx/models/texture.cpp
+++ b/gfx/models/texture.cpp
@@ -1,5 +1,6 @@
#include "texture.h"
#include "glArrays.h"
+#include "tga.h"
#include <GL/glew.h>
#include <cache.h>
#include <fcntl.h>
@@ -54,11 +55,9 @@ Texture::getSize(const glTexture & texture)
}
void
-Texture::save(const glTexture & texture, GLenum format, GLenum type, unsigned short channels, const char * path,
- short tgaFormat)
+Texture::save(
+ const glTexture & texture, GLenum format, GLenum type, uint8_t channels, const char * path, uint8_t tgaFormat)
{
- using TGAHead = std::array<short, 9>;
-
const auto size = getSize(texture);
const size_t dataSize = (static_cast<size_t>(size.x * size.y * channels));
const size_t fileSize = dataSize + sizeof(TGAHead);
@@ -66,8 +65,11 @@ Texture::save(const glTexture & texture, GLenum format, GLenum type, unsigned sh
filesystem::fh out {path, O_RDWR | O_CREAT, 0660};
out.truncate(fileSize);
auto tga = out.mmap(fileSize, 0, PROT_WRITE, MAP_SHARED);
- *tga.get<TGAHead>() = {0, tgaFormat, 0, 0, 0, 0, static_cast<short>(size.x), static_cast<short>(size.y),
- static_cast<short>(8 * channels)};
+ *tga.get<TGAHead>() = {
+ .format = tgaFormat,
+ .size = size,
+ .pixelDepth = static_cast<uint8_t>(8 * channels),
+ };
glGetTextureImage(texture, 0, format, type, static_cast<GLsizei>(dataSize), tga.get<TGAHead>() + 1);
tga.msync(MS_ASYNC);
}
diff --git a/gfx/models/texture.h b/gfx/models/texture.h
index f4e1476..68ec649 100644
--- a/gfx/models/texture.h
+++ b/gfx/models/texture.h
@@ -34,7 +34,7 @@ public:
static void saveNormal(const glTexture &, const char * path);
protected:
- static void save(const glTexture &, GLenum, GLenum, unsigned short channels, const char * path, short tgaFormat);
+ static void save(const glTexture &, GLenum, GLenum, uint8_t channels, const char * path, uint8_t tgaFormat);
static glm::ivec2 getSize(const glTexture &);
glTexture m_texture;
diff --git a/gfx/models/tga.h b/gfx/models/tga.h
new file mode 100644
index 0000000..1f400ef
--- /dev/null
+++ b/gfx/models/tga.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include <cstdint>
+#include <glm/vec2.hpp>
+
+struct TGAHead {
+ uint8_t idLength {}, colorMapType {}, format {};
+ uint16_t __attribute__((packed)) colorMapFirst {}, colorMapLength {};
+ uint8_t colorMapEntrySize {};
+ glm::vec<2, uint16_t> origin {}, size {};
+ uint8_t pixelDepth {};
+ uint8_t descriptor {};
+};
+static_assert(sizeof(TGAHead) == 18);