Skip to content

Add work on raylib Lua module #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
build
build.luarocks
17 changes: 12 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
12 changes: 6 additions & 6 deletions include/raylib-lua-sol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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>("Rectangle",
sol::call_constructor, sol::factories(
[]() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 .)
5 changes: 5 additions & 0 deletions lib/raylib-lua-sol-module-test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package.cpath = package.cpath .. ";./?.so"
r = require "raylib-lua-sol"

obj = r.test.new(24)
print(obj.value)
38 changes: 38 additions & 0 deletions lib/raylib-lua-sol-module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
// #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>("test",
sol::constructors<test(), test(int)>(),
"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 );
}
30 changes: 30 additions & 0 deletions raylib-lua-sol-0.0.1-1.rockspec
Original file line number Diff line number Diff line change
@@ -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)"
}
}