2 * $Source: /cvs/cvsroot/d2x/video/dos_gr.c,v $
5 * $Date: 2001-01-29 13:47:51 $
7 * Graphics functions for DOS.
9 * $Log: not supported by cvs2svn $
32 #include <sys/nearptr.h>
35 static int last_r=0, last_g=0, last_b=0;
38 unsigned char * gr_video_memory = (unsigned char *)0xa0000;
40 char gr_pal_default[768];
44 volatile ubyte * pVideoMode = (volatile ubyte *)0x449;
45 volatile ushort * pNumColumns = (volatile ushort *)0x44a;
46 volatile ubyte * pNumRows = (volatile ubyte *)0x484;
47 volatile ushort * pCharHeight = (volatile ushort *)0x485;
48 volatile ushort * pCursorPos = (volatile ushort *)0x450;
49 volatile ushort * pCursorType = (volatile ushort *)0x460;
50 volatile ushort * pTextMemory = (volatile ushort *)0xb8000;
52 typedef struct screen_save {
58 ubyte cursor_x, cursor_y;
59 ubyte cursor_sline, cursor_eline;
60 ushort * video_memory;
63 screen_save gr_saved_screen;
65 int gr_show_screen_info = 0;
66 extern int VGA_current_mode;
68 void gr_set_cellheight( ubyte height )
81 outpw( 0x3c4, 0xE04 ); // enable chain4 mode
82 outpw( 0x3d4, 0x4014 ); // turn on dword mode
83 outpw( 0x3d4, 0xa317 ); // turn off byte mode
88 outpw( 0x3ce, 0x4005 ); // set Shift reg to 1
90 inp( 0x3da ); // dummy input
93 outp( 0x3c0, 0x61 ); // turns on PCS & PCC
95 inp( 0x3da ); // dummy input
101 void gr_turn_screen_off()
108 void gr_turn_screen_on()
115 void gr_set_misc_mode( uint mode )
119 memset( ®s, 0, sizeof(regs) );
121 int386( 0x10, ®s, ®s );
125 void gr_set_3dbios_mode( uint mode )
128 memset( ®s, 0, sizeof(regs) );
130 regs.w.bx = 0x3d00 | (mode & 0xff);
131 int386( 0x10, ®s, ®s );
136 void gr_set_text_25()
141 int386( 0x10, ®s, ®s );
145 void gr_set_text_43()
151 int386( 0x10, ®s, ®s );
154 int386( 0x10, ®s, ®s );
158 int386( 0x10, ®s, ®s );
161 void gr_set_text_50()
167 int386( 0x10, ®s, ®s );
170 int386( 0x10, ®s, ®s );
174 int386( 0x10, ®s, ®s );
177 ubyte is_graphics_mode()
180 tmp = inp( 0x3DA ); // Reset flip-flip
181 outp( 0x3C0, 0x30 ); // Select attr register 10
182 tmp = inp( 0x3C1 ); // Get graphics/text bit
186 void gr_setcursor(ubyte x, ubyte y, ubyte sline, ubyte eline)
190 memset( ®s, 0, sizeof(regs) );
195 int386( 0x10, ®s, ®s );
197 memset( ®s, 0, sizeof(regs) );
199 regs.h.ch = sline & 0xf;
200 regs.h.cl = eline & 0xf;
201 int386( 0x10, ®s, ®s );
204 void gr_getcursor(ubyte *x, ubyte *y, ubyte * sline, ubyte * eline)
208 memset( ®s, 0, sizeof(regs) );
211 int386( 0x10, ®s, ®s );
223 gr_saved_screen.is_graphics = is_graphics_mode();
224 gr_saved_screen.video_mode = *pVideoMode;
226 if (!gr_saved_screen.is_graphics) {
227 gr_saved_screen.width = *pNumColumns;
228 gr_saved_screen.height = *pNumRows+1;
229 gr_saved_screen.char_height = *pCharHeight;
230 gr_getcursor(&gr_saved_screen.cursor_x, &gr_saved_screen.cursor_y, &gr_saved_screen.cursor_sline, &gr_saved_screen.cursor_eline );
231 MALLOC(gr_saved_screen.video_memory,ushort, gr_saved_screen.width*gr_saved_screen.height );
232 for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
233 gr_saved_screen.video_memory[i] = pTextMemory[i];
236 if (gr_show_screen_info ) {
237 printf( "Current video mode 0x%x:\n", gr_saved_screen.video_mode );
238 if (gr_saved_screen.is_graphics)
239 printf( "Graphics mode\n" );
241 printf( "Text mode\n" );
242 printf( "( %d columns by %d rows)\n", gr_saved_screen.width, gr_saved_screen.height );
243 printf( "Char height is %d pixel rows\n", gr_saved_screen.char_height );
244 printf( "Cursor of type 0x%x,0x%x is at (%d, %d)\n", gr_saved_screen.cursor_sline, gr_saved_screen.cursor_eline,gr_saved_screen.cursor_x, gr_saved_screen.cursor_y );
255 memset( ®s, 0, sizeof(regs) );
257 int386( 0x10, ®s, ®s );
259 if ( regs.h.al == 0x1a )
265 void gr_restore_mode()
272 gr_palette_fade_out( gr_palette, 32, 0 );
273 gr_palette_set_gamma(0);
274 if ( gr_saved_screen.video_mode == 3 ) {
275 switch( gr_saved_screen.height ) {
276 case 43: gr_set_text_43(); break;
277 case 50: gr_set_text_50(); break;
278 default: gr_set_text_25(); break;
281 gr_set_misc_mode(gr_saved_screen.video_mode);
283 if (gr_saved_screen.is_graphics==0) {
286 gr_palette_read( gr_pal_default );
288 for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
289 pTextMemory[i]=gr_saved_screen.video_memory[i];
290 gr_setcursor( gr_saved_screen.cursor_x, gr_saved_screen.cursor_y, gr_saved_screen.cursor_sline, gr_saved_screen.cursor_eline );
291 gr_palette_faded_out = 1;
292 gr_palette_fade_in( gr_pal_default, 32, 0 );
305 if( gr_saved_screen.video_memory ) {
306 free(gr_saved_screen.video_memory);
307 gr_saved_screen.video_memory = NULL;
313 int gr_vesa_setmode( int mode )
317 retcode=gr_vesa_checkmode( mode );
318 if ( retcode ) return retcode;
320 return gr_vesa_setmodea( mode );
325 int gr_set_mode(u_int32_t mode)
328 unsigned int w,h,t,data, r;
330 //JOHNgr_disable_default_palette_loading();
338 case SM(320,200)://0:
339 if (!isvga()) return 1;
340 gr_set_misc_mode(0x13);
341 w = 320; r = 320; h = 200; t=BM_LINEAR; data = (int)gr_video_memory;
343 case SM(640,400)://SM_640x400V:
344 retcode = gr_vesa_setmode( 0x100 );
345 if (retcode !=0 ) return retcode;
346 w = 640; r = 640; h = 400; t=BM_SVGA; data = 0;
348 case SM(640,480)://SM_640x480V:
349 retcode = gr_vesa_setmode( 0x101 );
350 if (retcode !=0 ) return retcode;
351 w = 640; r = 640; h = 480; t=BM_SVGA; data = 0;
353 case SM(800,600)://SM_800x600V:
354 retcode = gr_vesa_setmode( 0x103 );
355 if (retcode !=0 ) return retcode;
356 w = 800; r = 800; h = 600; t=BM_SVGA; data = 0;
358 case SM(1024,768)://SM_1024x768V:
359 retcode = gr_vesa_setmode( 0x105 );
360 if (retcode !=0 ) return retcode;
361 w = 1024; r = 1024; h = 768; t=BM_SVGA; data = 0;
363 /* case SM_640x480V15:
364 retcode = gr_vesa_setmode( 0x110 );
365 if (retcode !=0 ) return retcode;
366 w = 640; r = 640*2; h=480; t=BM_SVGA15; data = 0;
369 retcode = gr_vesa_setmode( 0x113 );
370 if (retcode !=0 ) return retcode;
371 w = 800; r = 800*2; h=600; t=BM_SVGA15; data = 0;
375 if (!isvga()) return 1;
376 gr_set_misc_mode(0x13);
383 gr_set_cellheight( 3 );
385 w = 320; r = 320; h = 100; t=BM_LINEAR; data = (int)gr_video_memory;
388 retcode = gr_vesa_setmode( 0x102 );
389 //gr_enable_default_palette_loading();
390 if (retcode !=0 ) return retcode;
393 //gr_set_cellheight( 1 );
394 gr_vesa_setlogical( 400 );
395 w = 400; r = 400; h = 600; t=BM_SVGA; data = 0;
398 if (!isvga()) return 1;
399 gr_set_misc_mode(0xd);
402 gr_set_cellheight( 3 );
403 w = 160; r = 160; h = 100; t=BM_LINEAR; data = (int)gr_video_memory;
405 case //22: // 3dmax 320x400
406 if (!isvga()) return 1;
407 gr_set_3dbios_mode(0x31);
408 //w = 320; r = 320/4; h = 400; t=BM_MODEX; data = 0;
409 w = 320; r = 320; h = 400; t=BM_SVGA; data = 0;
412 if (!isvga()) return 1;
413 if (mode==SM(320,240))
414 w = gr_modex_setmode( 2 );
415 else if (mode==SM(320,400))
416 w = gr_modex_setmode( 6 );
418 Error("unhandled vid mode\n");
419 //gr_enable_default_palette_loading();
420 h = w & 0xffff; w = w >> 16; r = w / 4;t = BM_MODEX; data = 0;
425 memset( grd_curscreen, 0, sizeof(grs_screen));
426 grd_curscreen->sc_mode = mode;
427 grd_curscreen->sc_w = w;
428 grd_curscreen->sc_h = h;
429 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
430 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
431 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
432 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
433 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
434 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = r;
435 grd_curscreen->sc_canvas.cv_bitmap.bm_type = t;
436 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)data;
437 VGA_current_mode = mode;
438 gr_set_current_canvas(NULL);
440 //gr_enable_default_palette_loading();
441 // gamefont_choose_game_font(w,h);
450 int mode = SM(320,200);
452 // Only do this function once!
457 if (!__djgpp_nearptr_enable()) {
458 printf("nearptr enable=%x\n", __dpmi_error);
462 gr_video_memory = (unsigned char *)(__djgpp_conventional_base + 0xa0000);
464 gr_video_memory=(unsigned char *)-1;
466 pVideoMode = (volatile ubyte *)(__djgpp_conventional_base+0x449);
467 pNumColumns = (volatile ushort *)(__djgpp_conventional_base+0x44a);
468 pNumRows = (volatile ubyte *)(__djgpp_conventional_base+0x484);
469 pCharHeight = (volatile ushort *)(__djgpp_conventional_base+0x485);
470 pCursorPos = (volatile ushort *)(__djgpp_conventional_base+0x450);
471 pCursorType = (volatile ushort *)(__djgpp_conventional_base+0x460);
472 pTextMemory = (volatile ushort *)(__djgpp_conventional_base+0xb8000);
479 // Save the current text screen mode
480 if (gr_save_mode()==1)
484 // Save the current palette, and fade it out to black.
485 gr_palette_read( gr_pal_default );
486 gr_palette_faded_out = 0;
487 org_gamma = gr_palette_get_gamma();
488 gr_palette_set_gamma( 0 );
489 gr_palette_fade_out( gr_pal_default, 32, 0 );
491 gr_palette_set_gamma( org_gamma );
498 __djgpp_nearptr_disable();
502 MALLOC( grd_curscreen,grs_screen,1 );
503 memset( grd_curscreen, 0, sizeof(grs_screen));
506 if ((retcode=gr_set_mode(mode)))
511 //JOHNgr_disable_default_palette_loading();
513 // Set all the screen, canvas, and bitmap variables that
514 // aren't set by the gr_set_mode call:
515 grd_curscreen->sc_canvas.cv_color = 0;
516 grd_curscreen->sc_canvas.cv_drawmode = 0;
517 grd_curscreen->sc_canvas.cv_font = NULL;
518 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
519 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
520 gr_set_current_canvas( &grd_curscreen->sc_canvas );
523 if (!dpmi_allocate_selector( &gr_fade_table, 256*GR_FADE_LEVELS, &gr_fade_table_selector ))
524 Error( "Error allocating fade table selector!" );
526 if (!dpmi_allocate_selector( &gr_palette, 256*3, &gr_palette_selector ))
527 Error( "Error allocating palette selector!" );
530 // if (!dpmi_allocate_selector( &gr_inverse_table, 32*32*32, &gr_inverse_table_selector ))
531 // Error( "Error allocating inverse table selector!" );
534 // Set flags indicating that this is installed.
538 atexit((void (*)) gr_close);
545 int gr_mode13_checkmode()
554 // 1=No VGA adapter installed
555 // 2=Program doesn't support this VESA granularity
556 // 3=Monitor doesn't support that VESA mode.:
557 // 4=Video card doesn't support that VESA mode.
558 // 5=No VESA driver found.
559 // 6=Bad Status after VESA call/
560 // 7=Not enough DOS memory to call VESA functions.
561 // 8=Error using DPMI.
562 // 9=Error setting logical line width.
563 // 10=Error allocating selector for A0000h
564 // 11=Not a valid mode support by gr.lib
566 int gr_check_mode(u_int32_t mode)
570 case SM(320,100)://19:
582 case SM(376,564): return gr_mode13_checkmode();
583 case SM(640,400): return gr_vesa_checkmode( 0x100 );
584 case SM(640,480): return gr_vesa_checkmode( 0x101 );
585 case SM(800,600): return gr_vesa_checkmode( 0x103 );
586 case SM(1024,768): return gr_vesa_setmode( 0x105 );
587 // case SM_640x480V15: return gr_vesa_setmode( 0x110 );
588 // case SM_800x600V15: return gr_vesa_setmode( 0x113 );
593 /* Palette Stuff Starts Here... */
595 void gr_palette_step_up( int r, int g, int b )
601 if (gr_palette_faded_out) return;
603 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
605 last_r = r; last_g = g; last_b = b;
611 for (i=0; i<256; i++ ) {
612 temp = (int)(*p++) + r + gr_palette_gamma;
614 else if (temp>63) temp=63;
616 temp = (int)(*p++) + g + gr_palette_gamma;
618 else if (temp>63) temp=63;
620 temp = (int)(*p++) + b + gr_palette_gamma;
622 else if (temp>63) temp=63;
628 void gr_palette_clear()
633 for (i=0; i<768; i++ ) {
636 gr_palette_faded_out = 1;
639 void gr_palette_load( ubyte * pal )
645 for (i=0; i<768; i++ ) {
646 c = pal[i] + gr_palette_gamma;
647 if ( c > 63 ) c = 63;
649 gr_current_pal[i] = pal[i];
651 gr_palette_faded_out = 0;
653 init_computed_colors();
657 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys )
661 fix fade_palette[768];
662 fix fade_palette_delta[768];
664 if (gr_palette_faded_out) return 0;
667 pal = gr_current_pal;
669 for (i=0; i<768; i++ ) {
670 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
671 fade_palette_delta[i] = fade_palette[i] / nsteps;
674 for (j=0; j<nsteps; j++ ) {
678 for (i=0; i<768; i++ ) {
679 fade_palette[i] -= fade_palette_delta[i];
680 if (fade_palette[i] < 0 )
682 c = f2i(fade_palette[i]);
683 if ( c > 63 ) c = 63;
687 gr_palette_faded_out = 1;
691 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
695 fix fade_palette[768];
696 fix fade_palette_delta[768];
699 if (!gr_palette_faded_out) return 0;
701 for (i=0; i<768; i++ ) {
702 gr_current_pal[i] = pal[i];
704 fade_palette_delta[i] = i2f(pal[i]+gr_palette_gamma) / nsteps;
707 for (j=0; j<nsteps; j++ ) {
711 for (i=0; i<768; i++ ) {
712 fade_palette[i] += fade_palette_delta[i];
713 if (fade_palette[i] > i2f(pal[i]+gr_palette_gamma) )
714 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
715 c = f2i(fade_palette[i]);
716 if ( c > 63 ) c = 63;
720 gr_palette_faded_out = 0;
724 void gr_palette_read(ubyte * palette)
729 for (i=0; i<768; i++ ) {
730 *palette++ = inp( 0x3c9 );