-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdrawLine.s
664 lines (566 loc) · 7.09 KB
/
drawLine.s
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
.setcpu "6502"
.autoimport on
.import addysp
.importzp sp
.export _automap_draw
.export _drawLine
.export _automap_sawEdge
.export _automap_resetEdges
.export _automap_setEdges
.import _getSinOf
.import _getCosOf
buffer = $BE00
.segment "LOWCODE"
minx:
.word 0
maxx:
.word 0
miny:
.word 0
maxy:
.word 0
player_x:
.word 0
player_y:
.word 0
player_a:
.byte 0
sina:
.word 0
cosa:
.word 0
zoom:
.byte 0
shift:
.byte 128, 64, 32, 16, 8, 4, 2, 1
edgesSeen:
.res 32, 0
_automap_resetEdges:
ldx #31
lda #0
:
sta edgesSeen,x
dex
bpl :-
rts
_automap_setEdges:
ldx #31
lda #$FF
:
sta edgesSeen,x
dex
bpl :-
rts
; zero page usage!
x0 = $50
y0 = $52
x1 = $54
y1 = $56
dx = $58
dy = $59
sx = $5a
sy = $5c
ndxp1 = $5e
err = $5f
addr = $60
chk = $62
drewAPixel = $63
.macro set16 q, p
lda p
sta q
lda p+1
sta q+1
.endmacro
.macro asr_a
cmp #$80
ror
.endmacro
.macro asr_a_xtimes
: cmp #$80
ror
dex
bne :-
.endmacro
.macro neg_a
eor #$ff
clc
adc #1
.endmacro
.macro add16 p, q
lda p
clc
adc q
sta p
lda p+1
adc q+1
sta p+1
.endmacro
.macro sub16 p, q
lda p
sec
sbc q
sta p
lda p+1
sbc q+1
sta p+1
.endmacro
.macro add16_imm8 p, q
lda p
clc
adc #q
sta p
bcc :+
inc p+1
:
.endmacro
.macro mul16_by2 p
asl p
rol p+1
.endmacro
.macro sign_extend p
ldy #0
lda p
bpl :+
dey
:
sty p+1
.endmacro
.macro load_char_from_stack p
lda (sp),y
sta p
iny
.endmacro
.macro load_int_from_stack p
lda (sp),y
sta p
iny
lda (sp),y
sta p+1
iny
.endmacro
drawLine:
; signed char err, e2;
; char drewAPixel = 0;
; int maxx = x1;
; int maxy = y1;
; int minx = x0;
; int miny = y0;
; signed char sx = 1;
; signed char sy = 1;
; signed char dx = x1-x0;
; signed char dy = y1-y0;
; if (dx < 0)
; {
; sx = -1;
; dx = -dx;
; maxx = x0;
; minx = x1;
; }
; if (dy < 0)
; {
; sy = -1;
; dy = -dy;
; maxy = y0;
; miny = y1;
; }
; if (maxx >= 0 && minx < 64 && maxy >= 0 && miny < 64)
; {
lda #0
sta drewAPixel
sta sx+1
sta sy+1
lda x1
sec
sbc x0
sta dx
bpl @dxpositive
; dxnegative
neg_a
sta dx
lda #-1
sta sx
sta sx+1
set16 maxx, x0
set16 minx, x1
jmp @checkxbbox
@dxpositive:
lda #1
sta sx
set16 maxx, x1
set16 minx, x0
@checkxbbox:
lda maxx+1
bmi @end1
lda minx+1
bmi @checky
cmp #1
bpl @end1
lda minx
and #$c0
beq @checky
@end1:
rts
@checky:
lda y1
sec
sbc y0
sta dy
bpl @dypositive
; dynegative
neg_a
sta dy
lda #-1
sta sy
sta sy+1
set16 maxy, y0
set16 miny, y1
jmp @checkybbox
@dypositive:
lda #1
sta sy
set16 maxy, y1
set16 miny, y0
@checkybbox:
lda maxy+1
bmi @end2
lda miny+1
bmi @checkspassed
cmp #1
bpl @end2
lda miny
and #$c0
beq @checkspassed
@end2:
rts
@checkspassed:
lda dx
neg_a
clc
adc #1
sta ndxp1
; err = (dx > dy) ? dx>>1 : -(dy>>1);
lda dx
cmp dy
bmi @dygreater
asr_a
sta err
jmp loop
@dygreater:
lda dy
neg_a
asr_a
sta err
loop:
; signed char chk = (x0 & 0xc0) | (y0 & 0xc0);
; if (!chk)
lda x0
and #$c0
sta chk
lda y0
and #$c0
ora chk
beq @drawAPixel
lda drewAPixel
beq @checkForEnd
rts
; int a = buffer + ((x0&0xf8)<<3) + y0;
; POKE(a, PEEK(a) | shift[x0&7]);
; drewAPixel = 1;
@drawAPixel:
lda x0
tax
and #$38
asl
asl
asl
sta addr
lda #>buffer
adc #0
sta addr+1
txa
and #$7
tax
ldy y0
lda (addr),y
ora shift,x
sta (addr),y
lda #1
sta drewAPixel
@checkForEnd:
; if (x0 == x1 && y0 == y1) return;
lda x0
cmp x1
bne @updateXandY
lda y0
cmp y1
bne @updateXandY
lda x0+1
cmp x1+1
bne @updateXandY
lda y0+1
cmp y1+1
bne @updateXandY
rts
@updateXandY:
; e2 = err;
; if (e2 > -dx)
; {
; err -= dy;
; x0 += sx;
; }
; if (e2 < dy)
; {
; err += dx;
; y0 += sy;
; }
lda err
tax
cmp ndxp1
bmi noxinc
sec
sbc dy
sta err
add16 x0, sx
noxinc:
txa
cmp dy
bpl loop
lda err
clc
adc dx
sta err
add16 y0, sy
jmp loop
numSectors:
.byte 0
sectorIndex:
.byte 0
numVerts:
.byte 0
edgeIndex:
.byte 0
globalEdgeIndex:
.byte 0
secondVertexIndex:
.byte 0
offsetX:
.word 0
offsetY:
.word 0
_automap_sawEdge:
tay
and #7
tax
tya
lsr
lsr
lsr
tay
lda edgesSeen,y
ora shift,x
sta edgesSeen,y
rts
offsetAndScaleVert0:
sign_extend x0
sign_extend y0
add16 x0, offsetX
lda offsetY
sec
sbc y0
sta y0
lda offsetY+1
sbc y0+1
sta y0+1
lda zoom
cmp #2
bne :+
mul16_by2 x0
mul16_by2 y0
:
add16_imm8 x0,32
add16_imm8 y0,32
rts
offsetAndScaleVert1:
sign_extend x1
sign_extend y1
add16 x1, offsetX
lda offsetY
sec
sbc y1
sta y1
lda offsetY+1
sbc y1+1
sta y1+1
lda zoom
cmp #2
bne :+
mul16_by2 x1
mul16_by2 y1
:
add16_imm8 x1,32
add16_imm8 y1,32
rts
offsetAndScaleVerts:
jsr offsetAndScaleVert0
jmp offsetAndScaleVert1
automap_drawSector:
sta sectorIndex
jsr _getNumVerts
sta numVerts
lda #0
sta edgeIndex
@loop:
lda edgeIndex
ldx sectorIndex
jsr getEdgeIndex
ldx sectorIndex
jsr getOtherSector
cmp #0
bmi :+
jmp @next
:
lda edgeIndex
ldx sectorIndex
jsr getEdgeIndex
tay
and #7
tax
tya
lsr
lsr
lsr
tay
lda edgesSeen,y
and shift,x
bne :+
jmp @next
:
ldx edgeIndex
inx
cpx numVerts
bne @skip
ldx #0
@skip:
stx secondVertexIndex
lda edgeIndex
ldx sectorIndex
jsr getSectorVertexXY
stx x0
sty y0
lda secondVertexIndex
ldx sectorIndex
jsr getSectorVertexXY
stx x1
sty y1
jsr offsetAndScaleVerts
jsr drawLine
@next:
inc edgeIndex
ldx edgeIndex
cpx numVerts
beq :+
jmp @loop
:
rts
drawPlayer:
lda player_a
jsr _getSinOf
ldx #5
asr_a_xtimes
sta sina
sign_extend sina
lda player_a
jsr _getCosOf
ldx #5
asr_a_xtimes
sta cosa
sign_extend cosa
lda player_x
sta x0
sta x1
lda player_y
sta y0
sta y1
jsr offsetAndScaleVerts
add16 x1, sina
sub16 y1, cosa
sub16 x0, sina
add16 y0, cosa
jsr drawLine
;lda sina
;asr_a
;sta sina
sign_extend sina
;lda cosa
;asr_a
;sta cosa
sign_extend cosa
lda player_x
sta x0
lda player_y
sta y0
jsr offsetAndScaleVert0
sub16 x0, cosa
sub16 y0, sina
jsr drawLine
lda player_x
sta x0
lda player_y
sta y0
jsr offsetAndScaleVert0
add16 x0, cosa
add16 y0, sina
jsr drawLine
rts
_automap_draw:
sta player_a
ldy #0
load_char_from_stack player_y
load_char_from_stack player_x
load_char_from_stack zoom
load_int_from_stack offsetY
load_int_from_stack offsetX
sign_extend player_x
sign_extend player_y
sub16 offsetX, player_x
add16 offsetY, player_y
; draw player
; a=0 is pointing right up
jsr drawPlayer
jsr _getNumSectors
sta numSectors
lda #0
sta sectorIndex
@loop:
jsr automap_drawSector
inc sectorIndex
lda sectorIndex
cmp numSectors
bne @loop
@end:
ldy #7
jmp addysp
_drawLine:
sta y1
stx y1+1
ldy #0
lda (sp),y
sta x1
iny
lda (sp),y
sta x1+1
iny
lda (sp),y
sta y0
iny
lda (sp),y
sta y0+1
iny
lda (sp),y
sta x0
iny
lda (sp),y
sta x0+1
jsr drawLine
ldy #6
jmp addysp