-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
188 lines (159 loc) · 4.47 KB
/
main.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
#include <avr/io.h>
#include "main.h"
dword t = 0;
dword btn_timer = 0;
const byte LANES[4][3][2] = {
{ {7, 2}, {7, 1}, {7, 0}, },
{ {7, 4}, {7, 5}, {7, 6}, },
{ {8, 3}, {9, 3}, {10, 3},},
{ {6, 3}, {5, 3}, {4, 3}, },
};
int main (void)
{
initialise();
byte buttons;
byte player = F_DOWN;
byte player_timer = 0;
byte baddies[4];
for (byte i=0 ; i<4 ; i++)
baddies[i] = 0;
dword game_timer = 0;
word game_timer_delay = 1500;
word score = 0;
for(ever)
{
t = millis();
buttons = ~PINC;
if (btn_timer <= t)
{
if (buttons & _UP)
{
click();
player = F_UP;
btn_timer = t+BTN_DELAY;
}
else if(buttons & _DOWN)
{
click();
player = F_DOWN;
btn_timer = t+BTN_DELAY;
}
else if(buttons & _LEFT)
{
click();
player = F_LEFT;
btn_timer = t+BTN_DELAY;
}
else if(buttons & _RIGHT)
{
click();
player = F_RIGHT;
btn_timer = t+BTN_DELAY;
}
else if(buttons & _A)
{
click();
if (player_timer == 0)
{
player += 4;
player_timer = t + FRAME_DURATION;
if (baddies[player-P_UP] & 1)
{
score += 1;
baddies[player-P_UP] &= ~1;
//TODO: animate baddie dieing
}
}
btn_timer = t+BTN_DELAY;
}
}
if (player_timer && t >= player_timer) //TODO: Edge case where t+dur overflows
{
player_timer = 0;
player -= 4;
}
clear_buffer();
for (byte row=0 ; row<SCREEN_ROWS ; row++)
{
for (byte col=0 ; col<SCREEN_COLUMNS ; col++)
{
draw_tile(&GLYPHS[MAP[ SCREEN_COLUMNS * row + col ]*8], col*8, row*8);
}
}
word _s = score;
for (byte d=0 ; d<4 ; d++)
{
draw_tile( &GLYPHS[((_s % 10)+1)*8], (4-d)*8, 7*8);
_s = _s / 10;
}
draw_tile( &GLYPHS[player*8], 7*8, 3*8);
for (byte i=0 ; i<4 ; i++)
{
for (byte j=0 ; j<3 ; j++)
{
if (baddies[i] & (1<<j))
draw_tile( &GLYPHS[(B_UP+i)*8], LANES[i][j][0]*8, LANES[i][j][1]*8 );
}
}
if (t >= game_timer)
{
for (byte i=0 ; i<4 ; i++)
{
if (baddies[i] & 1)
{
draw_tile( &GLYPHS[26*8], 7*8, 3*8 );
for(ever) {}
}
else
baddies[i] = baddies[i] >> 1;
}
baddies[ rng() % 4 ] |= 1<<2;
game_timer = t + game_timer_delay;
if (game_timer_delay > 450)
game_timer_delay -= SPEED_STEP;
}
draw();
}
}
void draw_tile(const byte __memx *glyph, int x, int y)
{
/* is the tile actually visible
Last one is y >= SCREEN_HEIGHT because of the HUD */
if (x < -7 || x >= SCREEN_WIDTH || y < -7 || y >= SCREEN_HEIGHT)
return;
int y_ = y;
if (y < 0)
y_ = 0-y;
int tile_start = ((y_ >> 3) * SCREEN_WIDTH) + x;
byte y_offset_a = y & 7; // y % 8
byte y_offset_b = 8-y_offset_a;
byte glyph_index = 0;
byte tile_width = 8;
if (x < 0)
{
tile_start -= x;
glyph_index = 0-x;
tile_width -= glyph_index;
}
if (x > SCREEN_WIDTH-8)
{
tile_width = SCREEN_WIDTH-x;
}
if (y < 0)
{
y_offset_a = 8;
y_offset_b = 0-y;
tile_start -= SCREEN_WIDTH;
}
if (y > SCREEN_HEIGHT-8)
{
y_offset_b = 8;
}
for(byte tile_offset=0 ; tile_offset<tile_width ; tile_offset++, glyph_index++)
{
if (y_offset_a < 8)
buffer[tile_start+tile_offset] |= glyph[glyph_index] << y_offset_a;
if (y_offset_b < 8)
buffer[tile_start+SCREEN_WIDTH+tile_offset] |= glyph[glyph_index] >> y_offset_b;
}
}