From c44e7537d027cba66abe564b247549040426ebfe Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Apr 2023 02:56:33 +0100 Subject: Externalise a neater definition of TGAHead --- gfx/models/texture.cpp | 14 ++++++++------ gfx/models/texture.h | 2 +- gfx/models/tga.h | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 gfx/models/tga.h 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 #include #include @@ -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; - const auto size = getSize(texture); const size_t dataSize = (static_cast(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() = {0, tgaFormat, 0, 0, 0, 0, static_cast(size.x), static_cast(size.y), - static_cast(8 * channels)}; + *tga.get() = { + .format = tgaFormat, + .size = size, + .pixelDepth = static_cast(8 * channels), + }; glGetTextureImage(texture, 0, format, type, static_cast(dataSize), tga.get() + 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 +#include + +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); -- cgit v1.2.3