twn_util: final cleaning up, introducton of powerful file_read()

This commit is contained in:
veclavtalica
2025-03-10 05:19:58 +03:00
parent f86f3dd41a
commit 56530f9864
11 changed files with 145 additions and 82 deletions

View File

@ -40,4 +40,12 @@ typedef struct Rect {
} Rect;
/* data packet exchanged between parties, assumed immutable */
/* length is whole number */
typedef struct String {
char *data;
float length;
} String;
#endif

View File

@ -8,48 +8,37 @@
#include <stddef.h>
#include <stdbool.h>
/* here as it's not part of standard C */
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288 /**< pi */
#endif
#ifndef TWN_NOT_C
#include <math.h>
/* read data from virtual filesystem, with assumption that you know what that underlying data is */
/* this routine supports commands, which define operations performed on filepaths */
/* empty result is returned when something goes wrong or file doesn't exist, with an error logged */
/* defined commands: */
/* <path:string> -- reads utf8 encoded file to null terminated string */
/* <path:binary> -- reads arbitrary binary file, resulted encoding depends on environment (could be base64, for example) */
/* <path:exists> -- returns "yes" or "no" ascii string */
/* <path:images> -- returns newline separated utf8 list of image files in given path */
TWN_API String file_read(char const *file, const char *operation);
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288 /**< pi */
#endif
/* multiply by these to convert degrees <---> radians */
#define DEG2RAD (M_PI / 180)
#define RAD2DEG (180 / M_PI)
TWN_API void log_info(const char *restrict format, ...);
TWN_API void log_critical(const char *restrict format, ...);
TWN_API void log_warn(const char *restrict format, ...);
/* saves all texture atlases as BMP files in the write directory */
TWN_API void textures_dump_atlases(void);
/* TODO: this is why generics were invented. sorry, i'm tired today */
TWN_API float clampf(float f, float min, float max);
/* sets buf_out to a pointer to a byte buffer which must be freed. */
/* returns the size of this buffer. */
TWN_API int64_t file_to_bytes(const char *path, unsigned char **buf_out);
/* returns a pointer to a string which must be freed */
TWN_API char *file_to_str(const char *path);
/* returns true if the file exists in the filesystem */
TWN_API bool file_exists(const char *path);
#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);
/* commit write to a file, which meaning depends on interpretation */
/* file_read should not be affected for current frame by this, operation is pending */
/* defined commands: */
/* <path:string> -- save utf8 file */
/* <path:binary> -- save binary file */
/* <path:image> -- write image data to a PNG file, potentially updating running textures used */
/* -- format is: [width:2b][height:2][alpha:1][data:(3+alpha)*width*height] */
TWN_API void file_write(char const *file, const char *operation, String string);
/* TODO: move to external templated lib */
/* calculates the overlap of two rectangles */
TWN_API Rect rect_overlap(Rect a, Rect b);
/* returns true if two rectangles are intersecting */
TWN_API bool rect_intersects(Rect a, Rect b);
/* TODO: move to external templated lib */
/* decrements a floating point second-based timer, stopping at 0.0f */
/* meant for poll based real time logic in game logic */
/* note that it should be decremented only on the next tick after its creation */
@ -60,6 +49,8 @@ typedef struct TimerElapseSecondsResult {
} TimerElapseSecondsResult;
TWN_API TimerElapseSecondsResult timer_elapse_seconds(float seconds_left, float interval);
TWN_API void log_string(char const *value, char const *identity);
TWN_API void log_float(float value, char const *identity);
TWN_API void log_vec2(Vec2 value, char const *identity);
TWN_API void log_vec3(Vec3 value, char const *identity);
TWN_API void log_rect(Rect value, char const *identity);