]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CSprite.cpp
Prevent a segmentation fault when using the -map option without specifying a map.
[quix0rs-blobwars.git] / src / CSprite.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #include "headers.h"
23
24 /**
25 * Creates a new Sprite object
26 */
27 Sprite::Sprite()
28 {
29         for (int i = 0 ; i < 8 ; i++)
30         {
31                 image[i] = NULL;
32                 frameLength[i] = 0;
33         }
34
35         currentFrame = 0;
36         currentTime = 0;
37         maxFrames = 0;
38
39         next = NULL;
40
41         randomFrames = false;
42 }
43
44 /**
45 * Sets the specified train with the specified image with
46 * a specified time
47 * @param i The index of the frame to set (0 - 7)
48 * @param shape The image to be used
49 * @param time How long this frame will last
50 */
51 void Sprite::setFrame(int i, SDL_Surface *shape, int time)
52 {
53         image[i] = shape;
54         frameLength[i] = time;
55
56         currentFrame = 0;
57         currentTime = frameLength[0];
58
59         if (i > maxFrames)
60                 maxFrames = i;
61 }
62
63 void Sprite::animate()
64 {
65         currentTime--;
66
67         if (currentTime > 0)
68                 return;
69
70         currentFrame++;
71         
72         if (currentFrame == 8)
73                 currentFrame = 0;
74
75         if (frameLength[currentFrame] == 0)
76                 currentFrame = 0;
77
78         currentTime = frameLength[currentFrame];
79 }
80
81 void Sprite::getNextFrame(unsigned char *frame, unsigned char *time)
82 {
83         if (*frame >= 8)
84                 *frame = 0;
85
86         if (frameLength[*frame] == 0)
87                 *frame = 0;
88
89         *time = frameLength[*frame];
90 }
91
92 SDL_Surface *Sprite::getCurrentFrame()
93 {
94         return image[currentFrame];
95 }
96
97 void Sprite::free()
98 {
99         for (int i = 0 ; i < 8 ; i++)
100         {
101                 if (image[i] != NULL)
102                 {
103                         SDL_FreeSurface(image[i]);
104                         image[i] = NULL;
105                         frameLength[i] = 0;
106                 }
107         }
108         
109         currentFrame = 0;
110         currentTime = 0;
111         maxFrames = 0;
112 }