-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphics.cpp
220 lines (184 loc) · 5.86 KB
/
graphics.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
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 "graphics.hpp"
#include <cstdlib>
#include <iostream>
#include "events.hpp"
#include "image.hpp"
#include "timer.hpp"
#include "ttf.hpp"
// events_engine
using events_engine::input;
// std
using std::cout;
namespace video_engine {
// Singleton pattern constructor
Graphics* Graphics::getInstance(void) {
static Graphics instance;
return &instance;
}
// Destructor
Graphics::~Graphics(void) {
delete images;
delete text;
SDL_Quit();
}
// Creates the surface that will be drawn directly to the screen
void Graphics::createWindow(const bool fullscreen, int width, int height) {
if (!screen) {
SDL_flags = (SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_VIDEORESIZE);
if (fullscreen) SDL_flags |= SDL_FULLSCREEN;
bpp = SDL_VideoModeOK( width, height, 16, SDL_flags);
if (!bpp) {
cout << "The choosen resolution (" << width << "x" << height
<< ") is not valid on your system. Trying default (1024x768%16)...\n";
width = 1024;
height = 768;
bpp = 16;
}
cout << "Opening " << width << "x" << height << "%" << bpp << " window...\t";
this->width = width;
this->height = height;
// Set the video mode
screen = SDL_SetVideoMode(width, height, bpp, SDL_flags);
SDL_WM_SetCaption("Heroes of Wesnoth", NULL);
if (screen == NULL) {
cout << "[fail]\n\n" << SDL_GetError() << "\n\n";
exit(EXIT_FAILURE);
}
cout << "[ ok ]\n";
}
}
// Adds a new image to the list
void Graphics::newImage(const char *image_name, const int alpha,
const int mirror, const int angle
) {
images->addImage(image_name, alpha, mirror, angle);
}
// Looks for the image in the list of loaded
// ones, if it's not there it loads it.
SDL_Surface* Graphics::getImage(const char *image_name, const int alpha,
const int mirror, const int angle
) {
SDL_Surface *temp;
temp = images->getSurface(image_name, alpha, mirror, angle);
if (!temp) { // The image hasn't been loaded.
newImage(image_name, alpha, mirror, angle);
temp = images->getSurface(image_name, alpha, mirror, angle);
}
return temp;
}
// Makes an image face the given side.
SDL_Surface* Graphics::face(const int side, SDL_Surface *image_surface) {
Image* temp;
temp = images->getImage(image_surface);
if (temp->getMirror() == MIRROR_X) {
if (side == FACE_RIGHT)
return getImage(temp->getName(), temp->getAlphaValue(),
NO_MIRROR, temp->getAngle());
else
return image_surface;
} else {
if (side == FACE_RIGHT)
return image_surface;
else
return getImage(temp->getName(), temp->getAlphaValue(),
MIRROR_X, temp->getAngle());
}
}
// This function should only be called inside the
// Events class after a SDL_VIDEORESIZE event.
void Graphics::resize(const int width, const int height) {
this->width = width;
this->height = height;
screen = SDL_SetVideoMode(width, height, bpp, SDL_flags);
}
// Returns the actual screen's size.
void Graphics::getScreenSize(int &width, int &height) {
width = this->width;
height = this->height;
}
// Before drawing looks for the image in the list
// of loaded ones, if it's not there it loads it.
void Graphics::draw(const char *image_name, SDL_Rect &position) {
SDL_Surface *temp = getImage(image_name);
draw(temp, position);
}
// Draws a surface to the indicated position.
void Graphics::draw(SDL_Surface *img, SDL_Rect &position) {
SDL_BlitSurface(img, NULL, screen, &position);
}
// Writes text in the screen.
void Graphics::write(const char *text, const int x, const int y) {
this->text->write(text, screen, x, y);
}
// The text is centered in between the given positions.
void Graphics::writeCentered(const char *text,
const int left_x, const int right_x,
const int top_y, const int bottom_y) {
this->text->writeCentered(text, screen, right_x, left_x, top_y, bottom_y);
}
// Puts the screen black.
void Graphics::erase(void) {
SDL_FillRect(screen, NULL, 0);
}
// Refreshes the screen.
void Graphics::update(void) {
input->drawMouse();
SDL_Flip(screen);
}
// Refreshes the screen with an especial effect.
void Graphics::transitionEffect(int effect) {
/// @todo Add/Improve effects.
Timer fps;
if (effect == RANDOM_EFFECT)
effect = rand() % NUM_EFFECTS;
switch (effect) {
case HORIZONTAL_EFFECT:
for (int x=0; x<width; x+=5) {
fps.start();
SDL_UpdateRect(screen, x, 0, 5, height);
fps.end(10);
}
break;
case VERTICAL_EFFECT:
for (int y=0; y<height; y+=5) {
fps.start();
SDL_UpdateRect(screen, 0, y, width, 5);
fps.end(10);
}
break;
default:
// Imposible case
break;
}
}
// Constructor
Graphics::Graphics(void) {
screen = NULL;
init();
images = new ImageList;
text = new Ttf;
}
// Initializes SDL and SDL_ttf.
void Graphics::init(void) {
cout << "Starting SDL...\t\t\t";
if ( SDL_Init (SDL_INIT_TIMER | SDL_INIT_VIDEO) < 0 ) {
cout << "[fail]\n\n" << SDL_GetError() << "\n\n";
exit(EXIT_FAILURE);
}
cout << "[ ok ]\n";
}
Graphics *screen = NULL;
} // namespace video_engine