-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVGA256.PAS
1854 lines (1641 loc) · 49.3 KB
/
VGA256.PAS
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
Unit VGA256;
{$R-}
INTERFACE
uses chardef,ttypes,gbasics,tmaths;
const
{VGA register locations}
MISC_ADDR=$3C2; MISC_READ_ADDR=$3CC;
ATTR_ADDR=$3c0; ATTR_DATA_ADDR=$3c1;
SEQU_ADDR=$3c4; SEQU_DATA_ADDR=$3c5;
CRTC_ADDR=$3D4; CRTC_DATA_ADDR=$3d5;
GRPH_ADDR=$3CE; GRPH_DATA_ADDR=$3CF;
STATUS_ADDR=$3da;
function set_mode(mode:byte):boolean;
Procedure bar(x1,y,x2,y2:integer);
Procedure GTriangle(xa,ya,xb,yb,xc,yc,ia,ib,ic:integer);
Procedure Triangle(xa,ya,xb,yb,xc,yc:integer);
Procedure Line(x1,y1,x2,y2:integer);
Procedure Hline(x1,x2,y:Integer);
Procedure Vline(x,y1,y2:Integer);
Procedure putpixel(x1,y1:Integer);
Function getpixel(x1,y1:Integer):byte;
Procedure outtextxy(x1,y1:integer;txt:string);
Procedure outtextxy_length(x1,y1:integer;txt:pchar;length:byte);
Procedure Drawbytes(x1,y1:Integer;pic:bytearray;nbytes:byte);
Procedure Drawbytesxy(x1,y1:Integer;pic:bytearray;xbytes,ybytes:byte);
Procedure Screencopy(x1,y1,x2,y2,xd,yd:integer;page1,page2:byte);
procedure putbitmap(x1,y1:integer;bitmap:bitmaptypeptr);
Procedure putbitmapmask(x1,y1:integer;mask:byte;bitmap:bitmaptypeptr);
Procedure putbitmaptintshade(x1,y1:integer;rangestart,rangesize:byte;bitmap:bitmaptypeptr);
Procedure putbitmaptintshademask(x1,y1:integer;rangestart,rangesize:byte;bitmap:bitmaptypeptr);
Procedure putbitmapshade(x1,y1:integer;rangesize:byte;bitmap:bitmaptypeptr);
procedure scanedge(x1,y1,x2,y2:integer;offset:byte);
function getbitmap(x1,y1,x2,y2:integer):bitmaptypeptr;
procedure drawhlines(ytop,ybottom:integer);
procedure ConvexPolygon(points:pointtypearray;numpoints:integer);
Procedure cleardevice;
procedure savescreenregs;
procedure restorescreenregs;
{quick generic interface to screen -good for file loading}
Procedure QSetXY(x,y:integer);
Procedure Qwrite(count:word);
function Qget:byte;
type dword=longint;
IMPLEMENTATION
procedure savescreenregs;
begin
end;
procedure restorescreenregs;
begin
end;
function set_mode(mode:byte):boolean;
begin
{set up the video mode}
if mode=$13 then asm
xor ax,ax
mov al,mode {mode}
int 10h {call screen interrupt}
end else asm
mov ax,4F02h
mov bh,64
mov bl,mode
int 10h
mov AX,800h
int 31h
end;
set_mode:=true;
end;
(*function set_mode(mode:byte):boolean;
var installed:boolean;
begin
installed:=true;
with SC.size do
case mode of
vga320x200x256:begin x:=320;y:=200;end;
else installed:=false;
end;
if installed then begin
{set up the video mode}
asm
mov al,mode; {mode}
mov ah,0; {function 0}
int 10h {call screen interrupt}
end;
ColourDepth:=256;
SC.offsw:=SC.size.x;
end;
set_mode:=installed;
end;*)
Procedure cleardevice;assembler;
Asm
les di,SC.scrptr
mov al,t_fillcol
mov ah,t_fillcol
mov cx,word ptr SC.Pagesize
shr cx,1
cld
rep stosw
End;
const
base:byte=0;
var
HlineList:array[0..319] of hlinetype;
deltax,px:longint;
procedure scanedge(x1,y1,x2,y2:integer;offset:byte);
var
chval:integerptr;
const
multby=1 shl 16;
hlinesize=sizeof(hlinetype);
begin
{slow due to 16 bit stuff; needs multiplies, 32bit shift would be good}
px:=x1 * multby;
dec(y2);
if y2<=y1 then exit;
deltax:=((x2 * multby)-px) div (y2-y1);
chval:=@HLineList[y1];
inc(chval,offset);
asm
{create counter}
mov cx,y2
sub cx,y1
{setup gradient fixed point longword in bx/dx}
mov bx,word ptr deltax
mov dx,word ptr deltax+2
{put current fixed point x position in si/ax}
mov si,word ptr px
mov ax,word ptr px+2
les di,chval {points to current hline value}
mov es:[di],ax {grab the integer bit and shove into memory}
@yloop:
add di,hlinesize {needs to move along by sizeof hlinetype bytes}
add si,bx {add fixed point gradiant longword }
adc ax,dx
mov es:[di],ax {grab the integer bit and shove into memory}
dec cx {next itteration}
jnz @yloop
end;
end;
procedure drawhlines(ytop,ybottom:integer);
var
chline:^hlinetype;
ylop:integer;
begin
chline:=@Hlinelist[ytop];
for ylop:=ytop to ybottom-1 do begin
hline(chline^.x1,chline^.x2,ylop);
inc(chline);
end;
end;
procedure ConvexPolygon(points:pointtypearray;numpoints:integer);
var top_y,bottom_y,top_idx,l_idx_start,r_idx_start:integer;
i:integer;
dir:shortint;
cpoint,nxtpoint:pointtypeptr;
ignorefirst:boolean; {if the top left and right values have the same x position, ignore}
function IDX_FWRD(var index:integer):integer;
begin
IDX_FWRD:=(index+1) mod numpoints;
end;
function IDX_BAKWRD(var index:integer):integer;
begin
IDX_BAKWRD:=(index-1+numpoints) mod numpoints;
end;
function IDX_MOVE(var index:integer;dir:byte):integer;
begin
if (dir>0) then
IDX_MOVE:=(index-1+numpoints) mod numpoints
else
IDX_MOVE:=(index-1+numpoints) mod numpoints;
end;
begin
if numpoints<3 then exit;
top_y:=32767;
bottom_y:=-32767;
cpoint:=pointtypeptr(points);
{determine the top and bottom of the list}
for i:=0 to numpoints-1 do begin
if cpoint^.y<top_y then begin
top_y:=cpoint^.y;
top_idx:=i;
end;
if cpoint^.y>bottom_y then bottom_y:=cpoint^.y;
inc(cpoint);
end;
if top_y=bottom_y then exit;
{figure out starting positions for left and right sides of the list}
i:=top_idx;
cpoint:=@points^[top_idx];
while points^[i].x=cpoint^.x do begin
i:=idx_bakwrd(i);
end;
i:=idx_fwrd(i);
l_idx_start:=i;
i:=top_idx;
while points^[i].x=cpoint^.x do begin
i:=idx_fwrd(i);
end;
i:=idx_bakwrd(i);
r_idx_start:=i;
i:=idx_fwrd(i);
if points^[i].x<points^[l_idx_start].x then
fswap(r_idx_start,l_idx_start,sizeof(integer));
i:=R_idx_start;
cpoint:=@pointtypearray(points)^[r_idx_start];
nxtpoint:=cpoint;
while (nxtpoint^.y<>bottom_y) do begin
cpoint:=@pointtypearray(points)^[i];
i:=idx_fwrd(i);
nxtpoint:=@pointtypearray(points)^[i];
scanedge(cpoint^.x,cpoint^.y,nxtpoint^.x,nxtpoint^.y,1);
end;
i:=L_idx_start;
cpoint:=@pointtypearray(points)^[L_idx_start];
nxtpoint:=cpoint;
while (nxtpoint^.y<>bottom_y) do begin
cpoint:=@pointtypearray(points)^[i];
i:=idx_bakwrd(i);
nxtpoint:=@pointtypearray(points)^[i];
scanedge(cpoint^.x,cpoint^.y,nxtpoint^.x,nxtpoint^.y,0);
end;
drawhlines(top_y,bottom_y);
end;
Procedure outtextxy_length(x1,y1:integer;txt:pchar;length:byte);
var txtlop:byte;
Begin
if tcharset.attrib<>CTproportional then
for txtlop:=1 to length do begin
drawbytes(x1,y1,@tcharset.typeface[byte(txt^)],fontheight);
inc(x1,8);
inc(txt);
end else
for txtlop:=1 to length do begin
drawbytes(x1,y1,@tcharset.typeface[byte(txt^)],fontheight);
inc(x1,tcharset.widths[byte(txt^)]);
inc(txt);
end;
end;
Procedure outtextxy(x1,y1:integer;txt:string);
Begin
outtextxy_length(x1,y1,@txt[1],byte(txt[0]));
end;
(*
Procedure outtextxy_length(x1,y1:integer;txt:pchar;length:byte);
var txtend,ylop,txtstart,lmask,rmask:byte;
Adrs,decoffs:word;
x2,y2,tmp:integer;
Begin
txtend:=length;
x2:=x1+(txtend shl 3);
y2:=y1+fontmax;
if (x1<=SC.viewport.x2)and(y1<=SC.viewport.y2)and(x2>=SC.viewport.x1)
and(y2>=SC.viewport.y1)and(txtend<>0) then begin
{**Y-cliping**}
If y2>SC.viewport.y2 then ylop:=(SC.viewport.y2-y1) else ylop:=fontmax;
if y1<SC.viewport.y1 then begin
tmp:=SC.viewport.y1-y1; {number ofpixels off screen}
y1:=SC.viewport.y1
end else tmp:=0;
dec(ylop,tmp);
asm
mov ax,y1 ;imul SC.offsw
add ax,x1
add ax,word ptr SC.scrptr
mov adrs,ax
end;
y1:=tmp;
{**X - Clipping**}
lmask:=$ff; rmask:=$ff;
txtstart:=1;
tmp:=(SC.viewport.x1-x1);
if x1<SC.viewport.x1 then begin
lmask:=lmask shr (tmp and 7);
tmp:=tmp shr 3;
inc(txtstart,tmp);
inc(adrs,(tmp shl 3));
end;
tmp:=(x2-SC.viewport.x2-1);
if x2>SC.viewport.x2 then begin
dec(txtend,tmp shr 3);
rmask:=rmask shl (tmp and 7);
end;
if txtstart=txtend+1 then exit;
decoffs:=(SC.offsw*(ylop+1)-8);
asm
mov di,adrs {load offset}
mov dl,t_col
mov bx,SC.offsw {load dx with screen offset}
sub bx,8
mov dh,txtstart {dh is the loop counter}
@Textloop:
les si,[bp+6] {point to string}
mov al,dh;xor ah,ah {get txtloop from dh into ax}
add si,ax {Add to offset the offset of the..}
mov al,es:[si] ;xor ah,ah {..current character..}
shl ax,fontshift {..shift to get the byte offset..}
add ax,y1 {..of the graphic and add y to clip}
mov es,word ptr SC.scrptr+2 {set up screen address segment}
lea si,tcharset {point to character set}
add si,ax {add character offset}
mov cl,ylop ;inc cl {set up y loop (from y to ylop)}
@drawloop:
push cx {save @drawloop}
mov cx,8
mov ah,ds:[si]
cmp dh,txtstart;jnz @noleft
and ah,lmask {mask with the left?}
@noleft:
cmp dh,txtend; jnz @noright
and ah,rmask {mask with the right?}
@noright:
@bitloop:
shl ah,1
jnc @nodraw
mov es:[di],dl
@nodraw:
inc di
loop @bitloop
add di,bx {shift to next line on screen}
inc si {go to next byte in pic}
pop cx
loop @drawloop
sub di,decoffs {correct the screen position..}
inc dh {next character}
cmp dh,txtend {is it the end?}
jle @Textloop {if not, do it all again}
end;
{for txtlop:=1 to txtlop do begin
currmask:=pointer(@tcharset^[ord(txt[txtlop])][y]);
for lop:=0 to lop do begin
hsub2(adrs,currmask^ shr dxr,currmask^ shl (8-dxr));
inc(Adrs,offs);
inc(currmask);
end;
dec(Adrs,decoffs);
end;}
end;
end;
Procedure outtextxy(x1,y1:integer;txt:string);
var txtend,ylop,txtstart,lmask,rmask:byte;
Adrs,decoffs:word;
x2,y2,tmp:integer;
Begin
txtend:=byte(txt[0]);
x2:=x1+(txtend shl 3);
y2:=y1+fontmax;
if (x1<=SC.viewport.x2)and(y1<=SC.viewport.y2)and(x2>=SC.viewport.x1)
and(y2>=SC.viewport.y1)and(txtend<>0) then begin
{**Y-cliping**}
If y2>SC.viewport.y2 then ylop:=(SC.viewport.y2-y1) else ylop:=fontmax;
if y1<SC.viewport.y1 then begin
tmp:=SC.viewport.y1-y1; {number ofpixels off screen}
y1:=SC.viewport.y1
end else tmp:=0;
dec(ylop,tmp);
asm
mov ax,y1 ;imul SC.offsw
add ax,x1
add ax,word ptr SC.scrptr
mov adrs,ax
end;
y1:=tmp;
{**X - Clipping**}
lmask:=$ff; rmask:=$ff;
txtstart:=1;
tmp:=(SC.viewport.x1-x1);
if x1<SC.viewport.x1 then begin
lmask:=lmask shr (tmp and 7);
tmp:=tmp shr 3;
inc(txtstart,tmp);
inc(adrs,(tmp shl 3));
end;
tmp:=(x2-SC.viewport.x2-1);
if x2>SC.viewport.x2 then begin
dec(txtend,tmp shr 3);
rmask:=rmask shl (tmp and 7);
end;
if txtstart=txtend+1 then exit;
decoffs:=(SC.offsw*(ylop+1)-8);
asm
mov di,adrs {load offset}
mov dl,t_col
mov bx,SC.offsw {load dx with screen offset}
sub bx,8
mov dh,txtstart {dh is the loop counter}
@Textloop:
les si,[bp+6] {point to string}
mov al,dh;xor ah,ah {get txtloop from dh into ax}
add si,ax {Add to offset the offset of the..}
mov al,es:[si] ;xor ah,ah {..current character..}
shl ax,fontshift {..shift to get the byte offset..}
add ax,y1 {..of the graphic and add y to clip}
mov es,word ptr SC.scrptr+2 {set up screen address segment}
lea si,tcharset {point to character set}
add si,ax {add character offset}
mov cl,ylop ;inc cl {set up y loop (from y to ylop)}
@drawloop:
push cx {save @drawloop}
mov cx,8
mov ah,ds:[si]
cmp dh,txtstart;jnz @noleft
and ah,lmask {mask with the left?}
@noleft:
cmp dh,txtend; jnz @noright
and ah,rmask {mask with the right?}
@noright:
@bitloop:
shl ah,1
jnc @nodraw
mov es:[di],dl
@nodraw:
inc di
loop @bitloop
add di,bx {shift to next line on screen}
inc si {go to next byte in pic}
pop cx
loop @drawloop
sub di,decoffs {correct the screen position..}
inc dh {next character}
cmp dh,txtend {is it the end?}
jle @Textloop {if not, do it all again}
end;
{for txtlop:=1 to txtlop do begin
currmask:=pointer(@tcharset^[ord(txt[txtlop])][y]);
for lop:=0 to lop do begin
hsub2(adrs,currmask^ shr dxr,currmask^ shl (8-dxr));
inc(Adrs,offs);
inc(currmask);
end;
dec(Adrs,decoffs);
end;}
end;
end;
*)
Procedure Drawbytesxy(x1,y1:Integer;pic:bytearray;xbytes,ybytes:byte);
begin
end;
Procedure Drawbytes(x1,y1:Integer;pic:bytearray;nbytes:byte);
var lop,mask:byte; Adrs:word;
x2:integer;
Begin
x2:=x1+8;
dec(nbytes);
if (x2>=sc.viewport.x1)and(x1<=SC.viewport.x2)and(y1+nbytes>=SC.viewport.y1)and(y1<SC.viewport.y2)then begin
Adrs:=(y1*SC.offsw)+x1;
{**XCliping**}
mask:=$ff;
if x1<SC.viewport.x1 then
mask:=$ff shr (SC.viewport.x1-x1);
if x2>SC.viewport.x2 then
mask:=mask and ($ff shl (x2-SC.viewport.x2-1));
{**YCliping**}
If y1+nbytes>SC.viewport.y2 then lop:=(SC.viewport.y2-y1) else lop:=nbytes;
if y1<SC.viewport.y1 then begin
y1:=SC.viewport.y1-y1;inc(Adrs,y1*SC.offsw);
end else y1:=0;
dec(lop,y1-1);
asm
mov es,word ptr SC.scrptr+2 {set up screen address}
mov di,adrs
add di,word ptr SC.scrptr
mov cl,lop
xor ch,ch
mov al,t_col
mov dx,SC.offsw {load dx with screen offset}
sub dx,8
push ds {save data segment}
lds si,pic
add si,y1
@drawloop:
push cx
mov ah,ds:[si]
and ah,mask
mov cx,8
@bitloop:
shl ah,1
jnc @nodraw
mov es:[di],al {write to screen memory}
@nodraw:
inc di;
loop @bitloop
add di,dx {shift to next line on screen}
inc si {go to next byte in pic}
pop cx
loop @drawloop
pop ds
end;
{for lop:=ystart to lop do begin
hsub2(Adrs,pic^[lop] shr (dx),(pic^[lop] shl (8-dx)));
inc(Adrs,offs);
end;}
end;
end;
{clipping subroutine for line drawing}
Function outcode(x1,y1:integer):byte;
var return:byte;
Begin
return:=0;
if y1>SC.viewport.y2 then Return:=bellow else
if y1<SC.viewport.y1 then Return:=above;
if x1>SC.viewport.x2 then inc(Return,right) else
if x1<SC.viewport.x1 then inc(Return,left);
outcode:=return;
end;
function clip(var xa,ya,xb,yb:integer):boolean;
var code0,code1:byte;
Begin
While 0=0 do begin
code0:=(byte(xb>SC.viewport.x2))+ {right =$01}
(byte(yb>SC.viewport.y2)shl 1)+{bellow =$02}
(byte(xb<SC.viewport.x1)shl 2)+{left =$04}
(byte(yb<SC.viewport.y1)shl 3) {above =$08};
code1:=(byte(xa>SC.viewport.x2))+ {right =$01}
(byte(ya>SC.viewport.y2)shl 1)+{bellow =$02}
(byte(xa<SC.viewport.x1)shl 2)+{left =$04}
(byte(ya<SC.viewport.y1)shl 3) {above =$08};
if (code0 and code1)<>0 then begin
clip:=false;exit;{Trivial reject}
end else begin
if not(code0 or code1<>0)then begin
clip:=true;exit;{Trivial accept}
end else If (code0=0) then begin
fswap(code1,code0,1);
fswap(xb,xa,2);
fswap(yb,ya,2);
end;
if (code0 and bellow)<>0 then begin
inc(xb,(xa-xb)*(SC.viewport.y2-yb)div(ya-yb));
yb:=SC.viewport.y2;
end else if (code0 and above)<>0 then begin
inc(xb,(xa-xb)*(SC.viewport.y1-yb)div(ya-yb));
yb:=SC.viewport.y1;
end else if (code0 and right)<>0 then begin
inc(yb,(ya-yb)*(SC.viewport.x2-xb)div(xa-xb));
xb:=SC.viewport.x2;
end else if (code0 and left)<>0 then begin
inc(yb,(ya-yb)*(SC.viewport.x1-xb)div(xa-xb));
xb:=SC.viewport.x1;
end;
end;
end;
end;
{biechmans line drawing algo- bit optimization still needed}
Procedure Line(x1,y1,x2,y2:integer);
Var dfx,dfy,d,d2,incr1,incr2,xinc:integer;
adrs:word;
Begin
if x1=x2 then vline(x1,y1,y2) else begin
if y1=y2 then hline(x1,x2,y1) else
if clip(x1,y1,x2,y2) then Begin
dfx:=abs(x2-x1);dfy:=abs(y2-y1);
asm mov es,word ptr SC.scrptr+2 end;
if dfx>dfy then begin
asm
mov ax,x2 ; cmp x1,ax ; jle @no_swap
Xchg x1,ax ; mov x2,ax
mov ax,y2; Xchg y1,ax;mov y2,ax;
@no_swap:
mov ax,y1; imul SC.offsw ;add ax,word ptr SC.scrptr; {sort out address:}
mov bx,x1; add ax,bx {adrs:=(y1*SC.offsw)+(x1 shr 3)+word ptr SC.scrptr;}
mov adrs,ax
end;
incr2:=(dfy-dfx)shl 1;
if(y2>y1) then xinc:=SC.offsw else xinc:=-SC.offsw;
asm
mov al,t_col
mov dx,xinc {get scanline offset}
mov bx,adrs {get offset}
mov cx,dfx ;inc cx {load in the length}
mov si,dfy ;shl si,1; {get incr1}
mov di,si ;sub di,dfx; {get 'd'}
push bp {remember stack frame}
mov bp,incr2
@Runloop: {start loop}
mov es:[bx],al
inc bx
cmp di,0;jl @noinc {new scan line?}
add di,bp {update di}
add bx,dx {add scanline offset}
jmp @check
@noinc:
add di,si {update fractional part}
@check:
loop @runloop
pop bp {recall stack frame}
end;
end else begin
asm
mov ax,y2 ; cmp y1,ax ; jle @no_swap
Xchg y1,ax ; mov y2,ax
mov ax,x2; Xchg x1,ax;mov x2,ax;
@no_swap:
mov ax,y1; imul SC.offsw ;add ax,word ptr SC.scrptr; {sort out address:}
mov bx,x1; add ax,bx {adrs:=(y1*SC.offsw)+(x1 shr 3)+word ptr SC.scrptr;}
mov adrs,ax
end;
d2:=dfx shl 1;
d:=d2-dfy;
incr1:=d2;
incr2:=(dfx-dfy) shl 1;
asm
inc dfy {add one to difference in y}
mov dx,SC.offsw {screen width}
mov bx,adrs {screen address}
mov al,t_col {Colour}
mov di,d {(diff in X*2)-diff in y}
mov si,incr1 {integer increment}
mov cx,x1; cmp cx,x2;
jg @left {check if line is doing left or right}
{do these loads for both directions because of the use of bp}
mov cx,dfy {difference in Y}
push bp
mov bp,incr2 {fractional increment}
@Runloop:
mov es:[bx],al {Write to screen}
add bx,dx {next scan line}
cmp di,0 {if no change}
jl @noinc {then don't do below}
add di,bp {do frac part}
inc bx {go to next byte on screen}
jmp @check
@noinc:
add di,si {add frac part}
@check:
loop @runloop
jmp @end
@left:
{do these loads for both directions because of the use of bp}
mov cx,dfy {difference in Y}
push bp
mov bp,incr2 {fractional increment}
@Runloopb:
mov es:[bx],al {Write to screen}
add bx,dx {next scan line}
cmp di,0 {if no change}
jl @noincb {then don't do below}
add di,bp {do frac part}
dec bx
jmp @checkb
@noincb:
add di,si {add frac part}
@checkb:
loop @runloopb
@end:
pop bp
end;
end;
end;
end;
end;
Procedure Hline(x1,x2,y:Integer);assembler;
asm
mov ax,y;mov bx,x1; mov cx,x2
{swap x1,x1 if x1>x2}
cmp bx,cx ;jle @no_swap ; Xchg bx,cx; @no_swap:
{is line at all visable?}
cmp bx,SC.viewport.x2 ;jg @fin
cmp cx,SC.viewport.x1 ;jl @fin
cmp ax,SC.viewport.y2 ;jg @fin
cmp ax,SC.viewport.y1 ;jl @fin
{X axis clipping}
mov dx,SC.viewport.x1 ;cmp bx,dx ; JGE @nochange_x1
mov bx,dx
@nochange_x1:
mov dx,SC.viewport.x2 ;cmp cx,dx ; JLE @nochange_x2
mov cx,dx
@nochange_x2:
{start of mode dependant stuff} {load up screen position}
mov es,word ptr SC.scrptr+2
inc cx ;imul SC.offsw ;add ax,word ptr SC.scrptr; mov di,ax {load up screen pos}
{calculate length (in bytes-1) of line}
sub cx,bx {get byte length}
add di,bx {add this to screen memory offset}
mov al,t_col
mov ah,al
shr cx,1 {get word count}
REP stosw {store words}
adc cx,cx {extra byte?}
REP stosb {Draw if there}
@fin:
end;
Procedure bar(x1,y,x2,y2:integer);
var adrs:word;
Begin
if x2<x1 then asm mov ax,x2;Xchg x1,ax;mov x2,ax;end;
if y2<y then asm mov ax,y2;xchg y,ax;mov y2,ax;end;
if (y>SC.viewport.y2)or(y2<SC.viewport.y1)or(x1>SC.viewport.x2)or(x2<SC.viewport.x1)=false then begin
if x1<SC.viewport.x1 then x1:=SC.viewport.x1;if y<SC.viewport.y1 then y:=SC.viewport.y1;
if x2>SC.viewport.x2 then x2:=SC.viewport.x2;if y2>SC.viewport.y2 then y2:=SC.viewport.y2;
inc(x2);inc(y2);
adrs:=(y*SC.offsw)+x1;
asm
mov es,word ptr SC.scrptr+2
mov di,adrs {address}
add di,word ptr SC.scrptr
mov ax,SC.offsw
mov si,ax {load offset}
mov al,t_fillcol
mov bx,x2
sub bx,x1
mov cx,y2 {load loop count -ycnt}
sub cx,y
mov dx,SC.offsw
sub dx,bx
@runloop:
push cx
mov cx,bx
rep stosb
pop cx
add di,dx
loop @runloop
end;
end;
end;
Procedure Vline(x,y1,y2:Integer);assembler;
asm
{es -screen seg
di -byte offset
al -byte mask
cx -Y length of line}
{remember that these registers are loaded thus:}
mov ax,y1; mov cx,y2 ;mov bx,x
{swap y2,y1 if y1>y2}
cmp ax,cx ;jle @no_swap ; Xchg ax,cx; @no_swap:
cmp ax,SC.viewport.y2 ;jg @fin {is line at all visable?}
cmp cx,SC.viewport.y1 ;jl @fin
cmp bx,SC.viewport.x2 ;jg @fin
cmp bx,SC.viewport.x1 ;jl @fin
{Y axis clipping}
mov dx,SC.viewport.y1 ;cmp ax,dx ; JGE @nochange_y1
mov ax,dx
@nochange_y1:
mov dx,SC.viewport.y2 ;cmp cx,dx ; JLE @nochange_y2
mov cx,dx
@nochange_y2:
inc cx {inc y2}
sub cx,ax {calc length}
les di,SC.scrptr {load up screen position}
imul SC.offsw;
add di,ax; add di,bx; {add di, y*SC.offsw +xposition}
{result of imul is stored (thank God) in ax-dx, perfect for svga}
mov bx,SC.offsw {load screen width}
mov al,t_col
@runloop:
mov es:[di],al {write to screen}
add di,bx {next pixel}
loop @runloop
@fin:
end;
Procedure putpixel(x1,y1:Integer);assembler;
asm
{if dot visable?}
mov bx,x1 ;mov ax,y1
cmp bx,SC.viewport.x1 ;jl @fin {clipping..}
cmp ax,SC.viewport.y1 ;jl @fin
cmp bx,SC.viewport.x2 ;jg @fin
cmp ax,SC.viewport.y2 ;jg @fin
mov es,word ptr SC.scrptr+2
imul SC.offsw; add ax,word ptr SC.scrptr
add ax,bx; mov di,ax {load up screen pos}
mov al,t_col
mov es:[di],al
@fin:
end;
Function getpixel(x1,y1:Integer):byte;
begin asm
mov es,word ptr SC.scrptr+2
imul SC.offsw; add ax,word ptr SC.scrptr
add ax,bx; mov di,ax {load up screen pos}
mov al,es:[di]
mov @result,al
end;
end;
Procedure Triangle(xa,ya,xb,yb,xc,yc:integer);assembler;
const shiftby=6;
Var
lop,Lx,Rx,
Lxadd,Rxadd,yrdiff,yldiff,xoff,Lxa,Rxa,xdiff,endp:integer;
right:boolean;
scroffset:word;
{sort by Y}
asm
{if ya>yc then swap xc,xa swap yc,ya fswap ic,ia}
mov ax,yc
cmp ya,ax
jl @NoSwapY1
xchg ya,ax; mov yc,ax
mov ax,xc; xchg xa,ax; mov xc,ax
@NoSwapY1:
{if ya>yb then swap xb,xa swap yb,ya fswap ib,ia}
mov ax,yb
cmp ya,ax
jl @NoSwapY2
xchg ya,ax; mov yb,ax
mov ax,xb; xchg xa,ax; mov xb,ax
@NoSwapY2:
{if yb>yc then swap xc,xb swap yc,yb fswap ic,ib}
mov ax,yc
cmp yb,ax
jl @NoSwapY3
xchg yb,ax; mov yc,ax
mov ax,xc; xchg xb,ax; mov xc,ax
@NoSwapY3:
{is the cross point to the left or right?}
mov di,yc;sub di,ya
mov ax,yb;sub ax,yc
mov cx,xc;sub cx,xa
imul cx
cmp di,0
jz @nodiv
idiv di
@nodiv:
add ax,xc
cmp xb,ax
mov right,true
jle @isright
mov right,false
@isright:
{get smallest x}
mov bx,xa;
mov ax,xb
cmp bx,ax; jl @nodo1 ;mov bx,ax ;@nodo1:
mov ax,xc
cmp bx,ax; jl @nodo2 ;mov bx,ax ;@nodo2:
mov xoff,bx
{sub by bx and shift left }
sub xa,bx;
sub xb,bx;
sub xc,bx;
shl xa,shiftby
shl xb,shiftby
shl xc,shiftby
{get y length}
mov si,yc
cmp si,SC.viewport.y2
jle @noyclip
mov si,SC.viewport.y2
inc si
@noyclip:
mov endp,si {exit routine if ends of tri are outside VP}
mov si,ya
cmp si,SC.viewport.y2 {if ya>SC.viewport then exit}
jg @triend
cmp si,endp {if ya=clipped yc then exit}
jz @triend
{yldiff:=yb-ya;if yldiff=0 then yldiff:=1;
Lxadd:=(xb-xa) div yldiff;}
mov ax,xb ;sub ax,xa
mov bx,yb ;sub bx,ya
jz @nodiv1
cwd
idiv bx
jmp @nocorrect1
@nodiv1:
mov bx,1
@nocorrect1:
mov yldiff,bx
mov lxadd,ax
{yrdiff:=yc-ya;if yrdiff=0 then yrdiff:=1;
Rxadd:=(xc-xa) div yrdiff;}
mov ax,xc ;sub ax,xa
mov bx,yc ;sub bx,ya
jz @nodiv2
cwd
idiv bx
jmp @nocorrect2
@nodiv2:
mov bx,1
@nocorrect2:
mov yrdiff,bx
mov rxadd,ax
mov ax,xa