Browse Source

lua: add support for lua/init.lua in a global World object and give

it a try in MrPigeon.
legacy
Sam Hocevar sam 12 years ago
parent
commit
6a519771f5
3 changed files with 30 additions and 46 deletions
  1. +3
    -0
      src/application/application.cpp
  2. +19
    -26
      src/world.cpp
  3. +8
    -20
      src/world.h

+ 3
- 0
src/application/application.cpp View File

@@ -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()


+ 19
- 26
src/world.cpp View File

@@ -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 "<world>";
}
Array<String> 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 */


+ 8
- 20
src/world.h View File

@@ -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__


Loading…
Cancel
Save