-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.c
218 lines (185 loc) · 7.14 KB
/
scene.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
/*
* scene.c
*
* Scene management module: animation and painting.
*/
#include "soda.h"
#include <string.h>
/***************************** MODULE GLOBALS *****************************/
int g_Cell[ROWS_MAX][COLS_MAX]; // Exported
/************************ INLINE UTILITY FUNCTIONS ************************/
/*
* FastMin - Computes the minimum of x and y without branching.
*/
static inline int FastMin (int x, int y)
{
int d;
d = x - y;
return y + (d & (d >> SIGN_SHIFT));
}
/*
* FastMax - Computes de maximum of x and y without branching.
*/
static inline int FastMax (int x, int y)
{
int d;
d = x - y;
return x - (d & (d >> SIGN_SHIFT));
}
/*
* FastAbs - Computes the absolute value of x without branching.
*/
static inline int FastAbs (int x)
{
int sign;
sign = x >> SIGN_SHIFT;
return (x ^ sign) - sign;
}
/**************************** PUBLIC FUNCTIONS ****************************/
/*
* ClearScene - This function prepares you a cup of coffee.
*/
void ClearScene (void)
{
memset(&g_Cell[0][0], -1, (COLS_MAX * ROWS_MAX) * sizeof(int));
memset(&g_Item[0], 0, ITEMS_MAX * sizeof(item_t));
}
/*
* AnimateScene - Move all active objects. This function also takes care of
* collisions.
*/
void AnimateScene (void)
{
int i, row0, col0, cmp;
int x, z, row, col;
item_t *item;
if (g_GameState != PLAYING)
return;
// Animation is done for every item
for (i = 0; i < ITEMS_MAX; ++i)
{
if (g_Item[i].type == NOTHING)
continue;
item = &g_Item[i];
switch (item->action)
{
case MOVING:
// Displace with bounds checking
x = item->x_pos + item->h_increment;
x = FastMin(x, SCREEN_WIDTH - item->box_width);
x = FastMax(x, 0);
z = item->z_pos + item->v_increment;
z = FastMin(z, Z_MAX);
z = FastMax(z, Z_MIN);
// Compute cell and check collisions
col0 = item->x_pos / CELL_WIDTH;
row0 = (item->z_pos - Z_MIN) / CELL_HEIGHT;
col = x / CELL_WIDTH;
row = (z - Z_MIN) / CELL_HEIGHT;
cmp = FastAbs(col - col0) + FastAbs(row - row0);
if (cmp == 0 || g_Cell[row][col] < 0)
{
if (item->type >= PLAYER_ATTACK &&
(item->x_pos == 0 ||
item->x_pos == SCREEN_WIDTH - item->box_width))
{
item->type = NOTHING;
g_Cell[row][col] = -1;
}
else
{
g_Cell[row0][col0] = -1;
g_Cell[row][col] = i;
item->x_pos = x;
item->z_pos = z;
}
}
else
{
// Collision detected, check if it's a hit
if (item->type >= PLAYER_ATTACK)
DoHit(row0, col0, row, col);
}
// Compute next frame respecting the delay
item->delay++;
item->frame += item->delay >> 2;
item->delay &= 0x03;
break;
case SHOOTING:
item->delay++;
if (item->delay > SHOOTING_DELAY)
item->action = item->previous_action;
break;
case HIT:
item->delay++;
if (item->delay > HIT_DELAY)
item->action = item->previous_action;
break;
case DYING:
col = item->x_pos / CELL_WIDTH;
row = (item->z_pos - Z_MIN) / CELL_HEIGHT;
item->delay++;
if (item->type >= PLAYER_ATTACK)
{
item->frame += item->delay >> 1;
item->delay &= 0x01;
if (item->frame >= 4)
{
item->type = NOTHING;
if (g_Cell[row][col] == i)
g_Cell[row][col] = -1;
else
g_Item[item->overlay].overlay = -1;
}
}
else
{
item->frame += item->delay >> 2;
item->delay &= 0x03;
if (item->frame >= 8)
{
if (item->type == PLAYER)
{
g_GameState = GAME_OVER;
SDL_AddTimer(SPLASH_DELAY, ChangeState, NULL);
}
else
{
g_RoundEnemiesLeft--;
if (g_RoundEnemiesLeft == 0)
{
LoadNextRound();
return;
}
}
item->type = NOTHING;
g_Cell[row][col] = -1;
}
}
// Fall through
default:
break;
}
}
}
/*
* DrawScene - Paint the screen completely.
*/
void DrawScene (void)
{
int i, c;
ClearScreen();
if (g_GameState == PLAYING)
for (c = 0; c < COLS_MAX * ROWS_MAX; ++c)
{
i = g_Cell[0][c];
if (i >= 0)
{
DrawItem(&g_Item[i]);
if (g_Item[i].overlay >= 0)
DrawItem(&g_Item[g_Item[i].overlay]);
}
}
DrawHealth(&g_Item[0]);
FlushScreen();
}