-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlesson18.c
executable file
·262 lines (216 loc) · 6.23 KB
/
lesson18.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
/* for cc65, for NES
* simple platformer game - now adding DMC samples (not in the music)
* JUMP = play first DMC sample, START = play second DMC sample
* doug fraker 2016
* feel free to reuse any code here
*/
#include "DEFINE.c"
void main (void) {
All_Off(); // turn off screen
Draw_Background();
X1 = 0x80;
Y1 = 0x70; // middle of screen
Load_Palette();
Reset_Scroll();
Reset_Music(); // note, this is famitone init, and I added the music data address. see famitone2.s
Play_Music(song); // song = 0
Wait_Vblank();
All_On(); // turn on screen
while (1){ // infinite loop
while (NMI_flag == 0); // wait till NMI
//every_frame(); // moved this to the nmi code in reset.s for greater stability
Get_Input();
move_logic();
update_Sprites();
Music_Update();
if (((joypad1old & START) == 0)&&((joypad1 & START) != 0)){
DMC_PLAY(27);
}
NMI_flag = 0;
}
}
// inside the startup code, the NMI routine will ++NMI_flag and ++Frame_Count at each V-blank
void All_Off(void) {
PPU_CTRL = 0;
PPU_MASK = 0;
}
void All_On(void) {
PPU_CTRL = 0x90; // screen is on, NMI on
PPU_MASK = 0x1e;
}
void Reset_Scroll (void) {
PPU_ADDRESS = 0;
PPU_ADDRESS = 0;
SCROLL = 0;
SCROLL = 0;
}
void Load_Palette (void) {
PPU_ADDRESS = 0x3f;
PPU_ADDRESS = 0x00;
for( index = 0; index < sizeof(PALETTE); ++index ){
PPU_DATA = PALETTE[index];
}
}
void update_Sprites (void) {
state4 = state << 2; // shift left 2 = multiply 4
index4 = 0;
if (direction == 0){ // right
for (index = 0; index < 4; ++index ){
SPRITES[index4] = MetaSprite_Y[index] + Y1; // relative y + master y
++index4;
SPRITES[index4] = MetaSprite_Tile_Right[index + state4]; // tile numbers
++index4;
SPRITES[index4] = MetaSprite_Attrib_Right[index]; // attributes, all zero here
++index4;
SPRITES[index4] = MetaSprite_X[index] + X1; // relative x + master x
++index4;
}
}
else { // left
for (index = 0; index < 4; ++index ){
SPRITES[index4] = MetaSprite_Y[index] + Y1; // relative y + master y
++index4;
SPRITES[index4] = MetaSprite_Tile_Left[index + state4]; // tile numbers
++index4;
SPRITES[index4] = MetaSprite_Attrib_Left[index]; // attributes, all zero here
++index4;
SPRITES[index4] = MetaSprite_X[index] + X1; // relative x + master x
++index4;
}
}
}
void Collision_Down(void){
if (NametableB == 0){ // first collision map
temp = C_MAP[collision_Index];
collision += PLATFORM[temp];
}
else { // second collision map
temp = C_MAP2[collision_Index];
collision += PLATFORM[temp];
}
}
void move_logic(void) {
if ((joypad1 & (RIGHT|LEFT)) == 0){ // no L or R
walk_count = 0;
// apply friction, if no L or R
if (X_speed >= 0){ // if positive, going right
if (X_speed >= 4){
X_speed -= 4;
}
else {
X_speed = 0;
}
}
else { // going left
if (X_speed <= (-4)){
X_speed +=4;
}
else {
X_speed = 0;
}
}
}
if ((joypad1 & RIGHT) != 0){
++walk_count;
direction = 0;
if (X_speed >= 0){ // going right
X_speed += 2;
}
else { // going left
X_speed += 8; // double friction
}
}
if ((joypad1 & LEFT) != 0){
++walk_count;
direction = 1;
if (X_speed <= 0){ // going left
X_speed -= 2;
}
else { // going right
X_speed -= 8; // double friction
}
}
// collision check, platform
// first check the bottom left corner of character
// which nametable am I in?
NametableB = Nametable;
Scroll_Adjusted_X = (X1 + Horiz_scroll + 3); // left
high_byte = Scroll_Adjusted_X >> 8;
if (high_byte != 0){ // if H scroll + Sprite X > 255, then we should use
++NametableB; // the other nametable's collision map
NametableB &= 1; // keep it 0 or 1
}
// we want to find which metatile in the collision map this point is in...is it solid?
collision_Index = (((char)Scroll_Adjusted_X>>4) + ((Y1+16) & 0xf0));
collision = 0;
Collision_Down(); // if on platform, ++collision
// now check the bottom right corner of character
// which nametable am I in?
NametableB = Nametable;
Scroll_Adjusted_X = (X1 + Horiz_scroll + 12);
high_byte = Scroll_Adjusted_X >> 8;
if (high_byte != 0){ // if H scroll + Sprite X > 255, then we should use
++NametableB; // the other nametable's collision map
NametableB &= 1; // keep it 0 or 1
}
collision_Index = (((char)Scroll_Adjusted_X>>4) + ((Y1+16) & 0xf0)); // bottom right
Collision_Down(); // if on platform, ++collision
if ((Y1 & 0x0f) > 1) // only platform collide if nearly aligned to a metatile
collision = 0;
if (collision == 0){
Y_speed += 2; // gravity
}
else {
Y_speed = 0; // collision = stop falling
Y1 &= 0xf0; // align to the metatile
}
// Jump - we already figured if we are on a platform, only jump if on a platform
if (collision > 0){
if (((joypad1 & A_BUTTON) != 0) && ((joypad1old & A_BUTTON) == 0)){
Y_speed = -0x38; //0xc8
DMC_PLAY(25);
}
}
// max speeds
if (X_speed >= 0){ // going right
if (X_speed > 0x20)
X_speed = 0x20;
}
else {
if (X_speed < (-0x20))
X_speed = (-0x20); // 0xe0
}
if (Y_speed >= 0){
if (Y_speed > 0x20)
Y_speed = 0x20;
}
// move player
Horiz_scroll_Old = Horiz_scroll;
Horiz_scroll += (X_speed >> 4); // use the high nibble
if (X_speed >= 0){ // going right
if (Horiz_scroll_Old > Horiz_scroll) // if pass 0, switch nametables
++Nametable;
}
else { // going left
if (Horiz_scroll_Old < Horiz_scroll)
++Nametable; // if pass 0, switch nametables
}
Nametable &= 1; // keep it 1 or 0
Y1 += (Y_speed >> 4); // use the high nibble
if (walk_count > 0x1f) // walk_count forced 0-1f
walk_count = 0;
state = Walk_Moves[(walk_count >> 3)]; // if not jumping
if (Y_speed < 0) // negative = jumping
state = 3;
}
void Draw_Background(void) {
PPU_ADDRESS = 0x20; // address of nametable #0 = 0x2000
PPU_ADDRESS = 0x00;
UnRLE(N1); // uncompresses our data
PPU_ADDRESS = 0x24; // address of nametable #1 = 0x2400
PPU_ADDRESS = 0x00;
UnRLE(N2); // uncompresses our data
// load the collision map
memcpy (C_MAP, C1, 240);
memcpy (C_MAP2, C2, 240);
}