-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUIImageCache.cpp
62 lines (47 loc) · 1.32 KB
/
GUIImageCache.cpp
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
//
// Flyweight.cpp
// Design Patterns
//
// Created by Nathan Daly on 4/24/12.
// Copyright 2012 Lions Entertainment. All rights reserved.
//
#include "GUIImageCache.h"
#include "GameDisplay.h"
#include "GUIUtility.h"
using std::string;
#include <map>
#include <iostream>
using std::map; using std::pair;
using std::cout; using std::endl;
using GUI::Error;
const SDL_Color transparent = {0xFF, 0, 0xFF, 0};
GUIImage_Cache::~GUIImage_Cache(){
SDL_FreeSurface(image);
}
void GUIImage_Cache::drawself(SDL_Surface *dest, int x, int y) const{
display_image(image, dest, x, y, 0);
}
int GUIImage_Cache::get_height() const {
return image->h;
}
int GUIImage_Cache::get_width() const {
return image->w;
}
GUIImage* GUIImage_Cache::get_image(std::string image_, bool alpha, const SDL_Color& color_key){
static image_map_t images;
GUIImage_Cache* image(images[image_]);
if (!image){
image = new GUIImage_Cache;
// While this "leaks", the cache is designed to last the entire execution.
// So this is okay! :)
images[image_] = image;
}
return &image->image;
}
void GUIImage_Cache::set_image(const std::string& filename) {
image = GUIImage(loadBMPAlpha(filename, transparent));
if (!image) {
string error_msg = "Couldn't load " + filename +": "+ SDL_GetError() +"\n";
throw Error(error_msg.c_str());
}
}