From 6a519771f5b22eed649874ccd6e67bfe62923869 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 7 Mar 2013 15:24:52 +0000 Subject: [PATCH] lua: add support for lua/init.lua in a global World object and give it a try in MrPigeon. --- src/application/application.cpp | 3 +++ src/world.cpp | 45 ++++++++++++++------------------- src/world.h | 28 ++++++-------------- 3 files changed, 30 insertions(+), 46 deletions(-) diff --git a/src/application/application.cpp b/src/application/application.cpp index c2b223b4..ccf23661 100644 --- a/src/application/application.cpp +++ b/src/application/application.cpp @@ -13,6 +13,7 @@ #endif #include "core.h" + #include "lolgl.h" #if defined __CELLOS_LV2__ @@ -68,6 +69,8 @@ class ApplicationData Application::Application(char const *name, ivec2 resolution, float framerate) { data = new ApplicationData(name, resolution, framerate); + + g_world.ExecLua("lua/init.lua"); } bool Application::MustTick() diff --git a/src/world.cpp b/src/world.cpp index da835f48..c4592976 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -18,6 +18,8 @@ #include "core.h" +#include "lua/lua.hpp" + namespace lol { @@ -29,51 +31,42 @@ class WorldData { friend class World; -private: - int width, height; + lua_State *m_lua_state; }; +static WorldData g_world_data; +World g_world; + /* * Public World class */ World::World() - : data(new WorldData()) { - data->width = 0; - data->height = 0; - - m_drawgroup = DRAWGROUP_BEFORE; + g_world_data.m_lua_state = luaL_newstate(); + luaL_openlibs(g_world_data.m_lua_state); } World::~World() { - delete data; + lua_close(g_world_data.m_lua_state); } -char const *World::GetName() +bool World::ExecLua(String const &lua) { - return ""; -} + Array pathlist = System::GetPathList(lua); + for (int i = 0; i < pathlist.Count(); ++i) + { + luaL_dofile(g_world_data.m_lua_state, pathlist[i].C()); + } -void World::TickGame(float seconds) -{ - Entity::TickGame(seconds); -} - -void World::TickDraw(float seconds) -{ - Entity::TickDraw(seconds); -} - -int World::GetWidth() -{ - return data->width * 32; + return true; } -int World::GetHeight() +double World::GetLuaNumber(String const &var) { - return data->height * 32; + lua_getglobal(g_world_data.m_lua_state, var.C()); + return lua_tonumber(g_world_data.m_lua_state, -1); } } /* namespace lol */ diff --git a/src/world.h b/src/world.h index 322568d9..616125ff 100644 --- a/src/world.h +++ b/src/world.h @@ -16,34 +16,22 @@ #if !defined __LOL_WORLD_H__ #define __LOL_WORLD_H__ -#include "entity.h" - namespace lol { -class WorldData; - -class World : public Entity +class World { public: - World(); - virtual ~World(); - -protected: - /* Inherited from Entity */ - virtual char const *GetName(); - virtual void TickGame(float seconds); - virtual void TickDraw(float seconds); - -public: - /* New methods */ - int GetWidth(); - int GetHeight(); + bool ExecLua(String const &lua); + double GetLuaNumber(String const &var); -private: - WorldData *data; +//private: + World(); + ~World(); }; +extern World g_world; + } /* namespace lol */ #endif // __LOL_WORLD_H__