Skip to content

Commit

Permalink
Use stdbyte (#88)
Browse files Browse the repository at this point in the history
* Refactor to use std::byte
  • Loading branch information
Jerboa-app authored Aug 7, 2024
1 parent b452381 commit 80147c4
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 57 deletions.
2 changes: 1 addition & 1 deletion examples/Sprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argv, char ** argc)

std::shared_ptr<jGL::Texture> jerboa = jGLInstance->createTexture
(
std::vector<unsigned char>(LOGO32,LOGO32+sizeof(LOGO32)),
std::vector(LOGO32.begin(), LOGO32.end()),
jGL::Texture::Type::RGBA
);

Expand Down
5 changes: 3 additions & 2 deletions include/jGL/Display/desktopDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,17 @@ namespace jGL
}
};

void setIcon(const std::vector<std::vector<unsigned char>> & icons)
void setIcon(const std::vector<std::vector<std::byte>> & icons)
{
glfwSetWindowIcon(glfwWindow, 0, NULL);
logo.clear();
for (auto icon : icons)
{
GLFWimage image;
unsigned char * chData = reinterpret_cast<unsigned char*>(icon.data());
image.pixels = stbi_load_from_memory
(
icon.data(),
chData,
icon.size(),
&image.width,
&image.height,
Expand Down
41 changes: 22 additions & 19 deletions include/jGL/OpenGL/Texture/glTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace jGL::GL
glDeleteTextures(1, &id);
}

virtual void upload(unsigned char * data) {};
virtual void upload(std::vector<std::byte> * data) {};

inline void bind() { glBindTexture(GL_TEXTURE_2D, id); }

Expand All @@ -46,33 +46,36 @@ namespace jGL::GL
GLuint id;
unsigned textureUnit;

std::vector<unsigned char> load_image(std::filesystem::path imageFilePath)
std::vector<std::byte> load_image(std::filesystem::path imageFilePath)
{
unsigned char * pixels = stbi_load(imageFilePath.generic_string().c_str(), &width, &height, &channels, 0);

if (!pixels)
{
throw std::runtime_error("Failed to load texture: "+imageFilePath.generic_string());
}
std::byte * bytes = reinterpret_cast<std::byte*>(pixels);

size_t dim = width*height;
if (channels > 0) { dim *= channels; }
std::vector<unsigned char> vdata(pixels, pixels+dim);
std::vector<std::byte> vdata(bytes, bytes+dim);
stbi_image_free(pixels);
return vdata;
}

std::vector<unsigned char> load_image(std::vector<unsigned char> imageFile)
std::vector<std::byte> load_image(std::vector<std::byte> imageFile)
{
unsigned char * pixels = stbi_load_from_memory(imageFile.data(), imageFile.size(), &width, &height, &channels, 4);
unsigned char * chData = reinterpret_cast<unsigned char*>(imageFile.data());
unsigned char * pixels = stbi_load_from_memory(chData, imageFile.size(), &width, &height, &channels, 4);
if (!pixels)
{
throw std::runtime_error("Failed to load texture from memory");
}
std::byte * bytes = reinterpret_cast<std::byte*>(pixels);

size_t dim = width*height;
if (channels > 0) { dim *= channels; }
std::vector<unsigned char> vdata(pixels, pixels+dim);
std::vector<std::byte> vdata(bytes, bytes+dim);
stbi_image_free(pixels);
return vdata;
}
Expand All @@ -87,21 +90,21 @@ namespace jGL::GL
glTexture2DRGB(std::filesystem::path imageFile)
: glTexture()
{
std::vector<unsigned char> pixels = load_image(imageFile);
std::vector<std::byte> pixels = load_image(imageFile);
create(width, height, channels);
upload(pixels);
}

glTexture2DRGB(std::vector<unsigned char> data)
glTexture2DRGB(std::vector<std::byte> data)
: glTexture()
{
std::vector<unsigned char> pixels = load_image(data);
std::vector<std::byte> pixels = load_image(data);
create(width, height, channels);
upload(pixels);
}

void create(int width, int height, int channels);
void upload(std::vector<unsigned char> pixels);
void upload(std::vector<std::byte> pixels);
};

class glTexture2DRGBA : public glTexture
Expand All @@ -112,21 +115,21 @@ namespace jGL::GL
glTexture2DRGBA(std::filesystem::path imageFile)
: glTexture()
{
std::vector<unsigned char> pixels = load_image(imageFile);
std::vector<std::byte> pixels = load_image(imageFile);
create(width, height, channels);
upload(pixels);
}

glTexture2DRGBA(std::vector<unsigned char> data)
glTexture2DRGBA(std::vector<std::byte> data)
: glTexture()
{
std::vector<unsigned char> pixels = load_image(data);
std::vector<std::byte> pixels = load_image(data);
create(width, height, channels);
upload(pixels);
}

void create(int width, int height, int channels);
void upload(std::vector<unsigned char> pixels);
void upload(std::vector<std::byte> pixels);
};

