twn_util: final cleaning up, introducton of powerful file_read()
This commit is contained in:
parent
f86f3dd41a
commit
56530f9864
@ -29,10 +29,6 @@ void game_tick(void) {
|
||||
ctx.debug = !ctx.debug;
|
||||
}
|
||||
|
||||
if (input_action_just_pressed("debug_dump_atlases")) {
|
||||
textures_dump_atlases();
|
||||
}
|
||||
|
||||
state->scene->tick(state);
|
||||
|
||||
/* there's a scene switch pending, we can do it now that the tick is done */
|
||||
|
@ -30,10 +30,6 @@ void game_tick(void) {
|
||||
ctx.debug = !ctx.debug;
|
||||
}
|
||||
|
||||
if (input_action_just_pressed("debug_dump_atlases")) {
|
||||
textures_dump_atlases();
|
||||
}
|
||||
|
||||
state->scene->tick(state);
|
||||
|
||||
/* there's a scene switch pending, we can do it now that the tick is done */
|
||||
|
@ -109,6 +109,12 @@ static float height_at(SceneIngame *scn, Vec2 position);
|
||||
static Vec3 normal_at(SceneIngame *scn, Vec2 position);
|
||||
|
||||
|
||||
static inline float clampf(float f, float min, float max) {
|
||||
const float t = f < min ? min : f;
|
||||
return t > max ? max : t;
|
||||
}
|
||||
|
||||
|
||||
static void draw_vehicle(SceneIngame *scn) {
|
||||
for (size_t i = 0; i < 12; ++i)
|
||||
draw_line_3d(vbp[vbs[i][0]], vbp[vbs[i][1]], 1, (Color){255, 255, 255, 255});
|
||||
|
@ -112,6 +112,9 @@ for procedure, procedure_desc in api["procedures"].items():
|
||||
binding += " lua_pushnumber(L, (float)(%s(%s)));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||
elif procedure_desc["return"] == "char *":
|
||||
binding += " lua_pushstring(L, %s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||
elif procedure_desc["return"] == "String":
|
||||
binding += " String result = %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||
binding += " lua_pushlstring(L, result.data, (int)result.length);\n"
|
||||
elif type(procedure_desc["return"]) is dict or procedure_desc["return"] in api["types"]:
|
||||
type_desc = procedure_desc["return"] if type(procedure_desc["return"]) is dict else api["types"][procedure_desc["return"]]
|
||||
binding += " %s result = %s(%s);\n" % (type_desc["c_type"], procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||
|
@ -26,7 +26,7 @@ static int physfs_loader(lua_State *L) {
|
||||
|
||||
static const char *name_breaker = NULL;
|
||||
if (name_breaker && SDL_strcmp(name, name_breaker) == 0) {
|
||||
log_critical("Recursive load on itself from lua module (%s)", name_breaker);
|
||||
log_string(name_breaker, "Recursive load on itself from lua module");
|
||||
return 0;
|
||||
} name_breaker = name;
|
||||
|
||||
@ -40,26 +40,26 @@ static int physfs_loader(lua_State *L) {
|
||||
SDL_asprintf(&final_path, "/scripts/%s.lua", path_copy);
|
||||
SDL_free(path_copy);
|
||||
|
||||
if (!file_exists(final_path)) {
|
||||
if (SDL_strcmp(file_read(final_path, ":exists").data, "yes") == 0) {
|
||||
char *error_message = NULL;
|
||||
SDL_asprintf(&error_message, "could not find module %s in filesystem", name);
|
||||
lua_pushstring(L, error_message);
|
||||
free(error_message);
|
||||
SDL_free(error_message);
|
||||
|
||||
free(final_path);
|
||||
SDL_free(final_path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char *buf = NULL;
|
||||
int64_t buf_size = file_to_bytes(final_path, &buf);
|
||||
free(final_path);
|
||||
char *final_path_binary = NULL;
|
||||
SDL_asprintf(&final_path_binary, "%s%s", final_path, ":binary");
|
||||
|
||||
String file = file_read(final_path, ":binary");
|
||||
SDL_free(final_path);
|
||||
|
||||
/* TODO: use reader interface for streaming instead */
|
||||
int const result = luaL_loadbuffer(L, (char *)buf, buf_size, name);
|
||||
free(buf);
|
||||
|
||||
int const result = luaL_loadbuffer(L, file.data, (size_t)file.length, name);
|
||||
if (result != LUA_OK)
|
||||
log_critical("%s", lua_tostring(L, -1));
|
||||
log_string(lua_tostring(L, -1), NULL);
|
||||
|
||||
return result == LUA_OK;
|
||||
}
|
||||
@ -110,7 +110,7 @@ static void *custom_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
|
||||
static void exchange_lua_states(lua_State *from, lua_State *to, int level, int index) {
|
||||
if (level >= UDATA_NESTING_LIMIT) {
|
||||
log_critical("ctx.udata nesting limit is reached (%u)", UDATA_NESTING_LIMIT);
|
||||
log_string("ctx.udata nesting limit is reached", NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -143,7 +143,7 @@ static void exchange_lua_states(lua_State *from, lua_State *to, int level, int i
|
||||
break;
|
||||
default:
|
||||
/* TODO: provide a path and type of it for better diagnostic */
|
||||
log_warn("Unserializable udata found and is ignored");
|
||||
log_string("Unserializable udata found and is ignored", NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -167,7 +167,6 @@ void game_tick(void) {
|
||||
SDL_assert(!lua_isnoneornil(state->L, -1));
|
||||
SDL_assert(!lua_isnoneornil(state->L, -2));
|
||||
if (!lua_isnoneornil(state->L, -1)) {
|
||||
log_info("Exchanging lua states...");
|
||||
lua_newtable(new_state);
|
||||
exchange_lua_states(state->L, new_state, 0, -1);
|
||||
lua_setfield(new_state, -2, "udata");
|
||||
@ -216,24 +215,21 @@ void game_tick(void) {
|
||||
bindgen_load_twn(state->L);
|
||||
|
||||
/* now finally get to running the code */
|
||||
unsigned char *game_buf = NULL;
|
||||
size_t game_buf_size = file_to_bytes("/scripts/game.lua", &game_buf);
|
||||
String file = file_read("/scripts/game.lua", ":binary");
|
||||
/* TODO: use reader interface for streaming instead */
|
||||
if (luaL_loadbuffer(state->L, (char *)game_buf, game_buf_size, "game.lua") == LUA_OK) {
|
||||
if (luaL_loadbuffer(state->L, file.data, (size_t)file.length, "game.lua") == LUA_OK) {
|
||||
if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) {
|
||||
log_critical("%s", lua_tostring(state->L, -1));
|
||||
log_string(luaL_tolstring(state->L, -1, NULL), "Error executing /scripts/game.lua entry");
|
||||
lua_pop(state->L, 1);
|
||||
} else
|
||||
state->loaded_successfully = true;
|
||||
} else {
|
||||
/* got some sort of error, it should be pushed on top of the stack */
|
||||
SDL_assert(lua_isstring(state->L, -1));
|
||||
log_critical("Error loading /scripts/game.lua entry: %s", luaL_tolstring(state->L, -1, NULL));
|
||||
log_string(luaL_tolstring(state->L, -1, NULL), "Error loading /scripts/game.lua entry");
|
||||
lua_pop(state->L, 1);
|
||||
}
|
||||
|
||||
SDL_free(game_buf);
|
||||
|
||||
/* from this point we have access to everything defined in lua */
|
||||
}
|
||||
|
||||
@ -251,7 +247,7 @@ void game_tick(void) {
|
||||
|
||||
lua_getglobal(state->L, "game_tick");
|
||||
if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) {
|
||||
log_critical("%s", lua_tostring(state->L, -1));
|
||||
log_string(luaL_tolstring(state->L, -1, NULL), "Error executing game_tick()");
|
||||
lua_pop(state->L, 1);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -312,9 +312,10 @@
|
||||
"symbol": "read",
|
||||
"header": "twn_util.h",
|
||||
"params": [
|
||||
{ "name": "file", "type": "char *" }
|
||||
{ "name": "file", "type": "char *" },
|
||||
{ "name": "operation", "type": "char *" }
|
||||
],
|
||||
"return": "char *",
|
||||
"return": "String",
|
||||
"restriction": "asset"
|
||||
},
|
||||
|
||||
|
@ -265,7 +265,7 @@ static void resolve_pack_dependencies(const char *pack_name) {
|
||||
/* no package manifest provided, abort */
|
||||
goto OK_NO_MANIFEST;
|
||||
|
||||
char *manifest_file = file_to_str(path);
|
||||
char *manifest_file = file_to_str(path, NULL);
|
||||
if (!manifest_file) {
|
||||
CRY_PHYSFS("Pack manifest file loading failed");
|
||||
goto ERR_PACK_MANIFEST_FILE_LOADING;
|
||||
@ -375,7 +375,7 @@ static bool initialize(void) {
|
||||
|
||||
/* load the config file into an opaque table */
|
||||
{
|
||||
char *config_file = file_to_str("/twn.toml");
|
||||
char *config_file = file_to_str("/twn.toml", NULL);
|
||||
if (config_file == NULL) {
|
||||
CRY("Configuration file loading failed", "Cannot access /twn.toml");
|
||||
goto fail;
|
||||
|
@ -109,12 +109,6 @@ void *ccalloc(size_t num, size_t size) {
|
||||
}
|
||||
|
||||
|
||||
float clampf(float f, float min, float max) {
|
||||
const float t = f < min ? min : f;
|
||||
return t > max ? max : t;
|
||||
}
|
||||
|
||||
|
||||
int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
|
||||
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
|
||||
|
||||
@ -133,7 +127,7 @@ int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
|
||||
}
|
||||
|
||||
|
||||
char *file_to_str(const char *path) {
|
||||
char *file_to_str(const char *path, size_t *out_len) {
|
||||
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
|
||||
|
||||
if (handle == NULL) {
|
||||
@ -151,23 +145,62 @@ char *file_to_str(const char *path) {
|
||||
|
||||
str_out[len] = '\0';
|
||||
|
||||
if (out_len)
|
||||
*out_len = len;
|
||||
|
||||
return str_out;
|
||||
}
|
||||
|
||||
static char **read_files;
|
||||
static String *read_files;
|
||||
|
||||
char const *file_read(char const *file) {
|
||||
char *s = file_to_str(file);
|
||||
String file_read(char const *file, char const *operation) {
|
||||
if (!file) {
|
||||
log_warn("No file specified");
|
||||
return (String){NULL, 0};
|
||||
}
|
||||
|
||||
if (s) arrpush(read_files, s);
|
||||
if (!operation) {
|
||||
log_warn("No operation specified");
|
||||
return (String){NULL, 0};
|
||||
}
|
||||
|
||||
return s;
|
||||
if (SDL_strncmp(operation, ":string", sizeof (":string") - 1) == 0) {
|
||||
size_t length;
|
||||
char *data = file_to_str(file, &length);
|
||||
|
||||
if (!data)
|
||||
return (String){NULL, 0};
|
||||
|
||||
String s = {data, (float)length};
|
||||
arrpush(read_files, s);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
if (SDL_strncmp(operation, ":binary", sizeof (":binary") - 1) == 0) {
|
||||
uint8_t *data;
|
||||
int64_t length = file_to_bytes(file, &data);
|
||||
|
||||
if (length == -1)
|
||||
return (String){NULL, 0};
|
||||
|
||||
String s = {(char *)data, (float)length};
|
||||
arrpush(read_files, s);
|
||||
|
||||
return s;
|
||||
|
||||
} else
|
||||
log_warn("No valid operation specified by %s", operation);
|
||||
|
||||
return (String){NULL, 0};
|
||||
}
|
||||
|
||||
|
||||
void file_read_garbage_collect(void) {
|
||||
for (size_t i = 0; i < arrlenu(read_files); ++i)
|
||||
SDL_free(read_files[i]);
|
||||
for (size_t i = 0; i < arrlenu(read_files); ++i) {
|
||||
SDL_assert_always(read_files[i].data && read_files[i].length >= 0);
|
||||
SDL_free(read_files[i].data);
|
||||
}
|
||||
arrfree(read_files);
|
||||
read_files = NULL;
|
||||
}
|
||||
@ -342,6 +375,18 @@ void profile_list_stats(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void log_string(char const *value, char const *message) {
|
||||
if (!value) return;
|
||||
if (!message)
|
||||
log_info("%s", value);
|
||||
else
|
||||
log_info("%s: %s", message, value);
|
||||
}
|
||||
|
||||
void log_float(float value, char const *message) {
|
||||
if (!message) message = "float";
|
||||
log_info("%s = %f", message, (double)value);
|
||||
}
|
||||
|
||||
void log_vec2(Vec2 vector, char const *message) {
|
||||
if (!message) message = "Vec2";
|
||||
|
@ -21,6 +21,10 @@ void cry_impl(const char *file, const int line, const char *title, const char *t
|
||||
#define CRY_PHYSFS(title) \
|
||||
cry_impl(__FILE__, __LINE__, title, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))
|
||||
|
||||
void log_info(const char *restrict format, ...);
|
||||
void log_critical(const char *restrict format, ...);
|
||||
void log_warn(const char *restrict format, ...);
|
||||
|
||||
/* for when there's absolutely no way to continue */
|
||||
_Noreturn void die_abruptly(void);
|
||||
|
||||
@ -31,6 +35,23 @@ void profile_list_stats(void);
|
||||
|
||||
void file_read_garbage_collect(void);
|
||||
|
||||
/* sets buf_out to a pointer to a byte buffer which must be freed. */
|
||||
/* returns the size of this buffer. */
|
||||
int64_t file_to_bytes(const char *path, unsigned char **buf_out);
|
||||
|
||||
/* returns a pointer to a string which must be freed */
|
||||
char *file_to_str(const char *path, size_t *out_len);
|
||||
|
||||
/* saves all texture atlases as BMP files in the write directory */
|
||||
void textures_dump_atlases(void);
|
||||
|
||||
bool file_exists(const char *path);
|
||||
|
||||
static inline float clampf(float f, float min, float max) {
|
||||
const float t = f < min ? min : f;
|
||||
return t > max ? max : t;
|
||||
}
|
||||
|
||||
/* http://www.azillionmonkeys.com/qed/sqroot.html */
|
||||
static inline float fast_sqrt(float x)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user