From f4fa52d7633d9d67d4c41f76b0e317c6232eb907 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 11 Dec 2022 12:39:26 +0000 Subject: Add a mini C filesystem wrapper library with mmap support --- gfx/models/texture.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'gfx/models') 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 #include #include +#include #include #include #include @@ -50,18 +51,13 @@ Texture::save(const glTexture & texture, GLenum format, const glm::ivec2 & size, size_t dataSize = (static_cast(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(fileSize)); - TGAHead * tga = static_cast(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(size.x), static_cast(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() = {0, tgaFormat, 0, 0, 0, 0, static_cast(size.x), static_cast(size.y), static_cast(8 * channels)}; - glGetTextureImage(texture, 0, format, GL_UNSIGNED_BYTE, static_cast(dataSize), tga + 1); - msync(tga, fileSize, MS_ASYNC); - munmap(tga, fileSize); + glGetTextureImage(texture, 0, format, GL_UNSIGNED_BYTE, static_cast(dataSize), tga.get() + 1); + tga.msync(MS_ASYNC); } void -- cgit v1.2.3