diff options
| author | jussi | 2023-12-06 18:41:01 +0200 |
|---|---|---|
| committer | jussi | 2023-12-06 18:41:01 +0200 |
| commit | 6ecbbcc282bb25fae4bcd2e9f394adb74ac60ae7 (patch) | |
| tree | 8aead333dbeb5abc5deacdcbb243a5a1291c5079 /src | |
| parent | 4a783a156e1ab6ea9a8b69cd75db6aa33bb6eefc (diff) | |
| download | reilua-enhanced-6ecbbcc282bb25fae4bcd2e9f394adb74ac60ae7.tar.gz reilua-enhanced-6ecbbcc282bb25fae4bcd2e9f394adb74ac60ae7.tar.bz2 reilua-enhanced-6ecbbcc282bb25fae4bcd2e9f394adb74ac60ae7.zip | |
Files management functions.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core.c | 111 | ||||
| -rw-r--r-- | src/lua_core.c | 7 | ||||
| -rw-r--r-- | src/platforms/core_desktop.c | 162 | ||||
| -rw-r--r-- | src/platforms/core_web.c | 1 | ||||
| -rw-r--r-- | src/text.c | 4 |
5 files changed, 181 insertions, 104 deletions
@@ -1419,6 +1419,95 @@ int lcoreSetGCUnload( lua_State *L ) { */ /* +> buffer = RL.LoadFileData( string fileName ) + +Load file data as byte array (read). Buffer type is BUFFER_UNSIGNED_CHAR + +- Success return Buffer +*/ +int lcoreLoadFileData( lua_State *L ) { + const char *fileName = luaL_checkstring( L, 1 ); + + Buffer buffer = { + .type = BUFFER_UNSIGNED_CHAR + }; + buffer.data = LoadFileData( fileName, (int*)&buffer.size ); + + uluaPushBuffer( L, buffer ); + + return 1; +} + +/* +> success = RL.SaveFileData( string fileName, buffer Buffer ) + +Save data to file from byte array (write), returns true on success + +- Success return bool +*/ +int lcoreSaveFileData( lua_State *L ) { + const char *fileName = luaL_checkstring( L, 1 ); + Buffer *buffer = uluaGetBuffer( L, 2 ); + + lua_pushboolean( L, SaveFileData( fileName, buffer->data, buffer->size ) ); + + return 1; +} + +/* +> success = RL.ExportDataAsCode( Buffer buffer, string fileName ) + +Export data to code (.h), returns true on success + +- Success return bool +*/ +int lcoreExportDataAsCode( lua_State *L ) { + Buffer *buffer = uluaGetBuffer( L, 1 ); + const char *fileName = luaL_checkstring( L, 2 ); + + lua_pushboolean( L, ExportDataAsCode( buffer->data, buffer->size, fileName ) ); + + return 1; +} + +/* +> text = RL.LoadFileText( string fileName ) + +Load text data from file (read) + +- Success return string +*/ +int lcoreLoadFileText( lua_State *L ) { + const char *fileName = luaL_checkstring( L, 1 ); + + char *text = LoadFileText( fileName ); + lua_pushstring( L, text ); + UnloadFileText( text ); + + return 1; +} + +/* +> success = RL.SaveFileText( string fileName, string text ) + +Save text data to file (write), returns true on success + +- Success return bool +*/ +int lcoreSaveFileText( lua_State *L ) { + const char *fileName = luaL_checkstring( L, 1 ); + char *text = (char*)luaL_checkstring( L, 2 ); + + lua_pushboolean( L, SaveFileText( fileName, text ) ); + + return 1; +} + +/* +## Core - Files system functions +*/ + +/* > path = RL.GetBasePath() Return game directory (where main.lua is located) @@ -2951,8 +3040,8 @@ Read buffer data from binary file - Success return Buffer */ int lcoreLoadBufferFromFile( lua_State *L ) { - int type = luaL_checkinteger( L, 2 ); const char *path = luaL_checkstring( L, 1 ); + int type = luaL_checkinteger( L, 2 ); int fileLen = GetFileLength( path ); Buffer buffer = { @@ -2993,7 +3082,7 @@ int lcoreUnloadBuffer( lua_State *L ) { /* > data = RL.GetBufferData( Buffer buffer ) -Get buffer data as table in the format is was stored +Get buffer data as table in the format it was stored - Success return data{} */ @@ -3135,24 +3224,8 @@ int lcoreExportBuffer( lua_State *L ) { FILE *file; file = fopen( path, "wb" ); - fwrite( buffer->data, elementSize, buffer->size / elementSize, file ) ; + fwrite( buffer->data, elementSize, buffer->size / elementSize, file ); fclose( file ); return 0; } - -/* -> success = RL.ExportBufferAsCode( Buffer buffer, string fileName ) - -Export buffer data to code (.h), returns true on success - -- Success return bool -*/ -int lcoreExportBufferAsCode( lua_State *L ) { - Buffer *buffer = uluaGetBuffer( L, 1 ); - const char *fileName = luaL_checkstring( L, 2 ); - - lua_pushboolean( L, ExportDataAsCode( buffer->data, buffer->size, fileName ) ); - - return 1; -} diff --git a/src/lua_core.c b/src/lua_core.c index ecd5f75..acd4ac7 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -1264,6 +1264,12 @@ void luaRegister() { assingGlobalFunction( "IsGCUnloadEnabled", lcoreIsGCUnloadEnabled ); assingGlobalFunction( "SetGCUnload", lcoreSetGCUnload ); /* Files management functions. */ + assingGlobalFunction( "LoadFileData", lcoreLoadFileData ); + assingGlobalFunction( "SaveFileData", lcoreSaveFileData ); + assingGlobalFunction( "ExportDataAsCode", lcoreExportDataAsCode ); + assingGlobalFunction( "LoadFileText", lcoreLoadFileText ); + assingGlobalFunction( "SaveFileText", lcoreSaveFileText ); + /* Files system functions. */ assingGlobalFunction( "GetBasePath", lcoreGetBasePath ); assingGlobalFunction( "FileExists", lcoreFileExists ); assingGlobalFunction( "DirectoryExists", lcoreDirectoryExists ); @@ -1374,7 +1380,6 @@ void luaRegister() { assingGlobalFunction( "GetBufferType", lcoreGetBufferType ); assingGlobalFunction( "GetBufferSize", lcoreGetBufferSize ); assingGlobalFunction( "ExportBuffer", lcoreExportBuffer ); - assingGlobalFunction( "ExportBufferAsCode", lcoreExportBufferAsCode ); /* Shapes. */ /* Basic shapes drawing functions. */ diff --git a/src/platforms/core_desktop.c b/src/platforms/core_desktop.c index d2c595e..fa47d4a 100644 --- a/src/platforms/core_desktop.c +++ b/src/platforms/core_desktop.c @@ -519,41 +519,41 @@ static void joystickEvent( int jid, int event ) { Called when the pen tablet data is updated. Type GLFW_PEN_TABLET_DATA_EVENT NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ -// static void penTabletDataEvent( double x, double y, double z, double pressure, double pitch, double yaw, double roll ) { -// lua_State *L = state->luaState; - -// lua_pushcfunction( L, luaTraceback ); -// int tracebackidx = lua_gettop( L ); - -// lua_getglobal( L, "RL" ); -// lua_getfield( L, -1, "event" ); - -// if ( lua_isfunction( L, -1 ) ) { -// lua_createtable( L, 8, 0 ); -// lua_pushinteger( L, GLFW_PEN_TABLET_DATA_EVENT ); -// lua_setfield( L, -2, "type" ); -// lua_pushnumber( L, x ); -// lua_setfield( L, -2, "x" ); -// lua_pushnumber( L, y ); -// lua_setfield( L, -2, "y" ); -// lua_pushnumber( L, z ); -// lua_setfield( L, -2, "z" ); -// lua_pushnumber( L, pressure ); -// lua_setfield( L, -2, "pressure" ); -// lua_pushnumber( L, pitch ); -// lua_setfield( L, -2, "pitch" ); -// lua_pushnumber( L, yaw ); -// lua_setfield( L, -2, "yaw" ); -// lua_pushnumber( L, roll ); -// lua_setfield( L, -2, "roll" ); - -// if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { -// TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); -// state->run = false; -// } -// } -// lua_pop( L, -1 ); -// } +static void penTabletDataEvent( double x, double y, double z, double pressure, double pitch, double yaw, double roll ) { + lua_State *L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 8, 0 ); + lua_pushinteger( L, GLFW_PEN_TABLET_DATA_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushnumber( L, x ); + lua_setfield( L, -2, "x" ); + lua_pushnumber( L, y ); + lua_setfield( L, -2, "y" ); + lua_pushnumber( L, z ); + lua_setfield( L, -2, "z" ); + lua_pushnumber( L, pressure ); + lua_setfield( L, -2, "pressure" ); + lua_pushnumber( L, pitch ); + lua_setfield( L, -2, "pitch" ); + lua_pushnumber( L, yaw ); + lua_setfield( L, -2, "yaw" ); + lua_pushnumber( L, roll ); + lua_setfield( L, -2, "roll" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} /* > GLFWpentabletcursorEvent = { int type, int identifier } @@ -561,29 +561,29 @@ NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 Called when the pen tablet cursor has changed. Type GLFW_PEN_TABLET_CURSOR_EVENT NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ -// static void penTabletCursorEvent( unsigned int identifier ) { -// lua_State *L = state->luaState; - -// lua_pushcfunction( L, luaTraceback ); -// int tracebackidx = lua_gettop( L ); - -// lua_getglobal( L, "RL" ); -// lua_getfield( L, -1, "event" ); - -// if ( lua_isfunction( L, -1 ) ) { -// lua_createtable( L, 2, 0 ); -// lua_pushinteger( L, GLFW_PEN_TABLET_CURSOR_EVENT ); -// lua_setfield( L, -2, "type" ); -// lua_pushinteger( L, identifier ); -// lua_setfield( L, -2, "identifier" ); - -// if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { -// TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); -// state->run = false; -// } -// } -// lua_pop( L, -1 ); -// } +static void penTabletCursorEvent( unsigned int identifier ) { + lua_State *L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_PEN_TABLET_CURSOR_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, identifier ); + lua_setfield( L, -2, "identifier" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} /* > GLFWpentabletproximityEvent = { int type, int proxState } @@ -591,29 +591,29 @@ NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 Called when the pen tablet proximity has changed. Type GLFW_PEN_TABLET_PROXIMITY_EVENT NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ -// static void penTabletProximityEvent( int proxState ) { -// lua_State *L = state->luaState; - -// lua_pushcfunction( L, luaTraceback ); -// int tracebackidx = lua_gettop( L ); - -// lua_getglobal( L, "RL" ); -// lua_getfield( L, -1, "event" ); - -// if ( lua_isfunction( L, -1 ) ) { -// lua_createtable( L, 2, 0 ); -// lua_pushinteger( L, GLFW_PEN_TABLET_PROXIMITY_EVENT ); -// lua_setfield( L, -2, "type" ); -// lua_pushinteger( L, proxState ); -// lua_setfield( L, -2, "state" ); - -// if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { -// TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); -// state->run = false; -// } -// } -// lua_pop( L, -1 ); -// } +static void penTabletProximityEvent( int proxState ) { + lua_State *L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_PEN_TABLET_PROXIMITY_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, proxState ); + lua_setfield( L, -2, "state" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} static void platformRegisterEvents() { /* Window events. */ diff --git a/src/platforms/core_web.c b/src/platforms/core_web.c index 24d3bf2..2cf255a 100644 --- a/src/platforms/core_web.c +++ b/src/platforms/core_web.c @@ -16,5 +16,4 @@ void platformDefineGlobals() { /* Events. */ void luaPlatformRegister() { - return; } @@ -19,7 +19,7 @@ bool wordWrap, Color *tints, int tintCount, Color *backTints, int backTintCount // Word/character wrapping mechanism variables enum { MEASURE_STATE = 0, DRAW_STATE = 1 }; - int state = wordWrap? MEASURE_STATE : DRAW_STATE; + int state = wordWrap ? MEASURE_STATE : DRAW_STATE; int startLine = -1; // Index where to begin drawing (where a line begins) int endLine = -1; // Index where to stop drawing (where a line ends) @@ -29,7 +29,7 @@ bool wordWrap, Color *tints, int tintCount, Color *backTints, int backTintCount Vector2 mousePos = GetMousePosition(); int mouseChar = -1; - for (int i = 0, k = 0; i < length; i++, k++) + for ( int i = 0, k = 0; i < length; i++, k++) { if ( i < tintCount ) { tint = tints[i]; |
