-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileCache.cpp
More file actions
64 lines (52 loc) · 1.9 KB
/
TileCache.cpp
File metadata and controls
64 lines (52 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "TileCache.h"
HBITMAP createPlaceholderBitmap() {
HBITMAP m_hPlaceholderBitmap = CreateBitmap(256, 256, 1, 32, NULL);
HDC hdc = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hPlaceholderBitmap);
HBRUSH hBrush = CreateSolidBrush(RGB(0xcc, 0xcc, 0xcc));
RECT rect = {0, 0, 256, 256};
FillRect(hMemDC, &rect, hBrush);
SelectObject(hMemDC, hOldBitmap);
DeleteObject(hBrush);
DeleteDC(hMemDC);
ReleaseDC(NULL, hdc);
return m_hPlaceholderBitmap;
}
TileCache::TileCache(const TileDownloader* tileDownloader, DownloadWorker* downloadWorker)
: m_tileDownloader(tileDownloader),
m_downloadWorker(downloadWorker) {
m_hPlaceholderBitmap = createPlaceholderBitmap();
}
TileCache::~TileCache() {
clear();
DeleteObject(m_hPlaceholderBitmap);
}
HBITMAP TileCache::get(TileKey tileKey) {
if (tileKey.x < 0 || tileKey.y < 0 || tileKey.x > (1 << tileKey.zoomLevel) || tileKey.y > (1 << tileKey.zoomLevel)) {
throw "Invalid tile requested";
}
m_downloadWorker->transferFinishedDownloads(&m_map);
std::map<TileKey, HBITMAP>::iterator iterator = m_map.find(tileKey);
if (iterator != m_map.end()) {
// When the map contains either the real tile or the placeholder, no triggering of download is required.
return iterator->second;
}
// download asynchronously
m_downloadWorker->download(tileKey);
m_map[tileKey] = m_hPlaceholderBitmap;
return m_map[tileKey];
}
void TileCache::unqueueInvisible(TileRange visibleTiles) {
m_downloadWorker->unqueueInvisible(visibleTiles, &m_map);
}
void TileCache::clear() {
std::map<TileKey, HBITMAP>::iterator iterator;
for (iterator = m_map.begin(); iterator != m_map.end(); iterator++) {
if (iterator->second != m_hPlaceholderBitmap) {
// there may be multiple m_hPlaceholderBitmap in the map
DeleteObject(iterator->second);
}
}
m_map.clear();
}