summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-12-11 12:39:26 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-12-11 12:39:26 +0000
commitf4fa52d7633d9d67d4c41f76b0e317c6232eb907 (patch)
tree54f24ace52d7a9c4a1302a9762cef5b3d2537905 /gfx
parentSave texture to TGA using mmap (diff)
downloadilt-f4fa52d7633d9d67d4c41f76b0e317c6232eb907.tar.bz2
ilt-f4fa52d7633d9d67d4c41f76b0e317c6232eb907.tar.xz
ilt-f4fa52d7633d9d67d4c41f76b0e317c6232eb907.zip
Add a mini C filesystem wrapper library with mmap support
Diffstat (limited to 'gfx')
-rw-r--r--gfx/models/texture.cpp18
1 files changed, 7 insertions, 11 deletions
diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp
index 5b8d376..6019bec 100644
--- a/gfx/models/texture.cpp
+++ b/gfx/models/texture.cpp
@@ -3,6 +3,7 @@
#include <GL/glew.h>
#include <cache.h>
#include <fcntl.h>
+#include <filesystem.h>
#include <gfx/image.h>
#include <glm/geometric.hpp>
#include <resource.h>
@@ -50,18 +51,13 @@ Texture::save(const glTexture & texture, GLenum format, const glm::ivec2 & size,
size_t dataSize = (static_cast<size_t>(size.x * size.y * channels));
size_t fileSize = dataSize + sizeof(TGAHead);
- auto out = open(path, O_RDWR | O_CREAT, 0660);
- std::ignore = ftruncate(out, static_cast<off_t>(fileSize));
- TGAHead * tga = static_cast<TGAHead *>(mmap(nullptr, fileSize, PROT_WRITE, MAP_SHARED, out, 0));
- close(out);
- if (tga == MAP_FAILED) {
- return;
- }
- *tga = {0, tgaFormat, 0, 0, 0, 0, static_cast<short>(size.x), static_cast<short>(size.y),
+ 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)};
- glGetTextureImage(texture, 0, format, GL_UNSIGNED_BYTE, static_cast<GLsizei>(dataSize), tga + 1);
- msync(tga, fileSize, MS_ASYNC);
- munmap(tga, fileSize);
+ glGetTextureImage(texture, 0, format, GL_UNSIGNED_BYTE, static_cast<GLsizei>(dataSize), tga.get<TGAHead>() + 1);
+ tga.msync(MS_ASYNC);
}
void