From 3d7a4ceaf0785c5a4d5bafec35eacf6cb052251b Mon Sep 17 00:00:00 2001 From: TSnake41 Date: Fri, 10 Apr 2020 21:32:36 +0200 Subject: [PATCH] =?UTF-8?q?raylua=20v3.0b=20=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support of asset loading from payload. (filesystem override) Update raylib. --- raylib | 2 +- src/raylua.lua | 2 +- src/raylua_e.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++-- tools/api.h | 5 ++-- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/raylib b/raylib index 62b7064..91e3677 160000 --- a/raylib +++ b/raylib @@ -1 +1 @@ -Subproject commit 62b7064e903fa04a403282ff2af55856da3e037d +Subproject commit 91e36777625b6d808e31fc2eb46a5519054bd801 diff --git a/src/raylua.lua b/src/raylua.lua index 8f59238..3a7c5a5 100644 --- a/src/raylua.lua +++ b/src/raylua.lua @@ -16,7 +16,7 @@ local load = loadstring -raylua.version = "v3.0a" +raylua.version = "v3.0b" function raylua.repl() print("> raylua " .. raylua.version .. " <") diff --git a/src/raylua_e.c b/src/raylua_e.c index 6778b79..450b477 100644 --- a/src/raylua_e.c +++ b/src/raylua_e.c @@ -23,6 +23,8 @@ #include #include +#include + #include "raylua.h" #include "lib/miniz.h" @@ -51,13 +53,12 @@ int raylua_loadfile(lua_State *L) } size_t size = stat.m_uncomp_size; - char *buffer = malloc(size + 1); + char *buffer = malloc(size); if (buffer == NULL) { lua_pushnil(L); lua_pushfstring(L, "%s: Can't allocate file buffer.", path); return 2; } - buffer[size] = '\0'; mz_zip_reader_extract_to_mem(&zip_file, index, buffer, size, 0); @@ -66,6 +67,60 @@ int raylua_loadfile(lua_State *L) return 1; } +unsigned char *raylua_loadFileData(const char *path, unsigned int *out_size) +{ + int index = mz_zip_reader_locate_file(&zip_file, path, NULL, 0); + if (index == -1) { + printf("RAYLUA: WARN: File not found in payload : '%s'", path); + return NULL; + } + + mz_zip_archive_file_stat stat; + if (!mz_zip_reader_file_stat(&zip_file, index, &stat)) { + printf("RAYLUA: WARN: Can't get file information of '%s' in payload.", path); + return NULL; + } + + size_t size = stat.m_uncomp_size; + unsigned char *buffer = RL_MALLOC(size); + if (buffer == NULL) { + printf("RAYLUA: WARN: Can't allocate file buffer for '%s'.", path); + return NULL; + } + + mz_zip_reader_extract_to_mem(&zip_file, index, buffer, size, 0); + + *out_size = size; + return buffer; +} + +char *raylua_loadFileText(const char *path) +{ + int index = mz_zip_reader_locate_file(&zip_file, path, NULL, 0); + if (index == -1) { + printf("RAYLUA: WARN: File not found in payload : '%s'", path); + return NULL; + } + + mz_zip_archive_file_stat stat; + if (!mz_zip_reader_file_stat(&zip_file, index, &stat)) { + printf("RAYLUA: WARN: Can't get file information of '%s' in payload.", path); + return NULL; + } + + size_t size = stat.m_uncomp_size; + char *buffer = RL_MALLOC(size + 1); + if (buffer == NULL) { + printf("RAYLUA: WARN: Can't allocate file buffer for '%s'.", path); + return NULL; + } + + buffer[size] = '\0'; + + mz_zip_reader_extract_to_mem(&zip_file, index, buffer, size, 0); + return buffer; +} + static bool raylua_init_payload(const char *path) { mz_zip_zero_struct(&zip_file); @@ -109,6 +164,11 @@ int main(int argc, const char **argv) } #endif + SetFilesystemOverride((FilesystemOverride){ + .loadFileData = &raylua_loadFileData, + .loadFileText = &raylua_loadFileText, + }); + if (!raylua_init_payload(path)) { #ifdef RAYLUA_NO_BUILDER puts("RAYLUA: No payload."); diff --git a/tools/api.h b/tools/api.h index 655211e..013eb82 100644 --- a/tools/api.h +++ b/tools/api.h @@ -181,7 +181,6 @@ bool CheckCollisionPointCircle(Vector2 point, Vector2 center, float radius); bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2 p3); Image LoadImage(const char *fileName); Image LoadImageEx(Color *pixels, int width, int height); -Image LoadImagePro(void *data, int width, int height, int format); Image LoadImageRaw(const char *fileName, int width, int height, int format, int headerSize); void ExportImage(Image image, const char *fileName); void ExportImageAsCode(Image image, const char *fileName); @@ -228,8 +227,8 @@ void ImageDrawCircle(Image *dst, int centerX, int centerY, int radius, Color col void ImageDrawCircleV(Image *dst, Vector2 center, int radius, Color color); void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color); void ImageDrawLineV(Image *dst, Vector2 start, Vector2 end, Color color); -void ImageDrawText(Image *dst, Vector2 position, const char *text, int fontSize, Color color); -void ImageDrawTextEx(Image *dst, Vector2 position, Font font, const char *text, float fontSize, float spacing, Color color); +void ImageDrawText(Image *dst, const char *text, int posX, int posY, int fontSize, Color color); +void ImageDrawTextEx(Image *dst, Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); void ImageFlipVertical(Image *image); void ImageFlipHorizontal(Image *image); void ImageRotateCW(Image *image);