mirror of
https://github.com/thisistherk/fast_obj.git
synced 2025-04-04 13:05:02 +00:00
Merge pull request #23 from BeastLe9enD/master
Virtual filesystem support
This commit is contained in:
commit
e7a99e63a0
1 changed files with 51 additions and 21 deletions
72
fast_obj.h
72
fast_obj.h
|
@ -34,6 +34,8 @@
|
|||
#define FAST_OBJ_VERSION_MINOR 0
|
||||
#define FAST_OBJ_VERSION ((FAST_OBJ_VERSION_MAJOR << 8) | FAST_OBJ_VERSION_MINOR)
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
@ -139,11 +141,20 @@ typedef struct
|
|||
|
||||
} fastObjMesh;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void*(*file_open) (const char* path, void* user_data);
|
||||
void(*file_close) (void* file, void* user_data);
|
||||
size_t(*file_read) (void* file, void* dst, size_t bytes, void* user_data);
|
||||
unsigned long(*file_size) (void* file, void* user_data);
|
||||
} fastObjCallbacks;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
fastObjMesh* fast_obj_read(const char* path);
|
||||
fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbacks* callbacks, void* user_data);
|
||||
void fast_obj_destroy(fastObjMesh* mesh);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -156,7 +167,6 @@ void fast_obj_destroy(fastObjMesh* mesh);
|
|||
#ifdef FAST_OBJ_IMPLEMENTATION
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef FAST_OBJ_REALLOC
|
||||
|
@ -269,16 +279,18 @@ static void* array_realloc(void* ptr, fastObjUInt n, fastObjUInt b)
|
|||
|
||||
|
||||
static
|
||||
void* file_open(const char* path)
|
||||
void* file_open(const char* path, void* user_data)
|
||||
{
|
||||
(void)(user_data);
|
||||
return fopen(path, "rb");
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
void file_close(void* file)
|
||||
void file_close(void* file, void* user_data)
|
||||
{
|
||||
FILE* f;
|
||||
FILE* f;
|
||||
(void)(user_data);
|
||||
|
||||
f = (FILE*)(file);
|
||||
fclose(f);
|
||||
|
@ -286,9 +298,10 @@ void file_close(void* file)
|
|||
|
||||
|
||||
static
|
||||
size_t file_read(void* file, void* dst, size_t bytes)
|
||||
size_t file_read(void* file, void* dst, size_t bytes, void* user_data)
|
||||
{
|
||||
FILE* f;
|
||||
FILE* f;
|
||||
(void)(user_data);
|
||||
|
||||
f = (FILE*)(file);
|
||||
return fread(dst, 1, bytes, f);
|
||||
|
@ -296,12 +309,13 @@ size_t file_read(void* file, void* dst, size_t bytes)
|
|||
|
||||
|
||||
static
|
||||
unsigned long file_size(void* file)
|
||||
unsigned long file_size(void* file, void* user_data)
|
||||
{
|
||||
FILE* f;
|
||||
long p;
|
||||
long n;
|
||||
|
||||
(void)(user_data);
|
||||
|
||||
f = (FILE*)(file);
|
||||
|
||||
p = ftell(f);
|
||||
|
@ -910,7 +924,7 @@ const char* read_map(fastObjData* data, const char* ptr, fastObjTexture* map)
|
|||
|
||||
|
||||
static
|
||||
int read_mtllib(fastObjData* data, void* file)
|
||||
int read_mtllib(fastObjData* data, void* file, const fastObjCallbacks* callbacks, void* user_data)
|
||||
{
|
||||
unsigned long n;
|
||||
const char* s;
|
||||
|
@ -923,13 +937,13 @@ int read_mtllib(fastObjData* data, void* file)
|
|||
|
||||
|
||||
/* Read entire file */
|
||||
n = file_size(file);
|
||||
n = callbacks->file_size(file, user_data);
|
||||
|
||||
contents = (char*)(memory_realloc(0, n + 1));
|
||||
if (!contents)
|
||||
return 0;
|
||||
|
||||
l = file_read(file, contents, n);
|
||||
l = callbacks->file_read(file, contents, n, user_data);
|
||||
contents[l] = '\n';
|
||||
|
||||
mtl = mtl_default();
|
||||
|
@ -1100,7 +1114,7 @@ int read_mtllib(fastObjData* data, void* file)
|
|||
|
||||
|
||||
static
|
||||
const char* parse_mtllib(fastObjData* data, const char* ptr)
|
||||
const char* parse_mtllib(fastObjData* data, const char* ptr, const fastObjCallbacks* callbacks, void* user_data)
|
||||
{
|
||||
const char* s;
|
||||
const char* e;
|
||||
|
@ -1121,11 +1135,11 @@ const char* parse_mtllib(fastObjData* data, const char* ptr)
|
|||
{
|
||||
string_fix_separators(lib);
|
||||
|
||||
file = file_open(lib);
|
||||
file = callbacks->file_open(lib, user_data);
|
||||
if (file)
|
||||
{
|
||||
read_mtllib(data, file);
|
||||
file_close(file);
|
||||
read_mtllib(data, file, callbacks, user_data);
|
||||
callbacks->file_close(file, user_data);
|
||||
}
|
||||
|
||||
memory_dealloc(lib);
|
||||
|
@ -1136,7 +1150,7 @@ const char* parse_mtllib(fastObjData* data, const char* ptr)
|
|||
|
||||
|
||||
static
|
||||
void parse_buffer(fastObjData* data, const char* ptr, const char* end)
|
||||
void parse_buffer(fastObjData* data, const char* ptr, const char* end, const fastObjCallbacks* callbacks, void* user_data)
|
||||
{
|
||||
const char* p;
|
||||
|
||||
|
@ -1209,7 +1223,7 @@ void parse_buffer(fastObjData* data, const char* ptr, const char* end)
|
|||
p[3] == 'i' &&
|
||||
p[4] == 'b' &&
|
||||
is_whitespace(p[5]))
|
||||
p = parse_mtllib(data, p + 5);
|
||||
p = parse_mtllib(data, p + 5, callbacks, user_data);
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
|
@ -1259,6 +1273,18 @@ void fast_obj_destroy(fastObjMesh* m)
|
|||
|
||||
|
||||
fastObjMesh* fast_obj_read(const char* path)
|
||||
{
|
||||
fastObjCallbacks callbacks;
|
||||
callbacks.file_open = file_open;
|
||||
callbacks.file_close = file_close;
|
||||
callbacks.file_read = file_read;
|
||||
callbacks.file_size = file_size;
|
||||
|
||||
return fast_obj_read_with_callbacks(path, &callbacks, 0);
|
||||
}
|
||||
|
||||
|
||||
fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbacks* callbacks, void* user_data)
|
||||
{
|
||||
fastObjData data;
|
||||
fastObjMesh* m;
|
||||
|
@ -1270,9 +1296,13 @@ fastObjMesh* fast_obj_read(const char* path)
|
|||
fastObjUInt read;
|
||||
fastObjUInt bytes;
|
||||
|
||||
/* Check if callbacks are valid */
|
||||
if(!callbacks)
|
||||
return 0;
|
||||
|
||||
|
||||
/* Open file */
|
||||
file = file_open(path);
|
||||
file = callbacks->file_open(path, user_data);
|
||||
if (!file)
|
||||
return 0;
|
||||
|
||||
|
@ -1335,7 +1365,7 @@ fastObjMesh* fast_obj_read(const char* path)
|
|||
for (;;)
|
||||
{
|
||||
/* Read another buffer's worth from file */
|
||||
read = (fastObjUInt)(file_read(file, start, BUFFER_SIZE));
|
||||
read = (fastObjUInt)(callbacks->file_read(file, start, BUFFER_SIZE, user_data));
|
||||
if (read == 0 && start == buffer)
|
||||
break;
|
||||
|
||||
|
@ -1370,7 +1400,7 @@ fastObjMesh* fast_obj_read(const char* path)
|
|||
|
||||
|
||||
/* Process buffer */
|
||||
parse_buffer(&data, buffer, last);
|
||||
parse_buffer(&data, buffer, last, callbacks, user_data);
|
||||
|
||||
|
||||
/* Copy overflow for next buffer */
|
||||
|
@ -1396,7 +1426,7 @@ fastObjMesh* fast_obj_read(const char* path)
|
|||
memory_dealloc(buffer);
|
||||
memory_dealloc(data.base);
|
||||
|
||||
file_close(file);
|
||||
callbacks->file_close(file, user_data);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue