forked from mills32/Little-Game-Engine-for-VGA-EGA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lt_gfx.c
3523 lines (3175 loc) · 74.2 KB
/
lt_gfx.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
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
/*##########################################################################
A lot of code from David Brackeen
http://www.brackeen.com/home/vga/
This is a 16-bit program.
Remember to compile in the LARGE memory model!
Please feel free to copy this source code.
Used: K1n9_Duk3's IMF Player - A simple IMF player for DOS
Copyright (C) 2013-2016 K1n9_Duk3
Based on the Apogee Sound System (ASS) and Wolfenstein 3-D (W3D)
ASS is Copyright (C) 1994-1995 Apogee Software, Ltd.
W3D is Copyright (C) 1992 Id Software, Inc.
##########################################################################*/
#include "lt__eng.h"
//Predefined structs
/*******************
OK let's do this console style, (of course You can do whatever you want).
At any moment there will only be:
You'll have to unload a music/map/tilset before loading another.
********************/
word LT_VRAM_Logical_Width;
// One map in ram stored at "LT_map"
word LT_map_width;
word LT_map_height;
word LT_map_ntiles;
word *LT_map_data;
byte *LT_map_collision;
// One tileset
word LT_tileset_width;
word LT_tileset_height;
word LT_tileset_ntiles;
byte LT_tileset_palette[256*3];
byte *LT_tileset_data;
byte LT_EGA_Font_Palette[4] = {0,9,7,15};
unsigned char *LT_tile_tempdata; //Temp storage of non tiled data. and also sound samples
unsigned char *LT_tile_tempdata2; //Second half
extern unsigned char *LT_sprite_data; //
SPRITE LT_Loading_Animation;
extern SPRITE *sprite;
extern word *sprite_id_table;
byte LT_Loaded_Image = 0; //If you load a 320x240 image (to the second page), the delete loading interrupt will paste it to first page
byte LT_PAL_TIMER = 0;
byte LT_EGA_FADE_STATE = 0;
word LT_FrameSkip = 0;
byte *LT_Filename; //Contains name of the images oppened, for LT_Header_BMP function
void LT_Error(char *error, char *file);
void LT_Reset_Sprite_Stack();
void DAT_Seek(FILE *fp, char *dat_string);
extern word LT_Sprite_Size_Table_EGA_32[];
extern byte LT_SFX_MODE;
extern byte LT_Sprite_Stack;
extern byte LT_Sprite_Stack_Table[];
extern byte LT_Active_AI_Sprites[];
extern byte selected_AI_sprite;
word Compile_Bitmap(word logical_width, unsigned char *bitmap, unsigned char *output);
//GLOBAL VARIABLES
word FONT_VRAM = 0;
word TILE_VRAM = 0;
//Palette for fading
byte LT_Temp_palette[256*3];
//Palette cycling
byte LT_Cycle_paldata[32*3]; //2 palettes of 8 lours each (duplicated to simplify code) for cycling olours.
byte LT_Cycle_palframe = 0;
byte LT_Cycle_palcounter = 0;
//Parallax palette
byte LT_Parallax_Frame = 0;
byte LT_Parallax_paldata[3*128];
//This points to video memory.
byte *CGA=(byte *)0xB8000000L;
byte *VGA=(byte *)0xA0000000L;
word *VRAM_MAP =(word *)0xB8000000L; //Map address
//Map Scrolling
byte scr_delay = 0; //copy tile data in several frames to avoid slowdown
int SCR_X = 0;
int SCR_Y = 16;
int LT_current_x = 0;
int LT_last_x = 0;
int LT_current_y = 0;
int LT_last_y = 0;
int LT_map_offset = 0;
int LT_map_offset_y = 0;
int LT_map_offset_Endless = 0;
int LT_follow = 0;
int vpage = 0;
int Enemies = 0;
void (*draw_map_column)(word, word, word, word);
word LT_offset_vram_TXT;
void LT_Draw_Sprites_TGA();
extern void interrupt (*LT_old_time_handler)(void);
//Loading palette
unsigned char LT_Loading_Palette[] = {
0x00,0x00,0x00, //colour 0
0xff,0xff,0xff //colour 1
};
void LT_VGA_Enable_4Planes(){
asm mov dx,03c4h //dx = indexregister
asm mov ax,0F02h //INDEX = MASK MAP, 3d4
asm out dx,ax //write all the bitplanes.
asm mov dx,03ceh //dx = indexregister 3ce
asm mov ax,0008h
asm out dx,ax
}
void LT_VGA_Return_4Planes(){
asm mov dx,03ceh +1 //dx = indexregister
asm mov ax,00ffh
asm out dx,ax
}
void LT_vsync(){
asm mov dx,INPUT_STATUS_0
WaitVsync:
asm in al,dx
asm test al,08h
asm jz WaitVsync
WaitNotVsync:
asm in al,dx
asm test al,08h
asm jnz WaitNotVsync
//while( inp( INPUT_STATUS_0 ) & 0x08 );
//while( !(inp( INPUT_STATUS_0 ) & 0x08 ) );
}
//Mode: 0 = image; 1 = tiles; 2 = sprite; 3 = window; 4/5 = animation/font
void LT_Header_BMP(FILE *fp,int mode, int sprite_number){
int index;
word num_colors;
byte get_pal = 1;
byte first_color = 0;
byte pal_colors = 0;
word header;
byte pixel_format = 0;
SPRITE *s = &sprite[sprite_number];
word _width;
word _height;
word *width;
word *height;
//Tiles or image
if (mode < 2){width = <_tileset_width; height = <_tileset_height;}
//Sprites
if (mode == 2){width = &s->width; height = &s->height;}
//Window //Animation //Font
if (mode > 2){width = &_width; height = &_height;}
//Read header
fread(&header, sizeof(word), 1, fp);
if (header != 0x4D42) LT_Error("Not a BMP file",LT_Filename);
fseek(fp, 16, SEEK_CUR);
fread(width, sizeof(word), 1, fp);
fseek(fp, 2, SEEK_CUR);
fread(height,sizeof(word), 1, fp);
fseek(fp, 4, SEEK_CUR);
fread(&pixel_format,sizeof(byte), 1, fp);
fseek(fp, 17, SEEK_CUR);
fread(&num_colors,sizeof(word), 1, fp);
fseek(fp, 6, SEEK_CUR);
if (num_colors==0) num_colors=256;
if (num_colors > 256) LT_Error("Image has more than 256 colors",LT_Filename);
//Place colors in appropiate offset of VGA palette
//
switch (mode){
case 0://If reading image
if (LT_VIDEO_MODE == 1) {
if (LT_tileset_height != 200) LT_Error("Wrong size for VGA image, size must be 320x200: ",LT_Filename);
if (LT_tileset_width != 320) LT_Error("Wrong size for VGA image, size must be 320x200: ",LT_Filename);
}
if (LT_VIDEO_MODE == 0){
if (LT_tileset_height != 200) LT_Error("Wrong size for EGA image, size must be 320x200: ",LT_Filename);
if (LT_tileset_width != 320) LT_Error("Wrong size for EGA image, size must be 320x200: ",LT_Filename);
}
if (pixel_format !=8) LT_Error("Wrong format for image, must be 8 bit per pixel: ",LT_Filename);
pal_colors = 208;first_color = 0;
break;
case 1://If reading tileset
if (pixel_format !=8) LT_Error("Wrong format for tileset, image must be 8 bit per pixel: ",LT_Filename);
pal_colors = 208;first_color = 0;
break;
case 2://If reading sprites
if (s->width & 1) LT_Error("Sprite width not even: ",LT_Filename);
if (s->height & 1) LT_Error("Sprite height not even: ",LT_Filename);
pal_colors = 32; first_color = 208;
break;
case 4://If reading Animation
case 5://or font
if (_height !=32) LT_Error("Wrong size for animation/font, image must be 128x32: ",LT_Filename);
if (_width !=128) LT_Error("Wrong size for animation/font, image must be 128x32: ",LT_Filename);
if (pixel_format !=4) LT_Error("Wrong format for animation/font image, must be 4 bit per pixel: ",LT_Filename);
if (mode == 4){pal_colors = 4; first_color = 248;}
else {pal_colors = 4; first_color = 252;}//If reading font
break;
}
//Load Palette
for(index=first_color;index<first_color+num_colors;index++){
if (index-first_color == pal_colors) get_pal = 0;
if (get_pal){
LT_tileset_palette[(int)(index*3+2)] = fgetc(fp) >> 2;
LT_tileset_palette[(int)(index*3+1)] = fgetc(fp) >> 2;
LT_tileset_palette[(int)(index*3+0)] = fgetc(fp) >> 2;
} else {
fgetc(fp);
fgetc(fp);
fgetc(fp);
}
fgetc(fp);
}
}
//Frame Counter
void interrupt LT_Frame_Counter(void){
asm CLI
asm inc LT_FrameSkip
asm STI
asm mov al,020h
asm mov dx,020h
asm out dx, al //PIC, EOI
}
//32x32 animation for loading scene
extern unsigned char *LT_sprite_data;
void LT_Load_Animation(char *file, char *dat_string){
long index,offset;
word data_offset = 0;
word x,y;
word i,j;
word frame = 0;
byte tileX;
byte tileY;
byte size = 32;
byte sizey = 32;
byte width = 0;
byte w = 0;
FILE *fp;
int code_size;
LT_Filename = file;
fp = fopen(file,"rb");
if(!fp) LT_Error("Can't find ",file);
if (dat_string) DAT_Seek(fp,dat_string);
LT_Header_BMP(fp,4,0);
if (LT_VIDEO_MODE < 2) {width = 128; w = 7;}
else {width = 64;w = 6;size = 16;sizey = 32;}
for(index=31*64;index>=0;index-=64){
for(x=0;x<64;x++){
unsigned char c = (byte)fgetc(fp);
if (LT_VIDEO_MODE == 1){
LT_tile_tempdata[(index+x<<1)] = ((c & 0xF0)>>4) + 248; //1111 0000c
LT_tile_tempdata[(index+x<<1)+1] = (c & 0x0F) + 248; //0000 1111c Animation colors from 248 to 251
}
if (LT_VIDEO_MODE == 0){
LT_tile_tempdata[(index+x<<1)] = ((c & 0xF0)>>4);
LT_tile_tempdata[(index+x<<1)+1] = (c & 0x0F);
}
if (LT_VIDEO_MODE == 4){
LT_tile_tempdata[index+x] = c ;
}
}
}
fclose(fp);
index = 0; //use a chunk of temp allocated RAM to rearrange the sprite frames
//Rearrange sprite frames one after another in temp memory
for (tileY=0;tileY<32;tileY+=32){
for (tileX=0;tileX<width;tileX+=size){
offset = (tileY<<w)+tileX;
if (LT_VIDEO_MODE == 1){
LT_tile_tempdata2[index] = size;
LT_tile_tempdata2[index+1] = size;
index+=2;
}
for(x=0;x<sizey;x++){
memcpy(<_tile_tempdata2[index],<_tile_tempdata[offset+(x<<w)],size);
index+=size;
}
}
}
LT_Loading_Animation.nframes = 4;
LT_Loading_Animation.code_size = 0;
LT_Loading_Animation.ega_size = <_Sprite_Size_Table_EGA_32[0];
//estimated size of code
//fsize = (size * size * 7) / 2 + 25;
for (frame = 0; frame < LT_Loading_Animation.nframes; frame++){ //
if (LT_VIDEO_MODE == 1){
/*if ((LT_Loading_Animation.frames[frame].compiled_code = farcalloc(fsize,sizeof(unsigned char))) == NULL){
LT_Error("Not enough RAM to load animation frames ",0);
}*/
//COMPILE SPRITE FRAME TO X86 MACHINE CODE
//& Store the compiled data at it's final destination
code_size = Compile_Bitmap(LT_VRAM_Logical_Width, <_tile_tempdata2[(frame*2)+(frame*(size*size))],<_sprite_data[data_offset]);
LT_Loading_Animation.frames[frame].compiled_code = <_sprite_data[data_offset];
data_offset += code_size;
LT_Loading_Animation.code_size += code_size;
}
if (LT_VIDEO_MODE == 0){
byte *pixels = <_tile_tempdata2[frame*size*size];
LT_Loading_Animation.frames[frame].compiled_code = <_sprite_data[data_offset];
offset = 0;
for (y = 0; y < size; y++){//Read lines
for (j = 0; j < 4; j++){
byte cmask = 0x80;
byte color = 0;
byte mask = 0;
for (x = 0; x < 8; x++){
byte bit = pixels[offset++];//colors 1,2
if (bit ) mask += cmask;
if (bit == 2) color += cmask;
cmask = cmask >> 1;
}
LT_sprite_data[data_offset++] = color;
}
}
}
}
if (LT_VIDEO_MODE == 4){
memcpy(<_sprite_data[0],LT_tile_tempdata2,2048);
}
//IINIT SPRITE
//sprite[sprite_number].bkg_data no bkg data
LT_Loading_Animation.width = 32;
LT_Loading_Animation.height = 32;
LT_Loading_Animation.init = 0;
LT_Loading_Animation.frame = 0;
LT_Loading_Animation.baseframe = 0;
LT_Loading_Animation.aframes = 4;
LT_Loading_Animation.animate = 1;
LT_Loading_Animation.anim_speed = 0;
LT_Loading_Animation.anim_counter = 0;
}
void LT_Set_Animation(byte speed){
LT_Loading_Animation.speed = speed;
}
void run_compiled_sprite(word XPos, word YPos, char *Sprite);
void run_compiled_tga_sprite(word XPos, word YPos, char *Sprite);
void interrupt LT_Loading(void){
asm CLI
{
SPRITE *s = <_Loading_Animation;
//animation
if (s->anim_speed == s->speed){
s->anim_speed = 0;
s->frame++;
if (s->frame == s->aframes) s->frame = 0;
}
s->anim_speed++;
if (LT_VIDEO_MODE == 1) run_compiled_sprite(s->pos_x,s->pos_y,s->frames[s->frame].compiled_code);
if (LT_VIDEO_MODE == 0){
//draw EGA sprite and destroy bkg
char *bitmap = s->frames[s->frame].compiled_code;
word screen_offset = (s->pos_y*LT_VRAM_Logical_Width)+(s->pos_x>>3);
asm{
push es
push ds
push di
push si
lds si,[bitmap]
mov ax,0A000h
mov es,ax
mov di,screen_offset //ES:DI destination vram
mov dx,0x03CE
mov ax,0x0005 //write mode 0
out dx,ax
mov dx,0x03CE //Set mask register
mov ax,0xFF08
out dx,ax
mov ax,8 //scanlines
}
_drawsprit_32:
asm{
mov cx,4
rep movsb
add di,44-4
mov cx,4
rep movsb
add di,44-4
mov cx,4
rep movsb
add di,44-4
mov cx,4
rep movsb
add di,44-4 //Next scanline
dec ax
jnz _drawsprit_32
}
asm{
pop si
pop di
pop ds
pop es
mov dx,0x03CE
mov ax,0x0205 //write mode 2
out dx,ax
}
}
if (LT_VIDEO_MODE == 4) {
int i;
word offset = s->frame<<9;
word offset1 = (160*26)+72;
for(i = 0; i < 8;i++){
memcpy(&CGA[offset1],<_sprite_data[offset],16);offset1+=8192;offset+=16;
memcpy(&CGA[offset1],<_sprite_data[offset],16);offset1+=8192;offset+=16;
memcpy(&CGA[offset1],<_sprite_data[offset],16);offset1+=8192;offset+=16;
memcpy(&CGA[offset1],<_sprite_data[offset],16);offset1-=((8192*3)-160);offset+=16;
}
}
}
asm STI
asm mov al,020h
asm mov dx,020h
asm out dx, al //PIC, EOI
}
void EGA_ClearScreen();
void LT_Set_Loading_Interrupt(){
unsigned long spd = 1193182/30;
LT_Cycle_palcounter = 0;
LT_Stop_Music();
LT_Fade_out();
LT_Reset_Sprite_Stack();
LT_Unload_Sprites();
//Wait Vsync
LT_vsync();
//Reset scroll
outport(0x03d4, 0x0D | (0 << 8));
outport(0x03d4, 0x0C | (0 & 0xFF00));
VGA_Scroll(0, 0);
if (LT_VIDEO_MODE == 0) EGA_ClearScreen();
if (LT_VIDEO_MODE == 1){
VGA_ClearPalette();
VGA_ClearScreen();//clear screen
}
if (LT_VIDEO_MODE == 2)memset(VRAM_MAP,0x00,84*31);
if (LT_VIDEO_MODE == 4)memset(CGA,0x00,32*1024);
memset(sprite_id_table,0,19*256*2);
//change color 0, 1, 2 (black and white)
LT_tileset_palette[0] = LT_Loading_Palette[0];
LT_tileset_palette[1] = LT_Loading_Palette[1];
LT_tileset_palette[2] = LT_Loading_Palette[1];
//center loading animation
LT_Loading_Animation.pos_x = 144;
LT_Loading_Animation.pos_y = 104;
asm CLI
//set timer
outportb(0x43, 0x36);
outportb(0x40, spd % 0x100); //lo-byte
outportb(0x40, spd / 0x100); //hi-byte
//set interrupt
setvect(0x1C, LT_Loading); //interrupt 1C not available on NEC 9800-series PCs.
asm STI
//LT_Draw_Text_Box(12,18,12,1,0,"LOADING... ");
//Wait Vsync
LT_vsync();
LT_Fade_in();
}
void LT_Delete_Loading_Interrupt(){
//unsigned long spd = 1193182/60;
LT_Fade_out();
asm CLI
//set frame counter
outportb(0x43, 0x36);
outportb(0x40, 0xFF); //lo-byte
outportb(0x40, 0xFF); //hi-byte
setvect(0x1C, LT_old_time_handler);
asm STI
if (LT_Loaded_Image){
int i;
LT_VGA_Enable_4Planes();
for (i = 0; i < LT_VRAM_Logical_Width*200;i++) VGA[i] = VGA[i + (304*LT_VRAM_Logical_Width)];
LT_Loaded_Image = 0;
LT_Fade_in();
}
}
void VGA_Scroll(word x, word y){
SCR_X = x;
SCR_Y = y;
}
void VGA_ClearScreen(){
outport(0x03c4,0xff02);
memset(&VGA[0],0,(352>>2)*240);
}
void EGA_ClearScreen(){
asm mov dx,0x03CE
asm mov ax,0x0005 //write mode 0
asm out dx,ax
asm mov dx,0x03CE //Set mask register
asm mov ax,0xFF08
asm out dx,ax
memset(&VGA[0],0,(352>>2)*240);
}
byte p[4] = {0,2,4,6};
byte p1[8] = {0,1,2,3,4,5,6,7};
byte pix;
void PC_Speaker_SFX_Player();//I don't want to use interrupts, play pc speaker sfx here if playing == 1
//So I had to add a "double buffer" just to update the sprites without flicker
//Don't worry!! I only update 19 tiles and the sprites every frame, so it is even faster than before on 8088 and 8086.
//The bad news is, I had to reduce map size and tilecount, but it is stil cool!
//These values are added to scroll registers so it iterates between the "two pages"
word pageflip[] = {0,304};
word gfx_draw_page = 1;
//////////////////////
//Hardware scrolling//
//////////////////////
void (*LT_WaitVsync)();
//works smooth in dosbox, PCem, Real VGA.
//Choppy on SVGA or modern VGA compatibles
void LT_WaitVsync_VGA(){
byte _ac;
word x = SCR_X;
word y = SCR_Y + pageflip[gfx_draw_page&1];
//
y*=LT_VRAM_Logical_Width;
if (LT_VIDEO_MODE == 0) y += x>>3;
if (LT_VIDEO_MODE == 1) y += x>>2;
gfx_draw_page++;
//change scroll registers:
asm mov dx,003d4h //VGA PORT
asm mov cl,8
asm mov ax,y
asm shl ax,cl
asm or ax,00Dh //LOW_ADDRESS 0x0D
asm out dx,ax //(y << 8) | 0x0D to VGA port
asm mov ax,y
asm and ax,0FF00h
asm or ax,00Ch //HIGH_ADDRESS 0x0C;
asm out dx,ax //(y & 0xFF00) | 0x0C to VGA port
//The smooth panning magic happens here
//disable interrupts
asm cli
//Wait Vsync
asm mov dx,INPUT_STATUS_0
WaitNotVsync:
asm in al,dx
asm test al,08h
asm jnz WaitNotVsync
WaitVsync:
asm in al,dx
asm test al,08h
asm jz WaitVsync
asm mov dx,INPUT_STATUS_0 //Read input status, to Reset the VGA flip/flop
//_ac = inp(AC_INDEX);//Store the value of the controller
if (LT_VIDEO_MODE == 0) pix = p1[SCR_X & 7]; //VGA
else pix = p[SCR_X & 3]; //VGA
//AC index 0x03c0
asm mov dx,0x03C0
asm mov al,0x33 //0x20 | 0x13 (palette normal operation | Pel panning reg)
asm out dx,al
asm mov al,byte ptr pix
asm out dx,al
//Restore controller value
//asm mov ax,word ptr _ac
//asm out dx,ax
LT_FrameSkip = 0;
//enable interrupts
asm sti
}
byte panning = 0;
byte static_screen = 0;
//CRT Page
//Bits 0-2 select the 16K page used by the video. In 32K modes, bit 0 is ignored.
//Processor Page
//bits 3-5 combined with the CPU address to select the
//32K segment of memory accessed at B8000. If an odd page number is selected
//(1,3,5, etc.) the window is reduced to 16K.
//Bits 6-7 mode 320x240 = 11
byte page_show[] = {0xC0,0xC2};//show Page 0 / show page 2 (+ mode 320x200)
byte page_write[] = {0xC0,0xD0};//write Page 0 / write page 2 (+ mode 320x200)
byte tga_page = 0;
void LT_TGA_MapPage(byte wpage){
byte val = page_write[wpage&1];
asm mov dx,03DFh
asm mov al,val //Select write page
asm out dx,al
}
void LT_WaitVsync_TGA(){
word x = SCR_X;
word y = SCR_Y;
byte val = page_show[tga_page&1];
tga_page++;
y = y>>2;//even
y = (y<<6)+(y<<4)+(x>>2); //(y*64)+(y*16) = y*80; + x/4
//Wait Vsync
asm mov dx,03DAh
WaitNotVsync:
asm in al,dx
asm test al,08h
asm jnz WaitNotVsync
WaitVsync:
asm in al,dx
asm test al,08h
asm jz WaitVsync
//change scroll registers: HIGH_ADDRESS 0x0C; LOW_ADDRESS 0x0D
//outport(0x03d4, 0x0D | (y << 8));
//outport(0x03d4, 0x0C | (y & 0xff00));
asm mov dx,003d4h //TGA PORT
asm mov cl,8
asm mov ax,y
asm shl ax,cl
asm or ax,00Dh
asm out dx,ax //(y << 8) | 0x0D to TGA port
asm mov ax,y
asm and ax,0FF00h
asm or ax,00Ch
asm out dx,ax //(y & 0xFF00) | 0x0C to TGA port
asm mov dx,03DFh
asm mov al,val //Select show page
asm out dx,al
}
//Works on all compatible VGA cards from SVGA to 2020 GPUS.
//Choppy on real EGA/VGA
void LT_WaitVsync_SVGA(){
word x = SCR_X;
word y = SCR_Y;
y = (y*LT_VRAM_Logical_Width) + (x>>2); //(y*64)+(y*16)+(y*4) = y*84; + x/4
asm mov dx,INPUT_STATUS_0
WaitDELoop:
asm in al,dx
asm and al,DE_MASK
asm jnz WaitDELoop
//change scroll registers:
asm mov dx,003d4h //VGA PORT
asm mov cl,8
asm mov ax,y
asm shl ax,cl
asm or ax,00Dh //LOW_ADDRESS 0x0D
asm out dx,ax //(y << 8) | 0x0D to VGA port
asm mov ax,y
asm and ax,0FF00h
asm or ax,00Ch //HIGH_ADDRESS 0x0C;
asm out dx,ax //(y & 0xFF00) | 0x0C to VGA port
asm mov dx,INPUT_STATUS_0
WaitNotVsync:
asm in al,dx
asm test al,08h
asm jnz WaitNotVsync
WaitVsync:
asm in al,dx
asm test al,08h
asm jz WaitVsync
//pixel panning value
inportb(0x3DA); //Reset the VGA flip/flop
pix = SCR_X & 3;
outportb(AC_INDEX, AC_HPP);
outportb(AC_INDEX, (byte) p[pix]);
}
void LT_Update(int sprite_follow, int sprite){
LT_Update_AI_Sprites();
if (sprite_follow) LT_scroll_follow(sprite);
if (!LT_IMAGE_MODE) {
//if (LT_ENDLESS_SIDESCROLL) LT_Endless_SideScroll_Map(0);
//else LT_Scroll_Map();
LT_Scroll_Map();
}
if (LT_SPRITE_MODE)LT_Draw_Sprites();
else LT_Draw_Sprites_Fast();
LT_WaitVsync();
}
// load_8x8 fonts to VRAM (64 characters)
void LT_Load_Font(char *file, char *dat_string){
word VGA_index = 0;
word w = 0;
int h = 0;
word ty = 0;
word jx = 0;
word x = 0;
word y = 0;
word tileX = 0;
word tileY = 0;
byte plane = 0;
dword offset = 0;
FILE *fp;
LT_Filename = file;
fp = fopen(file,"rb");
if(!fp)LT_Error("Can't find ",file);
if (dat_string) DAT_Seek(fp,dat_string);
LT_Header_BMP(fp,5,0);
//LOAD TO TEMP RAM
x = 0; //data offset
for (offset = 0; offset < 128*32/2; offset ++){
unsigned char c = fgetc(fp); //it is a 4 bit BMP, every byte contains 2 pixels
LT_tile_tempdata[x ] = (((c & 0xF0)>>4)); //1111 0000
LT_tile_tempdata[x+1] = ((c & 0x0F) ); //0000 1111
x+=2;
}
fclose(fp);
//COPY TO VRAM
w = 16;
h = 4;
jx = 128+8;
if (LT_VIDEO_MODE == 0){
asm mov dx, 0x03CE
asm mov ax, 0x0205 //write mode 2
asm out dx, ax
VGA_index = FONT_VRAM; //VRAM FONT ADDRESS //586*(336/4);
asm CLI //disable interrupts so that loading animation does not interfere
for (tileY = h; tileY > 0 ; tileY--){
ty = (tileY<<3)-1;
for (tileX = 0; tileX < w; tileX++){
int i;
offset = (ty*128) + (tileX<<3);
for(i = 0; i < 8; i++){
byte mask = 0x80;
//Get an 8 pixel chunk, and convert to 4 bytes
for(plane = 0; plane < 8; plane++){
int pixel = VGA[VGA_index]; //Read latch
pixel = LT_tile_tempdata[offset];
asm mov dx, 0x03CE //Set mask register
asm mov al, 0x08
asm mov ah, mask
asm out dx, ax
asm shr mask,1
VGA[VGA_index] = LT_EGA_Font_Palette[pixel];
offset++;
}
offset -= jx;
VGA_index++;
}
}
}
asm STI //Re enable interrupts so that loading animation is played again
}
if (LT_VIDEO_MODE == 1){
for (plane = 0; plane < 4; plane ++){
asm CLI //disable interrupts so that loading animation does not interfere
// select plane
asm mov dx,03c4h
asm mov ax,0F02h
asm out dx,ax
asm inc dx
asm mov al,1
asm mov cl,plane
asm shl al,cl
asm out dx,al
VGA_index = FONT_VRAM; //VRAM FONT ADDRESS
//SCAN ALL TILES
for (tileY = h; tileY > 0 ; tileY--){
ty = (tileY<<3)-1;
for (tileX = 0; tileX < w; tileX++){
offset = plane + (ty*128) + (tileX<<3);
//LOAD TILE
x=0;
for(y = 0; y < 16; y++){
VGA[VGA_index] = LT_tile_tempdata[offset] + 252; //Font color from 252 to 255
VGA_index++;
offset +=4;
x++;
if (x == 2){
x = 0;
offset -= jx;
}
}
}
}
asm STI //Re enable interrupts so that loading animation is played again
}
}
if (LT_VIDEO_MODE == 4){
VGA_index = (64*1024) - (64*32);
for (tileY = h; tileY > 0 ; tileY--){
asm CLI //disable interrupts so that loading animation does not interfere
ty = (tileY<<3)-1;
for (tileX = 0; tileX < w; tileX++){
offset = (ty*LT_tileset_width) + (tileX<<3);
//LOAD TILE
for(y = 0; y < 8; y++){
int j;
for (j = 0; j < 4; j++){
byte pl0 = LT_tile_tempdata[offset++];
byte pl1 = LT_tile_tempdata[offset++];
byte pixel = (pl0<<4) | pl1;
LT_tile_tempdata2[VGA_index] = pixel;
VGA_index++;
}
offset -= jx;
}
}
asm STI //Re enable interrupts so that loading animation is played again
}
}
}
//Print 8x8 tiles, it is a bit slow on 8086, but it works for text boxes
//Text Box borders: # UL; $ UR; % DL; & DR; ( UP; ) DOWN; * LEFT; + RIGHT;
//Special latin characters:
// - : Spanish N with tilde;
// < : Inverse question;
// = : Inverse exclamation;
// > : C cedille
// [ \ ] ^ _ : AEIOU with tilde. Use double "\\" in strings to represent backslash
void (*LT_Print)(word,word,word,char*);
void LT_Print_VGA(word x, word y, word w, char *string){
word FONT_ADDRESS = FONT_VRAM;
word screen_offset;
byte datastring;
word size = strlen(string);
word i = 0;
word line = 0;
word lwidth = LT_VRAM_Logical_Width-2;
word lwidth2 = LT_VRAM_Logical_Width*7;
word line_jump = (LT_VRAM_Logical_Width*8) -24;
y = (y<<3);
screen_offset = (y<<6)+(y<<4)+(y<<3); //(y*64)+(y*16)+(y*8) = y*88;
//if (size > 40) size = 40;
asm{
push ds
push di
push si
mov dx,SC_INDEX //dx = indexregister
mov ax,0F02h //INDEX = MASK MAP,
out dx,ax //write all the bitplanes.
mov dx,GC_INDEX //dx = indexregister
mov ax,0008h
out dx,ax
//
mov di,screen_offset
mov ax,x
shl ax,1
add di,ax
mov ax,0A000h
mov ds,ax
mov bx,size
}
printloop3:
asm push bx
datastring = string[i];
if (datastring > 96) datastring -=32;
asm{
mov dx,word ptr datastring
sub dx,32
}
asm{
mov si,FONT_ADDRESS; //ds:si VRAM FONT TILE ADDRESS
//go to desired tile
mov cl,4 //dx*16
shl dx,cl
add si,dx
mov ax,0A000h
mov es,ax //es:di destination address
mov bx,lwidth
//UNWRAPPED COPY 8x8 TILE LOOP
movsb
movsb
add di,bx
movsb
movsb
add di,bx
movsb
movsb
add di,bx
movsb
movsb
add di,bx
movsb
movsb
add di,bx
movsb
movsb
add di,bx
movsb
movsb
add di,bx
movsb
movsb
//END LOOP
sub di,lwidth2
mov ax,line
inc ax
mov line,ax
cmp ax,w
jne no_jump_line
add di,line_jump
mov line,0
}
no_jump_line:
i++;
asm{
pop bx
dec bx
jnz printloop3
//END
mov dx,GC_INDEX +1 //dx = indexregister
mov ax,00ffh
out dx,ax
pop si
pop di