-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_platformer.mfk
More file actions
1566 lines (1321 loc) · 49 KB
/
basic_platformer.mfk
File metadata and controls
1566 lines (1321 loc) · 49 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// compile with -t nes_small_v and -O4
//use the dpad to move the player around
//use a to jump
//use b to dash
//TODO: rework scrolling so that it works even
//if you skip over the specific xscroll values it needs
//to detect a new column
import nes_joy
const word ppu_nametable0 = $2000
const word ppu_nametable1 = $2400
const word ppu_nametable2 = $2800
const word ppu_nametable3 = $2C00
const word ppu_attrtable0 = $23C0
const word ppu_attrtable1 = $27C0
const word ppu_attrtable2 = $2BC0
const word ppu_attrtable3 = $2FC0
const word ppu_pallete_ram = $3F00
array oam_buffer [256] @$200 // sprite buffer
//constant offsets for oam_buffer
//so we can draw the player metasprite easily,
//in a real game you should use a oam manager
const byte playersprite0_x = 4
const byte playersprite0_tile = 5
const byte playersprite0_attrs = 6
const byte playersprite0_y = 7
const byte playersprite1_x = 8
const byte playersprite1_tile = 9
const byte playersprite1_attrs = 10
const byte playersprite1_y = 11
const byte playersprite2_x = 12
const byte playersprite2_tile = 13
const byte playersprite2_attrs = 14
const byte playersprite2_y = 15
const byte playersprite3_x = 16
const byte playersprite3_tile = 17
const byte playersprite3_attrs = 18
const byte playersprite3_y = 19
//player sprite constants
const byte sprite0_attrs = 0
const byte sprite1_attrs = 0
const byte sprite2_attrs = 0
const byte sprite3_attrs = 0
const byte sprite0_tile = 0
const byte sprite1_tile = 1
const byte sprite2_tile = 2
const byte sprite3_tile = 3
//collision flags
bool collis_flag
bool down_collis_flag
bool up_collis_flag
bool right_collis_flag
bool left_collis_flag
//player physics flags
bool on_ground
bool jumping
bool can_jump
byte playerx
byte playery
byte playerx_frac //fractional part of playerx, can range from 0-3 (first 2 bits)
byte playery_frac //fractional part of playery, can range from 0-3 (first 2 bits)
//velocities are added to playerx_frac and playery_frac, so they should be treated as
//fractional parts (i.e. playerxvel = 4 means playerx will increase by 1, playerxvel = 8
//means playerx will increase by 2, etc...)
volatile sbyte playerxvel
volatile sbyte playeryvel
//speed added to player when moving
byte playerx_speed
byte playerx_accel
byte playery_accel
byte scroll_speed
const byte player_width = 12 //width in pixels
const byte player_height = 14 //height in pixels
const byte player_max_xvel = 10
const byte player_max_xvel_neg = -10
const byte player_x_friction = 8
const byte player_x_air_friction = 8
const byte player_max_yvel = 32
const byte player_max_yvel_neg = -24
const byte player_gravity_accel = 4
byte nametable //current nametable
byte draw_nametable //the nametable we want to draw on
byte draw_seam_column //column at the draw/scroll seam
byte next_column //column to be drawn to next
byte current_screen //screen to load next
byte xscroll //screen x scroll value
byte prev_xscroll //previous xcroll value
bool scrolling_right
bool scrolling_left
byte previous_scroll //0 = scrolling right, 1 = scrolling left
byte draw_counter
//flags for drawing
bool changed_column
bool changed_meta_column
bool changed_attr_column
bool changed_screen
//How many pixels before a new attribute table column
//is needed should we load them? This is necessary because loading
//an attribute column and a tile column at the same time takes too
//much time for the nmi.
//Note: this should be changed to be greater than or equal to the max
//x velocity of the player
const byte attr_draw_offset = 8
//variables for debugging, find these
//in fceux's hex editor and watch them
byte _current_tile //the current tile player position is touching
void main() {
init_sprites()
load_palletes()
clear_status_bar()
draw_status_bar()
prepare_sprite0()
xscroll = 0
nametable = 0
draw_seam_column = 0
draw_counter = 0
previous_scroll = 0
current_screen = 0
playerx = 10
playery = $60
playerx_speed = 8
scroll_speed = 2
playerx_accel = 0
playery_accel = 0
scrolling_right = false
scrolling_left = false
collis_flag = false
down_collis_flag = false
up_collis_flag = false
right_collis_flag = false
left_collis_flag = false
changed_column = false
changed_meta_column = false
changed_attr_column = false
changed_screen = false
on_ground = false
jumping = false
can_jump = true
//screen 1 + screen 2 leftmost column
draw_full_screen(0,0)
draw_column(0,1,16)
load_attr_column(0,0,0)
load_attr_column(1,0,1)
load_attr_column(2,0,2)
load_attr_column(3,0,3)
load_attr_column(4,0,4)
load_attr_column(5,0,5)
load_attr_column(6,0,6)
load_attr_column(7,0,7)
load_attr_column(0,1,8)
ppu_set_scroll(0,0)
ppu_wait_vblank()
ppu_ctrl = %10010000 // enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
ppu_mask = %00011110 // enable sprites, enable background, no clipping on left side
while(true) {}
}
void nmi() {
ppu_oam_dma_write(oam_buffer.addr.hi)
//either scrolling right or have a column to finish drawing
if scrolling_right || (previous_scroll == 0 && draw_counter > 0) {
//update the scroll variable
if scrolling_right{
update_scroll_right()
}
right_metatile_scroll()
previous_scroll = 0
}
//either scrolling left or have a column to finish drawing
else if scrolling_left || (previous_scroll == 1 && draw_counter > 0) {
//left scroll logic
if scrolling_left {
update_scroll_left()
}
left_metatile_scroll()
previous_scroll = 1
}
//set scroll to 0 for now so that the status bar
//stays still
ppu_set_scroll(0,0)
ppu_ctrl = %10010000 // no need to or the nametable for this one,
// as the status bar should always be at the first nametable
ppu_mask = %00011100
//now we start checking for a sprite0 hit
//so that we know the status bar is done drawing
//first wait for the previous frame's sprite0 hit
//to clear
while (ppu_status & %01000000) != 0 {
}
//then wait for this frame's sprite0 hit
while (ppu_status & %01000000) == 0 {
}
//now wait for the end of the scanline
wait_cycles($10)
//we have now reached the end of the scanline, and the
//status bar is done drawing, so draw the rest of
//the screen with the correct scroll values
ppu_set_scroll(xscroll,0)
ppu_ctrl = %10010000 | nametable
ppu_mask = %00011100
game_logic()
}
inline void game_logic() {
//game logic goes here
read_joy1()
if input_b != 0 {
playerx_speed = 7
}
else {
playerx_speed = 5
}
if input_dx > 0 {
// scrolling_right = true
// scrolling_left = false
if playerx < $F0 {
if (playerx_accel >> 2) < playerx_speed {
playerx_accel += 1
}
else {
playerx_accel = playerx_speed << 2
}
// playerxvel += playerx_speed
playerxvel += (playerx_accel >> 2)
}
}
else if input_dx < 0 {
// scrolling_right = false
// scrolling_left = true
if playerx > $08 {
if (playerx_accel >> 2) < playerx_speed {
playerx_accel += 1
}
else {
playerx_accel = playerx_speed << 2
}
// playerxvel -= playerx_speed
playerxvel -= (playerx_accel >> 2)
}
}
else {
scrolling_left = false
scrolling_right = false
playerx_accel = 0
}
if input_a != 0 {
if can_jump {
playeryvel = 0
playery_accel = 8
jumping = true
can_jump = false
}
if playery_accel < 24 && jumping {
playery_accel += 2
playeryvel -= (playery_accel >> 2)
}
else {
playery_accel = player_gravity_accel
playeryvel += playery_accel
jumping = false
}
}
else {
// scrolling_right = false
// scrolling_left = true
playery_accel = player_gravity_accel
playeryvel += playery_accel
jumping = false
if on_ground {
can_jump = true
}
}
//cap velocity
if playerxvel >= player_max_xvel {
playerxvel = player_max_xvel
playerx_frac = 0
}
if playerxvel <= player_max_xvel_neg {
playerxvel = player_max_xvel_neg
playerx_frac = 0
}
if playeryvel >= player_max_yvel {
playeryvel = player_max_yvel
playery_frac = 0
}
if playeryvel <= player_max_yvel_neg {
playeryvel = player_max_yvel_neg
playery_frac = 0
}
check_player_collis_and_update_player_loc()
//apply friction
if playerxvel < 0 {
playerxvel += player_x_friction
if playerxvel > 0 {
playerxvel = 0
}
}
else if playerxvel > 0 {
playerxvel -= player_x_friction
if playerxvel < 0 {
playerxvel = 0
}
}
if playerx > $78 && not(current_screen == 3 && xscroll == 0) {
// playerx = $78
//update the position of the screen sprites
scroll_speed = playerx - $78
//fix scroll_speed so that we never overshoot the right scroll limit
if current_screen == 2 && scroll_speed + xscroll < xscroll {
scroll_speed = ($FF - xscroll) + 1
}
playerx -= scroll_speed
//scroll the screen as well
scrolling_right = true
scrolling_left = false
}
else if playerx < $78 && not(current_screen == 0 && xscroll == 0) {
// playerx = $78
//update the position of the screen sprites
scroll_speed = $78 - playerx
if current_screen == 0 && xscroll - scroll_speed > xscroll {
scroll_speed = xscroll
}
//set screen scroll left limit
playerx += scroll_speed
//scroll the screen as well
scrolling_right = false
scrolling_left = true
}
else {
scrolling_left = false
scrolling_right = false
}
snap_player_pos_limits()
update_player_sprites()
}
void irq() {
}
void check_player_collis_and_update_player_loc() {
sbyte temp_vel
byte old_playerx
byte old_playerx_frac
byte old_playery
byte old_playery_frac
old_playerx = playerx
old_playery = playery
old_playerx_frac = playerx_frac
old_playery_frac = playery_frac
//do vertical collision checking
//if we're going too fast, do the collision checking in two steps
//to ensure we stick to the ground
update_player_y_speed()
check_background_collis_vert(playerx, playery, player_width, player_height)
on_ground = down_collis_flag
if collis_flag{
// playery = old_playery
// playery_frac = old_playery_frac
playeryvel = 0
}
//if the speed was great enough to possibly have skipped over a
//landing point, then stick them to the ground
if down_collis_flag {
playery_frac = 0
do {
collis_flag = false
playery -= 1
check_background_collis_vert(playerx, playery, player_width, player_height)
} while collis_flag
}
//do the same for a negative velocities
else if up_collis_flag {
playery_frac = 0
do {
collis_flag = false
playery += 1
check_background_collis_vert(playerx, playery, player_width, player_height)
}while collis_flag
}
collis_flag = false
//do horizontal collision checking
update_player_x_speed()
check_background_collis_horiz(playerx, playery, player_width, player_height)
if collis_flag {
// playerx = old_playerx
// playerx_frac = old_playerx_frac
playerxvel = 0
}
//if the speed was great enough to possibly have skipped over a
//landing point, then stick them to the wall
if right_collis_flag {
playerx_frac = 0
do {
collis_flag = false
playerx -= 1
check_background_collis_vert(playerx, playery, player_width, player_height)
} while collis_flag
}
//do the same for a negative velocities
else if left_collis_flag {
playerx_frac = 0
do {
collis_flag = false
playerx += 1
check_background_collis_vert(playerx, playery, player_width, player_height)
}while collis_flag
}
reset_collis_flags()
}
inline void snap_player_pos_limits() {
//snap the player back to the playable area of the screen
if playerx > $F0 {
playerx = $F0
}
if playerx < $08 {
playerx = $08
}
if playery > $D0 {
playery = $D0
on_ground = true
}
if playery < $20 {
playery = $20
}
}
inline void reset_collis_flags() {
collis_flag = false
up_collis_flag = false
down_collis_flag = false
left_collis_flag = false
right_collis_flag = false
}
byte get_column_from_screenx(byte screenx) {
byte new_column
word new_column_offset //must be a word because (xscroll+newplayerx) can overflow
new_column = current_screen << 5 //leftmost column of current screen
new_column_offset = xscroll
new_column_offset += screenx
new_column_offset >>= 3
new_column += new_column_offset.lo
if new_column >= $80 {
new_column -= $80
}
return new_column
}
inline byte get_row_from_screeny(byte screeny) {
return screeny >> 3
}
macro void check_background_collis_horiz(byte screenx, byte screeny, byte spritewidth, byte spriteheight) {
check_background_collis(screenx, screeny, spritewidth, spriteheight, false)
}
macro void check_background_collis_vert(byte screenx, byte screeny, byte spritewidth, byte spriteheight) {
check_background_collis(screenx, screeny, spritewidth, spriteheight, true)
}
void check_background_collis(byte screenx, byte screeny, byte spritewidth, byte spriteheight, bool vert) {
//check if the bounding box is colliding with collidable background tiles
//on its left and right edges
byte new_column1
byte new_column2
byte new_row1
byte new_row2
byte i
byte limit
//do the collision stuff, set the left and right
//flags depending on left/right or up/down collisions
//assume that any tile that is not $24 (sky tile) is collidable
if vert {
//DOWN SIDE
new_row1 = get_row_from_screeny(screeny+spriteheight)
new_column1 = get_column_from_screenx(screenx)
new_column2 = get_column_from_screenx(screenx+spritewidth)
limit = new_column2-new_column1
i = 0
while i <= limit{
_current_tile = get_tile(new_column1+i, new_row1)
if _current_tile != $24 {
collis_flag = true
down_collis_flag = true
return
}
i += 1
}
//UP SIDE
new_row1 = get_row_from_screeny(screeny)
new_column1 = get_column_from_screenx(screenx)
new_column2 = get_column_from_screenx(screenx+spritewidth)
limit = new_column2-new_column1
i = 0
while i <= limit{
_current_tile = get_tile(new_column1+i, new_row1)
if _current_tile != $24 {
collis_flag = true
up_collis_flag = true
return
}
i += 1
}
}
else {
//it is expected that the vertical
//collision checks are done first
//RIGHT SIDE
new_column1 = get_column_from_screenx(screenx+spritewidth)
new_row1 = get_row_from_screeny(screeny)
new_row2 = get_row_from_screeny(screeny+spriteheight)
limit = new_row2-new_row1
i = 0
while i <= limit{
_current_tile = get_tile(new_column1, new_row1+i)
if _current_tile != $24 {
collis_flag = true
right_collis_flag = true
return
}
i += 1
}
//LEFT SIDE
new_column1 = get_column_from_screenx(screenx)
new_row1 = get_row_from_screeny(screeny)
new_row2 = get_row_from_screeny(screeny+spriteheight)
limit = new_row2-new_row1
i = 0
while i <= limit{
_current_tile = get_tile(new_column1, new_row1+i)
if _current_tile != $24 {
collis_flag = true
left_collis_flag = true
return
}
i += 1
}
}
}
inline void update_player_x_speed() {
if playerxvel < 0{
playerxvel = flip_sign(playerxvel) //playerxvel *= -1
playerx -= playerxvel >> 2
playerx_frac -= playerxvel & %00000011
if playerx_frac > 3 {
playerx_frac += 4 // 4 because 4 + -1 = 3, 4 + -2 = 2, 4 + -3 = 1, 4 + -4 = 0
// so basically, (max fractional part value) + 1
playerx -= 1
}
playerxvel = flip_sign(playerxvel) //playerxvel *= -1
}
else {
playerx += playerxvel >> 2
playerx_frac += playerxvel & %00000011
if playerx_frac > 3 {
playerx_frac -= 3
playerx += 1
}
}
}
void update_player_y_speed() {
//BUG: compiler bugs out when -O4 is set and
// you check yvel's sign by using this:
// if yvel < 0 {
// instead of what is here
if playeryvel < 0{
playeryvel = flip_sign(playeryvel)
playery -= playeryvel >> 2
playery_frac -= playeryvel & %00000011
if playery_frac > 3 {
playery_frac += 4 // 4 because 4 + -1 = 3, 4 + -2 = 2, 4 + -3 = 1, 4 + -4 = 0
playery -= 1
}
playeryvel = flip_sign(playeryvel) // yvel *= -1
}
else {
playery += playeryvel >> 2
playery_frac += playeryvel & %00000011
if playery_frac > 3 {
playery_frac -= 3
playery += 1
}
}
}
inline void update_scroll_left() {
prev_xscroll = xscroll
xscroll -= scroll_speed
passed_column_boundary(xscroll, prev_xscroll)
passed_meta_column_boundary(xscroll, prev_xscroll)
if xscroll > prev_xscroll {
//we overflowed to a new screen
current_screen -= 1
if current_screen >= 4 {
current_screen = 3
}
nametable ^= %00000001
}
if changed_column {
draw_seam_column = current_screen << 5
draw_seam_column += xscroll >> 3
}
//deal with the attribute column offset
xscroll -= attr_draw_offset
prev_xscroll -= attr_draw_offset
if xscroll > prev_xscroll {
changed_screen = true
draw_nametable = nametable ^ %00000001
}
else {
changed_screen = false
draw_nametable = nametable
}
passed_attr_column_boundary(xscroll, prev_xscroll)
xscroll += attr_draw_offset
prev_xscroll += attr_draw_offset
}
inline void left_metatile_scroll() {
if draw_counter == 0 && changed_attr_column{
// about to enter a 32 pixel boundary, so
// draw the attributes
next_column = draw_seam_column
if changed_screen {
//we've overflowed onto a new screen, so draw on the right nametable
load_attr_column($07,draw_nametable,(next_column>>2) - 1)
}
else {
load_attr_column((xscroll>>5) - 1,draw_nametable,(next_column>>2) - 1)
//we subtract 1 from the 1st and 3rd arguments because we're loading
//in attribute tables 1 step ahead in order to keep up with scrolling
}
}
else if changed_column || draw_counter > 0 {
// on a 16 pixel boundary, so
// load the appropriate tiles
next_column = draw_seam_column
draw_column_with_counter((xscroll>>4),draw_nametable,(next_column>>1))
}
}
inline void update_scroll_right() {
prev_xscroll = xscroll
xscroll += scroll_speed
passed_column_boundary(xscroll, prev_xscroll)
passed_meta_column_boundary(xscroll, prev_xscroll)
if xscroll < prev_xscroll {
//we overflowed to a new screen
current_screen += 1
if current_screen >= 4 {
current_screen = 0
}
nametable ^= %00000001
changed_screen = true
}
else {
changed_screen = false
}
if changed_column {
//we've passed a column boundary, update our current column
draw_seam_column = current_screen << 5
draw_seam_column += xscroll >> 3
}
//deal with the attribute column offset
xscroll += attr_draw_offset
prev_xscroll += attr_draw_offset
if xscroll < prev_xscroll {
changed_screen = true
draw_nametable = nametable
}
else {
changed_screen = false
draw_nametable = nametable ^ %00000001
}
passed_attr_column_boundary(xscroll, prev_xscroll)
xscroll -= attr_draw_offset
prev_xscroll -= attr_draw_offset
}
void passed_column_boundary(byte xscroll_new, byte xscroll_old) {
//compares the two, and then sets changed_column appropriately
if (xscroll_new & %11111000) != (xscroll_old & %11111000) {
changed_column = true
}
else {
changed_column = false
}
}
void passed_meta_column_boundary(byte xscroll_new, byte xscroll_old) {
//compares the two, and then sets changed_meta_column appropriately
if (xscroll_new & %11110000) != (xscroll_old & %11110000) {
changed_meta_column = true
}
else {
changed_meta_column = false
}
}
void passed_attr_column_boundary(byte xscroll_new, byte xscroll_old) {
//compares the two, and then sets changed_meta_column appropriately
if (xscroll_new & %11100000) != (xscroll_old & %11100000) {
changed_attr_column = true
}
else {
changed_attr_column = false
}
}
inline void right_metatile_scroll() {
// use an else-if here
// so that we don't load attrs and tiles
// on the same frame, as that would take
// too much time and bleed into screen rendering
if draw_counter == 0 && changed_attr_column {
// about to enter a 32 pixel boundary, so
// draw the attributes
next_column = draw_seam_column + $20
if next_column >= $80 {
// if we're on the final screen, then wrap around to the beginning
next_column -= $80
}
if (xscroll+attr_draw_offset) < (prev_xscroll+attr_draw_offset) {
//we've overflowed onto a new screen, so draw on the right nametable
load_attr_column((xscroll+attr_draw_offset)>>5,draw_nametable,(next_column>>2) + 1)
}
else {
load_attr_column((xscroll+attr_draw_offset)>>5,draw_nametable,(next_column>>2) + 1)
//add 1 to 3rd argument because we are loading the column that is one ahead
//of our loading seam
}
}
else if changed_meta_column || draw_counter > 0 {
// on a 16 pixel boundary, so
// load the appropriate tiles
next_column = draw_seam_column + $20
if next_column >= $80 {
// if we're on the final screen, then wrap around to the beginning
next_column -= $80
}
draw_column_with_counter(xscroll>>4,draw_nametable,next_column>>1)
}
}
void draw_status_bar() {
//draw the status bar accross both nametables
byte i
//set ppu address increment to 1 so we can draw the left and right borders
//(allows us to draw to the nametable in vertical strips rather than horizontal)
ppu_ctrl = %00000000
read_ppu_status()
//write to first nametable
ppu_set_addr(ppu_nametable0+$40)
ppu_write_data($24) //$24 = sky tile
ppu_write_data($11) //$11 = 'H'
ppu_write_data($0E) //$0E = 'E'
ppu_write_data($0A) //$0A = 'A'
ppu_write_data($15) //$15 = 'L'
ppu_write_data($1D) //$1D = 'T'
ppu_write_data($11) //$11 = 'H'
ppu_write_data($25) //$25 = colon
ppu_write_data($34) //$34 = full box
ppu_write_data($34) //$34 = full box
ppu_write_data($35) //$35 = empty box
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($26) //$26 = lives icon
ppu_write_data($27) //$26 = multiply icon
ppu_write_data($03) //$03 = 3
//write to second nametable
ppu_set_addr(ppu_nametable1+$40)
ppu_write_data($24) //$24 = sky tile
ppu_write_data($11) //$11 = 'H'
ppu_write_data($0E) //$0E = 'E'
ppu_write_data($0A) //$0A = 'A'
ppu_write_data($15) //$15 = 'L'
ppu_write_data($1D) //$1D = 'T'
ppu_write_data($11) //$11 = 'H'
ppu_write_data($25) //$25 = colon
ppu_write_data($34) //$34 = full box
ppu_write_data($34) //$34 = full box
ppu_write_data($35) //$35 = empty box
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($24) //$24 = sky tile
ppu_write_data($26) //$26 = lives icon
ppu_write_data($27) //$26 = multiply icon
ppu_write_data($03) //$03 = 3
}
void clear_status_bar() {
//clear the status bar to all sky tiles
byte i
//set ppu address increment to 1 so we can draw the left and right borders
//(allows us to draw to the nametable in vertical strips rather than horizontal)
ppu_ctrl = %00000000
read_ppu_status()
ppu_set_addr(ppu_nametable0)
for i,0,until,$80 {
ppu_write_data($24) //$24 = sky tile
}
ppu_set_addr(ppu_nametable1)
for i,0,until,$80 {
ppu_write_data($24) //$24 = sky tile
}
}
void load_attr_column(byte screen_column, byte nametable, byte attrs_column) {
byte i
pointer attrs_ptr
attrs_ptr = get_attrs_column(attrs_column)
//set ppu address increment to 1 so we can draw the left and right borders
//(allows us to draw to the nametable in vertical strips rather than horizontal)
ppu_ctrl = %00000000
read_ppu_status()
for i,1,until,$08 {
if nametable == 0 {
ppu_set_addr(ppu_attrtable0 + (i<<3) + screen_column)
}
else if nametable == 1 {
ppu_set_addr(ppu_attrtable1 + (i<<3) + screen_column)
}
else if nametable == 2 {
ppu_set_addr(ppu_attrtable2 + (i<<3) + screen_column)
}
else if nametable == 3 {
ppu_set_addr(ppu_attrtable3 + (i<<3) + screen_column)
}
ppu_write_data(attrs_ptr[i])
}
}
void load_attr_column_with_counter(byte screen_column, byte nametable, byte attrs_column) {
pointer attrs_ptr
attrs_ptr = get_attrs_column(attrs_column)
//set ppu address increment to 1 so we can draw the left and right borders
//(allows us to draw to the nametable in vertical strips rather than horizontal)
ppu_ctrl = %00000000
read_ppu_status()
if draw_counter == 0 || draw_counter == 3 {
load_attr_first_draw(nametable, screen_column, attrs_ptr)
}
else if draw_counter == 4 {
load_attr_second_draw(nametable, screen_column, attrs_ptr)
}
}
inline void load_attr_first_draw(byte nametable, byte screen_column, pointer attrs_ptr) {
byte i
for i,1,until,$05 {
if nametable == 0 {
ppu_set_addr(ppu_attrtable0 + (i<<3) + screen_column)
}
else if nametable == 1 {
ppu_set_addr(ppu_attrtable1 + (i<<3) + screen_column)
}
else if nametable == 2 {
ppu_set_addr(ppu_attrtable2 + (i<<3) + screen_column)
}
else if nametable == 3 {
ppu_set_addr(ppu_attrtable3 + (i<<3) + screen_column)
}
ppu_write_data(attrs_ptr[i])
}
draw_counter = 4
}