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;
56 void gr_set_cellheight( ubyte height )
69 outpw( 0x3c4, 0xE04 ); // enable chain4 mode
70 outpw( 0x3d4, 0x4014 ); // turn on dword mode
71 outpw( 0x3d4, 0xa317 ); // turn off byte mode
76 outpw( 0x3ce, 0x4005 ); // set Shift reg to 1
78 inp( 0x3da ); // dummy input
81 outp( 0x3c0, 0x61 ); // turns on PCS & PCC
83 inp( 0x3da ); // dummy input
89 void gr_turn_screen_off()
96 void gr_turn_screen_on()
103 void gr_set_misc_mode( uint mode )
107 memset( ®s, 0, sizeof(regs) );
109 int386( 0x10, ®s, ®s );
113 void gr_set_3dbios_mode( uint mode )
116 memset( ®s, 0, sizeof(regs) );
118 regs.w.bx = 0x3d00 | (mode & 0xff);
119 int386( 0x10, ®s, ®s );
124 void gr_set_text_25()
129 int386( 0x10, ®s, ®s );
133 void gr_set_text_43()
139 int386( 0x10, ®s, ®s );
142 int386( 0x10, ®s, ®s );
146 int386( 0x10, ®s, ®s );
149 void gr_set_text_50()
155 int386( 0x10, ®s, ®s );
158 int386( 0x10, ®s, ®s );
162 int386( 0x10, ®s, ®s );
165 ubyte is_graphics_mode()
168 tmp = inp( 0x3DA ); // Reset flip-flip
169 outp( 0x3C0, 0x30 ); // Select attr register 10
170 tmp = inp( 0x3C1 ); // Get graphics/text bit
174 void gr_setcursor(ubyte x, ubyte y, ubyte sline, ubyte eline)
178 memset( ®s, 0, sizeof(regs) );
183 int386( 0x10, ®s, ®s );
185 memset( ®s, 0, sizeof(regs) );
187 regs.h.ch = sline & 0xf;
188 regs.h.cl = eline & 0xf;
189 int386( 0x10, ®s, ®s );
192 void gr_getcursor(ubyte *x, ubyte *y, ubyte * sline, ubyte * eline)
196 memset( ®s, 0, sizeof(regs) );
199 int386( 0x10, ®s, ®s );
211 gr_saved_screen.is_graphics = is_graphics_mode();
212 gr_saved_screen.video_mode = *pVideoMode;
214 if (!gr_saved_screen.is_graphics) {
215 gr_saved_screen.width = *pNumColumns;
216 gr_saved_screen.height = *pNumRows+1;
217 gr_saved_screen.char_height = *pCharHeight;
218 gr_getcursor(&gr_saved_screen.cursor_x, &gr_saved_screen.cursor_y, &gr_saved_screen.cursor_sline, &gr_saved_screen.cursor_eline );
219 MALLOC(gr_saved_screen.video_memory,ushort, gr_saved_screen.width*gr_saved_screen.height );
220 for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
221 gr_saved_screen.video_memory[i] = pTextMemory[i];
224 if (gr_show_screen_info ) {
225 printf( "Current video mode 0x%x:\n", gr_saved_screen.video_mode );
226 if (gr_saved_screen.is_graphics)
227 printf( "Graphics mode\n" );
229 printf( "Text mode\n" );
230 printf( "( %d columns by %d rows)\n", gr_saved_screen.width, gr_saved_screen.height );
231 printf( "Char height is %d pixel rows\n", gr_saved_screen.char_height );
232 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 );
243 memset( ®s, 0, sizeof(regs) );
245 int386( 0x10, ®s, ®s );
247 if ( regs.h.al == 0x1a )
253 void gr_restore_mode()
260 gr_palette_fade_out( gr_palette, 32, 0 );
261 gr_palette_set_gamma(0);
262 if ( gr_saved_screen.video_mode == 3 ) {
263 switch( gr_saved_screen.height ) {
264 case 43: gr_set_text_43(); break;
265 case 50: gr_set_text_50(); break;
266 default: gr_set_text_25(); break;
269 gr_set_misc_mode(gr_saved_screen.video_mode);
271 if (gr_saved_screen.is_graphics==0) {
274 gr_palette_read( gr_pal_default );
276 for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
277 pTextMemory[i]=gr_saved_screen.video_memory[i];
278 gr_setcursor( gr_saved_screen.cursor_x, gr_saved_screen.cursor_y, gr_saved_screen.cursor_sline, gr_saved_screen.cursor_eline );
279 gr_palette_faded_out = 1;
280 gr_palette_fade_in( gr_pal_default, 32, 0 );
293 if( gr_saved_screen.video_memory ) {
294 free(gr_saved_screen.video_memory);
295 gr_saved_screen.video_memory = NULL;
301 int gr_vesa_setmode( int mode )
305 retcode=gr_vesa_checkmode( mode );
306 if ( retcode ) return retcode;
308 return gr_vesa_setmodea( mode );
313 int gr_set_mode(u_int32_t mode)
316 unsigned int w,h,t,data, r;
318 //JOHNgr_disable_default_palette_loading();
326 case SM(320,200)://0:
327 if (!isvga()) return 1;
328 gr_set_misc_mode(0x13);
329 w = 320; r = 320; h = 200; t=BM_LINEAR; data = (int)gr_video_memory;
331 case SM(640,400)://SM_640x400V:
332 retcode = gr_vesa_setmode( 0x100 );
333 if (retcode !=0 ) return retcode;
334 w = 640; r = 640; h = 400; t=BM_SVGA; data = 0;
336 case SM(640,480)://SM_640x480V:
337 retcode = gr_vesa_setmode( 0x101 );
338 if (retcode !=0 ) return retcode;
339 w = 640; r = 640; h = 480; t=BM_SVGA; data = 0;
341 case SM(800,600)://SM_800x600V:
342 retcode = gr_vesa_setmode( 0x103 );
343 if (retcode !=0 ) return retcode;
344 w = 800; r = 800; h = 600; t=BM_SVGA; data = 0;
346 case SM(1024,768)://SM_1024x768V:
347 retcode = gr_vesa_setmode( 0x105 );
348 if (retcode !=0 ) return retcode;
349 w = 1024; r = 1024; h = 768; t=BM_SVGA; data = 0;
351 /* case SM_640x480V15:
352 retcode = gr_vesa_setmode( 0x110 );
353 if (retcode !=0 ) return retcode;
354 w = 640; r = 640*2; h=480; t=BM_SVGA15; data = 0;
357 retcode = gr_vesa_setmode( 0x113 );
358 if (retcode !=0 ) return retcode;
359 w = 800; r = 800*2; h=600; t=BM_SVGA15; data = 0;
363 if (!isvga()) return 1;
364 gr_set_misc_mode(0x13);
371 gr_set_cellheight( 3 );
373 w = 320; r = 320; h = 100; t=BM_LINEAR; data = (int)gr_video_memory;
376 retcode = gr_vesa_setmode( 0x102 );
377 //gr_enable_default_palette_loading();
378 if (retcode !=0 ) return retcode;
381 //gr_set_cellheight( 1 );
382 gr_vesa_setlogical( 400 );
383 w = 400; r = 400; h = 600; t=BM_SVGA; data = 0;
386 if (!isvga()) return 1;
387 gr_set_misc_mode(0xd);
390 gr_set_cellheight( 3 );
391 w = 160; r = 160; h = 100; t=BM_LINEAR; data = (int)gr_video_memory;
393 case //22: // 3dmax 320x400
394 if (!isvga()) return 1;
395 gr_set_3dbios_mode(0x31);
396 //w = 320; r = 320/4; h = 400; t=BM_MODEX; data = 0;
397 w = 320; r = 320; h = 400; t=BM_SVGA; data = 0;
400 if (!isvga()) return 1;
401 if (mode==SM(320,240))
402 w = gr_modex_setmode( 2 );
403 else if (mode==SM(320,400))
404 w = gr_modex_setmode( 6 );
406 Error("unhandled vid mode\n");
407 //gr_enable_default_palette_loading();
408 h = w & 0xffff; w = w >> 16; r = w / 4;t = BM_MODEX; data = 0;
413 memset( grd_curscreen, 0, sizeof(grs_screen));
414 grd_curscreen->sc_mode = mode;
415 grd_curscreen->sc_w = w;
416 grd_curscreen->sc_h = h;
417 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
418 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
419 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
420 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
421 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
422 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = r;
423 grd_curscreen->sc_canvas.cv_bitmap.bm_type = t;
424 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)data;
425 gr_set_current_canvas(NULL);
427 //gr_enable_default_palette_loading();
428 gamefont_choose_game_font(w,h);
433 int gr_init(int mode)
438 // Only do this function once!
443 if (!__djgpp_nearptr_enable()) {
444 printf("nearptr enable=%x\n", __dpmi_error);
448 gr_video_memory = (unsigned char *)(__djgpp_conventional_base + 0xa0000);
450 gr_video_memory=(unsigned char *)-1;
452 pVideoMode = (volatile ubyte *)(__djgpp_conventional_base+0x449);
453 pNumColumns = (volatile ushort *)(__djgpp_conventional_base+0x44a);
454 pNumRows = (volatile ubyte *)(__djgpp_conventional_base+0x484);
455 pCharHeight = (volatile ushort *)(__djgpp_conventional_base+0x485);
456 pCursorPos = (volatile ushort *)(__djgpp_conventional_base+0x450);
457 pCursorType = (volatile ushort *)(__djgpp_conventional_base+0x460);
458 pTextMemory = (volatile ushort *)(__djgpp_conventional_base+0xb8000);
465 // Save the current text screen mode
466 if (gr_save_mode()==1)
470 // Save the current palette, and fade it out to black.
471 gr_palette_read( gr_pal_default );
472 gr_palette_faded_out = 0;
473 org_gamma = gr_palette_get_gamma();
474 gr_palette_set_gamma( 0 );
475 gr_palette_fade_out( gr_pal_default, 32, 0 );
477 gr_palette_set_gamma( org_gamma );
484 __djgpp_nearptr_disable();
488 MALLOC( grd_curscreen,grs_screen,1 );
489 memset( grd_curscreen, 0, sizeof(grs_screen));
492 if ((retcode=gr_set_mode(mode)))
497 //JOHNgr_disable_default_palette_loading();
499 // Set all the screen, canvas, and bitmap variables that
500 // aren't set by the gr_set_mode call:
501 grd_curscreen->sc_canvas.cv_color = 0;
502 grd_curscreen->sc_canvas.cv_drawmode = 0;
503 grd_curscreen->sc_canvas.cv_font = NULL;
504 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
505 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
506 gr_set_current_canvas( &grd_curscreen->sc_canvas );
509 if (!dpmi_allocate_selector( &gr_fade_table, 256*GR_FADE_LEVELS, &gr_fade_table_selector ))
510 Error( "Error allocating fade table selector!" );
512 if (!dpmi_allocate_selector( &gr_palette, 256*3, &gr_palette_selector ))
513 Error( "Error allocating palette selector!" );
516 // if (!dpmi_allocate_selector( &gr_inverse_table, 32*32*32, &gr_inverse_table_selector ))
517 // Error( "Error allocating inverse table selector!" );
520 // Set flags indicating that this is installed.
524 atexit((void (*)) gr_close);
531 int gr_mode13_checkmode()
540 // 1=No VGA adapter installed
541 // 2=Program doesn't support this VESA granularity
542 // 3=Monitor doesn't support that VESA mode.:
543 // 4=Video card doesn't support that VESA mode.
544 // 5=No VESA driver found.
545 // 6=Bad Status after VESA call/
546 // 7=Not enough DOS memory to call VESA functions.
547 // 8=Error using DPMI.
548 // 9=Error setting logical line width.
549 // 10=Error allocating selector for A0000h
550 // 11=Not a valid mode support by gr.lib
552 int gr_check_mode(u_int32_t mode)
556 case SM(320,100)://19:
568 case SM(376,564): return gr_mode13_checkmode();
569 case SM(640,400): return gr_vesa_checkmode( 0x100 );
570 case SM(640,480): return gr_vesa_checkmode( 0x101 );
571 case SM(800,600): return gr_vesa_checkmode( 0x103 );
572 case SM(1024,768): return gr_vesa_setmode( 0x105 );
573 // case SM_640x480V15: return gr_vesa_setmode( 0x110 );
574 // case SM_800x600V15: return gr_vesa_setmode( 0x113 );
579 /* Palette Stuff Starts Here... */
581 void gr_palette_step_up( int r, int g, int b )
587 if (gr_palette_faded_out) return;
589 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
591 last_r = r; last_g = g; last_b = b;
597 for (i=0; i<256; i++ ) {
598 temp = (int)(*p++) + r + gr_palette_gamma;
600 else if (temp>63) temp=63;
602 temp = (int)(*p++) + g + gr_palette_gamma;
604 else if (temp>63) temp=63;
606 temp = (int)(*p++) + b + gr_palette_gamma;
608 else if (temp>63) temp=63;
614 void gr_palette_clear()
619 for (i=0; i<768; i++ ) {
622 gr_palette_faded_out = 1;
625 void gr_palette_load( ubyte * pal )
631 for (i=0; i<768; i++ ) {
632 c = pal[i] + gr_palette_gamma;
633 if ( c > 63 ) c = 63;
635 gr_current_pal[i] = pal[i];
637 gr_palette_faded_out = 0;
639 init_computed_colors();
643 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys )
647 fix fade_palette[768];
648 fix fade_palette_delta[768];
650 if (gr_palette_faded_out) return 0;
653 pal = gr_current_pal;
655 for (i=0; i<768; i++ ) {
656 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
657 fade_palette_delta[i] = fade_palette[i] / nsteps;
660 for (j=0; j<nsteps; j++ ) {
664 for (i=0; i<768; i++ ) {
665 fade_palette[i] -= fade_palette_delta[i];
666 if (fade_palette[i] < 0 )
668 c = f2i(fade_palette[i]);
669 if ( c > 63 ) c = 63;
673 gr_palette_faded_out = 1;
677 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
681 fix fade_palette[768];
682 fix fade_palette_delta[768];
685 if (!gr_palette_faded_out) return 0;
687 for (i=0; i<768; i++ ) {
688 gr_current_pal[i] = pal[i];
690 fade_palette_delta[i] = i2f(pal[i]+gr_palette_gamma) / nsteps;
693 for (j=0; j<nsteps; j++ ) {
697 for (i=0; i<768; i++ ) {
698 fade_palette[i] += fade_palette_delta[i];
699 if (fade_palette[i] > i2f(pal[i]+gr_palette_gamma) )
700 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
701 c = f2i(fade_palette[i]);
702 if ( c > 63 ) c = 63;
706 gr_palette_faded_out = 0;
710 void gr_palette_read(ubyte * palette)
715 for (i=0; i<768; i++ ) {
716 *palette++ = inp( 0x3c9 );