Skip to content

Commit

Permalink
Move common code to dedicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Nov 28, 2015
1 parent d2d806b commit 702761e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
18 changes: 4 additions & 14 deletions tilecache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

#include <SDL2pp/Surface.hh>

#include "tiles.hh"

std::string TileCache::MakeTilePath(const SDL2pp::Point& coords) {
std::stringstream filename;
filename << DATADIR << "/" << coords.x << "/" << coords.y << ".png";
Expand Down Expand Up @@ -108,14 +106,10 @@ void TileCache::SetCacheSize(size_t cache_size) {
}

void TileCache::PreloadTilesSync(const SDL2pp::Rect& rect) {
SDL2pp::Point start_tile = Tile::TileForPoint(SDL2pp::Point(rect.x, rect.y));
SDL2pp::Point end_tile = Tile::TileForPoint(SDL2pp::Point(rect.GetX2(), rect.GetY2()));

SDL2pp::Point tilecoord;
for (tilecoord.x = start_tile.x; tilecoord.x <= end_tile.x; tilecoord.x++)
for (tilecoord.y = start_tile.y; tilecoord.y <= end_tile.y; tilecoord.y++)
ProcessTilesInRect(rect, [this](const SDL2pp::Point tilecoord) {
if (tiles_.find(tilecoord) == tiles_.end())
CreateTile(tilecoord, LoadTileData(tilecoord));
});
}

void TileCache::UpdateCache(const SDL2pp::Rect& rect) {
Expand All @@ -133,16 +127,12 @@ void TileCache::UpdateCache(const SDL2pp::Rect& rect) {
// Next add all missing tiles to the queue
std::list<SDL2pp::Point> missing_tiles;

SDL2pp::Point start_tile = Tile::TileForPoint(SDL2pp::Point(rect.x, rect.y));
SDL2pp::Point end_tile = Tile::TileForPoint(SDL2pp::Point(rect.GetX2(), rect.GetY2()));

SDL2pp::Point tilecoord;
for (tilecoord.x = start_tile.x; tilecoord.x <= end_tile.x; tilecoord.x++)
for (tilecoord.y = start_tile.y; tilecoord.y <= end_tile.y; tilecoord.y++)
ProcessTilesInRect(rect, [this, &missing_tiles, &seen_tiles](const SDL2pp::Point& tilecoord) {
if (tiles_.find(tilecoord) == tiles_.end() && (!currently_loading_ || *currently_loading_ != tilecoord))
missing_tiles.push_back(tilecoord);
else
seen_tiles.insert(tilecoord);
});

// Update loader queue
loader_queue_.swap(missing_tiles);
Expand Down
13 changes: 13 additions & 0 deletions tilecache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <SDL2pp/Surface.hh>
#include <SDL2pp/Renderer.hh>

#include "tiles.hh"

class Tile;

class TileCache {
Expand Down Expand Up @@ -60,6 +62,17 @@ private:
static SurfacePtr LoadTileData(const SDL2pp::Point& coords);
void CreateTile(const SDL2pp::Point& coords, SurfacePtr surface);

template<class T>
void ProcessTilesInRect(const SDL2pp::Rect& rect, T processor) {
SDL2pp::Point start_tile = Tile::TileForPoint(SDL2pp::Point(rect.x, rect.y));
SDL2pp::Point end_tile = Tile::TileForPoint(SDL2pp::Point(rect.GetX2(), rect.GetY2()));

SDL2pp::Point tilecoord;
for (tilecoord.x = start_tile.x; tilecoord.x <= end_tile.x; tilecoord.x++)
for (tilecoord.y = start_tile.y; tilecoord.y <= end_tile.y; tilecoord.y++)
processor(tilecoord);
}

public:
TileCache(SDL2pp::Renderer& renderer);
~TileCache();
Expand Down

0 comments on commit 702761e

Please sign in to comment.