aboutsummaryrefslogtreecommitdiff
path: root/graphics/nine_patch.cpp
diff options
context:
space:
mode:
authorBorja Lorente2016-07-31 18:25:02 +0200
committerBorja Lorente2016-08-02 08:33:50 +0200
commit02a18134c380acbddd8a7d3fa11a19fff3f21a55 (patch)
tree210738947c66127350289921172c677c9928f9de /graphics/nine_patch.cpp
parent03f2d9b01ed392488269647d557b25f41a86fc84 (diff)
downloadscummvm-rg350-02a18134c380acbddd8a7d3fa11a19fff3f21a55.tar.gz
scummvm-rg350-02a18134c380acbddd8a7d3fa11a19fff3f21a55.tar.bz2
scummvm-rg350-02a18134c380acbddd8a7d3fa11a19fff3f21a55.zip
GRAPHICS: Add basic caching to 9patch colors
Diffstat (limited to 'graphics/nine_patch.cpp')
-rw-r--r--graphics/nine_patch.cpp26
1 files changed, 17 insertions, 9 deletions
diff --git a/graphics/nine_patch.cpp b/graphics/nine_patch.cpp
index f89a0f2df7..fa2ef20a6e 100644
--- a/graphics/nine_patch.cpp
+++ b/graphics/nine_patch.cpp
@@ -235,6 +235,11 @@ void NinePatchBitmap::blit(Graphics::Surface &target, int dx, int dy, int dw, in
drawRegions(srf, dx, dy, dw, dh);
+ //TODO: This can be further optimized by keeping the data between draws,
+ // and using a unique identifier for each palette, so that it only gets
+ // recalculated when the palette changes.
+ _cached_colors.clear();
+
for (uint i = 0; i < srf.w; ++i) {
for (uint j = 0; j < srf.h; ++j) {
uint32 color = *(uint32*)srf.getBasePtr(i, j);
@@ -334,18 +339,21 @@ static inline uint32 dist(uint32 a, uint32 b) {
}
byte NinePatchBitmap::closestGrayscale(uint32 color, byte* palette, byte paletteLength) {
- byte target = grayscale(color);
- byte bestNdx = 0;
- byte bestColor = grayscale(palette[0], palette[1], palette[2]);
- for (byte i = 1; i < paletteLength; ++i) {
- byte current = grayscale(palette[i * 3], palette[(i * 3) + 1], palette[(i * 3) + 2]);
- if (dist(target, bestColor) >= dist(target, current)) {
- bestColor = current;
- bestNdx = i;
+ if (!_cached_colors.contains(color)) {
+ byte target = grayscale(color);
+ byte bestNdx = 0;
+ byte bestColor = grayscale(palette[0], palette[1], palette[2]);
+ for (byte i = 1; i < paletteLength; ++i) {
+ byte current = grayscale(palette[i * 3], palette[(i * 3) + 1], palette[(i * 3) + 2]);
+ if (dist(target, bestColor) >= dist(target, current)) {
+ bestColor = current;
+ bestNdx = i;
+ }
}
+ _cached_colors[color] = bestNdx;
}
- return bestNdx;
+ return _cached_colors[color];
}
} // end of namespace Graphics