1 // Graphics functions for DOS.
22 #include <sys/nearptr.h>
25 static int last_r=0, last_g=0, last_b=0;
28 unsigned char * gr_video_memory = (unsigned char *)0xa0000;
30 char gr_pal_default[768];
34 volatile ubyte * pVideoMode = (volatile ubyte *)0x449;
35 volatile ushort * pNumColumns = (volatile ushort *)0x44a;
36 volatile ubyte * pNumRows = (volatile ubyte *)0x484;
37 volatile ushort * pCharHeight = (volatile ushort *)0x485;
38 volatile ushort * pCursorPos = (volatile ushort *)0x450;
39 volatile ushort * pCursorType = (volatile ushort *)0x460;
40 volatile ushort * pTextMemory = (volatile ushort *)0xb8000;
42 typedef struct screen_save {
48 ubyte cursor_x, cursor_y;
49 ubyte cursor_sline, cursor_eline;
50 ushort * video_memory;
53 screen_save gr_saved_screen;
55 int gr_show_screen_info = 0;
56 extern int VGA_current_mode;
58 void gr_set_cellheight( ubyte height )
71 outpw( 0x3c4, 0xE04 ); // enable chain4 mode
72 outpw( 0x3d4, 0x4014 ); // turn on dword mode
73 outpw( 0x3d4, 0xa317 ); // turn off byte mode
78 outpw( 0x3ce, 0x4005 ); // set Shift reg to 1
80 inp( 0x3da ); // dummy input
83 outp( 0x3c0, 0x61 ); // turns on PCS & PCC
85 inp( 0x3da ); // dummy input
91 void gr_turn_screen_off()
98 void gr_turn_screen_on()
105 void gr_set_misc_mode( uint mode )
109 memset( ®s, 0, sizeof(regs) );
111 int386( 0x10, ®s, ®s );
115 void gr_set_3dbios_mode( uint mode )
118 memset( ®s, 0, sizeof(regs) );
120 regs.w.bx = 0x3d00 | (mode & 0xff);
121 int386( 0x10, ®s, ®s );
126 void gr_set_text_25()
131 int386( 0x10, ®s, ®s );
135 void gr_set_text_43()
141 int386( 0x10, ®s, ®s );
144 int386( 0x10, ®s, ®s );
148 int386( 0x10, ®s, ®s );
151 void gr_set_text_50()
157 int386( 0x10, ®s, ®s );
160 int386( 0x10, ®s, ®s );
164 int386( 0x10, ®s, ®s );
167 ubyte is_graphics_mode()
170 tmp = inp( 0x3DA ); // Reset flip-flip
171 outp( 0x3C0, 0x30 ); // Select attr register 10
172 tmp = inp( 0x3C1 ); // Get graphics/text bit
176 void gr_setcursor(ubyte x, ubyte y, ubyte sline, ubyte eline)
180 memset( ®s, 0, sizeof(regs) );
185 int386( 0x10, ®s, ®s );
187 memset( ®s, 0, sizeof(regs) );
189 regs.h.ch = sline & 0xf;
190 regs.h.cl = eline & 0xf;
191 int386( 0x10, ®s, ®s );
194 void gr_getcursor(ubyte *x, ubyte *y, ubyte * sline, ubyte * eline)
198 memset( ®s, 0, sizeof(regs) );
201 int386( 0x10, ®s, ®s );
213 gr_saved_screen.is_graphics = is_graphics_mode();
214 gr_saved_screen.video_mode = *pVideoMode;
216 if (!gr_saved_screen.is_graphics) {
217 gr_saved_screen.width = *pNumColumns;
218 gr_saved_screen.height = *pNumRows+1;
219 gr_saved_screen.char_height = *pCharHeight;
220 gr_getcursor(&gr_saved_screen.cursor_x, &gr_saved_screen.cursor_y, &gr_saved_screen.cursor_sline, &gr_saved_screen.cursor_eline );
221 MALLOC(gr_saved_screen.video_memory,ushort, gr_saved_screen.width*gr_saved_screen.height );
222 for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
223 gr_saved_screen.video_memory[i] = pTextMemory[i];
226 if (gr_show_screen_info ) {
227 printf( "Current video mode 0x%x:\n", gr_saved_screen.video_mode );
228 if (gr_saved_screen.is_graphics)
229 printf( "Graphics mode\n" );
231 printf( "Text mode\n" );
232 printf( "( %d columns by %d rows)\n", gr_saved_screen.width, gr_saved_screen.height );
233 printf( "Char height is %d pixel rows\n", gr_saved_screen.char_height );
234 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 );
245 memset( ®s, 0, sizeof(regs) );
247 int386( 0x10, ®s, ®s );
249 if ( regs.h.al == 0x1a )
255 void gr_restore_mode()
262 gr_palette_fade_out( gr_palette, 32, 0 );
263 gr_palette_set_gamma(0);
264 if ( gr_saved_screen.video_mode == 3 ) {
265 switch( gr_saved_screen.height ) {
266 case 43: gr_set_text_43(); break;
267 case 50: gr_set_text_50(); break;
268 default: gr_set_text_25(); break;
271 gr_set_misc_mode(gr_saved_screen.video_mode);
273 if (gr_saved_screen.is_graphics==0) {
276 gr_palette_read( gr_pal_default );
278 for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
279 pTextMemory[i]=gr_saved_screen.video_memory[i];
280 gr_setcursor( gr_saved_screen.cursor_x, gr_saved_screen.cursor_y, gr_saved_screen.cursor_sline, gr_saved_screen.cursor_eline );
281 gr_palette_faded_out = 1;
282 gr_palette_fade_in( gr_pal_default, 32, 0 );
295 if( gr_saved_screen.video_memory ) {
296 free(gr_saved_screen.video_memory);
297 gr_saved_screen.video_memory = NULL;
303 int gr_vesa_setmode( int mode )
307 retcode=gr_vesa_checkmode( mode );
308 if ( retcode ) return retcode;
310 return gr_vesa_setmodea( mode );
315 int gr_set_mode(u_int32_t mode)
318 unsigned int w,h,t,data, r;
320 //JOHNgr_disable_default_palette_loading();
328 case SM(320,200)://0:
329 if (!isvga()) return 1;
330 gr_set_misc_mode(0x13);
331 w = 320; r = 320; h = 200; t=BM_LINEAR; data = (int)gr_video_memory;
333 case SM(640,400)://SM_640x400V:
334 retcode = gr_vesa_setmode( 0x100 );
335 if (retcode !=0 ) return retcode;
336 w = 640; r = 640; h = 400; t=BM_SVGA; data = 0;
338 case SM(640,480)://SM_640x480V:
339 retcode = gr_vesa_setmode( 0x101 );
340 if (retcode !=0 ) return retcode;
341 w = 640; r = 640; h = 480; t=BM_SVGA; data = 0;
343 case SM(800,600)://SM_800x600V:
344 retcode = gr_vesa_setmode( 0x103 );
345 if (retcode !=0 ) return retcode;
346 w = 800; r = 800; h = 600; t=BM_SVGA; data = 0;
348 case SM(1024,768)://SM_1024x768V:
349 retcode = gr_vesa_setmode( 0x105 );
350 if (retcode !=0 ) return retcode;
351 w = 1024; r = 1024; h = 768; t=BM_SVGA; data = 0;
353 /* case SM_640x480V15:
354 retcode = gr_vesa_setmode( 0x110 );
355 if (retcode !=0 ) return retcode;
356 w = 640; r = 640*2; h=480; t=BM_SVGA15; data = 0;
359 retcode = gr_vesa_setmode( 0x113 );
360 if (retcode !=0 ) return retcode;
361 w = 800; r = 800*2; h=600; t=BM_SVGA15; data = 0;
365 if (!isvga()) return 1;
366 gr_set_misc_mode(0x13);
373 gr_set_cellheight( 3 );
375 w = 320; r = 320; h = 100; t=BM_LINEAR; data = (int)gr_video_memory;
378 retcode = gr_vesa_setmode( 0x102 );
379 //gr_enable_default_palette_loading();
380 if (retcode !=0 ) return retcode;
383 //gr_set_cellheight( 1 );
384 gr_vesa_setlogical( 400 );
385 w = 400; r = 400; h = 600; t=BM_SVGA; data = 0;
388 if (!isvga()) return 1;
389 gr_set_misc_mode(0xd);
392 gr_set_cellheight( 3 );
393 w = 160; r = 160; h = 100; t=BM_LINEAR; data = (int)gr_video_memory;
395 case //22: // 3dmax 320x400
396 if (!isvga()) return 1;
397 gr_set_3dbios_mode(0x31);
398 //w = 320; r = 320/4; h = 400; t=BM_MODEX; data = 0;
399 w = 320; r = 320; h = 400; t=BM_SVGA; data = 0;
402 if (!isvga()) return 1;
403 if (mode==SM(320,240))
404 w = gr_modex_setmode( 2 );
405 else if (mode==SM(320,400))
406 w = gr_modex_setmode( 6 );
408 Error("unhandled vid mode\n");
409 //gr_enable_default_palette_loading();
410 h = w & 0xffff; w = w >> 16; r = w / 4;t = BM_MODEX; data = 0;
415 memset( grd_curscreen, 0, sizeof(grs_screen));
416 grd_curscreen->sc_mode = mode;
417 grd_curscreen->sc_w = w;
418 grd_curscreen->sc_h = h;
419 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
420 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
421 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
422 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
423 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
424 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = r;
425 grd_curscreen->sc_canvas.cv_bitmap.bm_type = t;
426 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)data;
427 VGA_current_mode = mode;
428 gr_set_current_canvas(NULL);
430 //gr_enable_default_palette_loading();
431 // gamefont_choose_game_font(w,h);
440 int mode = SM(320,200);
442 // Only do this function once!
447 if (!__djgpp_nearptr_enable()) {
448 printf("nearptr enable=%x\n", __dpmi_error);
452 gr_video_memory = (unsigned char *)(__djgpp_conventional_base + 0xa0000);
454 gr_video_memory=(unsigned char *)-1;
456 pVideoMode = (volatile ubyte *)(__djgpp_conventional_base+0x449);
457 pNumColumns = (volatile ushort *)(__djgpp_conventional_base+0x44a);
458 pNumRows = (volatile ubyte *)(__djgpp_conventional_base+0x484);
459 pCharHeight = (volatile ushort *)(__djgpp_conventional_base+0x485);
460 pCursorPos = (volatile ushort *)(__djgpp_conventional_base+0x450);
461 pCursorType = (volatile ushort *)(__djgpp_conventional_base+0x460);
462 pTextMemory = (volatile ushort *)(__djgpp_conventional_base+0xb8000);
469 // Save the current text screen mode
470 if (gr_save_mode()==1)
474 // Save the current palette, and fade it out to black.
475 gr_palette_read( gr_pal_default );
476 gr_palette_faded_out = 0;
477 org_gamma = gr_palette_get_gamma();
478 gr_palette_set_gamma( 0 );
479 gr_palette_fade_out( gr_pal_default, 32, 0 );
481 gr_palette_set_gamma( org_gamma );
488 __djgpp_nearptr_disable();
492 MALLOC( grd_curscreen,grs_screen,1 );
493 memset( grd_curscreen, 0, sizeof(grs_screen));
496 if ((retcode=gr_set_mode(mode)))
501 //JOHNgr_disable_default_palette_loading();
503 // Set all the screen, canvas, and bitmap variables that
504 // aren't set by the gr_set_mode call:
505 grd_curscreen->sc_canvas.cv_color = 0;
506 grd_curscreen->sc_canvas.cv_drawmode = 0;
507 grd_curscreen->sc_canvas.cv_font = NULL;
508 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
509 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
510 gr_set_current_canvas( &grd_curscreen->sc_canvas );
513 if (!dpmi_allocate_selector( &gr_fade_table, 256*GR_FADE_LEVELS, &gr_fade_table_selector ))
514 Error( "Error allocating fade table selector!" );
516 if (!dpmi_allocate_selector( &gr_palette, 256*3, &gr_palette_selector ))
517 Error( "Error allocating palette selector!" );
520 // if (!dpmi_allocate_selector( &gr_inverse_table, 32*32*32, &gr_inverse_table_selector ))
521 // Error( "Error allocating inverse table selector!" );
524 // Set flags indicating that this is installed.
528 atexit((void (*)) gr_close);
535 int gr_mode13_checkmode()
544 // 1=No VGA adapter installed
545 // 2=Program doesn't support this VESA granularity
546 // 3=Monitor doesn't support that VESA mode.:
547 // 4=Video card doesn't support that VESA mode.
548 // 5=No VESA driver found.
549 // 6=Bad Status after VESA call/
550 // 7=Not enough DOS memory to call VESA functions.
551 // 8=Error using DPMI.
552 // 9=Error setting logical line width.
553 // 10=Error allocating selector for A0000h
554 // 11=Not a valid mode support by gr.lib
556 int gr_check_mode(u_int32_t mode)
560 case SM(320,100)://19:
572 case SM(376,564): return gr_mode13_checkmode();
573 case SM(640,400): return gr_vesa_checkmode( 0x100 );
574 case SM(640,480): return gr_vesa_checkmode( 0x101 );
575 case SM(800,600): return gr_vesa_checkmode( 0x103 );
576 case SM(1024,768): return gr_vesa_setmode( 0x105 );
577 // case SM_640x480V15: return gr_vesa_setmode( 0x110 );
578 // case SM_800x600V15: return gr_vesa_setmode( 0x113 );
583 /* Palette Stuff Starts Here... */
585 void gr_palette_step_up( int r, int g, int b )
591 if (gr_palette_faded_out) return;
593 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
595 last_r = r; last_g = g; last_b = b;
601 for (i=0; i<256; i++ ) {
602 temp = (int)(*p++) + r + gr_palette_gamma;
604 else if (temp>63) temp=63;
606 temp = (int)(*p++) + g + gr_palette_gamma;
608 else if (temp>63) temp=63;
610 temp = (int)(*p++) + b + gr_palette_gamma;
612 else if (temp>63) temp=63;
618 void gr_palette_clear()
623 for (i=0; i<768; i++ ) {
626 gr_palette_faded_out = 1;
629 void gr_palette_load( ubyte * pal )
635 for (i=0; i<768; i++ ) {
636 c = pal[i] + gr_palette_gamma;
637 if ( c > 63 ) c = 63;
639 gr_current_pal[i] = pal[i];
641 gr_palette_faded_out = 0;
643 init_computed_colors();
647 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys )
651 fix fade_palette[768];
652 fix fade_palette_delta[768];
654 if (gr_palette_faded_out) return 0;
657 pal = gr_current_pal;
659 for (i=0; i<768; i++ ) {
660 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
661 fade_palette_delta[i] = fade_palette[i] / nsteps;
664 for (j=0; j<nsteps; j++ ) {
668 for (i=0; i<768; i++ ) {
669 fade_palette[i] -= fade_palette_delta[i];
670 if (fade_palette[i] < 0 )
672 c = f2i(fade_palette[i]);
673 if ( c > 63 ) c = 63;
677 gr_palette_faded_out = 1;
681 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
685 fix fade_palette[768];
686 fix fade_palette_delta[768];
689 if (!gr_palette_faded_out) return 0;
691 for (i=0; i<768; i++ ) {
692 gr_current_pal[i] = pal[i];
694 fade_palette_delta[i] = i2f(pal[i]+gr_palette_gamma) / nsteps;
697 for (j=0; j<nsteps; j++ ) {
701 for (i=0; i<768; i++ ) {
702 fade_palette[i] += fade_palette_delta[i];
703 if (fade_palette[i] > i2f(pal[i]+gr_palette_gamma) )
704 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
705 c = f2i(fade_palette[i]);
706 if ( c > 63 ) c = 63;
710 gr_palette_faded_out = 0;
714 void gr_palette_read(ubyte * palette)
719 for (i=0; i<768; i++ ) {
720 *palette++ = inp( 0x3c9 );
728 #endif // __ENV_DJGPP__