Last change
on this file since 2565 was
2565,
checked in by sam, 8 years ago
|
lua: add support for lua/init.lua in a global World object and give
it a try in MrPigeon.
|
-
Property svn:keywords set to
Id
|
File size:
1.3 KB
|
Line | |
---|
1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstring> |
---|
16 | #include <cstdlib> |
---|
17 | #include <ctype.h> |
---|
18 | |
---|
19 | #include "core.h" |
---|
20 | |
---|
21 | #include "lua/lua.hpp" |
---|
22 | |
---|
23 | namespace lol |
---|
24 | { |
---|
25 | |
---|
26 | /* |
---|
27 | * World implementation class |
---|
28 | */ |
---|
29 | |
---|
30 | class WorldData |
---|
31 | { |
---|
32 | friend class World; |
---|
33 | |
---|
34 | lua_State *m_lua_state; |
---|
35 | }; |
---|
36 | |
---|
37 | static WorldData g_world_data; |
---|
38 | World g_world; |
---|
39 | |
---|
40 | /* |
---|
41 | * Public World class |
---|
42 | */ |
---|
43 | |
---|
44 | World::World() |
---|
45 | { |
---|
46 | g_world_data.m_lua_state = luaL_newstate(); |
---|
47 | luaL_openlibs(g_world_data.m_lua_state); |
---|
48 | } |
---|
49 | |
---|
50 | World::~World() |
---|
51 | { |
---|
52 | lua_close(g_world_data.m_lua_state); |
---|
53 | } |
---|
54 | |
---|
55 | bool World::ExecLua(String const &lua) |
---|
56 | { |
---|
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; |
---|
64 | } |
---|
65 | |
---|
66 | double World::GetLuaNumber(String const &var) |
---|
67 | { |
---|
68 | lua_getglobal(g_world_data.m_lua_state, var.C()); |
---|
69 | return lua_tonumber(g_world_data.m_lua_state, -1); |
---|
70 | } |
---|
71 | |
---|
72 | } /* namespace lol */ |
---|
73 | |
---|
Note: See
TracBrowser
for help on using the repository browser.