40 #include <QApplication> 51 m_currentPosition(0, 0, 0),
56 m_renderRequestsLock(QMutex::Recursive),
57 m_renderRequestSetLock(QMutex::Recursive),
58 m_precacheRequestsLock(QMutex::Recursive),
59 m_precacheRequestSetLock(QMutex::Recursive),
60 m_textureLoadedLock(QMutex::Recursive)
110 catch(
const std::exception& e)
112 std::cout <<
"An exception occurred queuing a tile to be cached: " << e.what() << std::endl;
129 int sizes[] = {3, 2, 2, 1, 1, 1};
133 for (
int i = 1; i <= 5; i++)
136 if (layer < m_tileSet->LayerCount())
156 int startRow = std::max(0, row - size);
157 int endRow = std::min(layer->
RowCount() - 1, row + size);
158 int startColumn = std::max(0, column - size);
159 int endColumn = std::min(layer->
ColumnCount() - 1, column + size);
161 for (
int c = startColumn; c <= endColumn; c++)
163 for (
int r = startRow; r <= endRow; r++)
178 catch (
const std::exception& e)
180 std::cout <<
"An exception occurred queuing tiles for precaching: " << e.what() << std::endl;
207 catch (
const std::exception& e)
209 std::cout <<
"An exception occurred loading texture: " << e.what() << std::endl;
216 QApplication::postEvent(
m_widget,
new QEvent(QEvent::UpdateRequest));
233 catch (
const std::exception& e)
235 std::cout <<
"An exception occurred unloading texture: " << e.what() << std::endl;
246 p->m_renderRequestsLock.lock();
248 if (p->m_renderRequests.size() > 0)
250 tile = p->m_renderRequests.top();
251 p->m_renderRequests.pop();
254 p->m_renderRequestsLock.unlock();
260 if (tile->
Layer() == p->m_currentLayer)
263 p->m_tileSet->GetLayer(tile->
Layer())->GetTileIndex(p->m_currentPosition, row, column);
265 if (abs(tile->
Row() - row) <= 3 || abs(tile->
Column() - column) < 3)
276 printf(
"failed to load image\n");
283 p->m_precacheRequests[tile->
Layer()].push(tile);
286 p->m_renderRequestSetLock.lock();
287 p->m_renderRequestSet.erase(tile->
TileID());
288 p->m_renderRequestSetLock.unlock();
296 p->m_precacheRequestsLock.lock();
300 for (uint32_t i = 0; (i < p->m_precacheRequests.size()) && (tile ==
NULL); i++)
302 int32_t index = p->m_currentLayer + i;
303 if ((index < (int64_t)p->m_precacheRequests.size()) &&
304 (p->m_precacheRequests[index].size() > 0))
306 tile = p->m_precacheRequests[index].front();
307 p->m_precacheRequests[index].pop();
311 index = p->m_currentLayer - i;
312 if (index >= 0 && p->m_precacheRequests[index].size() > 0)
314 tile = p->m_precacheRequests[index].front();
315 p->m_precacheRequests[index].pop();
320 catch (
const std::exception& e)
322 std::cout <<
"An exception occurred precaching texture: " << e.what() << std::endl;
325 p->m_precacheRequestsLock.unlock();
330 p->m_tileSet->GetLayer(tile->
Layer())->GetTileIndex(p->m_currentPosition, row, column);
331 if (abs(tile->
Row() - row) <= 3 || abs(tile->
Column() - column) <= 3)
341 printf(
"failed to precache load image\n");
345 p->m_precacheRequestSetLock.lock();
346 p->m_precacheRequestSet.erase(tile->
TileID());
347 p->m_precacheRequestSetLock.unlock();
362 std::map<int64_t, Tile*>* tiles;
363 p->m_textureLoadedLock.lock();
365 tiles =
new std::map<int64_t, Tile*>(p->m_textureLoaded);
367 p->m_textureLoadedLock.unlock();
369 std::map<int64_t, Tile*>::iterator iter;
371 for (iter = tiles->begin(); iter != tiles->end(); ++iter)
373 Tile* tile = iter->second;
375 p->m_tileSet->GetLayer(tile->
Layer())->GetTileIndex(p->m_currentPosition, row, column);
377 if (abs(tile->
Row() - row) > 6 || abs(tile->
Column() - column) > 6)
379 p->m_renderRequestSetLock.lock();
380 p->m_renderRequestSet.erase(tile->
TileID());
381 p->m_renderRequestSetLock.unlock();
383 p->m_precacheRequestSetLock.lock();
384 p->m_precacheRequestSet.erase(tile->
TileID());
385 p->m_precacheRequestSetLock.unlock();
bool TextureLoaded() const
QMutex m_renderRequestSetLock
void GetTileIndex(const tf::Point &position, int &row, int &column) const
void PrecacheLayer(int layer, const tf::Point &position, int size)
std::map< int64_t, Tile * > m_precacheRequestSet
void SignalMemorySize(int64_t)
bool LoadImageToMemory(bool gl=true)
CacheThread m_cacheThread
TileCache(TileSet *tileSet, QGLWidget *widget)
QMutex m_precacheRequestsLock
void LoadTexture(Tile *tile)
QMutex m_precacheRequestSetLock
std::stack< Tile * > m_renderRequests
std::map< int64_t, Tile * > m_renderRequestSet
std::map< int64_t, Tile * > m_textureLoaded
void SignalLoadTexture(Tile *)
QMutex m_renderRequestsLock
void DeleteTextureSlot(Tile *)
void Precache(const tf::Point &position)
std::vector< std::queue< Tile * > > m_precacheRequests
tf::Point m_currentPosition
TileSetLayer * GetLayer(int layer)
Tile * GetTile(int column, int row)
void UnloadTexture(Tile *tile)
void SignalDeleteTexture(Tile *)
void LoadTextureSlot(Tile *)
QMutex m_textureLoadedLock