Fleshed out the pager's API

This commit is contained in:
Kayne Ruse
2014-06-10 00:20:45 +10:00
parent 2d27399fd1
commit b7c12ba106
4 changed files with 135 additions and 29 deletions
+17 -8
View File
@@ -70,10 +70,14 @@ Region* RegionPager::LoadRegion(int x, int y) {
x = snapToBase(REGION_WIDTH, x); x = snapToBase(REGION_WIDTH, x);
y = snapToBase(REGION_HEIGHT, y); y = snapToBase(REGION_HEIGHT, y);
Region* ptr = new Region(x, y); //overwrite?
Region* ptr = FindRegion(x, y);
if (!ptr) {
ptr = new Region(x, y);
}
//API hook //API hook
lua_getglobal(luaState, "map"); lua_getglobal(luaState, "region");
lua_getfield(luaState, -1, "load"); lua_getfield(luaState, -1, "load");
lua_pushlightuserdata(luaState, ptr); lua_pushlightuserdata(luaState, ptr);
lua_pushstring(luaState, directory.c_str()); lua_pushstring(luaState, directory.c_str());
@@ -101,7 +105,7 @@ Region* RegionPager::SaveRegion(int x, int y) {
Region* ptr = FindRegion(x, y); Region* ptr = FindRegion(x, y);
if (ptr) { if (ptr) {
//API hook //API hook
lua_getglobal(luaState, "map"); lua_getglobal(luaState, "region");
lua_getfield(luaState, -1, "save"); lua_getfield(luaState, -1, "save");
lua_pushlightuserdata(luaState, ptr); lua_pushlightuserdata(luaState, ptr);
lua_pushstring(luaState, directory.c_str()); lua_pushstring(luaState, directory.c_str());
@@ -118,13 +122,17 @@ Region* RegionPager::CreateRegion(int x, int y) {
x = snapToBase(REGION_WIDTH, x); x = snapToBase(REGION_WIDTH, x);
y = snapToBase(REGION_HEIGHT, y); y = snapToBase(REGION_HEIGHT, y);
//create and push the object //overwrite?
Region* ptr = new Region(x, y); Region* ptr = FindRegion(x, y);
if (!ptr) {
ptr = new Region(x, y);
}
//API hook //API hook
lua_getglobal(luaState, "map"); lua_getglobal(luaState, "region");
lua_getfield(luaState, -1, "create"); lua_getfield(luaState, -1, "create");
lua_pushlightuserdata(luaState, ptr); lua_pushlightuserdata(luaState, ptr);
//TODO: parameters
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
@@ -139,7 +147,7 @@ void RegionPager::UnloadRegion(int x, int y) {
x = snapToBase(REGION_WIDTH, x); x = snapToBase(REGION_WIDTH, x);
y = snapToBase(REGION_HEIGHT, y); y = snapToBase(REGION_HEIGHT, y);
lua_getglobal(luaState, "map"); lua_getglobal(luaState, "region");
//custom loop, not FindRegion() //custom loop, not FindRegion()
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
@@ -148,7 +156,8 @@ void RegionPager::UnloadRegion(int x, int y) {
//API hook //API hook
lua_getfield(luaState, -1, "unload"); lua_getfield(luaState, -1, "unload");
lua_pushlightuserdata(luaState, *it); lua_pushlightuserdata(luaState, *it);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { lua_pushstring(luaState, directory.c_str());
if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
-1
View File
@@ -46,7 +46,6 @@ public:
Region* SaveRegion(int x, int y); Region* SaveRegion(int x, int y);
Region* CreateRegion(int x, int y); Region* CreateRegion(int x, int y);
void UnloadRegion(int x, int y); void UnloadRegion(int x, int y);
void DeleteRegion(int x, int y);
//accessors & mutators //accessors & mutators
std::list<Region*>* GetContainer() { return &regionList; } std::list<Region*>* GetContainer() { return &regionList; }
+71 -2
View File
@@ -21,12 +21,81 @@
*/ */
#include "pager_api.hpp" #include "pager_api.hpp"
static int func(lua_State* L) { #include "region_pager.hpp"
#include "region.hpp"
#include <string>
static int setTile(lua_State* L) {
RegionPager* pager = reinterpret_cast<RegionPager*>(lua_touserdata(L, 1));
int ret = pager->SetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
lua_pop(L, 5);
lua_pushinteger(L, ret);
return 1;
}
static int getTile(lua_State* L) {
RegionPager* pager = reinterpret_cast<RegionPager*>(lua_touserdata(L, 1));
int ret = pager->GetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
lua_pop(L, 4);
lua_pushinteger(L, ret);
return 1;
}
static int getRegion(lua_State* L) {
RegionPager* pager = reinterpret_cast<RegionPager*>(lua_touserdata(L, 1));
Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
lua_pop(L, 3);
lua_pushlightuserdata(L, region);
return 1;
}
static int setDirectory(lua_State* L) {
RegionPager* pager = reinterpret_cast<RegionPager*>(lua_touserdata(L, 1));
std::string s = pager->SetDirectory(lua_tostring(L, 2));
lua_pop(L, 2);
lua_pushstring(L, s.c_str());
return 1;
}
static int getDirectory(lua_State* L) {
RegionPager* pager = reinterpret_cast<RegionPager*>(lua_touserdata(L, 1));
std::string s = pager->GetDirectory();
lua_pop(L, 1);
lua_pushstring(L, s.c_str());
return 1;
}
static int loadRegion(lua_State* L) {
//TODO: fill this
return 0;
}
static int saveRegion(lua_State* L) {
//TODO: fill this
return 0;
}
static int createRegion(lua_State* L) {
//TODO: fill this
return 0;
}
static int unloadRegion(lua_State* L) {
//TODO: fill this
return 0; return 0;
} }
static const luaL_Reg pagerlib[] = { static const luaL_Reg pagerlib[] = {
{"name", func}, {"settile", setTile},
{"gettile", getTile},
{"getregion", getRegion},
{"setdirectory", setDirectory},
{"getdirectory", getDirectory},
{"loadregion", loadRegion},
{"saveregion", saveRegion},
{"createregion", createRegion},
{"unloadregion", unloadRegion},
{nullptr, nullptr} {nullptr, nullptr}
}; };
+47 -18
View File
@@ -24,55 +24,84 @@
#include "region.hpp" #include "region.hpp"
static int setTile(lua_State* L) { static int setTile(lua_State* L) {
//operating on a region Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
Region* ptr = (Region*)lua_touserdata(L, 1); int ret = region->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5)); lua_pop(L, 5);
return 0; lua_pushinteger(L, ret);
return 1;
} }
static int getTile(lua_State* L) { static int getTile(lua_State* L) {
//operating on a region Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
Region* ptr = (Region*)lua_touserdata(L, 1); int ret = region->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1);
int ret = ptr->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1); lua_pop(L, 4);
lua_pushnumber(L, ret); lua_pushinteger(L, ret);
return 1; return 1;
} }
static int getX(lua_State* L) { static int getX(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1); Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
lua_pushinteger(L, ptr->GetX()); int ret = region->GetX();
lua_pop(L, 1);
lua_pushinteger(L, ret);
return 1; return 1;
} }
static int getY(lua_State* L) { static int getY(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1); Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
lua_pushinteger(L, ptr->GetY()); int ret = region->GetY();
lua_pop(L, 1);
lua_pushinteger(L, ret);
return 1; return 1;
} }
static int getRegionWidth(lua_State* L) { static int getWidth(lua_State* L) {
lua_pushinteger(L, REGION_WIDTH); lua_pushinteger(L, REGION_WIDTH);
return 1; return 1;
} }
static int getRegionHeight(lua_State* L) { static int getHeight(lua_State* L) {
lua_pushinteger(L, REGION_HEIGHT); lua_pushinteger(L, REGION_HEIGHT);
return 1; return 1;
} }
static int getRegionDepth(lua_State* L) { static int getDepth(lua_State* L) {
lua_pushinteger(L, REGION_DEPTH); lua_pushinteger(L, REGION_DEPTH);
return 1; return 1;
} }
static int load(lua_State* L) {
//TODO: fill this
return 0;
}
static int save(lua_State* L) {
//TODO: fill this
return 0;
}
static int create(lua_State* L) {
//TODO: fill this
return 0;
}
static int unload(lua_State* L) {
//TODO: fill this
return 0;
}
static const luaL_Reg regionlib[] = { static const luaL_Reg regionlib[] = {
{"settile",setTile}, {"settile",setTile},
{"gettile",getTile}, {"gettile",getTile},
{"getx",getX}, {"getx",getX},
{"gety",getY}, {"gety",getY},
{"getregionwidth",getRegionWidth}, {"getwidth",getWidth},
{"getregionheight",getRegionHeight}, {"getheight",getHeight},
{"getregiondepth",getRegionDepth}, {"getdepth",getDepth},
{"load",load},
{"save",save},
{"create",create},
{"unload",unload},
{nullptr, nullptr} {nullptr, nullptr}
}; };