lua_State* is now available for sdfml_lua

This commit is contained in:
/nick haya 2022-02-20 16:10:20 +08:00
parent 9adffcf4cc
commit 862efe61ee
5 changed files with 35 additions and 2 deletions

View File

@ -1,7 +1,7 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
@ -13,4 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
- BASS and BASS_fx support.
### Deprecated
- `.MID` support due to crashes. (God dammit SoLoud)

View File

@ -29,6 +29,7 @@
#include "SDL2/SDL_stbimage.h"
#include "sdfml_lua.hpp"
#include "toml.hpp"
#include <fstream>
@ -404,6 +405,8 @@ namespace sdfml {
static GPU_Camera camera;
static LuaHandler lua;
inline int init(float width = DEFAULT_WINDOW_WIDTH, float height = DEFAULT_WINDOW_HEIGHT, string window_name = "Unknown",
int win_flags = SDL_WINDOW_OPENGL|SDL_WINDOW_ALLOW_HIGHDPI|SDL_WINDOW_SHOWN) {
std::ofstream logFile;

View File

@ -27239,7 +27239,7 @@ namespace sol {
using iterator = typename global_table::iterator;
using const_iterator = typename global_table::const_iterator;
state_view(lua_State* Ls) : L(Ls), reg(Ls, LUA_REGISTRYINDEX), global(Ls, global_tag) {
state_view(lua_State* Ls = NULL) : L(Ls), reg(Ls, LUA_REGISTRYINDEX), global(Ls, global_tag) {
}
state_view(this_state Ls) : state_view(Ls.L) {

View File

@ -1,6 +1,19 @@
#include "sdfml_lua.hpp"
int sdfml::LuaHandler::create_from_lua_state(lua_State *L, std::string path, bool immediate) {
_prefer_view = true;
sol::state_view temp(L);
_lua_view = temp;
_cur_result = _lua_view.load_file(path.c_str());
if (immediate)
return 0;
sol::protected_function_result result = _cur_result();
return 1 - result.valid();
}
int sdfml::LuaHandler::open_state(std::string path, bool immediate) {
_prefer_view = false;
_cur_result = _lua_state.load_file(path.c_str());
if (immediate)
return 0;
@ -16,15 +29,18 @@ int sdfml::LuaHandler::run_state() {
template <typename TName>
void sdfml::LuaHandler::set_variable(std::string var_name, TName value) {
if (_prefer_view) _lua_view[var_name] = value; return;
_lua_state[var_name] = value;
}
template <typename TName>
TName sdfml::LuaHandler::get_variable(std::string var_name) {
if (_prefer_view) return _lua_state.get<TName>(var_name);
return _lua_state.get<TName>(var_name);
}
template <typename... Args>
void sdfml::LuaHandler::add_libraries(Args&&... libraries) {
if (_prefer_view) _lua_view.open_libraries(libraries...); return;
_lua_state.open_libraries(libraries...);
}

View File

@ -1,6 +1,7 @@
#ifndef LUA_SDFML_IMPLEMENTATION
#define LUA_SDFML_IMPLEMENTATION
#include <cstddef>
#include <string>
#include <functional>
#include "../lua/sol.hpp"
@ -13,9 +14,20 @@ namespace sdfml {
sol::state _lua_state;
sol::state_view _lua_view;
sol::load_result _cur_result;
bool _prefer_view = false;
public:
LuaHandler() {
sol::state_view temp(NULL);
_lua_view = temp;
}
int open_state(std::string path, bool immediate = false);
int create_from_lua_state(lua_State* L, std::string path, bool immediate = false);
int open_from_lua_state(lua_State* L, std::string path, bool immediate = false) {
return create_from_lua_state(L, path, immediate);
}
int run_state();
template <typename TName = int>