Loading files under win32
This commit is contained in:
parent
70d23facf9
commit
7fae583084
|
@ -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()
|
|
@ -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;
|
||||
}
|
||||
}
|
69
app/src/main/cpp/Win32AssetManager.cpp
Normal file
69
app/src/main/cpp/Win32AssetManager.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include "Win32AssetManager.h"
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
|
||||
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<size_t>(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<char*>(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<void*>(fb.data);
|
||||
VirtualFree(data, 0, MEM_RELEASE);
|
||||
fb.data = nullptr;
|
||||
fb.size = 0;
|
||||
}
|
23
app/src/main/cpp/Win32AssetManager.h
Normal file
23
app/src/main/cpp/Win32AssetManager.h
Normal file
|
@ -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
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user