home *** CD-ROM | disk | FTP | other *** search
/ Monster Disc 2: The Best of 1992 / MONSTER2.ISO / prog / djgpp / cbgrx102.a01 / CONTRIB / LIBGRX / SRC / SETMODE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-12  |  6.4 KB  |  241 lines

  1. /**
  2.  ** SETMODE.C
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #define  _SETMODE_C_
  25.  
  26. #include "grx.h"
  27. #include "libgrx.h"
  28. #include "grdriver.h"
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33.  
  34.  
  35. GrContext _GrContext;            /* current graphics context */
  36. GrContext _GrVidPage;            /* the whole screen */
  37.  
  38. int  _GrCurrentMode    = GR_default_text;    /* current video mode */
  39. int  _GrCanBcopyInBlit = FALSE;        /* separate R/W pages in adapter */
  40. int  _GrBigFrameBuffer = FALSE;        /* set if frame buffer > 64K */
  41. int  _GrNumColors      = 256;        /* number of colors */
  42. int  _GrAdapterType    = GR_VGA;    /* type of video adapter */
  43. int  _GrDriverIndex    = 0;        /* low-level video routine selector */
  44. int  _GrRdOnlyOffset   = 0;        /* add for read-only video page */
  45. int  _GrWrOnlyOffset   = 0;        /* add for write-only video page */
  46.  
  47. int  _GrScreenX = 80;            /* screen width  */
  48. int  _GrScreenY = 25;            /* screen height */
  49.  
  50. char _GrMouseDrawn = 0;            /* set if mouse drawn */
  51. char _GrMouseCheck = 0;            /* set if mouse blocking needed */
  52.  
  53. static void dummy(void) {}
  54.  
  55. int  (*_GrMouseBlock)(GrContext *c,int x1,int y1,int x2,int y2);
  56. void (*_GrMouseUnBlock)(void);
  57. void (*_GrMouseUnInit)(void) = dummy;
  58.  
  59. static void (*setmodehook)(void) = NULL;
  60.  
  61. #ifndef _SINGLE_MODE_
  62. handler  _GrResetValues[NumOfHandlers]      = { NULL };
  63. handler *_GrResetAddresses[NumOfHandlers] = { NULL };
  64. #endif
  65.  
  66. void GrSetMode(int mode,int width,int height,int colors)
  67. {
  68.     long planesize;
  69.     int  flags;
  70.  
  71.     (*_GrMouseUnInit)();
  72. #ifndef _SINGLE_MODE_
  73.     for(flags = 0; flags < NumOfHandlers; flags++) {
  74.         if(_GrResetValues[flags] == NULL) continue;
  75.         *(_GrResetAddresses[flags]) = _GrResetValues[flags];
  76.     }
  77. #endif
  78.     if((mode >= GR_80_25_text) && (mode <= GR_width_height_color_graphics)) {
  79.         memset(&_GrVidPage,0,sizeof(GrContext));
  80.         flags = _GrLowSetMode(mode,width,height,colors);
  81.         _GrCurrentMode = mode;
  82.         switch(flags & GRD_TYPE_MASK) {
  83.           case GRD_VGA:
  84.         _GrVidPage.gc_baseaddr = (char far *)VGA_FRAME;
  85.         _GrAdapterType = GR_VGA;
  86.         break;
  87.           case GRD_EGA:
  88.         _GrVidPage.gc_baseaddr = (char far *)EGA_FRAME;
  89.         _GrAdapterType  = GR_EGA;
  90.         break;
  91.           case GRD_HERC:
  92.         _GrVidPage.gc_baseaddr = (char far *)HERC_FRAME;
  93.         _GrAdapterType  = GR_HERC;
  94.         break;
  95.           case GRD_8514A:
  96.         _GrAdapterType  = GR_8514A;
  97.         break;
  98.           case GRD_S3:
  99.         _GrAdapterType  = GR_S3;
  100.         break;
  101.           default:
  102.         _GrLowSetMode(GR_default_text,0,0,0);
  103.         fputs("GrSetMode: unknown adapter type in driver\n",stderr);
  104.         exit(1);
  105.         }
  106.         switch(flags & GRD_PLANE_MASK) {
  107. #if (GRXPLANES & 16)
  108.           case GRD_16_PLANES:
  109.         if(_GrAdapterType != GR_VGA) goto Default;
  110.         _GrDriverIndex = VGA32K_DRIVER;
  111.         _GrNumColors = 32768;
  112.         break;
  113. #endif
  114. #if (GRXPLANES & 8) || (GRXPLANES & MODE_8514A)
  115.           case GRD_8_PLANES:
  116.         switch(_GrAdapterType) {
  117.           case GR_VGA:
  118.             _GrDriverIndex = VGA256_DRIVER;
  119.             break;
  120.           case GR_8514A:
  121.             _GrDriverIndex = IBM_8514A_DRIVER;
  122.             break;
  123.           case GR_S3:
  124.             _GrDriverIndex = S3_DRIVER;
  125.             break;
  126.           default:
  127.             goto Default;
  128.         }
  129.         _GrNumColors = 256;
  130.         break;
  131. #endif
  132. #if (GRXPLANES & 4)
  133.           case GRD_4_PLANES:
  134.         if(_GrAdapterType == GR_HERC) goto Default;
  135.         _GrDriverIndex = VGA16_DRIVER;
  136.         _GrNumColors = 16;
  137.         break;
  138. #endif
  139. #if (GRXPLANES & 1)
  140.           case GRD_1_PLANE:
  141.         if(_GrAdapterType != GR_HERC) goto Default;
  142.         _GrDriverIndex = HERC_DRIVER;
  143.         _GrNumColors = 2;
  144.         break;
  145. #endif
  146.           default:
  147.           Default:
  148.         _GrLowSetMode(GR_default_text,0,0,0);
  149.         fputs("GrSetMode: bad color plane # in driver\n",stderr);
  150.         exit(1);
  151.         }
  152.         if(mode >= GR_320_200_graphics) {
  153.         planesize = GrPlaneSize(_GrScreenX,_GrScreenY);
  154. #ifdef _MAXVIDPLANESIZE
  155.         if((_GrAdapterType != GR_8514A) && (_GrAdapterType != GR_S3)) {
  156.             if(planesize > _MAXVIDPLANESIZE) {
  157.             _GrLowSetMode(GR_default_text,0,0,0);
  158.             fputs("GrSetMode: graphics mode too big\n",stderr);
  159.             exit(1);
  160.             }
  161.         }
  162. #endif
  163.         if(planesize < 0x10000L) {
  164.             _GrCanBcopyInBlit = TRUE;
  165.             _GrBigFrameBuffer = FALSE;
  166.             _GrRdOnlyOffset   = 0;
  167.             _GrWrOnlyOffset   = 0;
  168.         }
  169.         else if((flags & GRD_PAGING_MASK) == GRD_RW_64K) {
  170.             _GrCanBcopyInBlit = TRUE;
  171.             _GrBigFrameBuffer = TRUE;
  172.             _GrRdOnlyOffset   = RDONLY_OFF;
  173.             _GrWrOnlyOffset   = WRONLY_OFF;
  174.         }
  175.         else {
  176.             _GrCanBcopyInBlit = FALSE;
  177.             _GrBigFrameBuffer = TRUE;
  178.             _GrRdOnlyOffset   = 0;
  179.             _GrWrOnlyOffset   = 0;
  180.         }
  181.         _GrVidPage.gc_onscreen     = TRUE;
  182.         _GrVidPage.gc_frameaddr  = BASE_ADDRESS(&_GrVidPage);
  183.         _GrVidPage.gc_lineoffset = GrLineOffset(_GrScreenX);
  184.         _GrVidPage.gc_xmax = _GrVidPage.gc_xcliphi = _GrScreenX - 1;
  185.         _GrVidPage.gc_ymax = _GrVidPage.gc_ycliphi = _GrScreenY - 1;
  186.         _GrContext = _GrVidPage;
  187.         if(_GrNumColors == 16) _GrP4Init(flags & GRD_MEM_MASK);
  188.         GrRefreshColors();
  189.         _GrMouseDrawn = 0;
  190.         _GrMouseCheck = 0;
  191.         }
  192.         if(setmodehook != NULL) (*setmodehook)();
  193.     }
  194. }
  195.  
  196. void GrSetModeHook(void (*hookfunc)(void))
  197. {
  198.     setmodehook = hookfunc;
  199. }
  200.  
  201. int GrCurrentMode(void)
  202. {
  203.     return(_GrCurrentMode);
  204. }
  205.  
  206. int GrAdapterType(void)
  207. {
  208.     return(_GrAdapterType);
  209. }
  210.  
  211. int GrScreenX(void)
  212. {
  213.     return(_GrScreenX);
  214. }
  215.  
  216. int GrScreenY(void)
  217. {
  218.     return(_GrScreenY);
  219. }
  220.  
  221. int GrSizeX(void)
  222. {
  223.     return(_GrContext.gc_xmax + 1);
  224. }
  225.  
  226. int GrSizeY(void)
  227. {
  228.     return(_GrContext.gc_ymax + 1);
  229. }
  230.  
  231. int GrMaxX(void)
  232. {
  233.     return(_GrContext.gc_xmax);
  234. }
  235.  
  236. int GrMaxY(void)
  237. {
  238.     return(_GrContext.gc_ymax);
  239. }
  240.  
  241.