* 1
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
# # # # sol2
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2013-2022 Rapptz, ThePhD, and contributors
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
# this software and associated documentation files (the "Software"), to deal in
|
||||
# the Software without restriction, including without limitation the rights to
|
||||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
# the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
# # # sol2 Customization Examples
|
||||
|
||||
function (MAKE_CUSTOMIZATION_EXAMPLE example_suffix target_sol)
|
||||
set(customization_example_name customization_to_table${example_suffix})
|
||||
|
||||
add_executable(${customization_example_name} source/main.cpp source/lua_interop.cpp source/lua_zm_interop.cpp)
|
||||
set_target_properties(${customization_example_name}
|
||||
PROPERTIES
|
||||
OUTPUT_NAME "${customization_example_name}"
|
||||
EXPORT_NAME sol2::${customization_example_name})
|
||||
|
||||
if (MSVC)
|
||||
target_compile_options(${customization_example_name}
|
||||
PRIVATE /std:c++latest /EHsc "$<$<CONFIG:Debug>:/MDd>"
|
||||
"$<$<CONFIG:Release>:/MD>"
|
||||
"$<$<CONFIG:RelWithDebInfo>:/MD>"
|
||||
"$<$<CONFIG:MinSizeRel>:/MD>")
|
||||
target_compile_definitions(${customization_example_name}
|
||||
PRIVATE UNICODE _UNICODE
|
||||
_CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE)
|
||||
else()
|
||||
target_compile_options(${customization_example_name}
|
||||
PRIVATE -std=c++1z
|
||||
-ftemplate-backtrace-limit=0
|
||||
-Wno-unknown-warning -Wno-unknown-warning-option
|
||||
-Wall -Wpedantic -Werror -pedantic -pedantic-errors
|
||||
-Wno-noexcept-type)
|
||||
endif()
|
||||
|
||||
target_link_libraries(${customization_example_name}
|
||||
PRIVATE Threads::Threads ${target_sol} ${LUA_LIBRARIES})
|
||||
target_include_directories(${customization_example_name}
|
||||
PRIVATE include)
|
||||
endfunction()
|
||||
|
||||
if (SOL2_EXAMPLES)
|
||||
MAKE_CUSTOMIZATION_EXAMPLE("" sol2::sol2)
|
||||
endif()
|
||||
|
||||
if (SOL2_EXAMPLES_SINGLE)
|
||||
MAKE_CUSTOMIZATION_EXAMPLE(".single" sol2::sol2::single)
|
||||
endif()
|
||||
@@ -1,25 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef PROGRAM_ENTITY_HPP
|
||||
#define PROGRAM_ENTITY_HPP
|
||||
|
||||
#include <zm/vec3.hpp>
|
||||
|
||||
class entity {
|
||||
private:
|
||||
zm::vec3 position_;
|
||||
|
||||
public:
|
||||
entity() {
|
||||
this->position_ = { 1, 1, 1 };
|
||||
}
|
||||
|
||||
zm::vec3 get_position() const {
|
||||
return this->position_;
|
||||
}
|
||||
void set_position(zm::vec3 v) {
|
||||
this->position_ = v;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // PROGRAM_ENTITY_HPP
|
||||
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef PROGRAM_LUA_INTEROP_HPP
|
||||
#define PROGRAM_LUA_INTEROP_HPP
|
||||
|
||||
#include <lua_zm_interop.hpp>
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <sol/forward.hpp>
|
||||
|
||||
void register_lua(sol::state& lua);
|
||||
|
||||
#endif // PROGRAM_LUA_INTEROP_HPP
|
||||
@@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef PROGRAM_LUA_ZM_INTEROP_HPP
|
||||
#define PROGRAM_LUA_ZM_INTEROP_HPP
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <sol/forward.hpp>
|
||||
|
||||
#include <zm/vec3.hpp>
|
||||
|
||||
#include <functional>
|
||||
|
||||
bool sol_lua_check(sol::types<zm::vec3>, lua_State* L, int index,
|
||||
std::function<sol::check_handler_type> handler,
|
||||
sol::stack::record& tracking);
|
||||
|
||||
zm::vec3 sol_lua_get(sol::types<zm::vec3>, lua_State* L,
|
||||
int index, sol::stack::record& tracking);
|
||||
|
||||
int sol_lua_push(
|
||||
sol::types<zm::vec3>, lua_State* L, const zm::vec3& v);
|
||||
|
||||
#endif // PROGRAM_LUA_ZM_INTEROP_HPP
|
||||
@@ -1,74 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef ZM_VEC3_HPP
|
||||
#define ZM_VEC3_HPP
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace zm {
|
||||
|
||||
struct vec3 {
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
|
||||
#pragma clang diagnostic ignored "-Wnested-anon-types"
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
#elif defined(_MSC_VER)
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4201) // nonstandard extension used :
|
||||
// nameless struct/union
|
||||
#pragma warning(disable : 4324) // structure was padded due to
|
||||
// alignment specifier
|
||||
#endif
|
||||
union {
|
||||
float elements[3];
|
||||
struct {
|
||||
union {
|
||||
float x, r, s;
|
||||
};
|
||||
union {
|
||||
float y, g, t;
|
||||
};
|
||||
union {
|
||||
float z, b, p;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC diagnostic pop
|
||||
#elif defined(_MSC_VER)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
vec3() : x(0), y(0), z(0) {
|
||||
}
|
||||
|
||||
vec3(float x_, float y_, float z_)
|
||||
: x(x_), y(y_), z(z_) {
|
||||
}
|
||||
|
||||
constexpr float* data() {
|
||||
return elements;
|
||||
}
|
||||
|
||||
constexpr const float* data() const {
|
||||
return this->elements;
|
||||
}
|
||||
|
||||
constexpr float& operator[](std::size_t i) {
|
||||
return this->elements[i];
|
||||
}
|
||||
|
||||
constexpr const float& operator[](
|
||||
std::size_t i) const {
|
||||
return this->elements[i];
|
||||
}
|
||||
};
|
||||
} // namespace zm
|
||||
|
||||
#endif // ZM_VEC3_HPP
|
||||
@@ -1,13 +0,0 @@
|
||||
#include <lua_interop.hpp>
|
||||
|
||||
#include <entity.hpp>
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
void register_lua(sol::state& lua) {
|
||||
lua.new_usertype<entity>("entity",
|
||||
"position",
|
||||
sol::property(
|
||||
&entity::get_position, &entity::set_position));
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#include <lua_zm_interop.hpp>
|
||||
|
||||
#include <zm/vec3.hpp>
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
bool sol_lua_check(sol::types<zm::vec3>, lua_State* L, int index,
|
||||
std::function<sol::check_handler_type> handler,
|
||||
sol::stack::record& tracking) {
|
||||
// use sol's method for checking
|
||||
// specifically for a table
|
||||
return sol::stack::check<sol::lua_table>(
|
||||
L, index, handler, tracking);
|
||||
}
|
||||
|
||||
zm::vec3 sol_lua_get(sol::types<zm::vec3>, lua_State* L,
|
||||
int index, sol::stack::record& tracking) {
|
||||
sol::lua_table vec3table
|
||||
= sol::stack::get<sol::lua_table>(L, index, tracking);
|
||||
float x = vec3table["x"];
|
||||
float y = vec3table["y"];
|
||||
float z = vec3table["z"];
|
||||
return zm::vec3 { x, y, z };
|
||||
}
|
||||
|
||||
int sol_lua_push(
|
||||
sol::types<zm::vec3>, lua_State* L, const zm::vec3& v) {
|
||||
// create table
|
||||
sol::state_view lua(L);
|
||||
sol::table vec3table = sol::table::create_with(
|
||||
L, "x", v.x, "y", v.y, "z", v.z);
|
||||
// use base sol method to
|
||||
// push the table
|
||||
int amount = sol::stack::push(L, vec3table);
|
||||
// return # of things pushed onto stack
|
||||
return amount;
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
#include <lua_interop.hpp>
|
||||
#include <entity.hpp>
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(int, char*[]) {
|
||||
std::cout << "=== customization: vec3 as table ==="
|
||||
<< std::endl;
|
||||
|
||||
sol::state lua;
|
||||
lua.open_libraries(sol::lib::base);
|
||||
|
||||
std::cout << "registering entities into Lua ..."
|
||||
<< std::endl;
|
||||
register_lua(lua);
|
||||
std::cout << "running script ..." << std::endl;
|
||||
|
||||
const auto& script = R"lua(
|
||||
local e = entity.new()
|
||||
local pos = e.position
|
||||
print("pos type", type(pos))
|
||||
print("pos", pos.x, pos.y, pos.z)
|
||||
e.position = { x = 52, y = 5.5, z = 47.5 }
|
||||
local new_pos = e.position
|
||||
print("pos", pos.x, pos.y, pos.z)
|
||||
print("new_pos", new_pos.x, new_pos.y, new_pos.z)
|
||||
)lua";
|
||||
|
||||
sol::optional<sol::error> result = lua.safe_script(script);
|
||||
if (result.has_value()) {
|
||||
std::cerr << "Something went horribly wrong: "
|
||||
<< result.value().what() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "finishing ..." << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user