Updated map system (read more)

Project compiles and runs, multiplayer works, but the map is not visible

There are a number of major changes to the map system, beth directly from the
jam branch and from the splicing process. The changes that are listed here have
been noted as they were added to the index for committing.

* tile_sheet.*pp moved from 'common/graphics/' to 'common/map/'
* Minor method and member name changes to TileSheet
* TileSheet has a lua API, but it isn't used anywhere
    NOTE: Nothing uses both lua and graphics, but a theoretical editor might
* Region API's glue functions have been changed from triggers to simple dummy
    methods. These should simply be over written.
* RegionPagerBase::GetRegion(int x, int y) now snaps it's parameters
    Presicely why is unknown, but I do remember there was a bug without it
* RegionPagerLua has been rewritten to use the Region API's methods, rather
    than the triggers.
* RegionPagerLua no longer stores the map's save directory, or passes it to the
    the Region API's methods.
* RegionPagerLua::luaState renamed to RegionPagerLua::lua
    conforms to changes elsewhere
* Removed the directory glue functions from the RegionPager API
* region_pager_api.hpp preprocessor guard changed
* Adjusted makefiles to account for TileSheet's movement
This commit is contained in:
Kayne Ruse
2014-07-31 18:01:50 +10:00
parent 66815016ba
commit 2b3ea5eb80
12 changed files with 179 additions and 97 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../map INCLUDES+=.
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../utilities INCLUDES+=. ../graphics ../utilities
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+12 -13
View File
@@ -78,28 +78,27 @@ static int getDepth(lua_State* L) {
return 1; return 1;
} }
static int onLoad(lua_State* L) { static int load(lua_State* L) {
//TODO: onLoad() //EMPTY
lua_pushboolean(L, false); lua_pushboolean(L, false);
return 1; return 1;
} }
static int onSave(lua_State* L) { static int save(lua_State* L) {
//TODO: onSave() //EMPTY
return 0; return 0;
} }
static int onCreate(lua_State* L) { static int create(lua_State* L) {
//TODO: onCreate() //EMPTY
return 0; return 0;
} }
static int onUnload(lua_State* L) { static int unload(lua_State* L) {
//TODO: onUnload() //EMPTY
return 0; return 0;
} }
//TODO: wrappers for the collision map
static const luaL_Reg regionLib[] = { static const luaL_Reg regionLib[] = {
{"SetTile",setTile}, {"SetTile",setTile},
{"GetTile",getTile}, {"GetTile",getTile},
@@ -110,10 +109,10 @@ static const luaL_Reg regionLib[] = {
{"GetWidth",getWidth}, {"GetWidth",getWidth},
{"GetHeight",getHeight}, {"GetHeight",getHeight},
{"GetDepth",getDepth}, {"GetDepth",getDepth},
{"OnLoad",onLoad}, {"Load",load},
{"OnSave",onSave}, {"Save",save},
{"OnCreate",onCreate}, {"Create",create},
{"OnUnload",onUnload}, {"Unload",unload},
{nullptr, nullptr} {nullptr, nullptr}
}; };
+3 -20
View File
@@ -24,10 +24,9 @@
#include "region_pager_lua.hpp" #include "region_pager_lua.hpp"
#include "region.hpp" #include "region.hpp"
#include <stdexcept>
#include <string> #include <string>
//DOCS: These functions are just wrappers for the RegionPagerLua class //DOCS: These glue functions simply wrap RegionPagerLua's methods
static int setTile(lua_State* L) { static int setTile(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1)); RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
@@ -64,20 +63,6 @@ static int getRegion(lua_State* L) {
return 1; return 1;
} }
static int setDirectory(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
std::string s = pager->SetDirectory(lua_tostring(L, 2));
lua_pushstring(L, s.c_str());
return 1;
}
static int getDirectory(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
std::string s = pager->GetDirectory();
lua_pushstring(L, s.c_str());
return 1;
}
static int loadRegion(lua_State* L) { static int loadRegion(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1)); RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
Region* region = pager->LoadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); Region* region = pager->LoadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
@@ -105,14 +90,12 @@ static int unloadRegion(lua_State* L) {
return 0; return 0;
} }
static const luaL_Reg pagerlib[] = { static const luaL_Reg regionPagerLib[] = {
{"SetTile", setTile}, {"SetTile", setTile},
{"GetTile", getTile}, {"GetTile", getTile},
{"SetSolid", setSolid}, {"SetSolid", setSolid},
{"GetSolid", getSolid}, {"GetSolid", getSolid},
{"GetRegion", getRegion}, {"GetRegion", getRegion},
{"SetDirectory", setDirectory},
{"GetDirectory", getDirectory},
{"LoadRegion", loadRegion}, {"LoadRegion", loadRegion},
{"SaveRegion", saveRegion}, {"SaveRegion", saveRegion},
{"CreateRegion", createRegion}, {"CreateRegion", createRegion},
@@ -121,6 +104,6 @@ static const luaL_Reg pagerlib[] = {
}; };
LUAMOD_API int openRegionPagerAPI(lua_State* L) { LUAMOD_API int openRegionPagerAPI(lua_State* L) {
luaL_newlib(L, pagerlib); luaL_newlib(L, regionPagerLib);
return 1; return 1;
} }
+2 -2
View File
@@ -19,8 +19,8 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef PAGERAPI_HPP_ #ifndef REGIONPAGERAPI_HPP_
#define PAGERAPI_HPP_ #define REGIONPAGERAPI_HPP_
#include "lua/lua.hpp" #include "lua/lua.hpp"
+3
View File
@@ -47,6 +47,9 @@ bool RegionPagerBase::GetSolid(int x, int y) {
} }
Region* RegionPagerBase::GetRegion(int x, int y) { Region* RegionPagerBase::GetRegion(int x, int y) {
x = snapToBase(REGION_WIDTH, x);
y = snapToBase(REGION_HEIGHT, y);
//get the region by various means //get the region by various means
Region* ptr = nullptr; Region* ptr = nullptr;
ptr = FindRegion(x, y); ptr = FindRegion(x, y);
+32 -36
View File
@@ -32,19 +32,18 @@ Region* RegionPagerLua::LoadRegion(int x, int y) {
Region tmpRegion(x, y); Region tmpRegion(x, y);
//API hook //API hook
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
lua_getfield(luaState, -1, "OnLoad"); lua_getfield(lua, -1, "Load");
lua_pushlightuserdata(luaState, &tmpRegion); lua_pushlightuserdata(lua, &tmpRegion);
lua_pushstring(luaState, directory.c_str()); if (lua_pcall(lua, 1, 1, 0) != LUA_OK) {
if (lua_pcall(luaState, 2, 1, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
//success or failure //success or failure
if (!lua_toboolean(luaState, -1)) { if (!lua_toboolean(lua, -1)) {
lua_pop(luaState, 2); lua_pop(lua, 2);
return nullptr; return nullptr;
} }
lua_pop(luaState, 2); lua_pop(lua, 2);
regionList.push_front(tmpRegion); regionList.push_front(tmpRegion);
return &regionList.front(); return &regionList.front();
} }
@@ -54,14 +53,13 @@ Region* RegionPagerLua::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, "Region"); lua_getglobal(lua, "Region");
lua_getfield(luaState, -1, "OnSave"); lua_getfield(lua, -1, "Save");
lua_pushlightuserdata(luaState, ptr); lua_pushlightuserdata(lua, ptr);
lua_pushstring(luaState, directory.c_str()); if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
lua_pop(luaState, 1); lua_pop(lua, 1);
} }
return ptr; return ptr;
} }
@@ -75,30 +73,29 @@ Region* RegionPagerLua::CreateRegion(int x, int y) {
Region tmpRegion(x, y); Region tmpRegion(x, y);
//API hook //API hook
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
lua_getfield(luaState, -1, "OnCreate"); lua_getfield(lua, -1, "Create");
lua_pushlightuserdata(luaState, &tmpRegion); lua_pushlightuserdata(lua, &tmpRegion);
//TODO: parameters //TODO: parameters
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { if (lua_pcall(lua, 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(lua, -1) ));
} }
lua_pop(luaState, 1); lua_pop(lua, 1);
regionList.push_front(tmpRegion); regionList.push_front(tmpRegion);
return &regionList.front(); return &regionList.front();
} }
void RegionPagerLua::UnloadRegion(int x, int y) { void RegionPagerLua::UnloadRegion(int x, int y) {
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
regionList.remove_if([&](Region& region) -> bool { regionList.remove_if([&](Region& region) -> bool {
if (region.GetX() == x && region.GetY() == y) { if (region.GetX() == x && region.GetY() == y) {
//API hook //API hook
lua_getfield(luaState, -1, "OnUnload"); lua_getfield(lua, -1, "Unload");
lua_pushlightuserdata(luaState, &region); lua_pushlightuserdata(lua, &region);
lua_pushstring(luaState, directory.c_str()); if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
return true; return true;
@@ -106,22 +103,21 @@ void RegionPagerLua::UnloadRegion(int x, int y) {
return false; return false;
}); });
lua_pop(luaState, 1); lua_pop(lua, 1);
} }
void RegionPagerLua::UnloadAll() { void RegionPagerLua::UnloadAll() {
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
for (auto& it : regionList) { for (auto& it : regionList) {
//API hook //API hook
lua_getfield(luaState, -1, "OnUnload"); lua_getfield(lua, -1, "Unload");
lua_pushlightuserdata(luaState, &it); lua_pushlightuserdata(lua, &it);
lua_pushstring(luaState, directory.c_str()); if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
} }
lua_pop(luaState, 1); lua_pop(lua, 1);
regionList.clear(); regionList.clear();
} }
+3 -7
View File
@@ -42,14 +42,10 @@ public:
void UnloadAll() override; void UnloadAll() override;
//accessors & mutators //accessors & mutators
std::string SetDirectory(std::string s) { return directory = s; } lua_State* SetLuaState(lua_State* L) { return lua = L; }
std::string GetDirectory() { return directory; } lua_State* GetLuaState() { return lua; }
lua_State* SetLuaState(lua_State* L) { return luaState = L; }
lua_State* GetLuaState() { return luaState; }
protected: protected:
std::string directory; lua_State* lua = nullptr;
lua_State* luaState = nullptr;
}; };
#endif #endif
@@ -21,24 +21,24 @@
*/ */
#include "tile_sheet.hpp" #include "tile_sheet.hpp"
void TileSheet::Load(std::string fname, int xc, int yc) { void TileSheet::Load(std::string fname, int tileWidth, int tileHeight) {
XCount = xc;
YCount = yc;
image.LoadSurface(fname); image.LoadSurface(fname);
image.SetClipW(image.GetClipW()/XCount); image.SetClipW(tileWidth);
image.SetClipH(image.GetClipH()/YCount); image.SetClipH(tileHeight);
xCount = image.GetSurface()->w / image.GetClipW();
yCount = image.GetSurface()->h / image.GetClipH();
} }
void TileSheet::Unload() { void TileSheet::Unload() {
image.FreeSurface(); image.FreeSurface();
XCount = YCount = 0; xCount = yCount = 0;
} }
void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t tile) { void TileSheet::DrawTileTo(SDL_Surface* const dest, int x, int y, Region::type_t tile) {
//0 is invisible //0 is invisible
if (tile == 0) return; if (tile == 0) return;
image.SetClipX((tile-1) % XCount * image.GetClipW()); image.SetClipX((tile-1) % xCount * image.GetClipW());
image.SetClipY((tile-1) / XCount * image.GetClipH()); image.SetClipY((tile-1) / xCount * image.GetClipH());
image.DrawTo(dest, x, y); image.DrawTo(dest, x, y);
} }
@@ -50,8 +50,8 @@ void TileSheet::DrawRegionTo(SDL_Surface* const dest, Region* const region, int
tile = region->GetTile(i, j, k); tile = region->GetTile(i, j, k);
//0 is invisible //0 is invisible
if (tile == 0) continue; if (tile == 0) continue;
image.SetClipX((tile-1) % XCount * image.GetClipW()); image.SetClipX((tile-1) % xCount * image.GetClipW());
image.SetClipY((tile-1) / XCount * image.GetClipH()); image.SetClipY((tile-1) / xCount * image.GetClipH());
image.DrawTo(dest, image.DrawTo(dest,
(region->GetX() + i) * image.GetClipW() - camX, (region->GetX() + i) * image.GetClipW() - camX,
(region->GetY() + j) * image.GetClipH() - camY); (region->GetY() + j) * image.GetClipH() - camY);
@@ -31,24 +31,24 @@
class TileSheet { class TileSheet {
public: public:
TileSheet() = default; TileSheet() = default;
TileSheet(std::string f, int x, int y) { Load(f, x, y); } TileSheet(std::string f, int w, int h) { Load(f, w, h); }
~TileSheet() = default; ~TileSheet() = default;
void Load(std::string fname, int XCount, int YCount); void Load(std::string fname, int tileWidth, int tileHeight);
void Unload(); void Unload();
void DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t tile); void DrawTileTo(SDL_Surface* const dest, int x, int y, Region::type_t tile);
void DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY); void DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY);
//accessors //accessors
Image* GetImage() { return &image; } Image* GetImage() { return &image; }
int GetXCount() { return XCount; } int GetXCount() { return xCount; }
int GetYCount() { return YCount; } int GetYCount() { return yCount; }
int GetTileW() { return image.GetClipW(); } int GetTileW() { return image.GetClipW(); }
int GetTileH() { return image.GetClipH(); } int GetTileH() { return image.GetClipH(); }
private: private:
Image image; Image image;
int XCount = 0, YCount = 0; int xCount = 0, yCount = 0;
}; };
#endif #endif
+75
View File
@@ -0,0 +1,75 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "tile_sheet_api.hpp"
#include "tile_sheet.hpp"
static int load(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
sheet->Load(lua_tostring(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
return 0;
}
static int unload(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
sheet->Unload();
return 0;
}
static int getXCount(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
lua_pushinteger(L, sheet->GetXCount());
return 1;
}
static int getYCount(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
lua_pushinteger(L, sheet->GetYCount());
return 1;
}
static int getTileW(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
lua_pushinteger(L, sheet->GetTileW());
return 1;
}
static int getTileH(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
lua_pushinteger(L, sheet->GetTileH());
return 1;
}
static const luaL_Reg tileSheetLib[] = {
{"Load",load},
{"Unload",unload},
{"GetXCount",getXCount},
{"GetYCount",getYCount},
{"GetTileW",getTileW},
{"GetTileH",getTileH},
{nullptr, nullptr}
};
LUAMOD_API int openTileSheetAPI(lua_State* L) {
luaL_newlib(L, tileSheetLib);
return 1;
}
+30
View File
@@ -0,0 +1,30 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef TILESHEETAPI_HPP_
#define TILESHEETAPI_HPP_
#include "lua/lua.hpp"
#define TORTUGA_TILE_SHEET_NAME "TileSheet"
LUAMOD_API int openTileSheetAPI(lua_State* L);
#endif