From 0e626380474cf9f94abaeff769cc3d8bbbe2746b Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 6 Jun 2010 18:14:48 +0100 Subject: [PATCH] Start adding Lua VM inside game --- CMakeLists.txt | 3 +- include/ILua.hpp | 34 +++++++++++++++++++ src/LuaWrap.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Main.cpp | 4 +++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 include/ILua.hpp create mode 100644 src/LuaWrap.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 91b4d70..6f2f9be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ find_package (SDL_image REQUIRED) find_package (OpenGL REQUIRED) find_package (GLEW REQUIRED) find_package (Boost 1.37 REQUIRED COMPONENTS filesystem signals program_options) +find_package (Lua51 REQUIRED) if (NOT WIN32) include (FindPkgConfig) @@ -53,7 +54,7 @@ add_executable (${PROJECT_NAME} WIN32 ${folder_source}) target_link_libraries (${PROJECT_NAME} ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} ${XERCES_LIBRARIES} ${Boost_LIBRARIES} - ${FREETYPE_LIBRARIES} ${GLEW_LIBRARY}) + ${FREETYPE_LIBRARIES} ${GLEW_LIBRARY} ${LUA51_LIBRARIES}) # Profiling if (PROFILE) diff --git a/include/ILua.hpp b/include/ILua.hpp new file mode 100644 index 0000000..df1710a --- /dev/null +++ b/include/ILua.hpp @@ -0,0 +1,34 @@ +// +// Copyright (C) 2010 Nick Gasson +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#ifndef INC_ILUA_HPP +#define INC_ILUA_HPP + +#include "Platform.hpp" + +// Wrapper class for Lua +struct ILua { + virtual ~ILua() {} + + virtual void eval(const string& code) = 0; +}; + +typedef shared_ptr ILuaPtr; + +ILuaPtr getLua(); + +#endif diff --git a/src/LuaWrap.cpp b/src/LuaWrap.cpp new file mode 100644 index 0000000..73b6a28 --- /dev/null +++ b/src/LuaWrap.cpp @@ -0,0 +1,85 @@ +// +// Copyright (C) 2010 Nick Gasson +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#include "ILua.hpp" +#include "ILogger.hpp" + +#include + +extern "C" { +#include +#include +} + +class LuaWrap : public ILua { +public: + LuaWrap(); + ~LuaWrap(); + + void eval(const string& code); +private: + lua_State* luaVM; +}; + +LuaWrap::LuaWrap() +{ + log() << "Creating Lua VM"; + + if ((luaVM = lua_open()) == NULL) + throw runtime_error("Failed to initialise Lua"); +} + +LuaWrap::~LuaWrap() +{ + lua_close(luaVM); +} + +void LuaWrap::eval(const string& code) +{ + int result = luaL_loadstring(luaVM, code.c_str()); + + switch (result) { + case LUA_ERRSYNTAX: + error() << "Lua syntax error"; + return; + case LUA_ERRMEM: + error() << "Out of Lua memory"; + return; + } + + result = lua_pcall(luaVM, 0, 0, 0); + switch (result) { + case LUA_ERRRUN: + case LUA_ERRERR: + error() << "Lua: " << lua_tolstring(luaVM, -1, NULL); + lua_pop(luaVM, 1); + break; + case LUA_ERRMEM: + error() << "Lua out of memory!"; + break; + } +} + +ILuaPtr getLua() +{ + static ILuaPtr lua; + + if (!lua) + lua = ILuaPtr(new LuaWrap); + + return lua; +} diff --git a/src/Main.cpp b/src/Main.cpp index 3092b46..9d43fa2 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -20,6 +20,7 @@ #include "GameScreens.hpp" #include "IResource.hpp" #include "IConfig.hpp" +#include "ILua.hpp" #include #include @@ -103,6 +104,9 @@ int main(int argc, char** argv) IConfigPtr cfg = getConfig(); ::window = makeSDLWindow(); + + ILuaPtr lua = getLua(); + lua->eval("debug(\"hello\")"); IScreenPtr screen; if (::action == "edit") { -- 2.39.2