From 00c7e54d3c593dbddb036f2185e614e7e4b22a1f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 6 Aug 2016 11:32:35 +0200 Subject: Add raylib lua examples --- examples/core_drop_files.lua | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 examples/core_drop_files.lua (limited to 'examples/core_drop_files.lua') diff --git a/examples/core_drop_files.lua b/examples/core_drop_files.lua new file mode 100644 index 00000000..0a437422 --- /dev/null +++ b/examples/core_drop_files.lua @@ -0,0 +1,63 @@ +------------------------------------------------------------------------------------------- +-- +-- raylib [core] example - Windows drop files +-- +-- This example only works on platforms that support drag & drop (Windows, Linux, OSX) +-- +-- This example has been created using raylib 1.6 (www.raylib.com) +-- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +-- +-- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5) +-- +------------------------------------------------------------------------------------------- + +-- Initialization +------------------------------------------------------------------------------------------- +local screenWidth = 800 +local screenHeight = 450 + +InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files") + +local count = 0 +--char **droppedFiles -- ??? + +SetTargetFPS(60) +------------------------------------------------------------------------------------------- + +-- Main game loop +while not WindowShouldClose() do -- Detect window close button or ESC key + -- Update + --------------------------------------------------------------------------------------- + if (IsFileDropped()) then droppedFiles = GetDroppedFiles(count) end + --------------------------------------------------------------------------------------- + + -- Draw + --------------------------------------------------------------------------------------- + BeginDrawing() + + ClearBackground(RAYWHITE) + + if (count == 0) then DrawText("Drop your files to this window!", 100, 40, 20, DARKGRAY) + else + DrawText("Dropped files:", 100, 40, 20, DARKGRAY) + + for i = 0, count do + if (i%2 == 0) then DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.5)) + else DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.3)) end + + DrawText(droppedFiles[i], 120, 100 + 40*i, 10, GRAY) + end + + DrawText("Drop new files...", 100, 110 + 40*count, 20, DARKGRAY) + end + + EndDrawing() + --------------------------------------------------------------------------------------- +end + +-- De-Initialization +------------------------------------------------------------------------------------------- +ClearDroppedFiles() -- Clear internal buffers + +CloseWindow() -- Close window and OpenGL context +------------------------------------------------------------------------------------------- \ No newline at end of file -- cgit v1.2.3 From 6f27941e286eba9872d1c341b446c9d13272405a Mon Sep 17 00:00:00 2001 From: ghassanpl Date: Sat, 6 Aug 2016 21:51:08 +0200 Subject: GetDroppedFiles and SetShaderValue in Lua working Exposed Texture2D.id to Lua Lights now have settable/gettable fields --- examples/core_drop_files.lua | 9 ++-- examples/shaders_custom_uniform.lua | 6 +-- src/rlua.h | 102 ++++++++++++++++++++++++++++++------ 3 files changed, 96 insertions(+), 21 deletions(-) (limited to 'examples/core_drop_files.lua') diff --git a/examples/core_drop_files.lua b/examples/core_drop_files.lua index 0a437422..99ab4325 100644 --- a/examples/core_drop_files.lua +++ b/examples/core_drop_files.lua @@ -28,7 +28,10 @@ SetTargetFPS(60) while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - if (IsFileDropped()) then droppedFiles = GetDroppedFiles(count) end + if (IsFileDropped()) then + droppedFiles = GetDroppedFiles() + count = #droppedFiles + end --------------------------------------------------------------------------------------- -- Draw @@ -41,11 +44,11 @@ while not WindowShouldClose() do -- Detect window close button or ESC key else DrawText("Dropped files:", 100, 40, 20, DARKGRAY) - for i = 0, count do + for i = 0, count-1 do if (i%2 == 0) then DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.5)) else DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.3)) end - DrawText(droppedFiles[i], 120, 100 + 40*i, 10, GRAY) + DrawText(droppedFiles[i+1], 120, 100 + 40*i, 10, GRAY) end DrawText("Drop new files...", 100, 110 + 40*count, 20, DARKGRAY) diff --git a/examples/shaders_custom_uniform.lua b/examples/shaders_custom_uniform.lua index fb93adc1..3a8bbae5 100644 --- a/examples/shaders_custom_uniform.lua +++ b/examples/shaders_custom_uniform.lua @@ -60,11 +60,11 @@ while not WindowShouldClose() do -- Detect window close button or ESC key --------------------------------------------------------------------------------------- local mousePosition = GetMousePosition() - swirlCenter[0] = mousePosition.x - swirlCenter[1] = screenHeight - mousePosition.y + swirlCenter[1] = mousePosition.x + swirlCenter[2] = screenHeight - mousePosition.y -- Send new value to the shader to be used on drawing - SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2) + SetShaderValue(shader, swirlCenterLoc, swirlCenter) camera = UpdateCamera(camera) -- Update internal camera and our camera --------------------------------------------------------------------------------------- diff --git a/src/rlua.h b/src/rlua.h index 08ffbca0..fb3a6698 100644 --- a/src/rlua.h +++ b/src/rlua.h @@ -126,7 +126,7 @@ RLUADEF void CloseLuaDevice(void); // De-initialize Lua system #define LuaPush_SpriteFont(L, sf) LuaPushOpaqueTypeWithMetatable(L, sf, SpriteFont) #define LuaPush_Mesh(L, vd) LuaPushOpaqueType(L, vd) #define LuaPush_Shader(L, s) LuaPushOpaqueType(L, s) -#define LuaPush_Light(L, light) LuaPushOpaqueType(L, light) +#define LuaPush_Light(L, light) LuaPushOpaqueTypeWithMetatable(L, light, Light) #define LuaPush_Sound(L, snd) LuaPushOpaqueType(L, snd) #define LuaPush_Wave(L, wav) LuaPushOpaqueType(L, wav) #define LuaPush_Music(L, mus) LuaPushOpaqueType(L, mus) @@ -263,6 +263,8 @@ static int LuaIndexTexture2D(lua_State* L) lua_pushinteger(L, img.mipmaps); else if (!strcmp(key, "format")) lua_pushinteger(L, img.format); + else if (!strcmp(key, "id")) + lua_pushinteger(L, img.id); else return 0; return 1; @@ -296,6 +298,58 @@ static int LuaIndexSpriteFont(lua_State* L) return 1; } +static int LuaIndexLight(lua_State* L) +{ + Light light = LuaGetArgument_Light(L, 1); + const char *key = luaL_checkstring(L, 2); + if (!strcmp(key, "id")) + lua_pushinteger(L, light->id); + else if (!strcmp(key, "enabled")) + lua_pushboolean(L, light->enabled); + else if (!strcmp(key, "type")) + lua_pushinteger(L, light->type); + else if (!strcmp(key, "position")) + LuaPush_Vector3(L, light->position); + else if (!strcmp(key, "target")) + LuaPush_Vector3(L, light->target); + else if (!strcmp(key, "radius")) + lua_pushnumber(L, light->radius); + else if (!strcmp(key, "diffuse")) + LuaPush_Color(L, light->diffuse); + else if (!strcmp(key, "intensity")) + lua_pushnumber(L, light->intensity); + else if (!strcmp(key, "coneAngle")) + lua_pushnumber(L, light->coneAngle); + else + return 0; + return 1; +} + +static int LuaNewIndexLight(lua_State* L) +{ + Light light = LuaGetArgument_Light(L, 1); + const char *key = luaL_checkstring(L, 2); + if (!strcmp(key, "id")) + light->id = LuaGetArgument_int(L, 3); + else if (!strcmp(key, "enabled")) + light->enabled = lua_toboolean(L, 3); + else if (!strcmp(key, "type")) + light->type = LuaGetArgument_int(L, 3); + else if (!strcmp(key, "position")) + light->position = LuaGetArgument_Vector3(L, 3); + else if (!strcmp(key, "target")) + light->target = LuaGetArgument_Vector3(L, 3); + else if (!strcmp(key, "radius")) + light->radius = LuaGetArgument_float(L, 3); + else if (!strcmp(key, "diffuse")) + light->diffuse = LuaGetArgument_Color(L, 3); + else if (!strcmp(key, "intensity")) + light->intensity = LuaGetArgument_float(L, 3); + else if (!strcmp(key, "coneAngle")) + light->coneAngle = LuaGetArgument_float(L, 3); + return 0; +} + static void LuaBuildOpaqueMetatables(void) { luaL_newmetatable(L, "Image"); @@ -313,10 +367,17 @@ static void LuaBuildOpaqueMetatables(void) lua_setfield(L, -2, "__index"); lua_pop(L, 1); - luaL_newmetatable(L, "SpriteFont"); - lua_pushcfunction(L, &LuaIndexSpriteFont); - lua_setfield(L, -2, "__index"); - lua_pop(L, 1); + luaL_newmetatable(L, "SpriteFont"); + lua_pushcfunction(L, &LuaIndexSpriteFont); + lua_setfield(L, -2, "__index"); + lua_pop(L, 1); + + luaL_newmetatable(L, "Light"); + lua_pushcfunction(L, &LuaIndexLight); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, &LuaNewIndexLight); + lua_setfield(L, -2, "__newindex"); + lua_pop(L, 1); } //---------------------------------------------------------------------------------- @@ -1057,15 +1118,20 @@ int lua_IsFileDropped(lua_State* L) lua_pushboolean(L, result); return 1; } -/* -int lua_*GetDroppedFiles(lua_State* L) + +int lua_GetDroppedFiles(lua_State* L) { - int * arg1 = LuaGetArgument_int *(L, 1); - //char * result = *GetDroppedFiles(arg1); - LuaPush_//char *(L, result); - return 1; + int count = 0; + char ** result = GetDroppedFiles(&count); + lua_createtable(L, count, 0); + for (int i = 0; i < count; i++) + { + lua_pushstring(L, result[i]); + lua_rawseti(L, -2, i + 1); + } + return 1; } -*/ + int lua_ClearDroppedFiles(lua_State* L) { ClearDroppedFiles(); @@ -1638,7 +1704,6 @@ int lua_DrawPoly(lua_State* L) sz++; \ lua_pop(L, 1); \ } \ - lua_pop(L, 1); \ name = calloc(sz, sizeof(type)); \ sz = 0; \ lua_pushnil(L); \ @@ -2334,7 +2399,12 @@ int lua_DrawGizmo(lua_State* L) return 0; } -// TODO: DrawLight(Light light); +int lua_DrawLight(lua_State* L) +{ + Light arg1 = LuaGetArgument_Light(L, 1); + DrawLight(arg1); + return 0; +} int lua_Draw3DLine(lua_State* L) { @@ -3544,7 +3614,7 @@ static luaL_Reg raylib_functions[] = { REG(ShowLogo) REG(IsFileDropped) - //REG(*GetDroppedFiles) + REG(GetDroppedFiles) REG(ClearDroppedFiles) REG(StorageSaveValue) REG(StorageLoadValue) @@ -3698,6 +3768,8 @@ static luaL_Reg raylib_functions[] = { REG(DrawRay) REG(DrawGrid) REG(DrawGizmo) + + REG(DrawLight) REG(LoadModel) REG(LoadModelEx) -- cgit v1.2.3 From 2c079d7c6ea0a5a252f0d3f93bc39e8f5700e23a Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 7 Aug 2016 11:14:08 +0200 Subject: Review Lua examples and formatting --- examples/core_drop_files.lua | 8 ++-- examples/rlua_execute_file.c | 14 +++--- src/rlgl.c | 8 ++-- src/rlua.h | 100 +++++++++++++++++++++---------------------- 4 files changed, 65 insertions(+), 65 deletions(-) (limited to 'examples/core_drop_files.lua') diff --git a/examples/core_drop_files.lua b/examples/core_drop_files.lua index 99ab4325..1d27e618 100644 --- a/examples/core_drop_files.lua +++ b/examples/core_drop_files.lua @@ -19,7 +19,7 @@ local screenHeight = 450 InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files") local count = 0 ---char **droppedFiles -- ??? +local droppedFiles = {} SetTargetFPS(60) ------------------------------------------------------------------------------------------- @@ -29,9 +29,9 @@ while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- if (IsFileDropped()) then - droppedFiles = GetDroppedFiles() - count = #droppedFiles - end + droppedFiles = GetDroppedFiles() + count = #droppedFiles + end --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/rlua_execute_file.c b/examples/rlua_execute_file.c index 6050cf14..71720313 100644 --- a/examples/rlua_execute_file.c +++ b/examples/rlua_execute_file.c @@ -35,11 +35,11 @@ int main() // ExecuteLuaFile("core_input_gamepad.lua"); // OK! // ExecuteLuaFile("core_random_values.lua"); // OK! // ExecuteLuaFile("core_color_select.lua"); // OK! - // ExecuteLuaFile("core_drop_files.lua"); // ERROR: GetDroppedFiles() + // ExecuteLuaFile("core_drop_files.lua"); // OK! // ExecuteLuaFile("core_storage_values.lua"); // OK! // ExecuteLuaFile("core_gestures_detection.lua"); // OK! // ExecuteLuaFile("core_3d_mode.lua"); // OK! - // ExecuteLuaFile("core_3d_picking.lua"); // ISSUE: CheckCollisionRayBox() returns false despite touching box + // ExecuteLuaFile("core_3d_picking.lua"); // OK! // ExecuteLuaFile("core_3d_camera_free.lua"); // OK! // ExecuteLuaFile("core_3d_camera_first_person.lua"); // OK! // ExecuteLuaFile("core_2d_camera.lua"); // OK! @@ -54,8 +54,8 @@ int main() // ExecuteLuaFile("textures_rectangle.lua"); // OK! // ExecuteLuaFile("textures_srcrec_dstrec.lua"); // OK! // ExecuteLuaFile("textures_to_image.lua"); // OK! - // ExecuteLuaFile("textures_raw_data.lua"); // ERROR: bad argument #2 to 'LoadImageEx' (number expected, got no value) - // ExecuteLuaFile("textures_formats_loading.lua"); // ISSUE: texture.id not exposed to be checked (not really an issue...) + // ExecuteLuaFile("textures_raw_data.lua"); // ERROR: LoadImageEx() + // ExecuteLuaFile("textures_formats_loading.lua"); // OK! // ExecuteLuaFile("textures_particles_trail_blending.lua"); // OK! // ExecuteLuaFile("textures_image_processing.lua"); // ERROR: GetImageData() --> UpdateTexture() // ExecuteLuaFile("textures_image_drawing.lua"); // OK! @@ -73,13 +73,13 @@ int main() // ExecuteLuaFile("models_cubicmap.lua"); // OK! // ExecuteLuaFile("shaders_model_shader.lua"); // OK! // ExecuteLuaFile("shaders_shapes_textures.lua"); // OK! - // ExecuteLuaFile("shaders_custom_uniform.lua"); // ISSUE: SetShaderValue() + // ExecuteLuaFile("shaders_custom_uniform.lua"); // OK! // ExecuteLuaFile("shaders_postprocessing.lua"); // OK! - // ExecuteLuaFile("shaders_standard_lighting.lua"); // ERROR: CreateLight() returns an opaque pointer (fields can not be accessed) + // ExecuteLuaFile("shaders_standard_lighting.lua"); // OK! // ExecuteLuaFile("audio_sound_loading.lua"); // OK! // ExecuteLuaFile("audio_music_stream.lua"); // OK! // ExecuteLuaFile("audio_module_playing.lua"); // OK! - ExecuteLuaFile("audio_raw_stream.lua"); // ERROR: UpdateAudioStream() + // ExecuteLuaFile("audio_raw_stream.lua"); // ERROR: UpdateAudioStream() // De-Initialization //-------------------------------------------------------------------------------------- diff --git a/src/rlgl.c b/src/rlgl.c index 6fb4bf3d..68d562c7 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -2559,13 +2559,13 @@ void DestroyLight(Light light) { if (light != NULL) { - int light_id = light->id; + int lightId = light->id; // Free dynamic memory allocation - free(lights[light_id]); - + free(lights[lightId]); + // Remove *obj from the pointers array - for (int i = light_id; i < lightsCount; i++) + for (int i = lightId; i < lightsCount; i++) { // Resort all the following pointers of the array if ((i + 1) < lightsCount) diff --git a/src/rlua.h b/src/rlua.h index 85853793..153f2c37 100644 --- a/src/rlua.h +++ b/src/rlua.h @@ -300,29 +300,29 @@ static int LuaIndexSpriteFont(lua_State* L) static int LuaIndexLight(lua_State* L) { - Light light = LuaGetArgument_Light(L, 1); - const char *key = luaL_checkstring(L, 2); - if (!strcmp(key, "id")) - lua_pushinteger(L, light->id); - else if (!strcmp(key, "enabled")) - lua_pushboolean(L, light->enabled); - else if (!strcmp(key, "type")) - lua_pushinteger(L, light->type); - else if (!strcmp(key, "position")) - LuaPush_Vector3(L, light->position); - else if (!strcmp(key, "target")) - LuaPush_Vector3(L, light->target); - else if (!strcmp(key, "radius")) - lua_pushnumber(L, light->radius); - else if (!strcmp(key, "diffuse")) - LuaPush_Color(L, light->diffuse); - else if (!strcmp(key, "intensity")) - lua_pushnumber(L, light->intensity); - else if (!strcmp(key, "coneAngle")) - lua_pushnumber(L, light->coneAngle); - else - return 0; - return 1; + Light light = LuaGetArgument_Light(L, 1); + const char *key = luaL_checkstring(L, 2); + if (!strcmp(key, "id")) + lua_pushinteger(L, light->id); + else if (!strcmp(key, "enabled")) + lua_pushboolean(L, light->enabled); + else if (!strcmp(key, "type")) + lua_pushinteger(L, light->type); + else if (!strcmp(key, "position")) + LuaPush_Vector3(L, light->position); + else if (!strcmp(key, "target")) + LuaPush_Vector3(L, light->target); + else if (!strcmp(key, "radius")) + lua_pushnumber(L, light->radius); + else if (!strcmp(key, "diffuse")) + LuaPush_Color(L, light->diffuse); + else if (!strcmp(key, "intensity")) + lua_pushnumber(L, light->intensity); + else if (!strcmp(key, "coneAngle")) + lua_pushnumber(L, light->coneAngle); + else + return 0; + return 1; } static int LuaNewIndexLight(lua_State* L) @@ -335,17 +335,17 @@ static int LuaNewIndexLight(lua_State* L) light->enabled = lua_toboolean(L, 3); else if (!strcmp(key, "type")) light->type = LuaGetArgument_int(L, 3); - else if (!strcmp(key, "position")) + else if (!strcmp(key, "position")) light->position = LuaGetArgument_Vector3(L, 3); - else if (!strcmp(key, "target")) + else if (!strcmp(key, "target")) light->target = LuaGetArgument_Vector3(L, 3); - else if (!strcmp(key, "radius")) + else if (!strcmp(key, "radius")) light->radius = LuaGetArgument_float(L, 3); - else if (!strcmp(key, "diffuse")) + else if (!strcmp(key, "diffuse")) light->diffuse = LuaGetArgument_Color(L, 3); - else if (!strcmp(key, "intensity")) + else if (!strcmp(key, "intensity")) light->intensity = LuaGetArgument_float(L, 3); - else if (!strcmp(key, "coneAngle")) + else if (!strcmp(key, "coneAngle")) light->coneAngle = LuaGetArgument_float(L, 3); return 0; } @@ -367,17 +367,17 @@ static void LuaBuildOpaqueMetatables(void) lua_setfield(L, -2, "__index"); lua_pop(L, 1); - luaL_newmetatable(L, "SpriteFont"); - lua_pushcfunction(L, &LuaIndexSpriteFont); - lua_setfield(L, -2, "__index"); - lua_pop(L, 1); + luaL_newmetatable(L, "SpriteFont"); + lua_pushcfunction(L, &LuaIndexSpriteFont); + lua_setfield(L, -2, "__index"); + lua_pop(L, 1); - luaL_newmetatable(L, "Light"); - lua_pushcfunction(L, &LuaIndexLight); - lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, &LuaNewIndexLight); - lua_setfield(L, -2, "__newindex"); - lua_pop(L, 1); + luaL_newmetatable(L, "Light"); + lua_pushcfunction(L, &LuaIndexLight); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, &LuaNewIndexLight); + lua_setfield(L, -2, "__newindex"); + lua_pop(L, 1); } //---------------------------------------------------------------------------------- @@ -1121,14 +1121,14 @@ int lua_IsFileDropped(lua_State* L) int lua_GetDroppedFiles(lua_State* L) { - int count = 0; + int count = 0; char ** result = GetDroppedFiles(&count); - lua_createtable(L, count, 0); - for (int i = 0; i < count; i++) - { - lua_pushstring(L, result[i]); - lua_rawseti(L, -2, i + 1); - } + lua_createtable(L, count, 0); + for (int i = 0; i < count; i++) + { + lua_pushstring(L, result[i]); + lua_rawseti(L, -2, i + 1); + } return 1; } @@ -2401,9 +2401,9 @@ int lua_DrawGizmo(lua_State* L) int lua_DrawLight(lua_State* L) { - Light arg1 = LuaGetArgument_Light(L, 1); - DrawLight(arg1); - return 0; + Light arg1 = LuaGetArgument_Light(L, 1); + DrawLight(arg1); + return 0; } int lua_Draw3DLine(lua_State* L) @@ -3769,7 +3769,7 @@ static luaL_Reg raylib_functions[] = { REG(DrawGrid) REG(DrawGizmo) - REG(DrawLight) + REG(DrawLight) REG(LoadModel) REG(LoadModelEx) -- cgit v1.2.3