Changeset 2565


Ignore:
Timestamp:
Mar 7, 2013, 4:24:52 PM (7 years ago)
Author:
sam
Message:

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

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/application/application.cpp

    r2301 r2565  
    1414
    1515#include "core.h"
     16
    1617#include "lolgl.h"
    1718
     
    6970{
    7071    data = new ApplicationData(name, resolution, framerate);
     72
     73    g_world.ExecLua("lua/init.lua");
    7174}
    7275
  • trunk/src/world.cpp

    r2216 r2565  
    1919#include "core.h"
    2020
     21#include "lua/lua.hpp"
     22
    2123namespace lol
    2224{
     
    3032    friend class World;
    3133
    32 private:
    33     int width, height;
     34    lua_State *m_lua_state;
    3435};
     36
     37static WorldData g_world_data;
     38World g_world;
    3539
    3640/*
     
    3943
    4044World::World()
    41   : data(new WorldData())
    4245{
    43     data->width = 0;
    44     data->height = 0;
    45 
    46     m_drawgroup = DRAWGROUP_BEFORE;
     46    g_world_data.m_lua_state = luaL_newstate();
     47    luaL_openlibs(g_world_data.m_lua_state);
    4748}
    4849
    4950World::~World()
    5051{
    51     delete data;
     52    lua_close(g_world_data.m_lua_state);
    5253}
    5354
    54 char const *World::GetName()
     55bool World::ExecLua(String const &lua)
    5556{
    56     return "<world>";
     57    Array<String> pathlist = System::GetPathList(lua);
     58    for (int i = 0; i < pathlist.Count(); ++i)
     59    {
     60        luaL_dofile(g_world_data.m_lua_state, pathlist[i].C());
     61    }
     62
     63    return true;
    5764}
    5865
    59 void World::TickGame(float seconds)
     66double World::GetLuaNumber(String const &var)
    6067{
    61     Entity::TickGame(seconds);
    62 }
    63 
    64 void World::TickDraw(float seconds)
    65 {
    66     Entity::TickDraw(seconds);
    67 }
    68 
    69 int World::GetWidth()
    70 {
    71     return data->width * 32;
    72 }
    73 
    74 int World::GetHeight()
    75 {
    76     return data->height * 32;
     68    lua_getglobal(g_world_data.m_lua_state, var.C());
     69    return lua_tonumber(g_world_data.m_lua_state, -1);
    7770}
    7871
  • trunk/src/world.h

    r2216 r2565  
    1717#define __LOL_WORLD_H__
    1818
    19 #include "entity.h"
    20 
    2119namespace lol
    2220{
    2321
    24 class WorldData;
    25 
    26 class World : public Entity
     22class World
    2723{
    2824public:
     25    bool ExecLua(String const &lua);
     26    double GetLuaNumber(String const &var);
     27
     28//private:
    2929    World();
    30     virtual ~World();
     30    ~World();
     31};
    3132
    32 protected:
    33     /* Inherited from Entity */
    34     virtual char const *GetName();
    35     virtual void TickGame(float seconds);
    36     virtual void TickDraw(float seconds);
    37 
    38 public:
    39     /* New methods */
    40     int GetWidth();
    41     int GetHeight();
    42 
    43 private:
    44     WorldData *data;
    45 };
     33extern World g_world;
    4634
    4735} /* namespace lol */
Note: See TracChangeset for help on using the changeset viewer.