diff --git a/Dengine.vcxproj b/Dengine.vcxproj index 1e3fbcd..9edb52f 100644 --- a/Dengine.vcxproj +++ b/Dengine.vcxproj @@ -122,6 +122,7 @@ + @@ -135,6 +136,7 @@ + diff --git a/Dengine.vcxproj.filters b/Dengine.vcxproj.filters index 19a8c89..f7862f0 100644 --- a/Dengine.vcxproj.filters +++ b/Dengine.vcxproj.filters @@ -36,6 +36,9 @@ Source Files + + Source Files + @@ -74,5 +77,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 028d62d..05b1674 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -3,33 +3,31 @@ #define STBI_FAILURE_USERMSG #define STB_IMAGE_IMPLEMENTATION #include -#include std::map textures; std::map shaders; -Texture *asset_getTexture(const std::string name) +Texture *asset_getTexture(const char *const name) { // NOTE(doyle): Since we're using a map, the count of an object can // only be 1 or 0 if (textures.count(name) == 1) return &textures[name]; - return nullptr; + return NULL; } -const i32 asset_loadTextureImage(const std::string path, const std::string name) +const i32 asset_loadTextureImage(const char *const path, const char *const name) { /* Open the texture image */ i32 imgWidth, imgHeight, bytesPerPixel; stbi_set_flip_vertically_on_load(TRUE); u8 *image = - stbi_load(path.c_str(), &imgWidth, &imgHeight, &bytesPerPixel, 0); + stbi_load(path, &imgWidth, &imgHeight, &bytesPerPixel, 0); if (!image) { - std::cerr << "stdbi_load() failed: " << stbi_failure_reason() - << std::endl; + printf("stdbi_load() failed: %s\n", stbi_failure_reason()); return -1; } @@ -42,7 +40,7 @@ const i32 asset_loadTextureImage(const std::string path, const std::string name) return 0; } -Shader *asset_getShader(const std::string name) +Shader *asset_getShader(const char *const name) { if (shaders.count(name) == 1) return &shaders[name]; @@ -50,34 +48,15 @@ Shader *asset_getShader(const std::string name) return nullptr; } -INTERNAL std::string stringFromFile(const std::string &filename) +INTERNAL GLuint createShaderFromPath(const char *const path, GLuint shadertype) { - std::ifstream file; - file.open(filename.c_str(), std::ios::in | std::ios::binary); + PlatformFileReadResult file = {0}; - std::string output; - std::string line; + i32 status = platform_readFileToBuffer(path, &file); + if (status) + return status; - if (!file.is_open()) - { - std::runtime_error(std::string("Failed to open file: ") + filename); - } - else - { - while (file.good()) - { - std::getline(file, line); - output.append(line + "\n"); - } - } - file.close(); - return output; -} - -INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype) -{ - std::string shaderSource = stringFromFile(path); - const GLchar *source = shaderSource.c_str(); + const GLchar *source = CAST(char *)file.buffer; GLuint result = glCreateShader(shadertype); glShaderSource(result, 1, &source, NULL); @@ -89,15 +68,17 @@ INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype) if (!success) { glGetShaderInfoLog(result, 512, NULL, infoLog); - std::cout << "glCompileShader failed: " << infoLog << std::endl; + printf("glCompileShader() failed: %s\n", infoLog); } + platform_closeFileReadResult(&file); + return result; } -const i32 asset_loadShaderFiles(const std::string vertexPath, - const std::string fragmentPath, - const std::string name) +const i32 asset_loadShaderFiles(const char *const vertexPath, + const char *const fragmentPath, + const char *const name) { GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER); GLuint fragmentShader = diff --git a/src/Platform.cpp b/src/Platform.cpp new file mode 100644 index 0000000..a0f17b6 --- /dev/null +++ b/src/Platform.cpp @@ -0,0 +1,49 @@ +#include + +i32 platform_readFileToBuffer(const char *const filePath, + PlatformFileReadResult *file) +{ + HANDLE fileHandle = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, + NULL, OPEN_ALWAYS, 0, NULL); + if (fileHandle == INVALID_HANDLE_VALUE) + { + printf("CreateFile() failed: INVALID_HANDLE_VALUE\n"); + return -1; + } + + LARGE_INTEGER fileSize; + BOOL status = GetFileSizeEx(fileHandle, &fileSize); + if (!status) + { + + printf("GetFileSizeEx() failed: %d error number\n", + status); + return status; + } + + // TODO(doyle): Warning we assume files less than 4GB + file->buffer = (void *)calloc(fileSize.LowPart, sizeof(char)); + file->size = fileSize.LowPart; + + DWORD numBytesRead = 0; + + status = + ReadFile(fileHandle, file->buffer, file->size, &numBytesRead, NULL); + if (!status) + { + printf("ReadFile() failed: %d error number\n", + status); + free(file->buffer); + return status; + } + else if (numBytesRead != file->size) + { + printf( + "ReadFile() failed: Number of bytes read doesn't match file " + "size\n"); + free(file->buffer); + return -1; + } + + return 0; +} diff --git a/src/dengine.cpp b/src/dengine.cpp index b5a1c89..e719ffb 100644 --- a/src/dengine.cpp +++ b/src/dengine.cpp @@ -1,9 +1,6 @@ #if 1 #include -#include -#include #include -#include #include diff --git a/src/include/Dengine/AssetManager.h b/src/include/Dengine/AssetManager.h index a7198c4..781b10c 100644 --- a/src/include/Dengine/AssetManager.h +++ b/src/include/Dengine/AssetManager.h @@ -1,38 +1,25 @@ #ifndef DENGINE_ASSET_MANAGER_H #define DENGINE_ASSET_MANAGER_H -#include +#include #include #include -#include #include #include -enum Assets -{ - asset_vertShader, - asset_fragShader, - asset_hero, -}; - -struct AssetManager -{ - Textures textures[256]; - Shaders shaders[256]; -}; extern std::map textures; extern std::map shaders; /* Texture */ -Texture *asset_getTexture(const std::string name); -const i32 asset_loadTextureImage(const std::string path, - const std::string name); +Texture *asset_getTexture(const char *const name); +const i32 asset_loadTextureImage(const char *const path, + const char *const name); /* Shaders */ -Shader *asset_getShader(const std::string name); -const i32 asset_loadShaderFiles(const std::string vertexPath, - const std::string fragmentPath, - const std::string name); +Shader *asset_getShader(const char *const name); +const i32 asset_loadShaderFiles(const char *const vertexPath, + const char *const fragmentPath, + const char *const name); #endif diff --git a/src/include/Dengine/Common.h b/src/include/Dengine/Common.h index 0fabab7..49ce187 100644 --- a/src/include/Dengine/Common.h +++ b/src/include/Dengine/Common.h @@ -2,7 +2,6 @@ #define DENGINE_COMMON_H #include -#include typedef uint8_t u8; typedef uint32_t u32; diff --git a/src/include/Dengine/Entity.h b/src/include/Dengine/Entity.h index 9e60d7c..2ef7222 100644 --- a/src/include/Dengine/Entity.h +++ b/src/include/Dengine/Entity.h @@ -1,7 +1,6 @@ #ifndef DENGINE_ENTITY_H #define DENGINE_ENTITY_H -#include #include #include diff --git a/src/include/Dengine/Platform.h b/src/include/Dengine/Platform.h new file mode 100644 index 0000000..73ff7b9 --- /dev/null +++ b/src/include/Dengine/Platform.h @@ -0,0 +1,26 @@ +#ifndef DENGINE_PLATFORM_H +#define DENGINE_PLATFORM_H + +#include + +#include +#include + +typedef struct +{ + void *buffer; + i32 size; +} PlatformFileReadResult; + +i32 platform_readFileToBuffer(const char *const filePath, + PlatformFileReadResult *file); + +inline void platform_closeFileReadResult(PlatformFileReadResult *file) +{ + if (file->buffer) + { + free(file->buffer); + } +} + +#endif