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;

}

相关推荐
漫游者Nova4 小时前
虚幻引擎Unreal Engine5恐怖游戏设计制作教程,从入门到精通从零开始完整项目开发实战详细讲解中英字幕
ue5·游戏引擎·虚幻·游戏开发完整教程·恐怖游戏开发
死也不注释12 小时前
【Unity 编辑器工具开发:GUILayout 与 EditorGUILayout 对比分析】
unity·编辑器·游戏引擎
小赖同学啊21 小时前
物联网中的Unity/Unreal引擎集成:数字孪生与可视化控制
物联网·unity·游戏引擎
Zlzxzw1 天前
使用unity创建项目,进行动画制作
unity·游戏引擎
X_StarX1 天前
【Unity笔记01】基于单例模式的简单UI框架
笔记·ui·unity·单例模式·游戏引擎·游戏开发·大学生
KhalilRuan2 天前
Unity-MMORPG内容笔记-其一
unity·游戏引擎
向宇it2 天前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
qq_168278952 天前
Protobuf在游戏开发中的应用:TypeScript + Golang 实践
服务器·golang·游戏引擎
12 天前
Lua复习之何为闭包
开发语言·unity·游戏引擎·lua·交互
深空数字孪生12 天前
2025年小程序地图打车的5大技术革新:实时路况预测与智能调度升级
大数据·人工智能·unity·性能优化·小程序·游戏引擎