Repository navigation
Expand file tree
/
Copy pathScreen.cpp
More file actions
369 lines (342 loc) · 8.54 KB
/
Copy pathScreen.cpp
File metadata and controls
369 lines (342 loc) · 8.54 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright 2010-2012 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Screen.h"
#include <sstream>
#include <iomanip>
#include <iostream>
#include <SDL_rotozoom.h>
#include "Exception.h"
#include "Surface.h"
#include "Action.h"
#include "Options.h"
#include "CrossPlatform.h"
namespace OpenXcom
{
const double Screen::BASE_WIDTH = 320.0;
const double Screen::BASE_HEIGHT = 200.0;
/**
* Initializes a new display screen for the game to render contents to.
* @param width Width in pixels.
* @param height Height in pixels.
* @param bpp Bits-per-pixel.
*/
Screen::Screen(int width, int height, int bpp) : _scaleX(1.0), _scaleY(1.0), _fullscreen(false)
{
_flags = SDL_SWSURFACE|SDL_HWPALETTE;
std::cout << "Attempting to initialize display to " << width << "x" << height << "x" << bpp << "..." << std::endl;
_screen = SDL_SetVideoMode(width, height, bpp, _flags);
if (_screen == 0)
{
throw Exception(SDL_GetError());
}
std::cout << "Display initialized to " << _screen->w << "x" << _screen->h << "x" << (int)_screen->format->BitsPerPixel << "." << std::endl;
_surface = new Surface(width, height);
}
/**
* Deletes the buffer from memory. The display screen itself
* is automatically freed once SDL shuts down.
*/
Screen::~Screen()
{
delete _surface;
}
/**
* Returns the screen's internal buffer surface. Any
* contents that need to be shown will be blitted to this.
* @return Pointer to the buffer surface.
*/
Surface *const Screen::getSurface() const
{
return _surface;
}
/**
* Handles screen key shortcuts.
* @param action Pointer to an action.
*/
void Screen::handle(Action *action)
{
if (action->getDetails()->type == SDL_KEYDOWN && action->getDetails()->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) != 0)
{
setFullscreen(!_fullscreen);
}
else if (action->getDetails()->type == SDL_KEYDOWN && action->getDetails()->key.keysym.sym == SDLK_F12)
{
std::stringstream ss;
int i = 0;
do
{
ss.str("");
ss << Options::getUserFolder() << "screen" << std::setfill('0') << std::setw(3) << i << ".bmp";
i++;
}
while (CrossPlatform::fileExists(ss.str()));
SDL_SaveBMP(_screen, ss.str().c_str());
}
}
/**
* Internal 8 bit Zoomer without smoothing.
* Source code originally from SDL_gfx (LGPL) with permission by author.
*
* Zooms 8bit palette/Y 'src' surface to 'dst' surface.
* Assumes src and dst surfaces are of 8 bit depth.
* Assumes dst surface was allocated with the correct dimensions.
*
* @param src The surface to zoom (input).
* @param dst The zoomed surface (output).
* @param flipx Flag indicating if the image should be horizontally flipped.
* @param flipy Flag indicating if the image should be vertically flipped.
* @return 0 for success or -1 for error.
*/
int Screen::_zoomSurfaceY(SDL_Surface * src, SDL_Surface * dst, int flipx, int flipy)
{
int x, y;
Uint32 *sax, *say, *csax, *csay;
int csx, csy;
Uint8 *sp, *dp, *csp;
int dgap;
/*
* Allocate memory for row increments
*/
if ((sax = (Uint32 *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) {
return (-1);
}
if ((say = (Uint32 *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) {
free(sax);
return (-1);
}
/*
* Pointer setup
*/
sp = csp = (Uint8 *) src->pixels;
dp = (Uint8 *) dst->pixels;
dgap = dst->pitch - dst->w;
if (flipx) csp += (src->w-1);
if (flipy) csp = ( (Uint8*)csp + src->pitch*(src->h-1) );
/*
* Precalculate row increments
*/
csx = 0;
csax = sax;
for (x = 0; x < dst->w; x++) {
csx += src->w;
*csax = 0;
while (csx >= dst->w) {
csx -= dst->w;
(*csax)++;
}
(*csax) *= (flipx ? -1 : 1);
csax++;
}
csy = 0;
csay = say;
for (y = 0; y < dst->h; y++) {
csy += src->h;
*csay = 0;
while (csy >= dst->h) {
csy -= dst->h;
(*csay)++;
}
(*csay) *= src->pitch * (flipy ? -1 : 1);
csay++;
}
/*
* Draw
*/
csay = say;
for (y = 0; y < dst->h; y++) {
csax = sax;
sp = csp;
for (x = 0; x < dst->w; x++) {
/*
* Draw
*/
*dp = *sp;
/*
* Advance source pointers
*/
sp += (*csax);
csax++;
/*
* Advance destination pointer
*/
dp++;
}
/*
* Advance source pointer (for row)
*/
csp += (*csay);
csay++;
/*
* Advance destination pointers
*/
dp += dgap;
}
/*
* Remove temp arrays
*/
free(sax);
free(say);
return (0);
}
/**
* Renders the buffer's contents onto the screen, applying
* any necessary filters or conversions in the process.
* If the scaling factor is bigger than 1, the entire contents
* of the buffer are resized by that factor (eg. 2 = doubled)
* before being put on screen.
*/
void Screen::flip()
{
if (getWidth() != BASE_WIDTH || getHeight() != BASE_HEIGHT)
{
_zoomSurfaceY(_surface->getSurface(), _screen, 0, 0);
}
else
{
SDL_BlitSurface(_surface->getSurface(), 0, _screen, 0);
}
if (SDL_Flip(_screen) == -1)
{
throw Exception(SDL_GetError());
}
}
/**
* Clears all the contents out of the internal buffer.
*/
void Screen::clear()
{
_surface->clear();
SDL_Rect square;
square.x = 0;
square.y = 0;
square.w = getWidth();
square.h = getHeight();
SDL_FillRect(_screen, &square, 0);
}
/**
* Changes the 8bpp palette used to render the screen's contents.
* @param colors Pointer to the set of colors.
* @param firstcolor Offset of the first color to replace.
* @param ncolors Amount of colors to replace.
*/
void Screen::setPalette(SDL_Color* colors, int firstcolor, int ncolors)
{
_surface->setPalette(colors, firstcolor, ncolors);
if (SDL_SetColors(_screen, colors, firstcolor, ncolors) == 0)
{
std::cerr << "Display palette doesn't match requested palette" << std::endl;
}
// Sanity check
/*
SDL_Color *newcolors = _screen->format->palette->colors;
for (int i = firstcolor, j = 0; i < firstcolor + ncolors; i++, j++)
{
std::cout << (int)newcolors[i].r << " - " << (int)newcolors[i].g << " - " << (int)newcolors[i].b << std::endl;
std::cout << (int)colors[j].r << " + " << (int)colors[j].g << " + " << (int)colors[j].b << std::endl;
if (newcolors[i].r != colors[j].r ||
newcolors[i].g != colors[j].g ||
newcolors[i].b != colors[j].b)
{
std::cout << "WARNING: Display palette doesn't match requested palette" << std::endl;
break;
}
}
*/
}
/**
* Returns the screen's 8bpp palette.
* @return Pointer to the palette's colors.
*/
SDL_Color *const Screen::getPalette() const
{
return _surface->getPalette();
}
/**
* Returns the width of the screen.
* @return Width in pixels.
*/
int Screen::getWidth() const
{
return _screen->w;
}
/**
* Returns the height of the screen.
* @return Height in pixels
*/
int Screen::getHeight() const
{
return _screen->h;
}
/**
* Changes the screen's resolution. The display surface
* and palette have to be reset for this to happen properly.
* @param width Width in pixels.
* @param height Height in pixels.
*/
void Screen::setResolution(int width, int height)
{
_scaleX = width / BASE_WIDTH;
_scaleY = height / BASE_HEIGHT;
std::cout << "Attempting to change display to " << width << "x" << height << "..." << std::endl;
_screen = SDL_SetVideoMode(width, height, _screen->format->BitsPerPixel, _flags);
if (_screen == 0)
{
throw Exception(SDL_GetError());
}
std::cout << "Display changed to " << _screen->w << "x" << _screen->h << "x" << (int)_screen->format->BitsPerPixel << "." << std::endl;
setPalette(getPalette());
}
/**
* Switches the screen between full-screen and/or windowed.
* The screen has to be reset for this to happen properly.
* @param full True for full-screen, False for windowed.
*/
void Screen::setFullscreen(bool full)
{
if (full == _fullscreen)
return;
_fullscreen = full;
if (_fullscreen)
{
_flags |= SDL_FULLSCREEN;
}
else
{
_flags &= ~SDL_FULLSCREEN;
}
Options::setBool("fullscreen", _fullscreen);
setResolution(getWidth(), getHeight());
}
/**
* Returns the screen's X scale.
* @return Scale factor.
*/
double Screen::getXScale() const
{
return _scaleX;
}
/**
* Returns the screen's Y scale.
* @return Scale factor.
*/
double Screen::getYScale() const
{
return _scaleY;
}
}