1 // Graphics functions for DOS.
21 #include <sys/nearptr.h>
24 static int last_r=0, last_g=0, last_b=0;
27 unsigned char * gr_video_memory = (unsigned char *)0xa0000;
29 char gr_pal_default[768];
33 volatile ubyte * pVideoMode = (volatile ubyte *)0x449;
34 volatile ushort * pNumColumns = (volatile ushort *)0x44a;
35 volatile ubyte * pNumRows = (volatile ubyte *)0x484;
36 volatile ushort * pCharHeight = (volatile ushort *)0x485;
37 volatile ushort * pCursorPos = (volatile ushort *)0x450;
38 volatile ushort * pCursorType = (volatile ushort *)0x460;
39 volatile ushort * pTextMemory = (volatile ushort *)0xb8000;
41 typedef struct screen_save {
47 ubyte cursor_x, cursor_y;
48 ubyte cursor_sline, cursor_eline;
49 ushort * video_memory;
52 screen_save gr_saved_screen;
54 int gr_show_screen_info = 0;
55 extern int VGA_current_mode;
57 void gr_set_cellheight( ubyte height )
70 outpw( 0x3c4, 0xE04 ); // enable chain4 mode
71 outpw( 0x3d4, 0x4014 ); // turn on dword mode
72 outpw( 0x3d4, 0xa317 ); // turn off byte mode
77 outpw( 0x3ce, 0x4005 ); // set Shift reg to 1
79 inp( 0x3da ); // dummy input
82 outp( 0x3c0, 0x61 ); // turns on PCS & PCC
84 inp( 0x3da ); // dummy input
90 void gr_turn_screen_off()
97 void gr_turn_screen_on()
104 void gr_set_misc_mode( uint mode )
108 memset( ®s, 0, sizeof(regs) );
110 int386( 0x10, ®s, ®s );
114 void gr_set_3dbios_mode( uint mode )
117 memset( ®s, 0, sizeof(regs) );
119 regs.w.bx = 0x3d00 | (mode & 0xff);
120 int386( 0x10, ®s, ®s );
125 void gr_set_text_25()
130 int386( 0x10, ®s, ®s );
134 void gr_set_text_43()
140 int386( 0x10, ®s, ®s );
143 int386( 0x10, ®s, ®s );
147 int386( 0x10, ®s, ®s );
150 void gr_set_text_50()
156 int386( 0x10, ®s, ®s );
159 int386( 0x10, ®s, ®s );
163 int386( 0x10, ®s, ®s );
166 ubyte is_graphics_mode()
169 tmp = inp( 0x3DA ); // Reset flip-flip
170 outp( 0x3C0, 0x30 ); // Select attr register 10
171 tmp = inp( 0x3C1 ); // Get graphics/text bit
175 void gr_setcursor(ubyte x, ubyte y, ubyte sline, ubyte eline)
179 memset( ®s, 0, sizeof(regs) );
184 int386( 0x10, ®s, ®s );
186 memset( ®s, 0, sizeof(regs) );
188 regs.h.ch = sline & 0xf;
189 regs.h.cl = eline & 0xf;
190 int386( 0x10, ®s, ®s );
193 void gr_getcursor(ubyte *x, ubyte *y, ubyte * sline, ubyte * eline)
197 memset( ®s, 0, sizeof(regs) );
200 int386( 0x10, ®s, ®s );
212 gr_saved_screen.is_graphics = is_graphics_mode();
213 gr_saved_screen.video_mode = *pVideoMode;
215 if (!gr_saved_screen.is_graphics) {
216 gr_saved_screen.width = *pNumColumns;
217 gr_saved_screen.height = *pNumRows+1;
218 gr_saved_screen.char_height = *pCharHeight;
219 gr_getcursor(&gr_saved_screen.cursor_x, &gr_saved_screen.cursor_y, &gr_saved_screen.cursor_sline, &gr_saved_screen.cursor_eline );
220 MALLOC(gr_saved_screen.video_memory,ushort, gr_saved_screen.width*gr_saved_screen.height );
221 for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
222 gr_saved_screen.video_memory[i] = pTextMemory[i];
225 if (gr_show_screen_info ) {
226 printf( "Current video mode 0x%x:\n", gr_saved_screen.video_mode );
227 if (gr_saved_screen.is_graphics)
228 printf( "Graphics mode\n" );
230 printf( "Text mode\n" );
231 printf( "( %d columns by %d rows)\n", gr_saved_screen.width, gr_saved_screen.height );
232 printf( "Char height is %d pixel rows\n", gr_saved_screen.char_height );
233 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 );
244 memset( ®s, 0, sizeof(regs) );
246 int386( 0x10, ®s, ®s );
248 if ( regs.h.al == 0x1a )
254 void gr_restore_mode()
261 gr_palette_fade_out( gr_palette, 32, 0 );
262 gr_palette_set_gamma(0);
263 if ( gr_saved_screen.video_mode == 3 ) {
264 switch( gr_saved_screen.height ) {
265 case 43: gr_set_text_43(); break;
266 case 50: gr_set_text_50(); break;
267 default: gr_set_text_25(); break;
270 gr_set_misc_mode(gr_saved_screen.video_mode);
272 if (gr_saved_screen.is_graphics==0) {
275 gr_palette_read( gr_pal_default );
277 for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
278 pTextMemory[i]=gr_saved_screen.video_memory[i];
279 gr_setcursor( gr_saved_screen.cursor_x, gr_saved_screen.cursor_y, gr_saved_screen.cursor_sline, gr_saved_screen.cursor_eline );
280 gr_palette_faded_out = 1;
281 gr_palette_fade_in( gr_pal_default, 32, 0 );
294 if( gr_saved_screen.video_memory ) {
295 free(gr_saved_screen.video_memory);
296 gr_saved_screen.video_memory = NULL;
302 int gr_vesa_setmode( int mode )
306 retcode=gr_vesa_checkmode( mode );
307 if ( retcode ) return retcode;
309 return gr_vesa_setmodea( mode );
314 int gr_set_mode(u_int32_t mode)
317 unsigned int w,h,t,data, r;
319 //JOHNgr_disable_default_palette_loading();
327 case SM(320,200)://0:
328 if (!isvga()) return 1;
329 gr_set_misc_mode(0x13);
330 w = 320; r = 320; h = 200; t=BM_LINEAR; data = (int)gr_video_memory;
332 case SM(640,400)://SM_640x400V:
333 retcode = gr_vesa_setmode( 0x100 );
334 if (retcode !=0 ) return retcode;
335 w = 640; r = 640; h = 400; t=BM_SVGA; data = 0;
337 case SM(640,480)://SM_640x480V:
338 retcode = gr_vesa_setmode( 0x101 );
339 if (retcode !=0 ) return retcode;
340 w = 640; r = 640; h = 480; t=BM_SVGA; data = 0;
342 case SM(800,600)://SM_800x600V:
343 retcode = gr_vesa_setmode( 0x103 );
344 if (retcode !=0 ) return retcode;
345 w = 800; r = 800; h = 600; t=BM_SVGA; data = 0;
347 case SM(1024,768)://SM_1024x768V:
348 retcode = gr_vesa_setmode( 0x105 );
349 if (retcode !=0 ) return retcode;
350 w = 1024; r = 1024; h = 768; t=BM_SVGA; data = 0;
352 /* case SM_640x480V15:
353 retcode = gr_vesa_setmode( 0x110 );
354 if (retcode !=0 ) return retcode;
355 w = 640; r = 640*2; h=480; t=BM_SVGA15; data = 0;
358 retcode = gr_vesa_setmode( 0x113 );
359 if (retcode !=0 ) return retcode;
360 w = 800; r = 800*2; h=600; t=BM_SVGA15; data = 0;
364 if (!isvga()) return 1;
365 gr_set_misc_mode(0x13);
372 gr_set_cellheight( 3 );
374 w = 320; r = 320; h = 100; t=BM_LINEAR; data = (int)gr_video_memory;
377 retcode = gr_vesa_setmode( 0x102 );
378 //gr_enable_default_palette_loading();
379 if (retcode !=0 ) return retcode;
382 //gr_set_cellheight( 1 );
383 gr_vesa_setlogical( 400 );
384 w = 400; r = 400; h = 600; t=BM_SVGA; data = 0;
387 if (!isvga()) return 1;
388 gr_set_misc_mode(0xd);
391 gr_set_cellheight( 3 );
392 w = 160; r = 160; h = 100; t=BM_LINEAR; data = (int)gr_video_memory;
394 case //22: // 3dmax 320x400
395 if (!isvga()) return 1;
396 gr_set_3dbios_mode(0x31);
397 //w = 320; r = 320/4; h = 400; t=BM_MODEX; data = 0;
398 w = 320; r = 320; h = 400; t=BM_SVGA; data = 0;
401 if (!isvga()) return 1;
402 if (mode==SM(320,240))
403 w = gr_modex_setmode( 2 );
404 else if (mode==SM(320,400))
405 w = gr_modex_setmode( 6 );
407 Error("unhandled vid mode\n");
408 //gr_enable_default_palette_loading();
409 h = w & 0xffff; w = w >> 16; r = w / 4;t = BM_MODEX; data = 0;
414 memset( grd_curscreen, 0, sizeof(grs_screen));
415 grd_curscreen->sc_mode = mode;
416 grd_curscreen->sc_w = w;
417 grd_curscreen->sc_h = h;
418 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
419 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
420 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
421 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
422 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
423 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = r;
424 grd_curscreen->sc_canvas.cv_bitmap.bm_type = t;
425 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)data;
426 VGA_current_mode = mode;
427 gr_set_current_canvas(NULL);
429 //gr_enable_default_palette_loading();
430 // gamefont_choose_game_font(w,h);
439 int mode = SM(320,200);
441 // Only do this function once!
446 if (!__djgpp_nearptr_enable()) {
447 printf("nearptr enable=%x\n", __dpmi_error);
451 gr_video_memory = (unsigned char *)(__djgpp_conventional_base + 0xa0000);
453 gr_video_memory=(unsigned char *)-1;
455 pVideoMode = (volatile ubyte *)(__djgpp_conventional_base+0x449);
456 pNumColumns = (volatile ushort *)(__djgpp_conventional_base+0x44a);
457 pNumRows = (volatile ubyte *)(__djgpp_conventional_base+0x484);
458 pCharHeight = (volatile ushort *)(__djgpp_conventional_base+0x485);
459 pCursorPos = (volatile ushort *)(__djgpp_conventional_base+0x450);
460 pCursorType = (volatile ushort *)(__djgpp_conventional_base+0x460);
461 pTextMemory = (volatile ushort *)(__djgpp_conventional_base+0xb8000);
468 // Save the current text screen mode
469 if (gr_save_mode()==1)
473 // Save the current palette, and fade it out to black.
474 gr_palette_read( gr_pal_default );
475 gr_palette_faded_out = 0;
476 org_gamma = gr_palette_get_gamma();
477 gr_palette_set_gamma( 0 );
478 gr_palette_fade_out( gr_pal_default, 32, 0 );
480 gr_palette_set_gamma( org_gamma );
487 __djgpp_nearptr_disable();
491 MALLOC( grd_curscreen,grs_screen,1 );
492 memset( grd_curscreen, 0, sizeof(grs_screen));
495 if ((retcode=gr_set_mode(mode)))
500 //JOHNgr_disable_default_palette_loading();
502 // Set all the screen, canvas, and bitmap variables that
503 // aren't set by the gr_set_mode call:
504 grd_curscreen->sc_canvas.cv_color = 0;
505 grd_curscreen->sc_canvas.cv_drawmode = 0;
506 grd_curscreen->sc_canvas.cv_font = NULL;
507 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
508 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
509 gr_set_current_canvas( &grd_curscreen->sc_canvas );
512 if (!dpmi_allocate_selector( &gr_fade_table, 256*GR_FADE_LEVELS, &gr_fade_table_selector ))
513 Error( "Error allocating fade table selector!" );
515 if (!dpmi_allocate_selector( &gr_palette, 256*3, &gr_palette_selector ))
516 Error( "Error allocating palette selector!" );
519 // if (!dpmi_allocate_selector( &gr_inverse_table, 32*32*32, &gr_inverse_table_selector ))
520 // Error( "Error allocating inverse table selector!" );
523 // Set flags indicating that this is installed.
527 atexit((void (*)) gr_close);
534 int gr_mode13_checkmode()
543 // 1=No VGA adapter installed
544 // 2=Program doesn't support this VESA granularity
545 // 3=Monitor doesn't support that VESA mode.:
546 // 4=Video card doesn't support that VESA mode.
547 // 5=No VESA driver found.
548 // 6=Bad Status after VESA call/
549 // 7=Not enough DOS memory to call VESA functions.
550 // 8=Error using DPMI.
551 // 9=Error setting logical line width.
552 // 10=Error allocating selector for A0000h
553 // 11=Not a valid mode support by gr.lib
555 int gr_check_mode(u_int32_t mode)
559 case SM(320,100)://19:
571 case SM(376,564): return gr_mode13_checkmode();
572 case SM(640,400): return gr_vesa_checkmode( 0x100 );
573 case SM(640,480): return gr_vesa_checkmode( 0x101 );
574 case SM(800,600): return gr_vesa_checkmode( 0x103 );
575 case SM(1024,768): return gr_vesa_setmode( 0x105 );
576 // case SM_640x480V15: return gr_vesa_setmode( 0x110 );
577 // case SM_800x600V15: return gr_vesa_setmode( 0x113 );
582 /* Palette Stuff Starts Here... */
584 void gr_palette_step_up( int r, int g, int b )
590 if (gr_palette_faded_out) return;
592 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
594 last_r = r; last_g = g; last_b = b;
600 for (i=0; i<256; i++ ) {
601 temp = (int)(*p++) + r + gr_palette_gamma;
603 else if (temp>63) temp=63;
605 temp = (int)(*p++) + g + gr_palette_gamma;
607 else if (temp>63) temp=63;
609 temp = (int)(*p++) + b + gr_palette_gamma;
611 else if (temp>63) temp=63;
617 void gr_palette_clear()
622 for (i=0; i<768; i++ ) {
625 gr_palette_faded_out = 1;
628 void gr_palette_load( ubyte * pal )
634 for (i=0; i<768; i++ ) {
635 c = pal[i] + gr_palette_gamma;
636 if ( c > 63 ) c = 63;
638 gr_current_pal[i] = pal[i];
640 gr_palette_faded_out = 0;
642 init_computed_colors();
646 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys )
650 fix fade_palette[768];
651 fix fade_palette_delta[768];
653 if (gr_palette_faded_out) return 0;
656 pal = gr_current_pal;
658 for (i=0; i<768; i++ ) {
659 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
660 fade_palette_delta[i] = fade_palette[i] / nsteps;
663 for (j=0; j<nsteps; j++ ) {
667 for (i=0; i<768; i++ ) {
668 fade_palette[i] -= fade_palette_delta[i];
669 if (fade_palette[i] < 0 )
671 c = f2i(fade_palette[i]);
672 if ( c > 63 ) c = 63;
676 gr_palette_faded_out = 1;
680 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
684 fix fade_palette[768];
685 fix fade_palette_delta[768];
688 if (!gr_palette_faded_out) return 0;
690 for (i=0; i<768; i++ ) {
691 gr_current_pal[i] = pal[i];
693 fade_palette_delta[i] = i2f(pal[i]+gr_palette_gamma) / nsteps;
696 for (j=0; j<nsteps; j++ ) {
700 for (i=0; i<768; i++ ) {
701 fade_palette[i] += fade_palette_delta[i];
702 if (fade_palette[i] > i2f(pal[i]+gr_palette_gamma) )
703 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
704 c = f2i(fade_palette[i]);
705 if ( c > 63 ) c = 63;
709 gr_palette_faded_out = 0;
713 void gr_palette_read(ubyte * palette)
718 for (i=0; i<768; i++ ) {
719 *palette++ = inp( 0x3c9 );