From 91d2628d191978e933330e1b9b0d27e9df66e397 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Sat, 9 May 2020 02:10:02 -0400 Subject: [PATCH] Add work on raylib Lua module --- .gitignore | 3 ++- CMakeLists.txt | 17 +++++++++---- include/raylib-lua-sol.hpp | 12 +++++----- lib/CMakeLists.txt | 30 +++++++++++++++++++++++ lib/raylib-lua-sol-module-test.lua | 5 ++++ lib/raylib-lua-sol-module.cpp | 38 ++++++++++++++++++++++++++++++ raylib-lua-sol-0.0.1-1.rockspec | 30 +++++++++++++++++++++++ 7 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 lib/CMakeLists.txt create mode 100644 lib/raylib-lua-sol-module-test.lua create mode 100644 lib/raylib-lua-sol-module.cpp create mode 100644 raylib-lua-sol-0.0.1-1.rockspec diff --git a/.gitignore b/.gitignore index c795b05..8be0bc1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build \ No newline at end of file +build +build.luarocks diff --git a/CMakeLists.txt b/CMakeLists.txt index 3da4f40..1b06103 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,20 +22,22 @@ endif() # Options option(BUILD_BIN "Binary" ON) option(BUILD_EXAMPLES "Examples" ON) +option(BUILD_LIB "Library" ON) add_subdirectory(include) -if (BUILD_BIN) +if (BUILD_BIN OR BUILD_LIB) # raylib find_package(raylib 3.0.0) if (NOT raylib_FOUND) set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples set(BUILD_GAMES OFF CACHE BOOL "" FORCE) # or games - add_subdirectory(vendor/raylib) + set(WITH_PIC ON) + add_subdirectory(${CMAKE_SOURCE_DIR}/vendor/raylib) endif() # Lua - set(LUA_DIR vendor/lua) + set(LUA_DIR ${CMAKE_SOURCE_DIR}/vendor/lua) set(LUA_SRC ${LUA_DIR}/lapi.c ${LUA_DIR}/lcode.c @@ -75,11 +77,16 @@ if (BUILD_BIN) target_include_directories(lua INTERFACE ${LUA_DIR}) # Sol2 - set(SOL2_DIR vendor/sol2) + set(SOL2_DIR ${CMAKE_SOURCE_DIR}/vendor/sol2) add_library(sol2 INTERFACE) target_include_directories(sol2 INTERFACE ${SOL2_DIR}/include/) +endif() - # Add the binary. +if (BUILD_LIB) + add_subdirectory(lib) +endif() + +if (BUILD_BIN) add_subdirectory(bin) endif() diff --git a/include/raylib-lua-sol.hpp b/include/raylib-lua-sol.hpp index d56023b..b143b3c 100644 --- a/include/raylib-lua-sol.hpp +++ b/include/raylib-lua-sol.hpp @@ -4,7 +4,7 @@ #include "raylib.h" #include "sol/sol.hpp" -void raylib_lua_sol_color(sol::state& lua) { +void raylib_lua_sol_color(sol::state_view& lua) { lua["LIGHTGRAY"] = Color(LIGHTGRAY); lua["GRAY"] = Color(GRAY); lua["DARKGRAY"] = Color(DARKGRAY); @@ -33,7 +33,7 @@ void raylib_lua_sol_color(sol::state& lua) { lua["RAYWHITE"] = Color(RAYWHITE); } -void raylib_lua_sol_structs(sol::state& lua) { +void raylib_lua_sol_structs(sol::state_view& lua) { lua.new_usertype("Rectangle", sol::call_constructor, sol::factories( []() { @@ -339,7 +339,7 @@ void raylib_lua_sol_structs(sol::state& lua) { #define RAYLIB_LUA_SOL_ADD_ENUM(x) \ lua[#x] = x -void raylib_lua_sol_enums(sol::state &lua) { +void raylib_lua_sol_enums(sol::state_view &lua) { RAYLIB_LUA_SOL_ADD_ENUM(FLAG_RESERVED); RAYLIB_LUA_SOL_ADD_ENUM(FLAG_FULLSCREEN_MODE); RAYLIB_LUA_SOL_ADD_ENUM(FLAG_WINDOW_RESIZABLE); @@ -612,7 +612,7 @@ void raylib_lua_sol_enums(sol::state &lua) { #define RAYLIB_LUA_SOL_ADD_FUNCTION(x) \ lua.set_function(#x, x) -void raylib_lua_sol_functions(sol::state &lua) { +void raylib_lua_sol_functions(sol::state_view &lua) { RAYLIB_LUA_SOL_ADD_FUNCTION(InitWindow); RAYLIB_LUA_SOL_ADD_FUNCTION(WindowShouldClose); RAYLIB_LUA_SOL_ADD_FUNCTION(CloseWindow); @@ -1061,11 +1061,11 @@ void TraceLogWrapper(int messageType, const std::string& message) { TraceLog(messageType, message.c_str()); } -void raylib_lua_sol_function_wrappers(sol::state &lua) { +void raylib_lua_sol_function_wrappers(sol::state_view &lua) { lua.set_function("TraceLog", &TraceLogWrapper); } -void raylib_lua_sol(sol::state& lua) { +void raylib_lua_sol(sol::state_view& lua) { raylib_lua_sol_color(lua); raylib_lua_sol_enums(lua); raylib_lua_sol_functions(lua); diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000..d43e65a --- /dev/null +++ b/lib/CMakeLists.txt @@ -0,0 +1,30 @@ +add_library(raylib-lua-sol-module SHARED + raylib-lua-sol-module.cpp + ${LUA_SRC} +) + +target_compile_definitions(raylib-lua-sol-module PUBLIC -DLUA -DLUA_COMPAT_5_2 -DPHYSAC_NO_THREADS) + +set_target_properties(raylib-lua-sol-module PROPERTIES + CXX_STANDARD 17 + CXX_EXTENSIONS OFF + PREFIX "" + OUTPUT_NAME "raylib-lua-sol" + POSITION_INDEPENDENT_CODE ON +) + +target_link_libraries(raylib-lua-sol-module PUBLIC + raylib-lua-sol-hpp + raylib + sol2 +) + +target_include_directories(raylib-lua-sol-module PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../include + ${CMAKE_CURRENT_SOURCE_DIR}/../vendor/lua + ${CMAKE_CURRENT_SOURCE_DIR}/../vendor/raylib/src + ${CMAKE_CURRENT_SOURCE_DIR}/../vendor/sol2/include + ${LUA_DIR} +) + +file(COPY raylib-lua-sol-module-test.lua DESTINATION .) diff --git a/lib/raylib-lua-sol-module-test.lua b/lib/raylib-lua-sol-module-test.lua new file mode 100644 index 0000000..dfbe0e3 --- /dev/null +++ b/lib/raylib-lua-sol-module-test.lua @@ -0,0 +1,5 @@ +package.cpath = package.cpath .. ";./?.so" +r = require "raylib-lua-sol" + +obj = r.test.new(24) +print(obj.value) diff --git a/lib/raylib-lua-sol-module.cpp b/lib/raylib-lua-sol-module.cpp new file mode 100644 index 0000000..21a629b --- /dev/null +++ b/lib/raylib-lua-sol-module.cpp @@ -0,0 +1,38 @@ +#define SOL_ALL_SAFETIES_ON 1 +#include +// #include "../include/raylib-lua-sol.hpp" + +class test { +public: + int value; + test(){ + + }; + test(int val) { + value = val; + }; +}; + +namespace my_object { + sol::table open_my_object(sol::this_state L) { + sol::state_view lua(L); + + //raylib_lua_sol(lua); + sol::table module = lua.create_table(); + module.new_usertype("test", + sol::constructors(), + "value", &test::value); + + return module; + } + +} + +extern "C" int luaopen_raylib(lua_State* L) { + // pass the lua_State, + // the index to start grabbing arguments from, + // and the function itself + // optionally, you can pass extra arguments to the function if that's necessary, + // but that's advanced usage and is generally reserved for internals only + return sol::stack::call_lua(L, 1, my_object::open_my_object ); +} diff --git a/raylib-lua-sol-0.0.1-1.rockspec b/raylib-lua-sol-0.0.1-1.rockspec new file mode 100644 index 0000000..a70c777 --- /dev/null +++ b/raylib-lua-sol-0.0.1-1.rockspec @@ -0,0 +1,30 @@ +package = "raylib-lua-sol" +version = "0.0.1-1" +source = { + url = "git://github.com/RobLoach/raylib-lua-sol.git" +} +description = { + summary = "raylib for Lua, using sol.", + detailed = [[ + raylib-lua-sol bindings are self-contained in a header-only file: raylib-lua-sol.h. Just include that file in your project to allow loading and execution of raylib code written in Lua and Sol. + ]], + license = "zlib", + homepage = "https://github.com/robloach/raylib-lua-sol" +} +dependencies = { + "lua >= 5.2" +} +build = { + type = "cmake", + + variables = { + BUILD_BIN = "OFF", + BUILD_SHARED_LIBS = "ON", + LUA_INCLUDE_DIR = "$(LUA_INCDIR)", + LUA_PATH="$(LUADIR)", + LUA_CPATH="$(LIBDIR)", + CMAKE_BUILD_TYPE="Release", + CMAKE_PREFIX_PATH="$(LUA_BINDIR)/..", + CMAKE_INSTALL_PREFIX="$(PREFIX)" + } +}