twn_util.c: add file_read()
This commit is contained in:
parent
e15975bfaa
commit
a231d650f2
@ -42,6 +42,9 @@
|
||||
|
||||
#endif /* TWN_NOT_C */
|
||||
|
||||
/* read file to null terminated string, it is freed when the frame ends */
|
||||
TWN_API char const *file_read(char const *file);
|
||||
|
||||
/* calculates the overlap of two rectangles */
|
||||
TWN_API Rect rect_overlap(Rect a, Rect b);
|
||||
/* returns true if two rectangles are intersecting */
|
||||
|
@ -264,6 +264,16 @@
|
||||
]
|
||||
},
|
||||
|
||||
"file_read": {
|
||||
"module": "util",
|
||||
"symbol": "read",
|
||||
"header": "twn_util.h",
|
||||
"params": [
|
||||
{ "name": "file", "type": "char *" }
|
||||
],
|
||||
"return": "char *"
|
||||
},
|
||||
|
||||
"timer_tick_seconds": {
|
||||
"module": "util",
|
||||
"symbol": "tick_seconds",
|
||||
|
@ -754,6 +754,11 @@ static bool try_mounting_root_pack(char *path) {
|
||||
}
|
||||
|
||||
|
||||
static void garbage_collect(void) {
|
||||
file_read_garbage_collect();
|
||||
}
|
||||
|
||||
|
||||
int enter_loop(int argc, char **argv) {
|
||||
profile_start("startup");
|
||||
|
||||
@ -866,6 +871,7 @@ int enter_loop(int argc, char **argv) {
|
||||
/* dispatch all filewatch driven events, such as game object and asset pack reload */
|
||||
filewatch_poll();
|
||||
main_loop();
|
||||
garbage_collect();
|
||||
}
|
||||
|
||||
if (ctx.game.debug)
|
||||
|
@ -154,6 +154,24 @@ char *file_to_str(const char *path) {
|
||||
return str_out;
|
||||
}
|
||||
|
||||
static char **read_files;
|
||||
|
||||
char const *file_read(char const *file) {
|
||||
char *s = file_to_str(file);
|
||||
|
||||
if (s) arrpush(read_files, s);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
void file_read_garbage_collect(void) {
|
||||
for (size_t i = 0; i < arrlenu(read_files); ++i)
|
||||
SDL_free(read_files[i]);
|
||||
arrfree(read_files);
|
||||
read_files = NULL;
|
||||
}
|
||||
|
||||
|
||||
bool file_exists(const char *path) {
|
||||
return PHYSFS_exists(path);
|
||||
|
@ -29,6 +29,8 @@ char *expand_asterisk(const char *mask, const char *to);
|
||||
|
||||
void profile_list_stats(void);
|
||||
|
||||
void file_read_garbage_collect(void);
|
||||
|
||||
/* http://www.azillionmonkeys.com/qed/sqroot.html */
|
||||
static inline float fast_sqrt(float x)
|
||||
{
|
||||
|
64
third-party/fast_obj/fast_obj.h
vendored
64
third-party/fast_obj/fast_obj.h
vendored
@ -297,58 +297,6 @@ static void* array_realloc(void* ptr, fastObjUInt n, fastObjUInt b)
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
void* file_open(const char* path, void* user_data)
|
||||
{
|
||||
(void)(user_data);
|
||||
return fopen(path, "rb");
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
void file_close(void* file, void* user_data)
|
||||
{
|
||||
FILE* f;
|
||||
(void)(user_data);
|
||||
|
||||
f = (FILE*)(file);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
size_t file_read(void* file, void* dst, size_t bytes, void* user_data)
|
||||
{
|
||||
FILE* f;
|
||||
(void)(user_data);
|
||||
|
||||
f = (FILE*)(file);
|
||||
return fread(dst, 1, bytes, f);
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
unsigned long file_size(void* file, void* user_data)
|
||||
{
|
||||
FILE* f;
|
||||
long p;
|
||||
long n;
|
||||
(void)(user_data);
|
||||
|
||||
f = (FILE*)(file);
|
||||
|
||||
p = ftell(f);
|
||||
fseek(f, 0, SEEK_END);
|
||||
n = ftell(f);
|
||||
fseek(f, p, SEEK_SET);
|
||||
|
||||
if (n > 0)
|
||||
return (unsigned long)(n);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
char* string_copy(const char* s, const char* e)
|
||||
{
|
||||
@ -1408,18 +1356,6 @@ 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;
|
||||
|
Loading…
Reference in New Issue
Block a user