diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index d2c1412..0014582 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -44,7 +44,9 @@ elseif (WIN32) ${kde_SRCS} win32_kde.cpp glad.c - glad.h) + glad.h + Win32AssetManager.cpp + Win32AssetManager.h) target_link_libraries(kde glfw) target_include_directories(kde PRIVATE ${PROJECT_SOURCE_DIR}/../../../glfw/include) endif() \ No newline at end of file diff --git a/app/src/main/cpp/Texture.cpp b/app/src/main/cpp/Texture.cpp index e9e2001..c481d25 100644 --- a/app/src/main/cpp/Texture.cpp +++ b/app/src/main/cpp/Texture.cpp @@ -1,7 +1,6 @@ #include "Texture.h" -Texture::Texture(unsigned int width, unsigned int height, const void *data) - : m_texture(0) +Texture::Texture(unsigned int width, unsigned int height, const void* data) : m_texture(0) { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &m_texture); @@ -25,8 +24,7 @@ Texture::Texture(unsigned int width, unsigned int height, const void *data) height_px = height; } -Texture::Texture(unsigned int width, unsigned int height, const void* data, GLint format) - : m_texture(0) +Texture::Texture(unsigned int width, unsigned int height, const void* data, GLint format) : m_texture(0) { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &m_texture); @@ -60,13 +58,12 @@ void Texture::bind() const glBindTexture(GL_TEXTURE_2D, m_texture); } -void Texture::getTextureSize( - unsigned int* w, - unsigned int* h){ - if (w != NULL){ +void Texture::getTextureSize(unsigned int* w, unsigned int* h) +{ + if (w != nullptr) { *w = width_px; } - if (h != NULL){ + if (h != nullptr) { *h = height_px; } } \ No newline at end of file diff --git a/app/src/main/cpp/Win32AssetManager.cpp b/app/src/main/cpp/Win32AssetManager.cpp new file mode 100644 index 0000000..1a06715 --- /dev/null +++ b/app/src/main/cpp/Win32AssetManager.cpp @@ -0,0 +1,69 @@ +#include "Win32AssetManager.h" + +#define WIN32_LEAN_AND_MEAN +#include + +void Win32AssetManager::create(const char* asset_root_dir) +{ + static Win32AssetManager mgr; + mgr.m_asset_root = asset_root_dir; + AssetManager::ptr = &mgr; +} + +bool Win32AssetManager::loadFile(const char* path, FileBuffer* p_file_buffer) +{ + // TODO(Kevin): We could totally optimize this via a custom allocator + char _cp[MAX_PATH]; + strncpy(_cp, m_asset_root, MAX_PATH); + strncat(_cp, "\\", MAX_PATH); + strncat(_cp, path, MAX_PATH); + + WIN32_FILE_ATTRIBUTE_DATA attribs; + if (!GetFileAttributesExA(_cp, GetFileExInfoStandard, &attribs)) { + return false; + } + size_t size = static_cast(attribs.nFileSizeHigh) << 32 | attribs.nFileSizeLow; + void* data = VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + p_file_buffer->data = data; + p_file_buffer->size = size; + p_file_buffer->internal = nullptr; + + HANDLE file = + CreateFileA(_cp, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + if (!file) { + VirtualFree(data, 0, MEM_RELEASE); + return false; + } + char* dst = reinterpret_cast(data); + while (size >= UINT32_MAX) { + DWORD bytes_read = 0; + if (!ReadFile(file, dst, UINT32_MAX, &bytes_read, nullptr)) { + // Error occured + CloseHandle(file); + VirtualFree(data, 0, MEM_RELEASE); + return false; + } + size -= bytes_read; + dst += bytes_read; + } + if (size > 0) { + DWORD bytes_read = 0; + if (!ReadFile(file, dst, size, &bytes_read, nullptr)) { + // Error occured + CloseHandle(file); + VirtualFree(data, 0, MEM_RELEASE); + return false; + } + } + + CloseHandle(file); + return true; +} + +void Win32AssetManager::releaseFileBuffer(FileBuffer& fb) +{ + void* data = const_cast(fb.data); + VirtualFree(data, 0, MEM_RELEASE); + fb.data = nullptr; + fb.size = 0; +} \ No newline at end of file diff --git a/app/src/main/cpp/Win32AssetManager.h b/app/src/main/cpp/Win32AssetManager.h new file mode 100644 index 0000000..b9b62df --- /dev/null +++ b/app/src/main/cpp/Win32AssetManager.h @@ -0,0 +1,23 @@ +#ifndef KRIMI_DINNER_ENGINE_WIN32_ASSET_MANAGER_H +#define KRIMI_DINNER_ENGINE_WIN32_ASSET_MANAGER_H + +#include "AssetManager.h" + +class Win32AssetManager : public AssetManager +{ +public: + /// @brief Create the singleton instance + /// + /// Sets @ref AssetManager::ptr to the singleton instance + /// @param asset_root_dir Directory where asset files are located + static void create(const char* asset_root_dir); + + bool loadFile(const char* path, FileBuffer* p_file_buffer) override; + + void releaseFileBuffer(FileBuffer& fb) override; + +private: + const char* m_asset_root; +}; + +#endif diff --git a/app/src/main/cpp/win32_kde.cpp b/app/src/main/cpp/win32_kde.cpp index 4813837..475e2d0 100644 --- a/app/src/main/cpp/win32_kde.cpp +++ b/app/src/main/cpp/win32_kde.cpp @@ -3,6 +3,7 @@ #include "glad.h" #include "Renderer.h" +#include "Win32AssetManager.h" #undef APIENTRY #define WIN32_LEAN_AND_MEAN @@ -38,13 +39,24 @@ int main() return 1; } + const char* asset_dir = "app\\src\\main\\assets"; + Win32AssetManager::create(asset_dir); + Renderer::create(); glfwSwapInterval(1); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); - Renderer::ptr->addRect(100, 100, 500, 500, 0.3, 0.3, 0.3, 1.0f); + Renderer::ptr->addRect(100, + 100, + 500, + 500, + 0.3, + 0.3, + 0.3, + 1.0f, + StringRepository::global->internString("smiley_PNG42.png")); int w, h; glfwGetFramebufferSize(window, &w, &h);