-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite_anim.c
281 lines (200 loc) · 5.8 KB
/
sprite_anim.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
///////////////////////////////////////////////////////////////////////////////
// Headers.
#include <stdint.h>
#include "system.h"
#include <stdio.h>
#include "sprites_rgb333.h"
///////////////////////////////////////////////////////////////////////////////
// HW stuff.
#define WAIT_UNITL_0(x) while(x != 0){}
#define WAIT_UNITL_1(x) while(x != 1){}
#define SCREEN_IDX1_W 640
#define SCREEN_IDX1_H 480
#define SCREEN_IDX4_W 320
#define SCREEN_IDX4_H 240
#define SCREEN_RGB333_W 160
#define SCREEN_RGB333_H 120
#define SCREEN_IDX4_W8 (SCREEN_IDX4_W/8)
#define gpu_p32 ((volatile uint32_t*)LPRS2_GPU_BASE)
#define palette_p32 ((volatile uint32_t*)(LPRS2_GPU_BASE+0x1000))
#define unpack_idx1_p32 ((volatile uint32_t*)(LPRS2_GPU_BASE+0x400000))
#define pack_idx1_p32 ((volatile uint32_t*)(LPRS2_GPU_BASE+0x600000))
#define unpack_idx4_p32 ((volatile uint32_t*)(LPRS2_GPU_BASE+0x800000))
#define pack_idx4_p32 ((volatile uint32_t*)(LPRS2_GPU_BASE+0xa00000))
#define unpack_rgb333_p32 ((volatile uint32_t*)(LPRS2_GPU_BASE+0xc00000))
#define joypad_p32 ((volatile uint32_t*)LPRS2_JOYPAD_BASE)
typedef struct {
unsigned a : 1;
unsigned b : 1;
unsigned z : 1;
unsigned start : 1;
unsigned up : 1;
unsigned down : 1;
unsigned left : 1;
unsigned right : 1;
} bf_joypad;
#define joypad (*((volatile bf_joypad*)LPRS2_JOYPAD_BASE))
typedef struct {
uint32_t m[SCREEN_IDX1_H][SCREEN_IDX1_W];
} bf_unpack_idx1;
#define unpack_idx1 (*((volatile bf_unpack_idx1*)unpack_idx1_p32))
///////////////////////////////////////////////////////////////////////////////
// Game config.
#define STEP 1
#define PACMAN_ANIM_DELAY 3
///////////////////////////////////////////////////////////////////////////////
// Game data structures.
typedef struct {
uint16_t x;
uint16_t y;
} point_t;
typedef enum {
PACMAN_IDLE,
PACMAN_OPENING_MOUTH,
PACMAN_WITH_OPEN_MOUTH,
PACMAN_CLOSING_MOUTH,
PACMAN_WITH_CLOSED_MOUTH
} pacman_anim_states_t;
typedef struct {
pacman_anim_states_t state;
uint8_t delay_cnt;
} pacman_anim_t;
typedef struct {
point_t pos;
pacman_anim_t anim;
} pacman_t;
typedef struct {
pacman_t pacman;
} game_state_t;
void draw_sprite_from_atlas(
uint16_t src_x,
uint16_t src_y,
uint16_t w,
uint16_t h,
uint16_t dst_x,
uint16_t dst_y
) {
for(uint16_t y = 0; y < h; y++){
for(uint16_t x = 0; x < w; x++){
uint32_t src_idx =
(src_y+y)*Tank_Sprite_Map__w +
(src_x+x);
uint32_t dst_idx =
(dst_y+y)*SCREEN_RGB333_W +
(dst_x+x);
uint16_t pixel = Tank_Sprite_Map__p[src_idx];
unpack_rgb333_p32[dst_idx] = pixel;
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Game code.
int main(void) {
// Setup.
gpu_p32[0] = 3; // RGB333 mode.
gpu_p32[0x800] = 0x00ff00ff; // Magenta for HUD.
// Game state.
game_state_t gs;
gs.pacman.pos.x = 32;
gs.pacman.pos.y = 32;
gs.pacman.anim.state = PACMAN_IDLE;
gs.pacman.anim.delay_cnt = 0;
while(1){
/////////////////////////////////////
// Poll controls.
int mov_x = 0;
int mov_y = 0;
if(joypad.right){
mov_x = +1;
}
/////////////////////////////////////
// Gameplay.
gs.pacman.pos.x += mov_x*STEP;
gs.pacman.pos.y += mov_y*STEP;
switch(gs.pacman.anim.state){
case PACMAN_IDLE:
if(mov_x != 0 || mov_y != 0){
gs.pacman.anim.delay_cnt = PACMAN_ANIM_DELAY;
gs.pacman.anim.state = PACMAN_WITH_OPEN_MOUTH;
}
break;
case PACMAN_OPENING_MOUTH:
if(gs.pacman.anim.delay_cnt != 0){
gs.pacman.anim.delay_cnt--;
}else{
gs.pacman.anim.delay_cnt = PACMAN_ANIM_DELAY;
gs.pacman.anim.state = PACMAN_WITH_OPEN_MOUTH;
}
break;
case PACMAN_WITH_OPEN_MOUTH:
if(gs.pacman.anim.delay_cnt != 0){
gs.pacman.anim.delay_cnt--;
}else{
if(mov_x != 0 || mov_y != 0){
gs.pacman.anim.delay_cnt = PACMAN_ANIM_DELAY;
gs.pacman.anim.state = PACMAN_CLOSING_MOUTH;
}else{
gs.pacman.anim.state = PACMAN_IDLE;
}
}
break;
case PACMAN_CLOSING_MOUTH:
if(gs.pacman.anim.delay_cnt != 0){
gs.pacman.anim.delay_cnt--;
}else{
gs.pacman.anim.delay_cnt = PACMAN_ANIM_DELAY;
gs.pacman.anim.state = PACMAN_WITH_CLOSED_MOUTH;
}
break;
case PACMAN_WITH_CLOSED_MOUTH:
if(gs.pacman.anim.delay_cnt != 0){
gs.pacman.anim.delay_cnt--;
}else{
if(mov_x != 0 || mov_y != 0){
gs.pacman.anim.delay_cnt = PACMAN_ANIM_DELAY;
gs.pacman.anim.state = PACMAN_OPENING_MOUTH;
}else{
gs.pacman.anim.state = PACMAN_IDLE;
}
}
break;
}
/////////////////////////////////////
// Drawing.
// Detecting rising edge of VSync.
WAIT_UNITL_0(gpu_p32[2]);
WAIT_UNITL_1(gpu_p32[2]);
// Draw in buffer while it is in VSync.
// Black background.
for(uint16_t r = 0; r < SCREEN_RGB333_H; r++){
for(uint16_t c = 0; c < SCREEN_RGB333_W; c++){
unpack_rgb333_p32[r*SCREEN_RGB333_W + c] = 0000;
}
}
// Draw pacman.
switch(gs.pacman.anim.state){
case PACMAN_IDLE:
case PACMAN_OPENING_MOUTH:
case PACMAN_CLOSING_MOUTH:
// Half open mouth.
draw_sprite_from_atlas(
16, 0, 16, 16, gs.pacman.pos.x, gs.pacman.pos.y
);
break;
case PACMAN_WITH_OPEN_MOUTH:
// Full open mouth.
draw_sprite_from_atlas(
0, 0, 16, 16, gs.pacman.pos.x, gs.pacman.pos.y
);
break;
case PACMAN_WITH_CLOSED_MOUTH:
// Close mouth.
draw_sprite_from_atlas(
32, 0, 16, 16, gs.pacman.pos.x, gs.pacman.pos.y
);
break;
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////