Skip to content

Instantly share code, notes, and snippets.

@deccer
Created July 7, 2024 12:01
Show Gist options
  • Save deccer/9d947e9610ad04f2ea3dea81123b7bcf to your computer and use it in GitHub Desktop.
Save deccer/9d947e9610ad04f2ea3dea81123b7bcf to your computer and use it in GitHub Desktop.
SAssetImage -> Texture
#include <cstddef>
#include <fstream>
#include <filesystem>
#include <iostream>
#include <memory>
#include <unordered_map>
#include <string>
#include <utility>
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
struct SAssetImage {
char* EncodedData;
size_t EncodedDataSize;
char* DecodedData;
int32_t Width;
int32_t Height;
int32_t Components;
};
std::unordered_map<std::string, SAssetImage> g_assetImages = {};
auto ReadBinaryFromFile(const std::filesystem::path& filePath) -> std::pair<std::unique_ptr<std::byte[]>, std::size_t> {
std::size_t fileSize = std::filesystem::file_size(filePath);
auto memory = std::make_unique<std::byte[]>(fileSize);
std::ifstream file{filePath, std::ifstream::binary};
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), reinterpret_cast<char*>(memory.get()));
return {std::move(memory), fileSize};
}
auto LoadImageFromFile(const std::filesystem::path& filePath) -> SAssetImage {
auto [encodedData, encodedDataSize] = ReadBinaryFromFile(filePath);
int32_t width = 0;
int32_t height = 0;
int32_t components = 0;
auto decodedData = stbi_load_from_memory(
reinterpret_cast<const unsigned char*>(encodedData.get()),
static_cast<int32_t>(encodedDataSize),
&width,
&height,
&components,
4);
return SAssetImage{
.DecodedData = reinterpret_cast<char*>(decodedData),
.Width = width,
.Height = height,
.Components = components
};
}
auto AddAssetImage(const std::string& assetImageName, SAssetImage&& assetImage) -> void {
assert(!assetImageName.empty());
if (!g_assetImages.contains(assetImageName)) {
g_assetImages[assetImageName] = std::move(assetImage);
}
}
auto GetAssetImage(const std::string& assetImageName) -> SAssetImage& {
assert(!assetImageName.empty());
return g_assetImages.at(assetImageName);
}
int main() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
auto window = glfwCreateWindow(10, 10, "CppTest", nullptr, nullptr);
if (window == nullptr) {
return 0;
}
glfwMakeContextCurrent(window);
if (!gladLoadGL(glfwGetProcAddress)) {
return 0;
}
while (!glfwWindowShouldClose(window)) {
AddAssetImage("image-0", LoadImageFromFile("data/T_Atlas.png"));
auto& image = GetAssetImage("image-0");
uint32_t texture = 0;
glCreateTextures(GL_TEXTURE_2D, 1, &texture);
glTextureStorage2D(texture, 1, GL_RGBA8, image.Width, image.Height);
glTextureSubImage2D(texture, 0, 0, 0, image.Width, image.Height, GL_RGBA, GL_UNSIGNED_BYTE, image.DecodedData);
std::cout << "width: " << image.Width << "\nheight: " << image.Height << "\n";
glDeleteTextures(1, &texture);
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment