diff --git a/common/makefile b/common/makefile index e2d1029..9fe752a 100644 --- a/common/makefile +++ b/common/makefile @@ -4,5 +4,5 @@ all: $(MAKE) -C graphics $(MAKE) -C map $(MAKE) -C network - $(MAKE) -C ui +# $(MAKE) -C ui $(MAKE) -C utilities diff --git a/common/map/makefile b/common/map/makefile index 2020f4a..03a0db8 100644 --- a/common/map/makefile +++ b/common/map/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../utilities +INCLUDES+=. ../graphics LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/map/region.hpp b/common/map/region.hpp index 68511be..da919cb 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -52,6 +52,8 @@ public: std::bitset* GetSolidBitset(); private: + friend class TileSheet; + const int x; const int y; diff --git a/common/map/region_pager_lua.cpp b/common/map/region_pager_lua.cpp index 4e0a973..66771c9 100644 --- a/common/map/region_pager_lua.cpp +++ b/common/map/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/common/map/region_pager_lua.hpp b/common/map/region_pager_lua.hpp index 0b64b0a..bf2c039 100644 --- a/common/map/region_pager_lua.hpp +++ b/common/map/region_pager_lua.hpp @@ -28,6 +28,8 @@ #include #include +//DOCS: set the lua hook before use + class RegionPagerLua : public RegionPagerBase { public: RegionPagerLua() = default; diff --git a/common/graphics/tile_sheet.cpp b/common/map/tile_sheet.cpp similarity index 77% rename from common/graphics/tile_sheet.cpp rename to common/map/tile_sheet.cpp index 137b77a..2b458ca 100644 --- a/common/graphics/tile_sheet.cpp +++ b/common/map/tile_sheet.cpp @@ -21,6 +21,8 @@ */ #include "tile_sheet.hpp" +#include + TileSheet& TileSheet::operator=(TileSheet const& rhs) { //don't screw yourself if (this == &rhs) { @@ -81,25 +83,41 @@ void TileSheet::Free() { } void TileSheet::DrawLayerTo(SDL_Renderer* const renderer, Region* const region, int layer, int camX, int camY, double scaleX, double scaleY) { - //TODO: empty + //TODO: (2) empty } void TileSheet::DrawRegionTo(SDL_Renderer* const renderer, Region* const region, int camX, int camY, double scaleX, double scaleY) { - //TODO: (2) make TileSheet a friend class of Region + //NOTE: TileSheet is a friend class of Region + //reimplementing DrawTo() to improve performance (less indirection) + if (!texture) { + throw(std::logic_error("No image texture to draw")); + } + + //the local variables + SDL_Rect sclip = {0, 0, clip.w, clip.h}; + SDL_Rect dclip = {0, 0, Uint16(clip.w * scaleX), Uint16(clip.h * scaleY)}; Region::type_t tile = 0; + + //for each tile for (register int i = 0; i < REGION_WIDTH; ++i) { for (register int j = 0; j < REGION_HEIGHT; ++j) { for (register int k = 0; k < REGION_DEPTH; ++k) { - tile = region->GetTile(i, j, k); + //get the value to skip expensive lookups + tile = region->tiles[i][j][k]; + //0 is invisible if (tile == 0) continue; - clip.x = (tile-1) % countX * clip.h; - clip.y = (tile-1) / countX * clip.w; - //TODO: (2) raw rendering; improve preformance - Image::DrawTo(renderer, - (region->GetX() + i) * clip.w - camX, - (region->GetY() + j) * clip.h - camY, - scaleX, scaleY); + + //set the sclip + sclip.x = (tile-1) % countX * clip.h; + sclip.x = (tile-1) / countX * clip.w; + + //set the dclip + dclip.x = ((region->x + i) * clip.w - camX) * scaleX; + dclip.y = ((region->y + j) * clip.h - camY) * scaleY; + + //draw + SDL_RenderCopy(renderer, texture, &sclip, &dclip); } } } diff --git a/common/graphics/tile_sheet.hpp b/common/map/tile_sheet.hpp similarity index 98% rename from common/graphics/tile_sheet.hpp rename to common/map/tile_sheet.hpp index f327151..74affdf 100644 --- a/common/graphics/tile_sheet.hpp +++ b/common/map/tile_sheet.hpp @@ -47,7 +47,7 @@ public: void DrawRegionTo(SDL_Renderer* const renderer, Region* const region, int camX, int camY, double scaleX = 1.0, double scaleY = 1.0); //accessors - //DOCS: reuse Image::clip for tile sizes + //DOCS: Reuse Image::clip for tile sizes int GetCountX() { return countX; } int GetCountY() { return countY; } int GetTileW() { return clip.w; }