ApophisZerg的vulkan游戏引擎的platform平台

1.filesystem.h

#pragma once

#include "defines.h"

//Hold a handle to a file.

typedef struct file_handle{

void* handle;

b8 is_valid;

}file_handle;

typedef enum file_modes{

FILE_MODE_READ = 0x1,

FILE_MODE_WRITE = 0x2

}file_modes;

KAPI b8 filesystem_exists(const char* path);

KAPI b8 filesystem_open(const char* path,file_modes mode,b8 binary,file_handle* out_handle);

KAPI void filesystem_close(file_handle* handle);

KAPI b8 filesystem_read_line(file_handle* handle,char** line_buf);

KAPI b8 filesystem_write_line(file_handle* handle,const char* text);

KAPI b8 filesystem_read(file_handle* handle,u64 data_size,void* out_data,u64* out_bytes_read);

KAPI b8 filesystem_read_all_bytes(file_handle* handle,u8** out_bytes,u64* out_bytes_read);

KAPI b8 filesystem_write(file_handle* handle,u64 data_size,const void* data,u64* out_bytes_written);

2.filesystem.c

#include "filesystem.h"

//#include "renderer/vulkan/vulkan_types.inl"

#include "core/logger.h"

#include "core/kmemory.h"

#include <stdio.h>

#include <string.h>

#include <sys/stat.h>

b8 filesystem_exists(const char* path)

{

struct stat buffer;

return stat(path, &buffer) == 0;

}

b8 filesystem_open(const char* path,file_modes mode,b8 binary,file_handle* out_handle)

{

out_handle->is_valid = false;

out_handle->handle= 0;

const char* mode_str;

if((mode & FILE_MODE_READ)!=0 && (mode & FILE_MODE_WRITE)!=0)

{

mode_str = binary ? "w+b" : "w+";

}else if((mode & FILE_MODE_READ)!=0&&(mode & FILE_MODE_WRITE) == 0)

{

mode_str = binary ? "rb" : "r";

}else if((mode & FILE_MODE_READ)==0&&(mode & FILE_MODE_WRITE) != 0)

{

mode_str = binary ? "wb" : "w";

}

else

{

KERROR("Invalid mode passed while trying to open file: '%s'",path);

return false;

}

//Attempt to open the file

FILE* file = fopen(path,mode_str);

if(!file)

{

KERROR("Error opening file: '%s'",path);

return false;

}

out_handle->handle = file;

out_handle->is_valid = true;

return true;

}

void filesystem_close(file_handle* handle)

{

if(handle->handle)

{

fclose((FILE*)handle->handle);

handle->handle = 0;

handle->is_valid = false;

}

}

b8 filesystem_read_line(file_handle* handle,char** line_buf)

{

if(handle->handle)

{

char buffer[32000];

if(fgets(buffer,32000,(FILE*)handle->handle)!=0)

{

u64 length = strlen(buffer);

*line_buf = kallocate((sizeof(char)* length)+1,MEMORY_TAG_STRING);

strcpy(*line_buf,buffer);

return true;

}

}

return false;

}

b8 filesystem_write_line(file_handle* handle,const char* text)

{

if(handle->handle)

{

i32 result = fputs(text,(FILE*)handle->handle);

if(result != EOF)

{

result = fputc('\n',(FILE*)handle->handle);

}

fflush((FILE*)handle->handle);

return result != EOF;

}

return false;

}

b8 filesystem_read(file_handle* handle,u64 data_size,void* out_data,u64* out_bytes_read)

{

if(handle->handle && out_data)

{

*out_bytes_read = fread(out_data,1,data_size,(FILE*)handle->handle);

if(*out_bytes_read != data_size)

{

return false;

}

return true;

}

return false;

}

b8 filesystem_read_all_bytes(file_handle* handle,u8** out_bytes,u64* out_bytes_read)

{

if(handle->handle)

{

fseek((FILE*)handle->handle,0,SEEK_END);

u64 size = ftell((FILE*)handle->handle);

rewind((FILE*)handle->handle);

*out_bytes = kallocate(sizeof(u8)* size,MEMORY_TAG_STRING);

*out_bytes_read = fread(*out_bytes,1,size,(FILE*)handle->handle);

if(*out_bytes_read != size)

{

return false;

}

return true;

}

return false;

}

b8 filesystem_write(file_handle* handle,u64 data_size,const void* data,u64* out_bytes_written)

{

if(handle->handle)

{

*out_bytes_written = fwrite(data,1,data_size,(FILE*)handle->handle);

if(*out_bytes_written != data_size)

{

return false;

}

return true;

}

return false;

}

相关推荐
向宇it2 小时前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
快乐觉主吖12 小时前
Unity网络通信的插件分享,及TCP粘包分包问题处理
tcp/ip·unity·游戏引擎
erxij2 天前
【游戏引擎之路】登神长阶(十八):3天制作Galgame引擎《Galplayer》——无敌之道心
游戏引擎
lizz312 天前
GAMES101 lec2-数学基础1(线性代数)
线性代数·游戏引擎·图形渲染
X-mj2 天前
Unity URP + XR 自定义 Skybox 在真机变黑问题全解析与解决方案(支持 Pico、Quest 等一体机)
unity·游戏引擎·xr
erxij2 天前
【游戏引擎之路】登神长阶(十七):Humanoid动画——长风破浪会有时,直挂云帆济沧海
游戏引擎
erxij2 天前
【游戏引擎之路】登神长阶(十九):3D物理引擎——岁不寒,无以知松柏;事不难,无以知君子
3d·游戏引擎
心疼你的一切3 天前
Unity 多人游戏框架学习系列一
学习·游戏·unity·c#·游戏引擎
示申○言舌3 天前
Unity沉浸式/360View/全景渲染
unity·游戏引擎·沉浸式·360view·全景视图·全景渲染
Unity___3 天前
Unity Editor下拉框,支持搜索,多层级
windows·unity·游戏引擎