-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.cpp
176 lines (147 loc) · 4.68 KB
/
image.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
This file is part of Heroes of Wesnoth.
Copyright (C) 2007, 2008, 2009 Jon Ander Peñalba <[email protected]>
Heroes of Wesnoth is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.
Heroes of Wesnoth is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Heroes of Wesnoth. If not, see <http://www.gnu.org/licenses/>
*/
#include "image.hpp"
#include <cstdlib>
#include <iostream>
#include <string>
#include <SDL/SDL_image.h>
#include <SDL/SDL_rotozoom.h>
// std
using std::cout;
using std::string;
// video_engine
using video_engine::Graphics;
// Class Image
// Constructor
Graphics::Image::Image(const char *image_name, const int alpha,
const int mirror, const int angle
) {
name = strdup(image_name);
this->alpha = alpha;
this->mirror = mirror;
this->angle = angle;
loadImage();
}
// Destructor
Graphics::Image::~Image(void) {
free(name);
SDL_FreeSurface(img);
}
// Indicates if the given attributes correspond to this image.
bool Graphics::Image::isImage(const char *image_name, const int alpha,
const int mirror, const int angle
) {
return ( !strcmp(image_name, name) &&
alpha == this->alpha &&
mirror == this->mirror &&
angle == this->angle );
}
// Loads an image. If there's an error exits.
void Graphics::Image::loadImage(void) {
SDL_Surface *image;
// Create a string with the fisical location of the image
// "img/" + name + ".png"
string img = "img/";
string image_name(name);
string png = ".png";
string image_dir = img + image_name + png;
image = IMG_Load( image_dir.c_str() );
if ( image==NULL ) {
cout << "\n\t" << SDL_GetError() << "\n\n";
exit(EXIT_FAILURE);
} else {
/// @todo Improve alpha display.
if (alpha != OPAQUE) { // Image transparent
SDL_SetColorKey(image, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(image->format,0,0,0));
SDL_SetAlpha(image, SDL_SRCALPHA|SDL_RLEACCEL, alpha);
}
this->img = SDL_DisplayFormatAlpha(image);
if (this->img)
SDL_FreeSurface(image);
else
this->img = image;
// Add the corresponding modifications to the image
if (angle != 0) this->img = rotozoomSurface(this->img, angle, 1, SMOOTHING_OFF);
if (mirror == MIRROR_X)
this->img = zoomSurface(this->img, -1, 1, SMOOTHING_OFF);
else if (mirror == MIRROR_Y)
this->img = zoomSurface(this->img, 1, -1, SMOOTHING_OFF);
}
}
// ---End---
// Class ImageList
// Destructor
Graphics::ImageList::~ImageList(void) {
cout << "Freeing imageList...\t\t";
unsigned int size = images.size();
for (unsigned int i=0; i<size; i++) {
delete images[i];
}
cout << "[ ok ]\n";
}
// Loads the image and then places it at
// the beginning or the list.
void Graphics::ImageList::addImage(const char *image_name, const int alpha,
const int mirror, const int angle
) {
Image *temp;
temp = new Image(image_name, alpha, mirror, angle);
images.push_front(temp);
}
// Returns the surface of an image in the list.
SDL_Surface* Graphics::ImageList::getSurface(const char *image_name, const int alpha,
const int mirror, const int angle
) {
Image *temp;
temp = findImage(image_name, alpha, mirror, angle);
if (temp)
return temp->getSurface();
else
return NULL;
}
// Looks for an image in the list.
Graphics::Image* Graphics::ImageList::findImage(const char *image_name, const int alpha,
const int mirror, const int angle
) {
unsigned int i = 0;
unsigned int size = images.size();
bool found = false;
while (i<size && !found) {
if ( images[i]->isImage(image_name, alpha, mirror, angle) )
found = true;
else
i++;
}
if (!found)
return NULL;
else
return images[i];
}
// Looks for an image in the list.
Graphics::Image* Graphics::ImageList::findImage(SDL_Surface *image) {
unsigned int i = 0;
unsigned int size = images.size();
bool found = false;
while (i<size && !found) {
if ( images[i]->getSurface() == image )
found = true;
else
i++;
}
if (!found)
return NULL;
else
return images[i];
}
// ---End---