twn_util.c: add file_read()

This commit is contained in:
veclavtalica 2025-02-20 19:51:52 +03:00
parent e15975bfaa
commit a231d650f2
6 changed files with 39 additions and 64 deletions

View File

@ -42,6 +42,9 @@
#endif /* TWN_NOT_C */ #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 */ /* calculates the overlap of two rectangles */
TWN_API Rect rect_overlap(Rect a, Rect b); TWN_API Rect rect_overlap(Rect a, Rect b);
/* returns true if two rectangles are intersecting */ /* returns true if two rectangles are intersecting */

View File

@ -264,6 +264,16 @@
] ]
}, },
"file_read": {
"module": "util",
"symbol": "read",
"header": "twn_util.h",
"params": [
{ "name": "file", "type": "char *" }
],
"return": "char *"
},
"timer_tick_seconds": { "timer_tick_seconds": {
"module": "util", "module": "util",
"symbol": "tick_seconds", "symbol": "tick_seconds",

View File

@ -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) { int enter_loop(int argc, char **argv) {
profile_start("startup"); 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 */ /* dispatch all filewatch driven events, such as game object and asset pack reload */
filewatch_poll(); filewatch_poll();
main_loop(); main_loop();
garbage_collect();
} }
if (ctx.game.debug) if (ctx.game.debug)

View File

@ -154,6 +154,24 @@ char *file_to_str(const char *path) {
return str_out; 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) { bool file_exists(const char *path) {
return PHYSFS_exists(path); return PHYSFS_exists(path);

View File

@ -29,6 +29,8 @@ char *expand_asterisk(const char *mask, const char *to);
void profile_list_stats(void); void profile_list_stats(void);
void file_read_garbage_collect(void);
/* http://www.azillionmonkeys.com/qed/sqroot.html */ /* http://www.azillionmonkeys.com/qed/sqroot.html */
static inline float fast_sqrt(float x) static inline float fast_sqrt(float x)
{ {

View File

@ -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 static
char* string_copy(const char* s, const char* e) 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) fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbacks* callbacks, void* user_data)
{ {
fastObjData data; fastObjData data;