Repository navigation
Expand file tree
/
Copy pathSurface.cpp
More file actions
518 lines (465 loc) · 10.9 KB
/
Copy pathSurface.cpp
File metadata and controls
518 lines (465 loc) · 10.9 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* Copyright 2010 Daniel Albano
*
* 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 "Surface.h"
#include <fstream>
/**
* Sets up a blank 8bpp surface with the specified size and position,
* with pure black as the transparent color.
* @note Surfaces don't have to fill the whole size since their
* background is transparent, specially subclasses with their own
* drawing logic, so it just covers the maximum drawing area.
* @param width Width in pixels.
* @param height Height in pixels.
* @param x X position in pixels.
* @param y Y position in pixels.
*/
Surface::Surface(int width, int height, int x, int y) : _width(width), _height(height), _x(x), _y(y), _visible(true), _hidden(false)
{
_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
if (_surface == 0)
{
throw SDL_GetError();
}
SDL_SetColorKey(_surface, SDL_SRCCOLORKEY, 0);
_crop.w = 0;
_crop.h = 0;
_crop.x = 0;
_crop.y = 0;
}
/**
* Performs a deep copy of an existing surface.
* @param other Surface to copy from.
*/
Surface::Surface(const Surface& other)
{
_width = other._width;
_height = other._height;
_x = other._x;
_y = other._y;
_crop.w = other._crop.w;
_crop.h = other._crop.h;
_crop.x = other._crop.x;
_crop.y = other._crop.y;
_visible = other._visible;
_surface = SDL_DisplayFormat(other._surface);
}
/**
* Deletes the surface from memory.
*/
Surface::~Surface()
{
SDL_FreeSurface(_surface);
}
/**
* Loads the contents of an X-Com SCR image file into
* the surface. SCR files are simply uncompressed images
* containing the palette offset of each pixel.
* @param filename Filename of the SCR image.
* @sa http://www.ufopaedia.org/index.php?title=Image_Formats#SCR_.26_DAT
*/
void Surface::loadScr(string filename)
{
// Load file and put pixels in surface
ifstream imgFile (filename.c_str(), ios::in | ios::binary);
if (!imgFile)
{
throw "Failed to load SCR";
}
// Lock the surface
lock();
char value;
int x = 0, y = 0;
while (imgFile.read(&value, 1))
{
setPixelIterative(&x, &y, Uint8(value));
}
if (!imgFile.eof())
{
throw "Invalid data from file";
}
// Unlock the surface
unlock();
imgFile.close();
}
/**
* Loads the contents of an X-Com SPK image file into
* the surface. SPK files are compressed with a custom
* algorithm since they're usually full-screen images.
* @param filename Filename of the SPK image.
* @sa http://www.ufopaedia.org/index.php?title=Image_Formats#SPK
*/
void Surface::loadSpk(string filename)
{
// Load file and put pixels in surface
ifstream imgFile (filename.c_str(), ios::in | ios::binary);
if (!imgFile)
{
throw "Failed to load SPK";
}
// Lock the surface
lock();
Uint16 flag;
char value;
int x = 0, y = 0;
while (imgFile.read((char*)&flag, sizeof(flag)))
{
if (flag == 65535)
{
imgFile.read((char*)&flag, sizeof(flag));
for (int i = 0; i < flag*2; i++)
{
setPixelIterative(&x, &y, 0);
}
}
else if (flag == 65534)
{
imgFile.read((char*)&flag, sizeof(flag));
for (int i = 0; i < flag*2; i++)
{
imgFile.read(&value, 1);
setPixelIterative(&x, &y, Uint8(value));
}
}
else if (flag == 65533)
{
break;
}
}
/*
if (!imgFile.eof())
{
throw "Invalid data from file";
}
*/
// Unlock the surface
unlock();
imgFile.close();
}
/**
* Clears the entire contents of the surface, resulting
* in a blank image.
*/
void Surface::clear()
{
SDL_Rect square;
square.x = 0;
square.y = 0;
square.w = _width;
square.h = _height;
SDL_FillRect(_surface, &square, 0);
}
/**
* Shifts all the colors in the surface by a set amount.
* This is a common method in 8bpp games to simulate color
* effects for cheap.
* @param off Amount to shift.
* @param min Minimum color to shift to.
* @param max Maximum color to shift to.
*/
void Surface::offset(int off, int min, int max)
{
// Lock the surface
lock();
for (int x = 0, y = 0; x < _surface->w && y < _surface->h;)
{
Uint8 pixel = getPixel(x, y);
int p = pixel + off;
if (min != -1 && p < min)
p = min;
else if (max != -1 && p > max)
p = max;
if (pixel > 0)
setPixelIterative(&x, &y, p);
else
setPixelIterative(&x, &y, 0);
}
// Unlock the surface
unlock();
}
/**
* Inverts all the colors in the surface according to a middle point.
* Used for effects like shifting a button between pressed and unpressed.
* @param mid Middle point.
*/
void Surface::invert(Uint8 mid)
{
// Lock the surface
lock();
for (int x = 0, y = 0; x < _surface->w && y < _surface->h;)
{
Uint8 pixel = getPixel(x, y);
if (pixel > 0)
setPixelIterative(&x, &y, pixel + 2 * (mid - pixel));
else
setPixelIterative(&x, &y, 0);
}
// Unlock the surface
unlock();
}
/**
* Runs any code the surface needs to keep updating every
* game cycle, like animations and other real-time elements.
*/
void Surface::think()
{
}
/**
* Draws the graphic that the surface contains and
* gets blitted onto other surfaces.
*/
void Surface::draw()
{
}
/**
* Blits this surface onto another one, with its position
* relative to the top-left corner of the target surface.
* The cropping rectangle controls the portion of the surface
* that is blitted.
* @param surface Pointer to surface to blit onto.
*/
void Surface::blit(Surface *surface)
{
if (_visible && !_hidden)
{
SDL_Rect* cropper;
SDL_Rect target;
if (_crop.w == 0 && _crop.h == 0)
cropper = 0;
else
cropper = &_crop;
target.x = _x;
target.y = _y;
SDL_BlitSurface(_surface, cropper, surface->getSurface(), &target);
}
}
/**
* Copies the exact contents of another surface onto this one.
* Only the content that would overlap both surfaces is copied, in
* accordance with their positions. This is handy for applying
* effects over another surface without modifying the original.
* @param surface Pointer to surface to copy from.
*/
void Surface::copy(Surface *surface)
{
SDL_Rect from;
from.x = _x - surface->getX();
from.y = _y - surface->getY();
from.w = _width;
from.h = _height;
SDL_BlitSurface(surface->getSurface(), &from, _surface, 0);
}
/**
* Changes the position of the surface in the X axis.
* @param x X position in pixels.
*/
void Surface::setX(int x)
{
_x = x;
}
/**
* Returns the position of the surface in the X axis.
* @return X position in pixels.
*/
int Surface::getX()
{
return _x;
}
/**
* Changes the position of the surface in the Y axis.
* @param y Y position in pixels.
*/
void Surface::setY(int y)
{
_y = y;
}
/**
* Returns the position of the surface in the Y axis.
* @return Y position in pixels.
*/
int Surface::getY()
{
return _y;
}
/**
* Changes the cropping rectangle set for this surface.
* @param crop Pointer to the cropping rectangle. Null
* removes the cropping rectangle (the whole surface is blitted).
*/
void Surface::setCrop(SDL_Rect *crop)
{
if (crop == 0)
{
_crop.w = 0;
_crop.h = 0;
_crop.x = 0;
_crop.y = 0;
}
else
{
_crop.w = crop->w;
_crop.h = crop->h;
_crop.x = crop->x;
_crop.y = crop->y;
}
}
/**
* Returns the cropping rectangle for this surface.
* @return Pointer to the cropping rectangle.
*/
SDL_Rect *Surface::getCrop()
{
return &_crop;
}
/**
* Replaces a certain amount of colors in the surface's palette.
* @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 Surface::setPalette(SDL_Color *colors, int firstcolor, int ncolors)
{
SDL_SetColors(_surface, colors, firstcolor, ncolors);
}
/**
* Returns the surface's 8bpp palette.
* @return Pointer to the palette's colors.
*/
SDL_Color* Surface::getPalette()
{
return _surface->format->palette->colors;
}
/**
* Changes the color of a pixel in the surface, relative to
* the top-left corner of the surface.
* @param x X position of the pixel.
* @param y Y position of the pixel.
* @param pixel New color for the pixel.
*/
void Surface::setPixel(int x, int y, Uint8 pixel)
{
if (x < 0 || x > _width || y < 0 || y > _height)
return;
((Uint8 *)_surface->pixels)[y * _surface->pitch + x * _surface->format->BytesPerPixel] = pixel;
}
/**
* Changes the color of a pixel in the surface and returns the
* next pixel position. Useful when changing a lot of pixels in
* a row, eg. loading images.
* @param x Pointer to the X position of the pixel. Changed to the next X position in the sequence.
* @param y Pointer to the Y position of the pixel. Changed to the next Y position in the sequence.
* @param pixel New color for the pixel.
*/
void Surface::setPixelIterative(int *x, int *y, Uint8 pixel)
{
setPixel(*x, *y, pixel);
(*x)++;
if (*x == _width)
{
(*y)++;
*x = 0;
}
}
/**
* Returns the color of a specified pixel in the surface.
* @param x X position of the pixel.
* @param y Y position of the pixel.
* @return Color of the pixel.
*/
Uint8 Surface::getPixel(int x, int y)
{
return ((Uint8 *)_surface->pixels)[y * _surface->pitch + x * _surface->format->BytesPerPixel];
}
/**
* Returns the internal SDL_Surface for SDL calls.
* @return Pointer to the surface.
*/
SDL_Surface* Surface::getSurface()
{
return _surface;
}
/**
* Returns the width of the surface.
* @return Width in pixels.
*/
int Surface::getWidth()
{
return _width;
}
/**
* Returns the height of the surface.
* @return Height in pixels
*/
int Surface::getHeight()
{
return _height;
}
/**
* Changes the visibility of the surface. A hidden surface
* isn't blitted nor receives events.
* @param visible New visibility.
*/
void Surface::setVisible(bool visible)
{
_visible = visible;
}
/**
* Returns the visible state of the surface.
* @return Current visibility.
*/
bool Surface::getVisible()
{
return _visible;
}
/**
* Hides the surface if it's visible. This is a separate
* visibility setting intended for temporary effects,
* so as to not override the actual visibility setting.
*/
void Surface::hide()
{
_hidden = true;
}
/**
* Shows the surface if it's not invisible. This is a separate
* visibility setting intended for temporary effects,
* so as to not override the actual visibility setting.
*/
void Surface::show()
{
_hidden = false;
}
/**
* Locks the surface from outside access
* for pixel-level access. Must be unlocked
* afterwards.
* @sa unlock()
*/
void Surface::lock()
{
SDL_LockSurface(_surface);
}
/**
* Unlocks the surface after it's been locked
* to resume blitting operations.
* @sa lock()
*/
void Surface::unlock()
{
SDL_UnlockSurface(_surface);
}