-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
308 lines (256 loc) · 8.36 KB
/
graphics.c
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
/*
* graphics.c
*
* Module for loading sprites and painting on to the screen. Note that sprite
* management is done privately. Sprites outside the module are seen as
* integer handle IDs which are used to reference the sprite table.
*/
#include "soda.h"
#include <string.h>
#include <SDL.h>
/***************************** MODULE GLOBALS *****************************/
SDL_Surface *m_Screen;
SDL_Surface *m_Background;
SDL_Surface *m_Splash;
SDL_Surface *m_Font;
SDL_Surface *m_Sprite[ID_T_MAX];
Uint32 m_ColorKey;
Uint32 m_ClearColor;
Uint32 m_HealthColor;
/************************ INLINE UTILITY FUNCTIONS ************************/
/*
* SetRect - Sets the fields of an SDL_Rect in a handy way.
*/
static inline void SetRect (SDL_Rect *r, int x, int y,
unsigned int w, unsigned int h)
{
r->x = x; // Left
r->y = y; // Up
r->w = w; // Width
r->h = h; // Height
}
/*************************** PRIVATE FUNCTIONS ***************************/
/*
* LoadSprite - Load the sprite associated to the id given, color keyed if
* required.
*/
static SDL_Surface *LoadSprite (const char *filename, int keyed)
{
int e;
SDL_Surface *original, *optimized;
original = SDL_LoadBMP(MediaFile(filename));
if (original == NULL)
Fatal("Loading sprite '%s'.", filename);
// Optimize the surface for fast blitting
optimized = SDL_DisplayFormat(original);
SDL_FreeSurface(original);
if (keyed)
{
e = SDL_SetColorKey(optimized, SDL_SRCCOLORKEY | SDL_RLEACCEL,
m_ColorKey);
if (e < 0)
Fatal("Setting color key on '%s'.", filename);
}
return optimized;
}
/*
* PaintRoundMessage
*/
static void PaintRoundMessage (void)
{
int e, i, x, y;
char num[4];
SDL_Rect r1, r2;
x = CENTER_WIDTH(FONT_RO_WIDTH) + FONT_RO_WIDTH + 10;
y = (SCREEN_HEIGHT / 2) - (36 / 2);
snprintf(num, 3, "%d", g_Round); num[3] = '\0';
SetRect(&r1, 0, FONT_HEIGHT, FONT_RO_WIDTH, FONT_HEIGHT);
SetRect(&r2, CENTER_WIDTH(FONT_RO_WIDTH), y, 0, 0);
e = SDL_BlitSurface(m_Font, &r1, m_Screen, &r2);
if (e < 0)
Warning("Font not drawn.");
for (i = 0; num[i] != '\0'; ++i)
{
SetRect(&r1, 0, FONT_HEIGHT * ((num[i] - '0') + FONT_ZERO),
FONT_WIDTH, FONT_HEIGHT);
SetRect(&r2, x, y, FONT_WIDTH, FONT_HEIGHT);
e = SDL_BlitSurface(m_Font, &r1, m_Screen, &r2);
if (e < 0)
Warning("Font not drawn.");
x += FONT_WIDTH + 2;
}
}
/*
* PaintGameOverMessage
*/
static void PaintGameOverMessage (void)
{
int e;
SDL_Rect r1, r2;
SetRect(&r1, 0, 0, FONT_GO_WIDTH, FONT_HEIGHT);
SetRect(&r2, CENTER_WIDTH(FONT_GO_WIDTH), CENTER_HEIGHT(FONT_HEIGHT), 0, 0);
e = SDL_BlitSurface(m_Font, &r1, m_Screen, &r2);
if (e < 0)
Warning("Font not drawn.");
}
/**************************** PUBLIC FUNCTIONS ****************************/
/*
* InitGraphcis - Setups the screen settings. On error aborts.
*/
void InitGraphics (int fullscreen)
{
id_t i;
Uint32 screen_flags;
screen_flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
if (fullscreen)
screen_flags |= SDL_FULLSCREEN;
m_Screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH,
screen_flags);
if (m_Screen == NULL)
Fatal("Setting video mode.");
m_ColorKey = SDL_MapRGB(m_Screen->format, 0xFF, 0x00, 0xFF);
m_ClearColor = SDL_MapRGB(m_Screen->format, 0x00, 0x00, 0x00);
m_HealthColor = SDL_MapRGB(m_Screen->format, 0x00, 0x00, 0xFF);
m_Background = (SDL_Surface *) 0;
// Clear the sprite array
memset(m_Sprite, 0, ID_T_MAX * sizeof(SDL_Surface *));
// And load the sprites
m_Background = LoadSprite("background.bmp", 0);
m_Splash = LoadSprite("splash.bmp", 0);
m_Font = LoadSprite("font.bmp", 0);
for (i = PLAYER; i < ID_T_MAX; i++)
m_Sprite[i] = LoadSprite(c_Metrics[i].filename, 1);
}
/*
* ClearScreen
*/
void ClearScreen (void)
{
int e;
SDL_Rect r1, r2;
SetRect(&r1, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
SetRect(&r2, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
switch (g_GameState)
{
case INITIAL:
e = SDL_BlitSurface(m_Splash, &r1, m_Screen, &r2);
break;
case PLAYING:
e = SDL_BlitSurface(m_Background, &r1, m_Screen, &r2);
break;
case ROUND_CHANGE:
e = SDL_FillRect(m_Screen, &r1, m_ClearColor);
PaintRoundMessage();
break;
case GAME_OVER:
e = SDL_FillRect(m_Screen, &r1, m_ClearColor);
PaintGameOverMessage();
break;
default:
e = 0;
}
if (e < 0)
Warning("Screen not cleared.");
}
/*
* FlushScreen
*/
void FlushScreen (void)
{
int e;
e = SDL_Flip(m_Screen);
if (e < 0)
Warning("Buffers not swapped.");
}
/*
* DrawItem - Paints a drawable item onto the screen.
*/
void DrawItem (item_t *item)
{
SDL_Rect src, dst;
int e, w, h;
int x = 0, y = 0;
if (item->type >= PLAYER_ATTACK)
{
switch (item-> action)
{
case MOVING:
x = (item->direction & WEST) + (item->frame & 0x01);
break;
case DYING:
x = 4 + (item->frame & 0x03);
break;
default:
return;
}
}
else
{
switch (item->action)
{
case MOVING:
x = ((item->direction & WEST) << 1) + (item->frame & 0x03);
y = item->direction & NORTH;
break;
case AIMING:
x = (item->direction & WEST) >> 1;
y = 2;
break;
case SHOOTING:
x = 2 + ((item->direction & WEST) >> 1);
y = 2;
break;
case HIT:
x = 4 + ((item->direction & WEST) >> 1);
y = 2;
break;
case DYING:
x = item->frame & 0x07;
y = 3;
break;
default:
return;
}
}
w = 1 << item->h_frame_shift;
h = 1 << item->v_frame_shift;
x = x << item->h_frame_shift;
y = y << item->v_frame_shift;
SetRect(&src, x, y, w, h);
SetRect(&dst, item->x_pos, (item->z_pos - item->box_height), w, h);
e = SDL_BlitSurface(m_Sprite[item->type], &src, m_Screen, &dst);
if (e < 0)
Warning("Sprite '%s' not drawn.", c_Metrics[item->type].filename);
}
/*
* DrawHealth - Draws the player health status.
*/
void DrawHealth (item_t *player)
{
int percent, e;
SDL_Rect r;
if (g_GameState != PLAYING || player->strength <= 0)
return;
// Get percent value (quite expensive, could be optimized)
percent = (player->strength * 100) / c_Metrics[PLAYER].strength;
SetRect(&r, HEALTH_XPOS, HEALTH_YPOS, percent, HEALTH_HEIGHT);
e = SDL_FillRect(m_Screen, &r, m_HealthColor);
if (e < 0)
Warning("Health status not displayed.");
}
/*
* CleanUpGraphics - Free all surfaces loaded.
*/
void CleanUpGraphics (void)
{
id_t i;
if (m_Background != NULL)
SDL_FreeSurface(m_Background);
if (m_Splash != NULL)
SDL_FreeSurface(m_Splash);
if (m_Font != NULL)
SDL_FreeSurface(m_Font);
for (i = 0; i < ID_T_MAX; i++)
if (m_Sprite[i] != (SDL_Surface *) 0)
SDL_FreeSurface(m_Sprite[i]);
}