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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
|
/* $Id: dosmesa.c,v 1.4 2001/03/03 20:33:29 brianp Exp $ */
/*
* Mesa 3-D graphics library
* Version: 2.3
* Copyright (C) 1995-1997 Brian Paul
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* $Log: dosmesa.c,v $
* Revision 1.4 2001/03/03 20:33:29 brianp
* lots of gl_*() to _mesa_*() namespace clean-up
*
* Revision 1.3 2000/11/22 08:55:52 joukj
*
*
* Modified Files:
* Mesa/src/Allegro/amesa.c Mesa/src/DOS/dosmesa.c
* Mesa/src/FX/fxdd.c Mesa/src/FX/fxdrv.h
* Mesa/src/FX/fxfastpath.c
* Mesa/src/GGI/include/ggi/mesa/ggimesa.h
* Mesa/src/OSmesa/osmesa.c Mesa/src/SVGA/svgamesa.c
* Mesa/src/Trace/tr_control.c Mesa/src/Windows/wgl.c
* Mesa/src/X/xmesaP.h Mesa/src/X86/3dnow.c Mesa/src/X86/katmai.c
* Mesa/src/X86/x86.c
* Removed Files:
* Mesa/src/mms_depend
*
* Oops,... all files containing Caps in directory name or file name were
* missing in my types->mtypes commit.
*
* ----------------------------------------------------------------------
*
* Revision 1.2 2000/09/26 20:54:10 brianp
* First batch of OpenGL SI related changes:
* Renamed struct gl_context to struct __GLcontextRec.
* Include glcore.h, setup GL imports/exports.
* Replaced gl_ prefix with _mesa_ prefix in context.[ch] functions.
* GLcontext's Visual field is no longer a pointer.
*
* Revision 1.1.1.1 1999/08/19 00:55:41 jtg
* Imported sources
*
* Revision 1.2 1999/03/28 21:11:57 brianp
* updated SetBuffer driver function
*
* Revision 1.1 1999/02/24 03:56:31 brianp
* initial check-in
*
*
* Revision 1.5 1997/06/19 22:00:00 Brian Paul
* Removed all ColorShift stuff
*
* Revision 1.4 1997/06/03 19:00:00 Brian Paul
* Replaced VB->Unclipped[] with VB->ClipMask[]
*
* Revision 1.3 1997/05/28 07:00:00 Phil Frisbie, Jr.
* Now pass red/green/blue/alpha bits to gl_create_visual()
* Fixed DJGPP mode 13 support.
*
* Revision 1.2 1996/12/08 16:13:45 Charlie
* Added VESA support via Scitechs SVGA kit.
*
* Revision 1.1 1996/12/08 16:09:52 Charlie
* Initial revision
*
*/
/*
* DOS VGA/VESA/MGL/Mesa interface.
*
*/
/*
*
* TODO: (cw)
* Improve the colour matcher for rgb non vesa modes, its pretty bad and incorrect
* Keyboard interrupt.
* Comments and tidy up.
* Add support for VESA without SVGAKIT.
* DirectX Support.
* Better GLIDE Support.
* Clear up the #ifdef madness.
*/
#ifdef DOSVGA
#if defined(DOSVGA) && !defined(GLIDE) && defined(DJGPP) && !defined(UNIVBE) && !defined(MGL)
/* Should help cut down on the crazy #if`s */
#define MODE13 1
#else
#undef MODE13
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <dos.h>
#ifdef DJGPP
#include <go32.h>
#endif
#ifdef MGL
#include <mgraph.h>
#endif
#include "GL/DOSmesa.h"
#include "context.h"
#include "matrix.h"
#include "mtypes.h"
#ifdef GLIDE
static void glideshutdown( void );
#include "vb.h"
#include "glide.h"
/* the glide modes available, set one */
//#define GLIDE_MODE GR_RESOLUTION_
#define GLIDE_MODE GR_RESOLUTION_800x600
//#define GLIDE_MODE GR_RESOLUTION_640x480
static GrVertex gr_vtx,gr_vtx1,gr_vtx2;
#endif
#ifdef UNIVBE
/* You get this file from Scitechs VESA development kit */
#include "svga.h"
/*
Set these to the VESA mode you require, the first is for 256 colour modes,
the other is High colour modes, they must both be the same XRes and YRes.
*/
#define VESA_256COLOUR_MODE 0x11c
#define VESA_HICOLOUR_MODE 0x11f
#endif
struct DOSmesa_context {
GLcontext *gl_ctx; /* the core Mesa context */
GLvisual *gl_vis; /* describes the color buffer */
GLframebuffer *gl_buffer; /* the ancillary buffers */
GLuint index; /* current color index */
GLint red, green, blue; /* current rgb color */
GLint width, height; /* size of color buffer */
GLint depth; /* bits per pixel (8,16,24 or 32) */
};
static DOSMesaContext DOSMesa = NULL; /* the current context */
#ifdef UNIVBE
SV_devCtx *DC=NULL;
int useLinear = TRUE;
SV_modeInfo *mi=NULL;
unsigned long modeNumber = 0;
int activePage = 0;
int visualPage = 1;
#endif
#if defined(MODE13)
/* DOSVGA With no UniVBE support */
unsigned char *video_buffer;
#define VID_BUF(x,y) *(video_buffer+x+(y*320))
#if defined(__WATCOMC__) && defined(__386__) && defined(__DOS__)
void setupcopy(void);
void copyscr(void);
/* Watcom C specfic, screen copy and clear */
#pragma aux setupcopy = \
".386P"\
"push ebp" \
"mov esi,video_buffer" \
"mov edi,0xa0000" \
"mov ecx,2000" \
"xor ebp,ebp";
#pragma aux copyscr = \
".386P" \
"lop1: mov eax,[esi]"\
"mov ebx,[esi+4]"\
"mov edx,[esi+8]"\
"mov [edi],eax"\
"mov eax,[esi+12]"\
"mov dword ptr [esi],ebp"\
"mov dword ptr [esi+4],ebp"\
"mov dword ptr [esi+8],ebp"\
"mov dword ptr [esi+12],ebp"\
"mov [edi+4],ebx"\
"mov [edi+8],edx"\
"mov [edi+12],eax"\
"mov eax,[esi+16]"\
"mov ebx,[esi+4+16]"\
"mov edx,[esi+8+16]"\
"mov [edi+16],eax"\
"mov eax,[esi+12+16]"\
"mov dword ptr [esi+16],ebp"\
"mov dword ptr [esi+4+16],ebp"\
"mov dword ptr [esi+8+16],ebp"\
"mov dword ptr [esi+12+16],ebp"\
"mov [edi+4+16],ebx"\
"mov [edi+8+16],edx"\
"mov [edi+12+16],eax"\
"add esi,32"\
"add edi,32"\
"dec ecx"\
"jnz lop1"\
"pop ebp"\
modify exact [edi esi eax ebx ecx] ;
#endif // WATCOM
#endif // MODE13
/*
* Convert Mesa window Y coordinate to VGA screen Y coordinate:
*/
#define FLIP(Y) (DOSMesa->height-(Y)-1)
unsigned short vga_cindex = 32768 ;
static points_func choose_points_function( void );
static line_func choose_line_function( void );
static triangle_func choose_polygon_function( void );
static void fast_tri(GLcontext *ctx,GLuint v0, GLuint v1, GLuint v2, GLuint pv );
static points_func choose_points_function( void )
{
return NULL;
}
static line_func choose_line_function( void )
{
return NULL;
}
static triangle_func choose_triangle_function( void )
{
#if defined(MODE13)
return NULL;
#endif
#if defined(GLIDE)
return fast_tri;
#endif
return NULL;
}
#if defined(MODE13)
void setgfxmode(void);
void settextmode(void);
#ifndef DJGPP
#pragma aux setgfxmode = \
"mov ax,13h" \
"int 10h" \
modify [eax];
#pragma aux settextmode = \
"mov ax,3h" \
"int 10h" \
modify [eax];
#else
void setgfxmode(void)
{
union REGS in_regs,out_regs;
in_regs.x.ax = 0x13;
int386(0x10,&in_regs,&out_regs);
}
void settextmode(void)
{
union REGS in_regs,out_regs;
in_regs.x.ax = 0x3;
int386(0x10,&in_regs,&out_regs);
}
#endif
int set_video_mode(unsigned short x,unsigned short y,char mode)
{
setgfxmode();
return 1; /* likelyhood of this failing is very small */
}
void restore_video_mode(void)
{
settextmode();
}
int vga_getcolors(void)
{
return vga_cindex;
}
int vga_getxdim(void)
{
return 320;
}
int vga_getydim(void)
{
return 200;
}
static unsigned short num_rgb_alloc = 1; /* start from 1, zero is black */
/* an unlikely colour */
static unsigned char last_r=1,last_g=255,last_b=99;
extern void set_onecolor(int index,int R,int G,int B);
static unsigned char rgbtable[64][64][64];
void vga_setrgbcolor(int r,int g,int b)
{
/*
* make this into a translation table
*/
DOSMesa->red = r;
DOSMesa->green = g ;
DOSMesa->blue = b ;
r/=4; g/=4; b/=4;
if( (r == last_r) && (g == last_g) && (b == last_b) ) return;
last_r = r ;
last_g = g ;
last_b = b ;
if(r+g+b == 0 ) {
DOSMesa->index = 0 ;
return ;
}
if( rgbtable[r][g][b] == 0 ) {
/* not allocated yet */
if(num_rgb_alloc<256) {
DOSMesa->index = num_rgb_alloc;
set_onecolor(num_rgb_alloc,r,g,b);
rgbtable[r][g][b] = num_rgb_alloc;
num_rgb_alloc++;
return;
} else {
/* need to search for a close colour */
{
unsigned short pass ;
for(pass=0;pass<64;pass++) {
if(r-pass>0) {
if( rgbtable[r-pass][g][b] !=0 ) {
rgbtable[r][g][b] = rgbtable[r-pass][g][b];
DOSMesa->index = rgbtable[r-pass][g][b];
return;
}
}
if(r+pass<64) {
if( rgbtable[r+pass][g][b] !=0 ) {
rgbtable[r][g][b] = rgbtable[r+pass][g][b];
DOSMesa->index = rgbtable[r+pass][g][b];
return;
}
}
}
}
rgbtable[r][g][b] = rand()%255;
}
}
DOSMesa->index = rgbtable[r][g][b];
}
#if defined(DJGPP)
static int dos_seg;
#endif
void vga_clear(void)
{
/* Check if we`re using watcom and DOS */
#if defined(__WATCOMC__) && defined(__386__) && defined(__DOS__)
setupcopy();
copyscr();
#else
#if defined (DJGPP)
asm ("
pusha
pushw %es
movw _dos_seg, %es
movl _video_buffer, %esi
movl $0xa0000, %edi
movl $64000, %ecx
rep ; movsb
popw %es
popa
");
#else
/* copy the RAM buffer to the Video memory */
memcpy((unsigned char *)0xa0000,video_buffer, vga_getxdim()*vga_getydim() );
#endif //DJGPP
/* clear the RAM buffer */
memset(video_buffer,0, vga_getxdim()*vga_getydim() );
#endif //WATCOMC
}
#ifndef DEBUG
#define vga_drawpixel(x,y) { VID_BUF(x,y) = DOSMesa->index; }
#else
void vga_drawpixel(x,y)
{
VID_BUF(x,y) = DOSMesa->index;
}
#endif
int vga_getpixel(int x,int y)
{
return 1;
}
void vga_flip(void)
{
}
void vga_setcolor(int index)
{
/* does something, what i`ve no idea */
DOSMesa->index = index;
}
#endif
#if defined(UNIVBE)
/* UniVBE VESA support */
void set_video_mode(unsigned short x,unsigned short y,char mode)
{
if( setup_vesa_mode(x,y,mode) == FALSE ) {
fprintf(stderr,"VESA: Set mode failed\n");
exit(1);
}
}
/*
This is problematic as we don`t know what resolution the user program
wants when we reach here. This is why the 256 colour and HiColour modes
should be the same resolution, perhaps i`ll make this an environment
variable
*/
void check_mi(void)
{
if(mi!=0) return;
if(DC==NULL) {
DC = SV_init( TRUE );
}
if(modeNumber == 0 ) {
modeNumber = VESA_HICOLOUR_MODE;
}
SV_getModeInfo(modeNumber,mi);
return;
}
int setup_vesa_mode(short height,short width,short depth)
{
if(DC==NULL) {
DC = SV_init( TRUE );
/* how many bits per pixel */
if( depth == 0)
modeNumber = VESA_256COLOUR_MODE;
else
modeNumber = VESA_HICOLOUR_MODE;
}
/* Check if correct VESA Version is available */
if( !DC || DC->VBEVersion < 0x0102) {
fprintf(stderr,"Require a VESA VBE version 1.2 or higher\n");
return FALSE;
}
/* Check for LFB Supprt */
if(DC->VBEVersion < 0x0200 ) {
useLinear = FALSE;
} else {
useLinear = TRUE ;
}
/* Get desired mode info */
if(!SV_getModeInfo( modeNumber, mi))
return FALSE;
/* Set VESA mode */
if(!SV_setMode(modeNumber | svMultiBuffer, FALSE, TRUE, mi->NumberOfPages) )
return FALSE;
return TRUE;
}
void restore_video_mode(void)
{
SV_restoreMode();
}
void vga_clear(void)
{
SV_clear(0);
}
void vga_flip(void)
{
activePage = 1-activePage;
visualPage = 1-activePage;
SV_setActivePage(activePage);
/*
Change false to true if you`re getting flickering
even in double buffer mode, ( sets wait for Vertical retrace )
*/
SV_setVisualPage(visualPage,false);
}
int vga_getcolors(void)
{
check_mi();
switch ( mi->BitsPerPixel ) {
case 8:
return 256;
case 15:
case 16:
return 32768;
default:
return 64000;
}
}
int vga_getxdim(void)
{
check_mi();
return mi->XResolution;
}
int vga_getydim(void)
{
check_mi();
return mi->YResolution;
}
unsigned long current_color = 255;
void vga_setrgbcolor(int r,int g,int b)
{
DOSMesa->red = r;
DOSMesa->green = g ;
DOSMesa->blue = b ;
current_color = SV_rgbColor(r,g,b);
}
void vga_setcolor(int index)
{
DOSMesa->index = index;
current_color = index;
}
void vga_drawpixel(x,y)
{
SV_putPixel(x,y,current_color);
}
/* TODO: */
int vga_getpixel(x,y)
{
/* return (int)SV_getPixel(x,y); */
fprintf(stderr,"vga_getpixel: Not implemented yet\n");
return 1;
}
/* End of UNIVBE section */
#endif
/* Scitechs MegaGraphicsLibrary http://www.scitechsoft.com/ */
#if defined(MGL)
/* MGL support */
struct MI {
unsigned short BitsPerPixel;
unsigned long XResolution;
unsigned long YResolution;
};
struct MI*mi;
static MGLDC*DC;
static int activePage = 0;
static int visualPage = 1;
static int modeNumber = 0;
void set_video_mode(unsigned short xres,unsigned short yres,char mode)
{
int i,driver = grDETECT,dmode = grDETECT;
event_t evt;
/* Start the MGL with only the SVGA 16m driver active */
MGL_registerDriver(MGL_SVGA16NAME,SVGA16_driver);
if (!MGL_init(&driver,&dmode,"..\\..\\"))
MGL_fatalError(MGL_errorMsg(MGL_result()));
if ((DC = MGL_createDisplayDC(false)) == NULL)
MGL_fatalError(MGL_errorMsg(MGL_result()));
MGL_makeCurrentDC(DC);
}
/*
This is problematic as we don`t know what resolution the user program
wants when we reach here. This is why the 256 colour and HiColour modes
should be the same resolution, perhaps i`ll make this an environment
variable
*/
#define MGL_HICOLOUR_MODE 0
void check_mi(void)
{
if(mi!=0) return;
if(DC==NULL) {
// DC = SV_init( TRUE );
}
if(modeNumber == 0 ) {
modeNumber = MGL_HICOLOUR_MODE;
}
// SV_getModeInfo(modeNumber,mi);
return;
}
void restore_video_mode(void)
{
MGL_exit();
}
void vga_clear(void)
{
MGL_clearDevice();
}
void vga_flip(void)
{
activePage = 1-activePage;
visualPage = 1-activePage;
// SV_setActivePage(activePage);
/*
Change false to true if you`re getting flickering
even in double buffer mode, ( sets wait for Vertical retrace )
*/
// SV_setVisualPage(visualPage,false);
}
int vga_getcolors(void)
{
check_mi();
switch ( mi->BitsPerPixel ) {
case 8:
return 256;
case 15:
case 16:
return 32768;
default:
return 64000;
}
}
int vga_getxdim(void)
{
check_mi();
return mi->XResolution;
}
int vga_getydim(void)
{
check_mi();
return mi->YResolution;
}
unsigned long current_color = 255;
void vga_setrgbcolor(int r,int g,int b)
{
DOSMesa->red = r;
DOSMesa->green = g ;
DOSMesa->blue = b ;
current_color = MGL_rgbColor(DC,r,g,b);
}
void vga_setcolor(int index)
{
DOSMesa->index = index;
current_color = index;
}
void vga_drawpixel(x,y)
{
MGL_pixelCoord(x,y);
}
/* TODO: */
int vga_getpixel(x,y)
{
/* return (int)SV_getPixel(x,y); */
fprintf(stderr,"vga_getpixel: Not implemented yet\n");
return 1;
}
/* End of UNIVBE section */
#endif
#ifdef GLIDE
/* GLIDE support */
static GrHwConfiguration hwconfig;
void set_video_mode(unsigned short x,unsigned short y,char mode)
{
grGlideInit();
if( grSstQueryHardware( &hwconfig ) ) {
grSstSelect( 0 ) ;
if( !grSstOpen( GLIDE_MODE,
GR_REFRESH_60Hz,
GR_COLORFORMAT_ABGR,
GR_ORIGIN_UPPER_LEFT,
GR_SMOOTHING_ENABLE,
2 ) ) {
fprintf(stderr,"Detected 3DFX board, but couldn`t initialize!");
exit(1);
}
grBufferClear( 0, 0, GR_WDEPTHVALUE_FARTHEST);
grDisableAllEffects();
atexit( glideshutdown );
// guColorCombineFunction( GR_COLORCOMBINE_ITRGB );
// grTexCombineFunction( GR_TMU0, GR_TEXTURECOMBINE_ZERO);
}
}
void restore_video_mode(void)
{
}
static void glideshutdown( void )
{
grGlideShutdown() ;
}
void vga_clear(void)
{
grBufferSwap(0);
grBufferClear( 0, 0, GR_WDEPTHVALUE_FARTHEST);
}
void vga_flip(void)
{
}
int vga_getcolors(void)
{
return 32768;
}
int vga_getxdim(void)
{
#if GLIDE_MODE == GR_RESOLUTION_800x600
return 800;
#else
return 640;
#endif
}
int vga_getydim(void)
{
#if GLIDE_MODE == GR_RESOLUTION_800x600
return 600;
#else
return 480;
#endif
}
unsigned long current_color = 255;
void vga_setrgbcolor(int r,int g,int b)
{
DOSMesa->red = r;
DOSMesa->green = g ;
DOSMesa->blue = b ;
}
void vga_setcolor(int index)
{
DOSMesa->index = index;
}
void vga_drawpixel(x,y)
{
gr_vtx.x = x;
gr_vtx.y = y;
gr_vtx.z = 0;
gr_vtx.r = DOSMesa->red;
gr_vtx.g = DOSMesa->green;
gr_vtx.b = DOSMesa->blue;
grDrawPoint( &gr_vtx );
}
static void fast_tri(GLcontext *ctx,GLuint v0, GLuint v1, GLuint v2, GLuint pv )
{
struct vertex_buffer *VB = ctx->VB;
gr_vtx.z = 0;
gr_vtx1.z = 0;
gr_vtx2.z = 0;
if (VB->MonoColor) {
gr_vtx.r = DOSMesa->red;
gr_vtx.g = DOSMesa->green;
gr_vtx.b = DOSMesa->blue;
gr_vtx1.r = DOSMesa->red;
gr_vtx1.g = DOSMesa->green;
gr_vtx1.b = DOSMesa->blue;
gr_vtx2.r = DOSMesa->red;
gr_vtx2.g = DOSMesa->green;
gr_vtx2.b = DOSMesa->blue;
} else {
if(ctx->Light.ShadeModel == GL_SMOOTH ) {
gr_vtx.r = FixedToInt( VB->Color[v0][0] );
gr_vtx.g = FixedToInt( VB->Color[v0][1] );
gr_vtx.b = FixedToInt( VB->Color[v0][2] );
gr_vtx1.r = FixedToInt( VB->Color[v1][0] );
gr_vtx1.g = FixedToInt( VB->Color[v1][1] );
gr_vtx1.b = FixedToInt( VB->Color[v1][2] );
gr_vtx2.r = FixedToInt( VB->Color[v2][0] );
gr_vtx2.g = FixedToInt( VB->Color[v2][1] );
gr_vtx2.b = FixedToInt( VB->Color[v2][2] );
} else {
gr_vtx.r = VB->Color[pv][0];
gr_vtx.g = VB->Color[pv][1];
gr_vtx.b = VB->Color[pv][2];
gr_vtx1.r = VB->Color[pv][0];
gr_vtx1.g = VB->Color[pv][1];
gr_vtx1.b = VB->Color[pv][2];
gr_vtx2.r = VB->Color[pv][0];
gr_vtx2.g = VB->Color[pv][1];
gr_vtx2.b = VB->Color[pv][2];
}
}
gr_vtx.x = (VB->Win[v0][0] );
gr_vtx.y = FLIP( (VB->Win[v0][1] ) );
gr_vtx1.x = (VB->Win[v1][0] );
gr_vtx1.y = FLIP( (VB->Win[v1][1] ) );
gr_vtx2.x = (VB->Win[v2][0] );
gr_vtx2.y = FLIP( (VB->Win[v2][1] ) );
if(gr_vtx.x <0 || gr_vtx.x > 639 )
return;
if(gr_vtx1.x <0 || gr_vtx1.x > 639 )
return;
if(gr_vtx2.x <0 || gr_vtx2.x > 639 )
return;
if(gr_vtx.y <0 || gr_vtx.y > 479 )
return;
if(gr_vtx1.y <0 || gr_vtx1.y > 479 )
return;
if(gr_vtx2.y <0 || gr_vtx2.y > 479 )
return;
grDrawTriangle( &gr_vtx,&gr_vtx1,&gr_vtx2);
}
void fast_plot(GLcontext *ctx,GLuint first,GLuint last )
{
struct vertex_buffer *VB = ctx->VB;
register GLuint i;
if(VB->MonoColor) {
/* all same color */
gr_vtx.r = DOSMesa->red;
gr_vtx.g = DOSMesa->green;
gr_vtx.b = DOSMesa->blue;
for(i=first;i<last;i++) {
if(VB->ClipMask[i]==0) {
gr_vtx.x = VB->Win[i][0];
gr_vtx.y = FLIP(VB->Win[i][1]);
}
}
}
}
/* TODO: */
int vga_getpixel(x,y)
{
/* return (int)SV_getPixel(x,y); */
fprintf(stderr,"vga_getpixel: Not implemented yet\n");
return 1;
}
/* End of GLIDE section */
#endif // GLIDE
/**********************************************************************/
/***** Miscellaneous functions *****/
/**********************************************************************/
static void get_buffer_size( GLcontext *ctx, GLuint *width, GLuint *height )
{
*width = DOSMesa->width = vga_getxdim();
*height = DOSMesa->height = vga_getydim();
}
/* Set current color index */
static void set_index( GLcontext *ctx, GLuint index )
{
DOSMesa->index = index;
/*vga_setcolor( index );*/
}
/* Set current drawing color */
static void set_color( GLcontext *ctx,
GLubyte red, GLubyte green,
GLubyte blue, GLubyte alpha )
{
DOSMesa->red = red;
DOSMesa->green = green;
DOSMesa->blue = blue;
vga_setrgbcolor( red, green, blue );
}
static void clear_index( GLcontext *ctx, GLuint index )
{
/* TODO: Implements glClearIndex() */
}
static void clear_color( GLcontext *ctx,
GLubyte red, GLubyte green,
GLubyte blue, GLubyte alpha )
{
/* TODO: Implements glClearColor() */
}
static void clear( GLcontext *ctx,
GLboolean all,
GLint x, GLint y, GLint width, GLint height )
{
vga_clear();
}
static GLboolean set_buffer( GLcontext *ctx,
GLenum mode )
{
/* TODO: implement double buffering and use this function to select */
/* between front and back buffers. */
if (buffer == GL_FRONT_LEFT)
return GL_TRUE;
else if (buffer == GL_BACK_LEFT)
return GL_TRUE;
else
return GL_FALSE;
}
/**********************************************************************/
/***** Write spans of pixels *****/
/**********************************************************************/
static void write_index_span( GLcontext *ctx,
GLuint n, GLint x, GLint y,
const GLuint index[],
const GLubyte mask[] )
{
int i;
y = FLIP(y);
#ifdef UNIVBE
SV_beginPixel();
#endif
for (i=0;i<n;i++,x++) {
if (mask[i]) {
#ifdef UNIVBE
SV_putPixelFast(x,y,current_color);
#else
vga_setcolor( index[i] );
vga_drawpixel( x, y );
#endif
}
}
#ifdef UNIVBE
SV_endPixel();
#endif
}
static void write_monoindex_span( GLcontext *ctx,
GLuint n, GLint x, GLint y,
const GLubyte mask[] )
{
int i;
y = FLIP(y);
/* use current color index */
vga_setcolor( DOSMesa->index );
#ifdef UNIVBE
SV_beginPixel();
#endif
for (i=0;i<n;i++,x++) {
if (mask[i]) {
#ifdef UNIVBE
SV_putPixelFast(x,y,current_color);
#else
vga_drawpixel( x, y );
#endif
}
}
#ifdef UNIVBE
SV_endPixel();
#endif
}
static void write_color_span( GLcontext *ctx,
GLuint n, GLint x, GLint y,
const GLubyte red[], const GLubyte green[],
const GLubyte blue[], const GLubyte alpha[],
const GLubyte mask[] )
{
int i;
y=FLIP(y);
#ifdef UNIVBE
SV_beginPixel();
#endif
if (mask) {
/* draw some pixels */
for (i=0; i<n; i++, x++) {
if (mask[i]) {
#ifdef UNIVBE
SV_putPixelFast(x,y,SV_rgbColor(red[i], green[i], blue[i]) );
#else
vga_setrgbcolor( red[i], green[i], blue[i] );
vga_drawpixel( x, y );
#endif
}
}
}
else {
/* draw all pixels */
for (i=0; i<n; i++, x++) {
#ifdef UNIVBE
SV_putPixelFast(x,y,SV_rgbColor(red[i], green[i], blue[i]) );
#else
vga_setrgbcolor( red[i], green[i], blue[i] );
vga_drawpixel( x, y );
#endif
}
}
#ifdef UNIVBE
SV_endPixel();
#endif
}
static void write_monocolor_span( GLcontext *ctx,
GLuint n, GLint x, GLint y,
const GLubyte mask[])
{
int i;
y=FLIP(y);
#ifdef UNIVBE
SV_beginPixel();
#endif
/* use current rgb color */
vga_setrgbcolor( DOSMesa->red, DOSMesa->green, DOSMesa->blue );
for (i=0; i<n; i++, x++) {
if (mask[i]) {
#ifdef UNIVBE
SV_putPixelFast(x,y,current_color);
#else
vga_drawpixel( x, y );
#endif
}
}
#ifdef UNIVBE
SV_endPixel();
#endif
}
/**********************************************************************/
/***** Read spans of pixels *****/
/**********************************************************************/
static void read_index_span( GLcontext *ctx,
GLuint n, GLint x, GLint y, GLuint index[])
{
int i;
y = FLIP(y);
for (i=0; i<n; i++,x++) {
index[i] = vga_getpixel( x, y );
}
}
static void read_color_span( GLcontext *ctx,
GLuint n, GLint x, GLint y,
GLubyte red[], GLubyte green[],
GLubyte blue[], GLubyte alpha[] )
{
int i;
for (i=0; i<n; i++, x++) {
/* TODO */
}
}
/**********************************************************************/
/***** Write arrays of pixels *****/
/**********************************************************************/
static void write_index_pixels( GLcontext *ctx,
GLuint n, const GLint x[], const GLint y[],
const GLuint index[], const GLubyte mask[] )
{
int i;
#ifdef UNIVBE
SV_beginPixel();
#endif
for (i=0; i<n; i++) {
if (mask[i]) {
#ifdef UNIVBE
SV_putPixelFast(x[i], FLIP(y[i]), index[i] );
#else
vga_setcolor( index[i] );
vga_drawpixel( x[i], FLIP(y[i]) );
#endif
}
}
#ifdef UNIVBE
SV_endPixel();
#endif
}
static void write_monoindex_pixels( GLcontext *ctx,
GLuint n,
const GLint x[], const GLint y[],
const GLubyte mask[] )
{
int i;
/* use current color index */
vga_setcolor( DOSMesa->index );
#ifdef UNIVBE
SV_beginPixel();
#endif
for (i=0; i<n; i++) {
if (mask[i]) {
#ifdef UNIVBE
SV_putPixelFast(x[i], FLIP(y[i]), DOSMesa->index);
#else
vga_drawpixel( x[i], FLIP(y[i]) );
#endif
}
}
#ifdef UNIVBE
SV_endPixel();
#endif
}
static void write_color_pixels( GLcontext *ctx,
GLuint n, const GLint x[], const GLint y[],
const GLubyte r[], const GLubyte g[],
const GLubyte b[], const GLubyte a[],
const GLubyte mask[] )
{
int i;
#ifdef UNIVBE
SV_beginPixel();
#endif
for (i=0; i<n; i++) {
if (mask[i]) {
#ifdef UNIVBE
SV_putPixelFast(x[i], FLIP(y[i]), SV_rgbColor(r[i], g[i], b[i]) );
#else
vga_setrgbcolor( r[i], g[i], b[i] );
vga_drawpixel( x[i], FLIP(y[i]) );
#endif
}
}
#ifdef UNIVBE
SV_endPixel();
#endif
}
static void write_monocolor_pixels( GLcontext *ctx,
GLuint n,
const GLint x[], const GLint y[],
const GLubyte mask[] )
{
int i;
/* use current rgb color */
vga_setrgbcolor( DOSMesa->red, DOSMesa->green, DOSMesa->blue );
#ifdef UNIVBE
SV_beginPixel();
#endif
for (i=0; i<n; i++) {
if (mask[i]) {
#ifdef UNIVBE
SV_putPixelFast(x[i], FLIP(y[i]), current_color );
#else
vga_drawpixel( x[i], FLIP(y[i]) );
#endif
}
}
#ifdef UNIVBE
SV_endPixel();
#endif
}
/**********************************************************************/
/***** Read arrays of pixels *****/
/**********************************************************************/
/* Read an array of color index pixels. */
static void read_index_pixels( GLcontext *ctx,
GLuint n, const GLint x[], const GLint y[],
GLuint index[], const GLubyte mask[] )
{
int i;
for (i=0; i<n; i++) {
index[i] = vga_getpixel( x[i], FLIP(y[i]) );
}
}
static void read_color_pixels( GLcontext *ctx,
GLuint n, const GLint x[], const GLint y[],
GLubyte red[], GLubyte green[],
GLubyte blue[], GLubyte alpha[],
const GLubyte mask[] )
{
/* TODO */
}
static void DOSmesa_setup_DD_pointers( GLcontext *ctx )
{
/* Initialize all the pointers in the DD struct. Do this whenever */
/* a new context is made current or we change buffers via set_buffer! */
ctx->Driver.UpdateState = DOSmesa_setup_DD_pointers;
ctx->Driver.ClearIndex = clear_index;
ctx->Driver.ClearColor = clear_color;
ctx->Driver.Clear = clear;
ctx->Driver.Index = set_index;
ctx->Driver.Color = set_color;
ctx->Driver.SetBuffer = set_buffer;
ctx->Driver.GetBufferSize = get_buffer_size;
ctx->Driver.PointsFunc = choose_points_function();
ctx->Driver.LineFunc = choose_line_function();
ctx->Driver.TriangleFunc = choose_triangle_function();
/* Pixel/span writing functions: */
/* TODO: use different funcs for 8, 16, 32-bit depths */
ctx->Driver.WriteColorSpan = write_color_span;
ctx->Driver.WriteMonocolorSpan = write_monocolor_span;
ctx->Driver.WriteColorPixels = write_color_pixels;
ctx->Driver.WriteMonocolorPixels = write_monocolor_pixels;
ctx->Driver.WriteIndexSpan = write_index_span;
ctx->Driver.WriteMonoindexSpan = write_monoindex_span;
ctx->Driver.WriteIndexPixels = write_index_pixels;
ctx->Driver.WriteMonoindexPixels = write_monoindex_pixels;
/* Pixel/span reading functions: */
/* TODO: use different funcs for 8, 16, 32-bit depths */
ctx->Driver.ReadIndexSpan = read_index_span;
ctx->Driver.ReadColorSpan = read_color_span;
ctx->Driver.ReadIndexPixels = read_index_pixels;
ctx->Driver.ReadColorPixels = read_color_pixels;
}
/*
* Create a new VGA/Mesa context and return a handle to it.
*/
DOSMesaContext DOSMesaCreateContext( void )
{
DOSMesaContext ctx;
GLboolean rgb_flag;
GLfloat redscale, greenscale, bluescale, alphascale;
GLboolean db_flag = GL_FALSE;
GLboolean alpha_flag = GL_FALSE;
int colors;
GLint index_bits;
GLint redbits, greenbits, bluebits, alphabits;
#if !defined(UNIVBE) && !defined(GLIDE) && !defined(MGL)
video_buffer = (unsigned char *) malloc( vga_getxdim() * vga_getydim() );
memset(video_buffer,0, vga_getxdim() * vga_getydim() );
memset(rgbtable,0,sizeof( rgbtable ) );
#endif
#if defined( DJGPP ) && !defined(UNIVBE) && !defined(GLIDE)
dos_seg = _go32_conventional_mem_selector();
#endif
/* determine if we're in RGB or color index mode */
colors = vga_getcolors();
if (colors==32768) {
rgb_flag = GL_TRUE;
redscale = greenscale = bluescale = alphascale = 255.0;
redbits = greenbits = bluebits = 8;
alphabits = 0;
index_bits = 0;
}
else if (colors==256) {
rgb_flag = GL_FALSE;
redscale = greenscale = bluescale = alphascale = 0.0;
redbits = greenbits = bluebits = alphabits = 0;
index_bits = 8;
}
else {
restore_video_mode();
fprintf(stderr,"[%d] >16 bit color not implemented yet!\n",colors);
return NULL;
}
ctx = (DOSMesaContext) calloc( 1, sizeof(struct DOSmesa_context) );
if (!ctx) {
return NULL;
}
ctx->gl_vis = _mesa_create_visual( rgb_flag,
db_flag,
GL_FALSE, /* stereo */
redbits,
greenbits,
bluebits,
alphabits,
index_bits,
16, /* depth_size */
8, /* stencil_size */
16, 16, 16, 16, /* accum_size */
1 );
ctx->gl_ctx = _mesa_create_context( ctx->gl_vis,
NULL, /* share list context */
(void *) ctx
);
ctx->gl_buffer = _mesa_create_framebuffer( ctx->gl_vis );
ctx->index = 1;
ctx->red = ctx->green = ctx->blue = 255;
ctx->width = ctx->height = 0; /* temporary until first "make-current" */
return ctx;
}
/*
* Destroy the given VGA/Mesa context.
*/
void DOSMesaDestroyContext( DOSMesaContext ctx )
{
if (ctx) {
_mesa_destroy_visual( ctx->gl_vis );
_mesa_destroy_context( ctx->gl_ctx );
_mesa_destroy_framebuffer( ctx->gl_buffer );
free( ctx );
if (ctx==DOSMesa) {
DOSMesa = NULL;
}
}
}
/*
* Make the specified VGA/Mesa context the current one.
*/
void DOSMesaMakeCurrent( DOSMesaContext ctx )
{
DOSMesa = ctx;
_mesa_make_current( ctx->gl_ctx, ctx->gl_buffer );
DOSmesa_setup_DD_pointers( ctx->gl_ctx );
if (ctx->width==0 || ctx->height==0) {
/* setup initial viewport */
ctx->width = vga_getxdim();
ctx->height = vga_getydim();
_mesa_set_viewport( ctx->gl_ctx, 0, 0, ctx->width, ctx->height );
}
}
/*
* Return a handle to the current VGA/Mesa context.
*/
DOSMesaContext DOSMesaGetCurrentContext( void )
{
return DOSMesa;
}
/*
* Swap front/back buffers for current context if double buffered.
*/
void DOSMesaSwapBuffers( void )
{
#if !defined(UNIVBE)
/* Assume double buffering is available if in UNIVBE,
if it isn`t its taken care of anyway */
// if (DOSMesa->gl_vis->DBflag)
#endif
{
vga_flip();
}
}
#else
/*
* Need this to provide at least one external definition when DOS is
* not defined on the compiler command line.
*/
int gl_DOS_dummy_function(void)
{
return 0;
}
#endif /*DOS*/
|