home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xlibo2.zip / XLIBOS2.C < prev    next >
Text File  |  1993-09-18  |  9KB  |  456 lines

  1. #define INCL_BASE
  2. #define INCL_SUB
  3. #define HANDLE 0
  4. #define XRES 320
  5. #define XRES_BYTES 80
  6. #define YRES 240
  7. #define NUM 5
  8.  
  9. #define SC_INDEX 0x3c4
  10. #define GC_INDEX 0x3ce
  11. #define CRTC_INDEX 0x3d4
  12. #define CRT_C 0x3d5
  13. #define MISC_OUT 0x3c2
  14. #define MISC_OUTPUT 0x3c2
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <os2.h>
  19.  
  20.  
  21.  
  22. extern _Far16 _Pascal outp( USHORT, UCHAR );
  23. extern _Far16 _Pascal outpw( USHORT, USHORT );
  24. extern UCHAR _Far16 _Pascal inp( USHORT );
  25. extern USHORT _Far16 _Pascal inpw( USHORT );
  26. extern void draw( PCH, ULONG );
  27.  
  28. PCH screen;
  29. CHAR ActivePage;
  30. CHAR VisualPage;
  31. USHORT SCREEN_Width = 20;
  32. struct _VIOPHYSBUF phys;
  33. struct _VIOMODEINFO orig, moda;
  34. PCH XPage[4];
  35.  
  36. struct SPRITE_INFO
  37. {
  38.     PCHAR image;
  39.     char xs, ys;
  40.     int x, y;
  41.     int spx, spy;
  42.     char xdir, ydir;
  43. };
  44.  
  45. struct IMAGE_TABLE
  46. {
  47.     PCHAR img[4];
  48. };
  49.  
  50. struct PAGE_TABLE
  51. {
  52.     PCHAR p[4];
  53. };
  54.  
  55. int InitModeX( struct PAGE_TABLE *page_table )
  56. {
  57.     char tmp;
  58.     int i;
  59.     USHORT ModeXTable[] =
  60.     {
  61.         0x0d06,
  62.         0x3e07,
  63.         0x4109,
  64.         0xea10,
  65.         0xac11,
  66.         0xdf12,
  67.         0x0014,
  68.         0xe715,
  69.         0x0616,
  70.         0xe317
  71.     };
  72.  
  73.     phys.pBuf = (unsigned char *) 0xA0000;
  74.     phys.cb = 128000;
  75.     moda.cb = 12;
  76.     moda.fbType = 3;
  77.     moda.color = 8;
  78.     moda.col = 40;
  79.     moda.row = 25;
  80.     moda.hres = 320;
  81.     moda.vres = 200;
  82.     orig.cb = sizeof( orig );
  83.     VioGetMode( &orig, HANDLE );
  84.     VioSetMode( &moda, HANDLE );
  85.     VioGetPhysBuf( &phys, 0 );
  86.     screen = MAKEP( phys.asel[0], 0 );
  87.  
  88.     outpw( (USHORT) SC_INDEX, 0x0604 );
  89.     outpw( (USHORT) SC_INDEX, 0x0100 );
  90.     outp( (USHORT) MISC_OUTPUT, 0xe3 );
  91.     outpw( (USHORT) SC_INDEX, 0x0300 );
  92.     outp( (USHORT) CRTC_INDEX, 0x11 );
  93.     tmp = inp( (USHORT) CRT_C );
  94.     tmp = tmp & 0x7f;
  95.     outp( CRT_C, tmp );
  96.  
  97.     for( i = 0; i < 10; i++ )
  98.     {
  99.         outpw( (USHORT) CRTC_INDEX, ModeXTable[i] );
  100.     }
  101.  
  102.     outpw( (USHORT) SC_INDEX, 0x0f02 );
  103.     XPage[0] = screen;
  104.     XPage[1] = screen + 19200;
  105.     XPage[2] = screen + 38400;
  106.     XPage[3] = XPage[2] + 19200;
  107.  
  108.     for( i = 0; i < 4; i++ )
  109.     {
  110.         page_table->p[i] = XPage[i];
  111.     }
  112.  
  113.     ActivePage = 0;
  114.     VisualPage = 0;
  115.     DosSetPriority( 0, PRTYC_TIMECRITICAL, 30, 0 );
  116. }
  117.  
  118.  
  119. void XPixel( int x, int y, char Color )
  120. {
  121.     int loc, tmp;
  122.     char c;
  123.  
  124.     loc = ((y * (SCREEN_Width << 2)) + (x >> 2)) + (ActivePage * 19200);
  125.     x = x & 0x03;
  126.     c = 0x01;
  127.     c = c << x;
  128.     tmp = (c << 8) + 0x02;
  129.     outpw( SC_INDEX, tmp );
  130.     screen[loc] = Color;
  131. }
  132.  
  133.  
  134. int XReadPixel( int x, int y )
  135. {
  136.     int loc, tmp;
  137.     char c;
  138.  
  139.     loc = ((y * 80) + (x >> 2)) + (ActivePage * 19200);
  140.     x = x & 0x03;
  141.     tmp = (x << 8) & 0x04;
  142.     outpw( GC_INDEX, tmp );
  143.     return screen[loc];
  144. }
  145.  
  146.  
  147. int CloseModeX( void )
  148. {
  149.     VioSetMode( &orig, HANDLE );
  150. }
  151.  
  152.  
  153. void XSelectPlane( char plane )
  154. {
  155.     int tmp;
  156.  
  157.     plane = plane & 0x0f;
  158.     outpw((USHORT) SC_INDEX, (plane << 8) & 0x02);
  159.     tmp = (plane << 8) & 0x04;
  160.     outpw( GC_INDEX, tmp );
  161. }
  162.  
  163.  
  164. int XCls( char color )
  165. {
  166.     int count;
  167.  
  168.     outpw( (USHORT) SC_INDEX, 0x0f02 );
  169.     memset (screen, color, 64000 );
  170. }
  171.  
  172.  
  173. XBoxAt( int x1, int y1, int x2, int y2, char color )
  174. {
  175.     unsigned int y, lx, ly, ux, uy;
  176.     unsigned long tmp;
  177.     int size;
  178.     ULONG pageadder;
  179.  
  180.     x1 = x1 >> 2;
  181.     x2 = x2 >> 2;
  182.     size = x2 - x1;
  183.     lx = min(x1, x2);
  184.     ux = max(x1, x2);
  185.     ly = min(y1, y2);
  186.     uy = max(y1, y2);
  187.     pageadder = 19200 * ActivePage;
  188.  
  189.     tmp = SCREEN_Width << 2;
  190.     outpw( (USHORT) SC_INDEX, 0x0f02 ); /* all pages */
  191.     for( y = (ly * tmp) + x1; y <= uy * tmp; y += tmp )
  192.     {
  193.         memset(&screen[pageadder + y], color, size);
  194.     }
  195. }
  196.  
  197. void XWaitRetrace( void )
  198. {
  199.     char ret;
  200. /* some prob here I'll fix it later */
  201.     while( (inp( 0x3da ) & 0x08) != 0 )
  202.         ;
  203.     while( (inp( 0x3da ) & 0x08) == 0 )
  204.         ;
  205. }
  206.  
  207. void XWaitPeriod( void )
  208. {
  209.     char ret;
  210.  
  211.     while( (inp( 0x3da ) & 0x08) == 0 )
  212.         ;
  213.     while( (inp( 0x3da ) & 0x08) != 0 )
  214.         ;
  215. }
  216.  
  217. void XCopyFromPageToPage( char SourcePage, int x, int y, int xs,
  218.                                                          int ys,
  219.                                                          char DestPage,
  220.                                                          int dx,
  221.                                                          int dy )
  222. {
  223.     ULONG i, a, source, dest;
  224.     char *td, *ts;
  225.  
  226.     dest = (dx >> 2) + (dy * 80) + (19200 * DestPage);
  227.     source = (x >> 2) + (y * 80) + (19200 * SourcePage);
  228.     xs = (xs >> 2);
  229.  
  230.     outpw( 0x03ce, 0x0008 );
  231.     outpw( SC_INDEX, 0x0f02 );
  232.  
  233.     for( i = 0; i < ys; i++ )
  234.     {
  235.         for( a = 0; a < xs; a++ )
  236.         {
  237.             screen[dest + a] = screen[source + a];
  238.         }
  239.         dest = dest + 80;
  240.         source = source + 80;
  241.     }
  242.  
  243.     outp( 0x03cf, 0xff );
  244. }
  245.  
  246. void XSetVisualPage( char page )
  247. {
  248.     char tmp;
  249.     char low, hi;
  250.     USHORT offset;
  251.  
  252.     offset = page * 19200;
  253.     low = (offset & 0x0ff);
  254.     hi = (offset >> 8);
  255.     VisualPage = page;
  256.  
  257.     do
  258.     {
  259.         tmp = inp(0x3da);
  260.     }
  261.     while( (tmp & 0x08) != 0 );
  262.  
  263.     outp( 0x03d4, 0x0c );
  264.     outp( 0x03d5, hi );
  265.     outp( 0x03d4, 0x0d );
  266.     outp( 0x03d5, low );
  267.  
  268.     do
  269.     {
  270.         tmp = inp( 0x3da );
  271.     }
  272.     while( (tmp & 0x08) == 0 );
  273. }
  274.  
  275. void XSetActivePage( char page )
  276. {
  277.     ActivePage = page;
  278. }
  279.  
  280. void XSetPan( USHORT pan )
  281. {
  282.     pan = pan >> 2;
  283.     outpw( 0x03d4, 0x000d );
  284.     outpw( 0x03d5, (pan & 0x0ff) );
  285. }
  286.  
  287. void XBltImage( int x, int y, char xs, char ys, char *image )
  288. {
  289.     int loc, tmp, tx, ty;
  290.     char c, StartPlane;
  291.     int a = 0, pagenum;
  292.     USHORT page[4] =
  293.     {0x102, 0x0202, 0x0402, 0x0802};
  294.  
  295.     StartPlane = (x & 0x03);
  296.     loc = ((y * (SCREEN_Width << 2)) + (x >> 2));
  297.     loc = loc + (ActivePage * 19200);
  298.  
  299.     for( pagenum = 0; pagenum < 4; pagenum++ )
  300.     {
  301.         tmp = loc;
  302.         outpw( SC_INDEX, page[StartPlane] ); /*pagenum*/
  303.         StartPlane = StartPlane + 1;
  304.         if( StartPlane == 4 )
  305.         {
  306.             StartPlane = 0;
  307.             loc++;
  308.         }
  309.         for( ty = 0; ty < (ys); ty++ )
  310.         {
  311.             for( tx = 0; tx < (xs >> 2); tx++ )
  312.             {
  313.                 screen[tmp + tx] = image[a++];
  314.             }
  315.             tmp = tmp + 80;
  316.         }
  317.     }
  318. }
  319.  
  320. PCHAR XLoadImage( char filename[50], char *xs, char *ys )
  321. {
  322.     FILE *img;
  323.     int i2 = 0;
  324.     char ix, iy;
  325.     char *image;
  326.  
  327.     img = fopen( filename, "rb" );
  328.     if( img == NULLHANDLE )
  329.     {
  330.         DosBeep(1000, 100);
  331.         return NULL;
  332.     }
  333.     ix = fgetc( img );
  334.     iy = fgetc( img );
  335.     image = malloc( ix * iy );
  336.     for( i2 = 0; i2 < 64; i2++ )
  337.     {
  338.         image[0 + i2] = fgetc(img);
  339.         image[64 + i2] = fgetc(img);
  340.         image[128 + i2] = fgetc(img);
  341.         image[(128 + 64) + i2] = fgetc(img);
  342.     }
  343.     *xs = ix;
  344.     *ys = iy;
  345.     return image;
  346. }
  347.  
  348.  
  349. void XCBltImage( int x, int y, struct IMAGE_TABLE *image )
  350. {
  351.     USHORT loc, tmp, tx, ty;
  352.     char c, StartPlane;
  353.     USHORT a = 0, pagenum;
  354.     USHORT page[4] =
  355.     {0x102, 0x0202, 0x0402, 0x0802};
  356.  
  357.     StartPlane = (x & 0x03);
  358.     loc = ((y * (SCREEN_Width << 2)) + (x >> 2)) + (19200 * ActivePage);
  359.  
  360.     for( pagenum = 0; pagenum < 4; pagenum++ )
  361.     {
  362.         tmp = loc;
  363.         outpw(SC_INDEX, page[StartPlane]);
  364.         StartPlane = StartPlane + 1;
  365.         if (StartPlane == 4)
  366.         {
  367.             StartPlane = 0;
  368.             loc++;
  369.         }
  370.         draw(image->img[pagenum], screen + tmp);
  371.     }
  372. }
  373.  
  374. XSetRGB( char color, char red, char green, char blue )
  375. {
  376.     outp( 0x3c8, color );
  377.     outp( 0x3c9, red ); 
  378.     outp( 0x3c9, green );
  379.     outp( 0x3c9, blue );
  380. }
  381.  
  382. #define pn(x) ( 797-((255-x)*3) )
  383.  
  384. XLoadCel( char filename[60], char *where )
  385. {
  386.     char *tmp, c;
  387.     int i;
  388.     char pal[800];
  389.     FILE *cel;
  390.  
  391.     cel = fopen( filename, "rb" );
  392.     tmp = malloc( 64000 );
  393.     for( i = 0; i < 800; i++ )
  394.     {
  395.         pal[i] = fgetc(cel);
  396.     }
  397.     for( i = 255; i >= 0; i-- )
  398.     {
  399.         XSetRGB(i, pal[pn(i)], pal[pn(i) + 1], pal[pn(i) + 2]);
  400.     }
  401.  
  402.     for( i = 0; i < 64000; i++ )
  403.     {
  404.         tmp[i] = fgetc( cel );
  405.     }
  406.  
  407.     outpw( SC_INDEX, 0x102 );
  408.     for( i = 0; i < 64000; i = i + 4 )
  409.     {
  410.         where[(i >> 2)] = tmp[i];
  411.     }
  412.     outpw( SC_INDEX, 0x202 );
  413.     for( i = 1; i < 64000; i = i + 4 )
  414.     {
  415.         where[(i >> 2)] = tmp[i];
  416.     }
  417.  
  418.     outpw( SC_INDEX, 0x402 );
  419.     for( i = 2; i < 64000; i = i + 4 )
  420.     {
  421.         where[(i >> 2)] = tmp[i];
  422.     }
  423.     outpw( SC_INDEX, 0x802 );
  424.     for( i = 3; i < 64000; i = i + 4 )
  425.     {
  426.         where[(i >> 2)] = tmp[i];
  427.     }
  428. }
  429.  
  430.  
  431. XCLoadImage( char filename[60], struct IMAGE_TABLE *image )
  432. {
  433.     USHORT size[4], tmp;
  434.     FILE *sprite;
  435.     char hi, lo, i, *spr;
  436.  
  437.     sprite = fopen( filename, "rb" );
  438.     for( i = 0; i < 4; i++ )
  439.     {
  440.         lo = fgetc( sprite );
  441.         hi = fgetc( sprite );
  442.         size[i] = (hi << 8) + lo;
  443.     }
  444.     for( i = 0; i < 4; i++ )
  445.     {
  446.         image->img[i] = malloc( size[i] );
  447.         spr = image->img[i];
  448.         for( tmp = 0; tmp < size[i]; tmp++ )
  449.         {
  450.             spr[tmp] = fgetc(sprite);
  451.         }
  452.     }
  453.     return 0;
  454. }
  455.  
  456.