diff --git a/rsc/startup.lua b/rsc/startup.lua index f03e295..3ec0d74 100644 --- a/rsc/startup.lua +++ b/rsc/startup.lua @@ -6,7 +6,9 @@ pager = ... ---[[ ---BUG: RegionPagerLua fails without these +--DOCS: These lambdas should return true or false, depending on if the operation succeeded or not +--DOCS: No return value given is recognized as a failure +--DOCS: OnCreate() and OnUnload() return values are currently ignored region_pager.SetOnLoad(pager, function(r) print("Calling SetOnLoad's lambda") end) diff --git a/src/application.cpp b/src/application.cpp index a4e1b32..4589371 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -119,9 +119,6 @@ void Application::Quit() { BaseScene::SetRenderer(nullptr); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); - - //TODO: is this necessary? -// SDL_Quit(); } //------------------------- diff --git a/src/libmap/region_pager_api.cpp b/src/libmap/region_pager_api.cpp index 6a14b54..fd366db 100644 --- a/src/libmap/region_pager_api.cpp +++ b/src/libmap/region_pager_api.cpp @@ -104,8 +104,6 @@ static int unloadRegion(lua_State* L) { return 0; } -//TODO: (1) check that parameters are not null - static int setOnLoad(lua_State* L) { RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); luaL_unref(L, LUA_REGISTRYINDEX, pager->GetLoadReference()); diff --git a/src/libmap/region_pager_base.cpp b/src/libmap/region_pager_base.cpp index 79017da..857dabd 100644 --- a/src/libmap/region_pager_base.cpp +++ b/src/libmap/region_pager_base.cpp @@ -28,8 +28,6 @@ RegionPagerBase::~RegionPagerBase() { UnloadAll(); }; -//TODO: (1) add nullptr checks to the calls to GetRegion() - Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) { Region* ptr = GetRegion(x, y); return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); diff --git a/src/libmap/region_pager_lua.cpp b/src/libmap/region_pager_lua.cpp index 4e0a973..66771c9 100644 --- a/src/libmap/region_pager_lua.cpp +++ b/src/libmap/region_pager_lua.cpp @@ -23,9 +23,6 @@ #include -//DOCS: Load, Save and Create fail unless the lua function has been set -//DOCS: UnloadIf and UnloadAll will still continue without the function set - RegionPagerLua::~RegionPagerLua() { //unload all regions UnloadAll(); @@ -44,6 +41,7 @@ Region* RegionPagerLua::LoadRegion(int x, int y) { //check if this function is available if (lua_isnil(lua, -1)) { lua_pop(lua, 1); + //signal that there is no load function return nullptr; } @@ -57,17 +55,20 @@ Region* RegionPagerLua::LoadRegion(int x, int y) { } //check the return value, success or failure - if (lua_toboolean(lua, -1)) { + if (lua_isboolean(lua, -1) && lua_toboolean(lua, -1)) { lua_pop(lua, 1); + //push and return the loaded region regionList.push_front(tmpRegion); return ®ionList.front(); } else { lua_pop(lua, 1); + //signal a failure return nullptr; } } +//NOTE: this return value seems strange; could replace it with a boolean //return the saved region, or nullptr on failure Region* RegionPagerLua::SaveRegion(int x, int y) { //get the pager's function from the registry @@ -76,6 +77,7 @@ Region* RegionPagerLua::SaveRegion(int x, int y) { //check if this function is available if (lua_isnil(lua, -1)) { lua_pop(lua, 1); + //signal that the region wasn't saved return nullptr; } @@ -83,6 +85,7 @@ Region* RegionPagerLua::SaveRegion(int x, int y) { Region* ptr = FindRegion(x, y); if (!ptr) { lua_pop(lua, 1); + //signal that there is no save function return nullptr; } lua_pushlightuserdata(lua, ptr); @@ -93,17 +96,20 @@ Region* RegionPagerLua::SaveRegion(int x, int y) { } //check the return value, success or failure - if (lua_toboolean(lua, -1)) { + if (lua_isboolean(lua, -1) && lua_toboolean(lua, -1)) { lua_pop(lua, 1); + //return the specified region that was saved return ptr; } else { lua_pop(lua, 1); + //signal a failure return nullptr; } } -//return the created region, or nullptr on failure +//DOCS: since this method is the last ditch call from GetRegion, it must return a valid region object, even if the create function hasn't been set. +//return a new region, throwing an error on failure Region* RegionPagerLua::CreateRegion(int x, int y) { if (FindRegion(x, y)) { throw(std::logic_error("Cannot overwrite an existing region")); @@ -115,7 +121,9 @@ Region* RegionPagerLua::CreateRegion(int x, int y) { //check if this function is available if (lua_isnil(lua, -1)) { lua_pop(lua, 1); - return nullptr; + //return an empty region object + regionList.emplace_front(x, y); + return ®ionList.front(); } //something to work on diff --git a/src/libmap/region_pager_lua.hpp b/src/libmap/region_pager_lua.hpp index 3789ff0..bf2c039 100644 --- a/src/libmap/region_pager_lua.hpp +++ b/src/libmap/region_pager_lua.hpp @@ -28,7 +28,7 @@ #include #include -//NOTE: set the lua hook before use +//DOCS: set the lua hook before use class RegionPagerLua : public RegionPagerBase { public: diff --git a/src/main.cpp b/src/main.cpp index ae47b38..90f7d67 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,7 @@ #include int main(int argc, char** argv) { + std::cout << "Beginning " << argv[0] << std::endl; try { Application app; app.Init(argc, argv); @@ -37,5 +38,6 @@ int main(int argc, char** argv) { std::cerr << "Fatal Error: " << e.what() << std::endl; return 1; } + std::cout << "Clean exit from " << argv[0] << std::endl; return 0; } \ No newline at end of file