Remove stdstring, use platform layer to open files
This commit is contained in:
parent
3c51010e77
commit
6251e105c8
@ -122,6 +122,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="src\AssetManager.cpp" />
|
<ClCompile Include="src\AssetManager.cpp" />
|
||||||
<ClCompile Include="src\dengine.cpp" />
|
<ClCompile Include="src\dengine.cpp" />
|
||||||
|
<ClCompile Include="src\Platform.cpp" />
|
||||||
<ClCompile Include="src\Renderer.cpp" />
|
<ClCompile Include="src\Renderer.cpp" />
|
||||||
<ClCompile Include="src\Shader.cpp" />
|
<ClCompile Include="src\Shader.cpp" />
|
||||||
<ClCompile Include="src\WorldTraveller.cpp" />
|
<ClCompile Include="src\WorldTraveller.cpp" />
|
||||||
@ -135,6 +136,7 @@
|
|||||||
<None Include="data\shaders\sprite.vert.glsl" />
|
<None Include="data\shaders\sprite.vert.glsl" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="src\include\Dengine\Platform.h" />
|
||||||
<ClInclude Include="src\include\Dengine\AssetManager.h" />
|
<ClInclude Include="src\include\Dengine\AssetManager.h" />
|
||||||
<ClInclude Include="src\include\Dengine\Common.h" />
|
<ClInclude Include="src\include\Dengine\Common.h" />
|
||||||
<ClInclude Include="src\include\Breakout\Game.h" />
|
<ClInclude Include="src\include\Breakout\Game.h" />
|
||||||
|
@ -36,6 +36,9 @@
|
|||||||
<ClCompile Include="src\WorldTraveller.cpp">
|
<ClCompile Include="src\WorldTraveller.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\Platform.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="data\shaders\default.vert.glsl" />
|
<None Include="data\shaders\default.vert.glsl" />
|
||||||
@ -74,5 +77,8 @@
|
|||||||
<ClInclude Include="src\include\Dengine\Math.h">
|
<ClInclude Include="src\include\Dengine\Math.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\include\Dengine\Platform.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -3,33 +3,31 @@
|
|||||||
#define STBI_FAILURE_USERMSG
|
#define STBI_FAILURE_USERMSG
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include <STB/stb_image.h>
|
#include <STB/stb_image.h>
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
std::map<std::string, Texture> textures;
|
std::map<std::string, Texture> textures;
|
||||||
std::map<std::string, Shader> shaders;
|
std::map<std::string, Shader> 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
|
// NOTE(doyle): Since we're using a map, the count of an object can
|
||||||
// only be 1 or 0
|
// only be 1 or 0
|
||||||
if (textures.count(name) == 1)
|
if (textures.count(name) == 1)
|
||||||
return &textures[name];
|
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 */
|
/* Open the texture image */
|
||||||
i32 imgWidth, imgHeight, bytesPerPixel;
|
i32 imgWidth, imgHeight, bytesPerPixel;
|
||||||
stbi_set_flip_vertically_on_load(TRUE);
|
stbi_set_flip_vertically_on_load(TRUE);
|
||||||
u8 *image =
|
u8 *image =
|
||||||
stbi_load(path.c_str(), &imgWidth, &imgHeight, &bytesPerPixel, 0);
|
stbi_load(path, &imgWidth, &imgHeight, &bytesPerPixel, 0);
|
||||||
|
|
||||||
if (!image)
|
if (!image)
|
||||||
{
|
{
|
||||||
std::cerr << "stdbi_load() failed: " << stbi_failure_reason()
|
printf("stdbi_load() failed: %s\n", stbi_failure_reason());
|
||||||
<< std::endl;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +40,7 @@ const i32 asset_loadTextureImage(const std::string path, const std::string name)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Shader *asset_getShader(const std::string name)
|
Shader *asset_getShader(const char *const name)
|
||||||
{
|
{
|
||||||
if (shaders.count(name) == 1)
|
if (shaders.count(name) == 1)
|
||||||
return &shaders[name];
|
return &shaders[name];
|
||||||
@ -50,34 +48,15 @@ Shader *asset_getShader(const std::string name)
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL std::string stringFromFile(const std::string &filename)
|
INTERNAL GLuint createShaderFromPath(const char *const path, GLuint shadertype)
|
||||||
{
|
{
|
||||||
std::ifstream file;
|
PlatformFileReadResult file = {0};
|
||||||
file.open(filename.c_str(), std::ios::in | std::ios::binary);
|
|
||||||
|
|
||||||
std::string output;
|
i32 status = platform_readFileToBuffer(path, &file);
|
||||||
std::string line;
|
if (status)
|
||||||
|
return status;
|
||||||
|
|
||||||
if (!file.is_open())
|
const GLchar *source = CAST(char *)file.buffer;
|
||||||
{
|
|
||||||
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();
|
|
||||||
|
|
||||||
GLuint result = glCreateShader(shadertype);
|
GLuint result = glCreateShader(shadertype);
|
||||||
glShaderSource(result, 1, &source, NULL);
|
glShaderSource(result, 1, &source, NULL);
|
||||||
@ -89,15 +68,17 @@ INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype)
|
|||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
glGetShaderInfoLog(result, 512, NULL, infoLog);
|
glGetShaderInfoLog(result, 512, NULL, infoLog);
|
||||||
std::cout << "glCompileShader failed: " << infoLog << std::endl;
|
printf("glCompileShader() failed: %s\n", infoLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
platform_closeFileReadResult(&file);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const i32 asset_loadShaderFiles(const std::string vertexPath,
|
const i32 asset_loadShaderFiles(const char *const vertexPath,
|
||||||
const std::string fragmentPath,
|
const char *const fragmentPath,
|
||||||
const std::string name)
|
const char *const name)
|
||||||
{
|
{
|
||||||
GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
|
GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
|
||||||
GLuint fragmentShader =
|
GLuint fragmentShader =
|
||||||
|
49
src/Platform.cpp
Normal file
49
src/Platform.cpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#include <Dengine/Platform.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
@ -1,9 +1,6 @@
|
|||||||
#if 1
|
#if 1
|
||||||
#include <Dengine/AssetManager.h>
|
#include <Dengine/AssetManager.h>
|
||||||
#include <Dengine/Common.h>
|
|
||||||
#include <Dengine/OpenGL.h>
|
|
||||||
#include <Dengine/Renderer.h>
|
#include <Dengine/Renderer.h>
|
||||||
#include <Dengine/Shader.h>
|
|
||||||
|
|
||||||
#include <WorldTraveller/WorldTraveller.h>
|
#include <WorldTraveller/WorldTraveller.h>
|
||||||
|
|
||||||
|
@ -1,38 +1,25 @@
|
|||||||
#ifndef DENGINE_ASSET_MANAGER_H
|
#ifndef DENGINE_ASSET_MANAGER_H
|
||||||
#define DENGINE_ASSET_MANAGER_H
|
#define DENGINE_ASSET_MANAGER_H
|
||||||
|
|
||||||
#include <Dengine/Common.h>
|
#include <Dengine/Platform.h>
|
||||||
#include <Dengine/Shader.h>
|
#include <Dengine/Shader.h>
|
||||||
#include <Dengine/Texture.h>
|
#include <Dengine/Texture.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
enum Assets
|
|
||||||
{
|
|
||||||
asset_vertShader,
|
|
||||||
asset_fragShader,
|
|
||||||
asset_hero,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AssetManager
|
|
||||||
{
|
|
||||||
Textures textures[256];
|
|
||||||
Shaders shaders[256];
|
|
||||||
};
|
|
||||||
extern std::map<std::string, Texture> textures;
|
extern std::map<std::string, Texture> textures;
|
||||||
extern std::map<std::string, Shader> shaders;
|
extern std::map<std::string, Shader> shaders;
|
||||||
|
|
||||||
/* Texture */
|
/* Texture */
|
||||||
Texture *asset_getTexture(const std::string name);
|
Texture *asset_getTexture(const char *const name);
|
||||||
const i32 asset_loadTextureImage(const std::string path,
|
const i32 asset_loadTextureImage(const char *const path,
|
||||||
const std::string name);
|
const char *const name);
|
||||||
|
|
||||||
/* Shaders */
|
/* Shaders */
|
||||||
Shader *asset_getShader(const std::string name);
|
Shader *asset_getShader(const char *const name);
|
||||||
const i32 asset_loadShaderFiles(const std::string vertexPath,
|
const i32 asset_loadShaderFiles(const char *const vertexPath,
|
||||||
const std::string fragmentPath,
|
const char *const fragmentPath,
|
||||||
const std::string name);
|
const char *const name);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#define DENGINE_COMMON_H
|
#define DENGINE_COMMON_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
typedef uint8_t u8;
|
typedef uint8_t u8;
|
||||||
typedef uint32_t u32;
|
typedef uint32_t u32;
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#ifndef DENGINE_ENTITY_H
|
#ifndef DENGINE_ENTITY_H
|
||||||
#define DENGINE_ENTITY_H
|
#define DENGINE_ENTITY_H
|
||||||
|
|
||||||
#include <Dengine/Common.h>
|
|
||||||
#include <Dengine/Texture.h>
|
#include <Dengine/Texture.h>
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
26
src/include/Dengine/Platform.h
Normal file
26
src/include/Dengine/Platform.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef DENGINE_PLATFORM_H
|
||||||
|
#define DENGINE_PLATFORM_H
|
||||||
|
|
||||||
|
#include <Dengine/Common.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
|
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
|
Loading…
Reference in New Issue
Block a user