-
Notifications
You must be signed in to change notification settings - Fork 20
/
lt_gfx.c
4011 lines (3636 loc) · 89.5 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.
********************/
#define INPUT_STATUS_0 0x03da
#define SC_INDEX 0x03c4 // VGA sequence controller
#define SC_DATA 0x03c5
#define GC_INDEX 0x03ce // VGA graphics controller
#define GC_DATA 0x03cf
#define MAP_MASK 0x02
#define ALL_PLANES 0xff02
extern byte LT_DETECTED_CARD;
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 far *LT_map_data;
byte far *LT_map_collision;
// One tileset
word LT_tileset_width;
word LT_tileset_height;
word LT_tileset_ntiles;
byte LT_tileset_palette[256*3];
byte far *LT_tileset_data;
byte LT_EGA_Font_Palette[4] = {0,9,7,15};
unsigned char far *LT_tile_tempdata; //Temp storage of non tiled data. and also sound samples
unsigned char far *LT_tile_tempdata2; //Second half
unsigned char far *LT_CGA_TGA_FONT;
extern unsigned char *LT_sprite_data; //
SPRITE LT_Loading_Animation;
extern SPRITE far *sprite;
extern byte far *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;
byte LT_EGA_TEXT_TRANSLUCENT = 0;
word LT_FrameSkip = 0;
byte *LT_Filename; //Contains name of the images oppened, for LT_Load_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_VIDEO_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;
//Map Scrolling
byte scr_delay = 0; //copy tile data in several frames to avoid slowdown
int SCR_X = 0;
int SCR_Y = 0;
int TGA_SCR_X = 0;
int TGA_SCR_Y = 0;
int TANDY_SCROLL_X = 0;
int TANDY_SCROLL_Y = 0;
int LT_C64 = 0;
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;
byte LT_var = 0;//To disable fade in/out
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);
void interrupt (far * LT_getvect(byte intr))();
void LT_setvect(byte intr, void interrupt (far *func)());
void LT_TGA_MapPage(byte);
byte tga_page = 0;
void DAT_Seek2(word fp,char *dat_string);
void LT_fread(word file_handle,word bytes,void *buffer);
unsigned long LT_fseek(word file_handle,unsigned long bytes,byte origin);
word _strlen(char *str){
word len = 0;
while(1){if (str[len] == 0) break;len++;}
return len;
}
void _memcpy(void *dest, void *src, word number){
asm push ds;asm push si;asm push es;asm push di;
asm lds si,src
asm les di,dest
asm mov cx,number
asm rep movsb //ds:si to es:di
asm pop di;asm pop es;asm pop si;asm pop ds;
}
void Check_Graphics_Card(){
byte reg = 0;
asm mov ax,0x1A00
asm mov bl,0x32
asm int 0x10; asm mov reg,al;
if (reg == 0x1A) {_printf("Card detected: VGA\n\r$"); LT_DETECTED_CARD = 1;LT_VIDEO_MODE = 1;}
else {
asm mov ah,0x12
asm mov bx,0x1010
asm int 0x10; asm mov reg,bh;
if (reg == 0) {
_printf("Card detected: EGA\n\r$");
LT_DETECTED_CARD = 0;
} else {
asm mov ah,0x0F
asm mov bl,0x00
asm int 0x10; asm mov reg,al;
if (reg == 0x07) {
_printf("Card detected: Hercules\n\r$");
LT_Exit();
} else {_printf("Card detected: CGA or TANDY\n\r$");LT_DETECTED_CARD = 2;}
}
}
Clearkb();
}
//Loading palette
unsigned char LT_Loading_Palette[] = {
0x00,0x00,0x00, //colour 0
0xff,0xff,0xff //colour 1
};
void LT_VGA_Enable_4Planes(){
if (LT_VIDEO_MODE <2){
word planes = 0x0F02;
if ((LT_VIDEO_MODE == 0) && (LT_EGA_TEXT_TRANSLUCENT)) planes = 0x0702;
asm mov dx,03c4h //dx = indexregister
asm mov ax,planes //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(){
if (LT_VIDEO_MODE <2){
asm mov dx,03ceh +1 //dx = indexregister
asm mov ax,00ffh
asm out dx,ax
if ((LT_VIDEO_MODE == 0)&& (LT_EGA_TEXT_TRANSLUCENT)){
asm mov dx,03C4h //dx = indexregister
asm mov ax,0F02h //INDEX = MASK MAP,
asm out dx,ax //write plane 1.
}
}
}
void LT_vsync(){
//Try to catch Vertical retrace
asm mov dx,0x3DA; asm mov bl,0x08;
WaitNotVsync: asm in al,dx; asm test al,bl; asm jnz WaitNotVsync;
WaitVsync: asm in al,dx; asm test al,bl; asm jz WaitVsync;
//while( inp( INPUT_STATUS_0 ) & 0x08 );
//while( !(inp( INPUT_STATUS_0 ) & 0x08 ) );
}
void LT_ClearScreen(){
if (LT_VIDEO_MODE == 0){
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
LT_memset(VGA,0x00,(352>>2)*240);
}
if (LT_VIDEO_MODE == 1){
VGA_ClearPalette();
//clear screen
asm mov dx,0x03C4
asm mov ax,0xFF02
asm out dx,ax
LT_memset(VGA,0x00,(352>>2)*240);
}
if (LT_VIDEO_MODE == 2);
if (LT_VIDEO_MODE == 3){
LT_TGA_MapPage(1);LT_memset(CGA,0x00,32*1024);
LT_TGA_MapPage(0);LT_memset(CGA,0x00,32*1024);
}
}
// Uncompress BMP image (either RLE8 or RLE4)
// FROM GIMP: removed most multiplications, all divisions, and delta
void decompress_RLE_BMP(word fp, unsigned char bpp, int width, int height,byte palette){
int xpos = 0; int ypos = 0;
int i,j,n;
int total_bytes_read = 0;
byte i_max = 0;
byte _bpp = 0;
byte count_val[2] = {0,0};//Get mode Get data
if (bpp == 8) _bpp = 3;
if (bpp == 4) _bpp = 2;
while (ypos < height && xpos <= width){
int y_offset = ypos * width;
LT_fread(fp,2,count_val);
// Count + Color - record
if (count_val[0] != 0){
// encoded mode run - count == run_length; val == pixel data
if (count_val[1]) (count_val[1]+=palette);
for (j = 0; ( j < count_val[0]) && (xpos < width);){
for (i = 1;((i <= (8 >> _bpp)) && (xpos < width) && ( j < count_val[0]));i++, xpos++, j++){
LT_tile_tempdata[y_offset + xpos] = (count_val[1] & (((1<<bpp)-1) << (8 - (i << _bpp)))) >> (8 - (i << _bpp));
}
}
}
// uncompressed record
if ((count_val[0] == 0) && (count_val[1] > 2)){
n = count_val[1];
total_bytes_read = 0;
for (j = 0; j < n; j += (8 >> _bpp)){
// read the next byte in the record
byte c; byte d = 0;
LT_fread(fp,1,&c);
if (c) d = palette; else d = 0;
c+=d;
total_bytes_read++;
// read all pixels from that byte
i_max = 8 >> _bpp;
if (n - j < i_max) i_max = n - j;
i = 1;
while ((i <= i_max) && (xpos < width)){
LT_tile_tempdata[y_offset + xpos] = (c >> (8-(i<<_bpp))) & ((1<<bpp)-1);
i++; xpos++;
}
}
// absolute mode runs are padded to 16-bit alignment
if (total_bytes_read & 1) LT_fseek(fp,1,1);
}
// Line end
if ((count_val[0] == 0) && (count_val[1] == 0)){ypos++;xpos = 0;}
// Bitmap end
if ((count_val[0] == 0) && (count_val[1] == 1)) break;
// Deltarecord. I did not find any BMP using this
//if ((count == 0) && (val == 2)){
// count = fgetc(fp);val = fgetc(fp);xpos += count; ypos += val;
//}
}
}
byte LT_mode_sprite = 0;
byte LT_pixel_format = 0;
//Compatible with all 4/8 bpp BMP files
//Mode: 0 = image; 1 = tiles; 2 = sprite; 3 = window; 4/5 = animation/font
void LT_Load_BMP(char *file, char *dat_string, int mode, int sprite_number){
int index;
int read_pal = 1;
word x;
word _offset = 0;
byte b = 0;
byte d = 0;
word num_colors;
byte get_pal = 1;
byte first_color = 0;
byte pal_colors = 0;
byte RLE = 0;
word DIB_HEADER_SIZE = 0;
word header;
SPRITE *s = &sprite[sprite_number];
word _width;
word _height;
word *width;
word *height;
word fp = LT_fopen(file,0);
if(!fp) LT_Error("Can't find $",file);
if (dat_string) {DAT_Seek2(fp,dat_string);LT_Filename = dat_string;}
else LT_Filename = file;
LT_mode_sprite = 0;
LT_pixel_format = 0;
//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
LT_fread(fp,2,&header);
if (header != 0x4D42) LT_Error("Not a BMP file $",LT_Filename);
LT_fseek(fp, 12,SEEK_CUR);
LT_fread(fp, 2,&DIB_HEADER_SIZE );
LT_fseek(fp, 2, SEEK_CUR);
LT_fread(fp, 2, width);
LT_fseek(fp, 2, SEEK_CUR);
LT_fread(fp, 2, height);
LT_fseek(fp, 4, SEEK_CUR);
LT_fread(fp, 1, <_pixel_format);
LT_fseek(fp, 1, SEEK_CUR);
LT_fread(fp, 1, &RLE); //0 none, 1 = 8 bit, 2 = 4 bit
LT_fseek(fp, 15,SEEK_CUR);
LT_fread(fp, 2, &num_colors);
//Advance to palette data
//Skip "color space information" (if it is there) added by modern apps like GIMP
DIB_HEADER_SIZE -=34;
LT_fseek(fp,DIB_HEADER_SIZE, 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_tileset_height != 200) LT_Error("Wrong size for image, size must be 320x200: ",LT_Filename);
//if (LT_tileset_width != 320) LT_Error("Wrong size for image, size must be 320x200: ",LT_Filename);
pal_colors = 208;first_color = 0;
break;
case 1://If reading tileset
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;
LT_tileset_width = s->width; LT_tileset_height = s->height;
LT_mode_sprite = 1;
break;
case 4://If reading Animation
case 5://or font
if (LT_pixel_format !=4)LT_Error("Wrong format for loading animation, must be 4 bit per pixel: $",LT_Filename);
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);
LT_tileset_width = _width; LT_tileset_height = _height;
if (mode == 4){pal_colors = 4; first_color = 248;}
else {pal_colors = 4; first_color = 252;}//If reading font
LT_mode_sprite = 2;
break;
}
if (mode == 4) LT_mode_sprite=2;
if (mode == 5) LT_mode_sprite=3;
if (LT_mode_sprite == 1){
if ((LT_pixel_format !=4)&&(LT_VIDEO_MODE == 3))LT_Error("TANDY sprite must be 4 bit per pixel: $",LT_Filename);
if ((LT_pixel_format !=4)&&(LT_VIDEO_MODE == 0))LT_Error("EGA sprite must be 4 bit per pixel: $",LT_Filename);
if ((LT_pixel_format !=8)&&(LT_VIDEO_MODE == 1))LT_Error("VGA sprite must be 8 bit per pixel: $",LT_Filename);
}
if ((LT_DETECTED_CARD == 1)&& (LT_VIDEO_MODE ==0)){
if (mode > 1) read_pal = 0;
else {first_color = 0; pal_colors = 16; read_pal = 1;}
}
//Load Palette
for(index=first_color;index<first_color+num_colors;index++){
if (index-first_color == pal_colors) get_pal = 0;
if (get_pal && read_pal){
byte col[3];
LT_fread(fp,3,col);
LT_tileset_palette[(int)(index*3+2)] = col[0] >> 2;
LT_tileset_palette[(int)(index*3+1)] = col[1] >> 2;
LT_tileset_palette[(int)(index*3+0)] = col[2] >> 2;
} else {
LT_fseek(fp,3,1);
}
LT_fseek(fp,1,1);
}
//
//EGA INDEX NUMBERS:
// 0-7/15-23
if ((LT_VIDEO_MODE == 0) && (LT_DETECTED_CARD == 1)){
_memcpy(<_tileset_palette[16*3],<_tileset_palette[8*3],8*3);;
}
//PROCESS IMAGE
if (LT_VIDEO_MODE == 1){
if (LT_mode_sprite==1) b=208;
if (LT_mode_sprite==2) b=248;
if (LT_mode_sprite==3) b=252; //Font color from 252 to 255
}
if (LT_pixel_format == 4){
if (RLE){
word w = width[0];word h = height[0];
decompress_RLE_BMP(fp,LT_pixel_format,w,h,b);
} else {
int pixels = (LT_tileset_width>>1)*LT_tileset_height;
LT_fread(fp,pixels,LT_tile_tempdata2);
for (x = 0; x < pixels; x ++){
unsigned char c = LT_tile_tempdata2[x];//it is a 4 bit BMP, every byte contains 2 pixels
LT_tile_tempdata[_offset++] = (c >>4) +b;
LT_tile_tempdata[_offset++] = (c & 0x0F)+b;
}
}
}
if (LT_pixel_format == 8){
if (RLE){
word w = width[0];word h = height[0];
decompress_RLE_BMP(fp,LT_pixel_format,w,h,b);
} else {
for (x = 0; x < LT_tileset_width*LT_tileset_height; x ++){
unsigned char c = 0;
LT_fread(fp,1,&c);
if (c) d = b; else d = 0;
LT_tile_tempdata[_offset] = c + d;
_offset++;
}
}
}
if (LT_pixel_format == 1){
}
LT_fclose(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 = 0;
byte size = 32;
int code_size;
LT_Filename = file;
LT_Load_BMP(file,dat_string,4,0);
index = 0; //use a chunk of temp allocated RAM to rearrange the sprite frames
//Rearrange sprite frames one after another in temp memory
for (tileX=0;tileX<128;tileX+=size){
offset = (128*31)+tileX;
if (LT_VIDEO_MODE == 1){
LT_tile_tempdata2[index] = size;
LT_tile_tempdata2[index+1] = size;
index+=2;
}
for(x=0;x<32;x++){
_memcpy(<_tile_tempdata2[index],<_tile_tempdata[offset-(x<<7)],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];
if (LT_VIDEO_MODE <2){
for (frame = 0; frame < LT_Loading_Animation.nframes; frame++){ //
if (LT_VIDEO_MODE == 1){
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 == 3){
offset = 0;index = 0;
for (offset = 0; offset < 4096; offset+=2){
LT_sprite_data[index++] = (LT_tile_tempdata2[offset]<<4)+LT_tile_tempdata2[offset+1];
}
}
//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
{
int i;
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 == 3) {
int i;
word offset = s->frame<<9;
word offset1 = (160*26)+72;
tga_page = 0;
LT_TGA_MapPage(0);
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;
}
LT_TGA_MapPage(1);
}
}
asm STI
asm mov al,020h
asm mov dx,020h
asm out dx, al //PIC, EOI
}
void LT_Start_Loading(){//77.144
LT_Cycle_palcounter = 0;
LT_Stop_Music();
LT_Fade_out();
LT_Disable_Speaker();
LT_Reset_Sprite_Stack();
LT_Unload_Sprites();
//Wait Vsync
LT_vsync();
//Reset scroll
asm mov dx,0x03D4
asm mov ax,0x000D
asm out dx,ax
asm mov ax,0x000C
asm out dx,ax
SCR_X = 0; SCR_Y = 0;
TGA_SCR_X = 0;TGA_SCR_Y = 0;
TANDY_SCROLL_X = 0;TANDY_SCROLL_Y = 0;
LT_ClearScreen();
LT_memset(sprite_id_table,0,19*256);
//change color 0, 1, 2 (black and white)
if (!LT_var){
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
asm mov al,0x36
asm out 0x43,al
asm mov al,92
asm out 0x40,al //lo-byte
asm mov al,155
asm out 0x40,al //hi-byte
//set interrupt
LT_setvect(0x1C, LT_Loading); //interrupt 1C not available on NEC 9800-series PCs.
asm STI
//Wait Vsync
LT_vsync();
LT_Keys[LT_ESC] = 0;LT_Keys[LT_ENTER] = 0;LT_Keys[LT_ACTION] = 0; LT_Keys[LT_JUMP] = 0;
LT_Fade_in();
}
void LT_End_Loading(){
//unsigned long spd = 1193182/60;
LT_Fade_out();
LT_Keys[LT_ESC] = 0;LT_Keys[LT_ENTER] = 0;LT_Keys[LT_ACTION] = 0; LT_Keys[LT_JUMP] = 0;
asm CLI
//set frame counter
asm mov al,0x36
asm out 0x43,al
asm mov al,0xFF
asm out 0x40,al
asm out 0x40,al
LT_setvect(0x1C, LT_old_time_handler);
asm STI
if (LT_Loaded_Image){
int i;
if (LT_VIDEO_MODE<2){
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;
}
if (LT_VIDEO_MODE == 3){
word offset = 0;
for (i = 0; i < 8;i++){
LT_TGA_MapPage(1);
_memcpy(LT_map_collision,&CGA[offset],4096);
LT_TGA_MapPage(0);
_memcpy(&CGA[offset],LT_map_collision,4096);
offset+=4096;
}
LT_Loaded_Image = 0;
}
LT_Fade_in();
}
LT_VGA_Return_4Planes();
}
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)();
//This is smooth in dosbox, PCem, 86Box, original EGA/VGA many VGA compatibles on very slow systems.
//This is optimized to be as fast as it can be for the poor 8088
void LT_WaitVsync_VGA(){
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; pix = p1[SCR_X & 7];} //EGA
if (LT_VIDEO_MODE == 1) {y += x>>2; pix = p[SCR_X & 3];} //VGA
gfx_draw_page++;
//change scroll registers:
asm {
cli
mov cl,8; mov dx,003d4h; //VGA PORT
mov ax,y; shl ax,cl; or ax,00Dh; out dx,ax; //(y << 8) | 0x0D; to VGA port
mov ax,y; and ax,0FF00h; or ax,00Ch; out dx,ax; //(y & 0xFF00) | 0x0C; to VGA port
sti
}
//Try to catch Vertical retrace
asm mov dx,0x3DA; asm mov bl,0x08;
WaitNotVsync: asm in al,dx; asm test al,bl; asm jnz WaitNotVsync;
WaitVsync: asm in al,dx; asm test al,bl; asm jz WaitVsync;
//The smooth panning happens here
asm {
cli
mov dx,0x03C0;
mov al,0x33; out dx,al; //0x20 | 0x13 (palette normal operation | Pel panning reg)
mov al,byte ptr pix; out dx,al;
sti
}
//I don't think this is necessary
//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
//AC index 0x03c0
//Restore controller value
//asm mov ax,word ptr _ac
//asm out dx,ax
//enable interrupts
}
//Alternative functions taken from keen70Hz, should be compatible with most cards
static unsigned char MaxHblankLength = 1;
static unsigned char benchmark_hblank_length(){
unsigned char hblank_length;
asm cli
asm mov dx, 3DAh
wait_blank_end: // if we are in hblank or vblank, wait for it to end
asm in al, dx // Read 3DAh - Status Register
asm test al, 1 // Bit 0: Display Blank
asm jnz wait_blank_end
wait_active_end: // wait for the end of the active scanline
asm in al, dx // Read 3DAh again
asm test al, 1
asm jz wait_active_end
// We are now right at the start of a blank period, either hblank or vblank
// Calculate how many cycles this blank lasts.
asm mov cx, 0 // store in cl: seen_status, ch: hblank_len
calc_blank_length: // measure how many I/O port read cmds we can do in blank
asm in al, dx // Read port.
asm or cl, al // Track in cl if this blank period contained vsync bit
asm inc ch // Accumulate count of I/Os performed
asm test al, 1 // Still in blank period?
asm jnz calc_blank_length
asm test cl, 8 // Blank period is now over. Check if it included vsync
asm jnz wait_blank_end // If so, restart all from scratch. We wanted hblank.
asm mov hblank_length, ch
asm sti
return hblank_length;
}
void CalibrateMaxHblankLength(){
int i;
MaxHblankLength = 1;
// Benchmark: What is the maximum amount of ISA/PCI/AGP port I/Os that can be
// performed within a single hblank period?
// I.e. find what is the max length on the bus that a Hblank can take.
for(i = 0; i < 1000; ++i)
{
int length = benchmark_hblank_length();
if (length > MaxHblankLength) MaxHblankLength = length;
}
// Add just a tiny bit more over the count that we got.
if (MaxHblankLength <= 253) MaxHblankLength += 2;
//printf("MaxHblankLength: %d\n", (int)MaxHblankLength);
}
void LT_WaitVsync_VGA_Compatible(){
word x = SCR_X;
word y = SCR_Y + pageflip[gfx_draw_page&1];
int i;
//
y*=LT_VRAM_Logical_Width;
if (LT_VIDEO_MODE == 0) {y += x>>3; pix = p1[SCR_X & 7];} //EGA
if (LT_VIDEO_MODE == 1) {y += x>>2; pix = p[SCR_X & 3];} //VGA
gfx_draw_page++;
asm mov dx, 3DAh
wait_for_active_picture:// Wait until we are in visible picture area.
asm in al,dx // Read 3DAh - Status Register
asm test al,1 // Bit 0: set if we are in Display Blank.
asm jnz wait_for_active_picture
// We are now in visible picture area (so can't be in vsync, or right headed
// into it)
asm mov cx,0
wait_for_hblank:
asm cli // Enter time critical stage: estimating the length of a blank.
asm mov cl, MaxHblankLength // Reset wait counter to default
loop_hblank_length_times:
asm in al,dx // Read status port
asm test al,1 // Are we in display blank?
asm jnz in_blank// If 1, then we are in blank
// Else 0, we are still in visible picture area. Enable interrupts and restart the wait.
asm sti
asm jmp wait_for_hblank
in_blank: // We are in blank, but have we slipped over to vsync?
asm test al,8 // Test if in vsync?
asm jz in_blank_not_vsync // If 0, we are not in vsync
asm sti // Else 1, we are in vsync, so we blew it. Restart the wait.
asm jmp wait_for_active_picture
in_blank_not_vsync: // We are in blank, either hblank or vblank
// Decrement search counter
asm loop loop_hblank_length_times // And loop back if we still need to.
// If we get here, we have entered a display blank period that is longer than
// a hblank interval, so we conclude we must have just now entered a vblank.
// (but we aren't yet at start of vsync)
// Interrupts are disabled at this point, so we can safely update
// Display Start (DS) and Horizontal Shift Count (HS) registers so all
// adapters will latch it properly, with all their varying quirky behaviors.
// (Pedantically, it is tiny bit better better to write DS register before HS,
// because IBM EGA and VGA latch the DS register before the HS register)
//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
//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
//enable interrupts
asm sti
}
//Generic function that may work on many "SVGA" cards on very fast systems
//Assumes a very fast PC which will catch the vsync at the very beginning,
//and the CPU will update all registers at once, no need to optimize in asm.
//Tested on toshiba satellite laptop
void LT_WaitVsync_VGA_crappy(){//77.128
word x = SCR_X;
word y = SCR_Y + pageflip[gfx_draw_page&1];
int i;
y*=LT_VRAM_Logical_Width;
if (LT_VIDEO_MODE == 0) {y += x>>3; pix = p1[SCR_X & 7];} //EGA
if (LT_VIDEO_MODE == 1) {y += x>>2; pix = p[SCR_X & 3];} //VGA
gfx_draw_page++;
//Try to catch Vertical retrace
asm mov dx,0x3DA; asm mov bl,0x08;
WaitNotVsync: asm in al,dx; asm test al,bl; asm jnz WaitNotVsync;
WaitVsync: asm in al,dx; asm test al,bl; asm jz WaitVsync;
//change scroll registers:
asm {
cli
mov cl,8; mov dx,003d4h; //VGA PORT
mov ax,y; shl ax,cl; or ax,00Dh; out dx,ax; //(y << 8) | 0x0D; to VGA port
mov ax,y; and ax,0FF00h; or ax,00Ch; out dx,ax; //(y & 0xFF00) | 0x0C; to VGA port
mov dx,0x03C0
mov al,0x33 //0x20 | 0x13 (palette normal operation | Pel panning reg)
out dx,al
mov al,byte ptr pix
out dx,al
sti
}
}
byte panning = 0;
byte static_screen = 0;
//CRT Page
//Bits 0-2 select the 16K page showed 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
//
//Tandy graphics will use the last 128K of RAM for the 8 pages (16k each)
//We should use the last 4 pages (2 32K pages) for owr double buffer
byte page_show[] = {0xC4,0xC6};//show Page 4 / show page 6 (+ mode 320x200)
byte page_write[] = {0x20,0x30};//write Page 4 / write page 6
void LT_TGA_MapPage(byte wpage){
byte val = (page_write[wpage&1] | page_show[tga_page&1]);