class glTexture2DByte : public glTexture
Expand All @@ -137,28 +140,28 @@ namespace jGL::GL
glTexture2DByte(std::filesystem::path imageFile)
: glTexture()
{
std::vector<unsigned char> pixels = load_image(imageFile);
std::vector<std::byte> pixels = load_image(imageFile);
create(width, height);
upload(pixels);
}

glTexture2DByte(std::vector<unsigned char> data)
glTexture2DByte(std::vector<std::byte> data)
: glTexture()
{
std::vector<unsigned char> pixels = load_image(data);
std::vector<std::byte> pixels = load_image(data);
create(width, height);
upload(pixels);
}

glTexture2DByte(std::vector<unsigned char> pixels, int width, int height)
glTexture2DByte(std::vector<std::byte> pixels, int width, int height)
: glTexture()
{
create(width, height);
upload(pixels);
}

void create(int width, int height);
void upload(std::vector<unsigned char> pixels);
void upload(std::vector<std::byte> pixels);

};
}
Expand Down
2 changes: 1 addition & 1 deletion include/jGL/OpenGL/openGLInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace jGL::GL
}
}

std::shared_ptr<Texture> createTexture(std::vector<unsigned char> data, Texture::Type type)
std::shared_ptr<Texture> createTexture(std::vector<std::byte> data, Texture::Type type)
{
switch (type)
{
Expand Down
6 changes: 3 additions & 3 deletions include/jGL/Vulkan/Texture/vkTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace jGL::Vulkan
uint32_t height,
uint32_t channels,
VkFormat format,
std::vector<unsigned char> pixels,
std::vector<std::byte> pixels,
VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT
);

Expand All @@ -96,7 +96,7 @@ namespace jGL::Vulkan
uint32_t height,
uint32_t channels,
VkFormat format,
std::vector<unsigned char> pixels
std::vector<std::byte> pixels
);

protected:
Expand Down Expand Up @@ -130,7 +130,7 @@ namespace jGL::Vulkan
void uploadImage
(
const Command & command,
std::vector<unsigned char> pixels,
std::vector<std::byte> pixels,
VkFormat format,
uint32_t width,
uint32_t height,
Expand Down
2 changes: 1 addition & 1 deletion include/jGL/Vulkan/vulkanInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace jGL::Vulkan
)
);
}
std::shared_ptr<Texture> createTexture(std::vector<unsigned char> data, Texture::Type type)
std::shared_ptr<Texture> createTexture(std::vector<std::byte> data, Texture::Type type)
{
TODO("VulkanInstance::createTexture from bytes undefined");
return std::static_pointer_cast<Texture>
Expand Down
2 changes: 1 addition & 1 deletion include/jGL/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace jGL
std::map<unsigned char, glm::vec4> glyphOffset;

std::shared_ptr<Texture> fontBitmap;
std::vector<unsigned char> bitmapPixels;
std::vector<std::byte> bitmapPixels;
uint16_t bw, bh;

};
Expand Down
2 changes: 1 addition & 1 deletion include/jGL/jGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace jGL

virtual std::shared_ptr<Particles> createParticles(size_t sizeHint) = 0;
virtual std::shared_ptr<Texture> createTexture(std::filesystem::path imageFile, Texture::Type type) = 0;
virtual std::shared_ptr<Texture> createTexture(std::vector<unsigned char> data, Texture::Type type) = 0;
virtual std::shared_ptr<Texture> createTexture(std::vector<std::byte> data, Texture::Type type) = 0;
virtual std::shared_ptr<SpriteRenderer> createSpriteRenderer(size_t sizeHint) = 0;
virtual std::shared_ptr<ShapeRenderer> createShapeRenderer(size_t sizeHint) = 0;

Expand Down
35 changes: 21 additions & 14 deletions include/logo.h

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/jGL/Display/desktopDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ namespace jGL
glfwSetMouseButtonCallback(glfwWindow, mouseButtonCallback);
glfwSetScrollCallback(glfwWindow, mouseScrollCallback);

std::vector<std::vector<unsigned char>> logos =
std::vector<std::vector<std::byte>> logos =
{
{std::vector<unsigned char>(LOGO32, LOGO32+sizeof(LOGO32))},
{std::vector<unsigned char>(LOGO64, LOGO64+sizeof(LOGO64))},
{std::vector<unsigned char>(LOGO128, LOGO128+sizeof(LOGO128))}
{std::vector(LOGO32.begin(), LOGO32.end())},
{std::vector(LOGO64.begin(), LOGO64.end())},
{std::vector(LOGO128.begin(), LOGO128.end())}
};

setIcon(logos);
Expand Down
6 changes: 3 additions & 3 deletions src/jGL/OpenGL/Texture/glTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace jGL::GL

}

void glTexture2DRGB::upload(std::vector<unsigned char> pixels)
void glTexture2DRGB::upload(std::vector<std::byte> pixels)
{
bind();

Expand Down Expand Up @@ -114,7 +114,7 @@ namespace jGL::GL
glBindTexture(GL_TEXTURE_2D, 0);
}

void glTexture2DRGBA::upload(std::vector<unsigned char> pixels)
void glTexture2DRGBA::upload(std::vector<std::byte> pixels)
{
bind();
glTexImage2D(
Expand Down Expand Up @@ -178,7 +178,7 @@ namespace jGL::GL

}

void glTexture2DByte::upload(std::vector<unsigned char> pixels)
void glTexture2DByte::upload(std::vector<std::byte> pixels)
{
bind();

Expand Down
13 changes: 8 additions & 5 deletions src/jGL/Vulkan/Texture/vkTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ namespace jGL::Vulkan
this->channels = 4;
format = VK_FORMAT_R8G8B8A8_SRGB;
}
std::byte * bytes = reinterpret_cast<std::byte*>(pixels);

this->width = width;
this->height = height;
int dim = width*height;
if (channels > 0){ dim *= channels; }

uploadImage
(
command,
std::vector<unsigned char> {pixels, pixels+width*height*channels},
std::vector<std::byte>(bytes, bytes+dim),
format,
this->width,
this->height,
Expand All @@ -64,7 +67,7 @@ namespace jGL::Vulkan
uint32_t height,
uint32_t channels,
VkFormat format,
std::vector<unsigned char> pixels,
std::vector<std::byte> pixels,
VkSampleCountFlagBits msaaSamples
)
: device(device), msaaSamples(msaaSamples)
Expand Down Expand Up @@ -103,7 +106,7 @@ namespace jGL::Vulkan
height,
channels,
format,
std::vector<unsigned char>(width*height*channels, 0),
std::vector<std::byte>(width*height*channels),
msaaSamples
)
{}
Expand Down Expand Up @@ -299,7 +302,7 @@ namespace jGL::Vulkan
void vkTexture::uploadImage
(
const Command & command,
std::vector<unsigned char> pixels,
std::vector<std::byte> pixels,
VkFormat format,
uint32_t width,
uint32_t height,
Expand Down Expand Up @@ -373,7 +376,7 @@ namespace jGL::Vulkan
uint32_t height,
uint32_t channels,
VkFormat format,
std::vector<unsigned char> pixels
std::vector<std::byte> pixels
)
{
if
Expand Down
4 changes: 2 additions & 2 deletions src/jGL/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace jGL
int chWidth = s;
int chHeight = s;

bitmapPixels = std::vector<unsigned char>(chWidth*chHeight*128, 0);
bitmapPixels = std::vector<std::byte>(chWidth*chHeight*128);

uint16_t cursorX = 0;
uint16_t cursorY = 0;
Expand Down Expand Up @@ -70,7 +70,7 @@ namespace jGL
px = cursorX;
py += 1;
}
bitmapPixels[py*width+px+cursorX*chWidth] = chPixels[i];
bitmapPixels[py*width+px+cursorX*chWidth] = std::byte(chPixels[i]);
px++;
}

Expand Down

0 comments on commit 80147c4

Please sign in to comment.