home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / foxbear / plane.c < prev    next >
C/C++ Source or Header  |  1997-07-14  |  8KB  |  424 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1997 Microsoft Corporation. All Rights Reserved.
  4.  *  Copyright (C) 1994-1995 ATI Technologies Inc. All Rights Reserved.
  5.  *
  6.  *  File:       plane.c
  7.  *  Content:    plane manipulation functions
  8.  *
  9.  ***************************************************************************/
  10. #include "foxbear.h"
  11.  
  12. /*
  13.  * CreatePlane
  14.  */
  15. HPLANE *CreatePlane ( USHORT width, USHORT height, USHORT denom )
  16. {
  17.     HPLANE      *hPlane;
  18.     USHORT      num_elems;
  19.     USHORT      elem_size;
  20.  
  21.     num_elems = width * height;
  22.     elem_size = sizeof (GFX_HBM);
  23.  
  24.     hPlane = MemAlloc( sizeof (HPLANE) );
  25.     if( hPlane == NULL )
  26.     {
  27.     ErrorMessage( "hPlane in CreatePlane" );
  28.     }
  29.  
  30.     hPlane->hBM  = CMemAlloc( num_elems, elem_size );
  31.     hPlane->surface = CMemAlloc( num_elems, sizeof hPlane->surface );
  32.     hPlane->x = 0;
  33.     hPlane->y = 0;
  34.     hPlane->width = width;
  35.     hPlane->height = height;
  36.     hPlane->denom = denom;
  37.     hPlane->xslide = 0;
  38.     hPlane->xincrem = 0;
  39.     hPlane->xv = 0;
  40.  
  41.     if( hPlane->hBM == NULL )
  42.     {
  43.     MemFree( hPlane );
  44.     ErrorMessage( "hPlane->hBM in CreatePlane" );
  45.     }
  46.  
  47.     return hPlane;
  48.  
  49. } /* CreatePlane */
  50.  
  51.  
  52. /*
  53.  * TilePlane
  54.  */
  55. BOOL TilePlane( HPLANE *hPlane, HBITMAPLIST *hTileList, HPOSLIST *posList )
  56. {
  57.     USHORT i;
  58.  
  59.     for( i = 0; i < hPlane->width * hPlane->height; ++i )
  60.     {
  61.     hPlane->surface[i] = FALSE;
  62.  
  63.     if( posList[i] >= 0 )
  64.     {
  65.         hPlane->hBM[i] = hTileList[ posList[i] ].hBM;
  66.     }
  67.     else
  68.     {
  69.         hPlane->hBM[i] = NULL;
  70.     }
  71.     }
  72.  
  73.     return TRUE;
  74.  
  75. } /* TilePlane */
  76.  
  77. /*
  78.  * SurfacePlane
  79.  */
  80. BOOL SurfacePlane( HPLANE *hPlane, HSURFACELIST *hSurfaceList )
  81. {
  82.     USHORT i;
  83.  
  84.     for( i = 0; i < hPlane->width * hPlane->height; ++i )
  85.     {
  86.     if( hSurfaceList[i] == FALSE )
  87.     {
  88.         hPlane->surface[i] = FALSE;
  89.     }
  90.     else
  91.     {
  92.         hPlane->surface[i] = TRUE;
  93.     }
  94.     }
  95.  
  96.     return TRUE;
  97.  
  98. } /* SurfacePlane */
  99.  
  100. /*
  101.  * SetSurface
  102.  */
  103. BOOL SetSurface( HPLANE  *hPlane, HSPRITE *hSprite )
  104. {
  105.     SHORT c;
  106.     SHORT n;
  107.     SHORT x;
  108.     SHORT y;
  109.  
  110.     c = hSprite->currentBitmap;
  111.     x = (hSprite->x >> 16) / C_TILE_W;
  112.     y = (SHORT) hSprite->y >> 16;
  113.  
  114.     y += hSprite->hSBM[c].y + hSprite->hSBM[c].height;
  115.     y /= C_TILE_H;
  116.  
  117.     n = (x % hPlane->width) + y * hPlane->width;
  118.  
  119.     if( hPlane->surface[n] == FALSE )
  120.     {
  121.     if( !hPlane->surface[n + hPlane->width] == FALSE )
  122.     {
  123.         y += 1;
  124.     }
  125.     if( !hPlane->surface[n - hPlane->width] == FALSE )
  126.     {
  127.         y -= 1;
  128.     }
  129.     }
  130.     
  131.     y *= C_TILE_H;
  132.     y -= hSprite->hSBM[c].y + hSprite->hSBM[c].height;
  133.  
  134.     SetSpriteY( hSprite, y << 16, P_ABSOLUTE );
  135.  
  136.     return TRUE;
  137.  
  138. } /* SetSurface */
  139.  
  140. /*
  141.  * GetSurface
  142.  */
  143. BOOL GetSurface( HPLANE *hPlane, HSPRITE *hSprite )
  144. {   
  145.     SHORT  c;
  146.     SHORT  x;
  147.     SHORT  y;
  148.  
  149.     c = hSprite->currentBitmap;
  150.     x = ((hSprite->x >> 16) + hSprite->width / 2) / C_TILE_H;
  151.     y = ((hSprite->y >> 16) + hSprite->hSBM[c].y + hSprite->hSBM[c].height) / C_TILE_W;
  152.  
  153.     return hPlane->surface[(x % hPlane->width) + y * hPlane->width];
  154.  
  155. } /* GetSurface */
  156.  
  157. /*
  158.  * SetPlaneX
  159.  */
  160. BOOL SetPlaneX( HPLANE *hPlane, LONG x, POSITION position )
  161. {
  162.     LONG xincrem;    
  163.  
  164.     if( position == P_ABSOLUTE )
  165.     {
  166.     hPlane->x = x;
  167.     }
  168.     else if( position == P_RELATIVE )
  169.     {
  170.     hPlane->x += x;
  171.     }
  172.     else if( position == P_AUTOMATIC )
  173.     {
  174.     if( hPlane->xslide > 0 )
  175.     {
  176.         xincrem = hPlane->xincrem;
  177.     }
  178.     else if( hPlane->xslide < 0 )
  179.     {
  180.         xincrem = -hPlane->xincrem;
  181.     }
  182.     else
  183.     {
  184.         xincrem = 0;
  185.     }
  186.  
  187.     hPlane->x += (hPlane->xv + xincrem) / hPlane->denom;
  188.     hPlane->xslide -= xincrem;
  189.     hPlane->xv = 0;
  190.  
  191.     if( abs(hPlane->xslide) < hPlane->xincrem )
  192.     {
  193.         hPlane->x += hPlane->xslide / hPlane->denom;
  194.         hPlane->xslide = 0;
  195.         hPlane->xincrem = 0;
  196.     }
  197.     }
  198.  
  199.     if( hPlane->x < 0 )
  200.     {
  201.     hPlane->x += (hPlane->width * C_TILE_W) << 16;
  202.     }
  203.     else if( hPlane->x >= (hPlane->width * C_TILE_W) << 16 )
  204.     {
  205.     hPlane->x -= (hPlane->width * C_TILE_W) << 16;
  206.     }
  207.  
  208.     return TRUE;
  209.  
  210. } /* SetPlaneX */
  211.  
  212. /*
  213.  * GetPlaneX
  214.  */
  215. LONG GetPlaneX( HPLANE *hPlane )
  216. {
  217.     return hPlane->x;
  218.  
  219. } /* GetPlaneX */
  220.  
  221. /*
  222.  * SetPlaneY
  223.  */
  224. BOOL SetPlaneY( HPLANE *hPlane, LONG y, POSITION position )
  225. {
  226.     if( position == P_ABSOLUTE )
  227.     {
  228.     hPlane->y = y;
  229.     }
  230.     else
  231.     {
  232.     hPlane->y += y;
  233.     }
  234.  
  235.     if( hPlane->y < 0 )
  236.     {
  237.     hPlane->y += (hPlane->height * C_TILE_H) << 16;
  238.     }
  239.     else if( hPlane->y >= (hPlane->height * C_TILE_H) << 16 )
  240.     {
  241.     hPlane->y -= (hPlane->height * C_TILE_H) << 16;
  242.     }
  243.  
  244.     return TRUE;
  245.  
  246. } /* SetPlaneY */
  247.  
  248. /*
  249.  * GetPlaneY
  250.  */
  251. LONG GetPlaneY( HPLANE *hPlane )
  252. {
  253.     return hPlane->y;
  254.  
  255. } /* GetPlaneY */
  256.  
  257. /*
  258.  * SetPlaneSlideX
  259.  */
  260. BOOL SetPlaneSlideX( HPLANE *hPlane, LONG xslide, POSITION position )
  261. {
  262.     if( position == P_ABSOLUTE )
  263.     {
  264.     hPlane->xslide = xslide;
  265.     }
  266.     else if( position == P_RELATIVE )
  267.     {
  268.     hPlane->xslide += xslide;
  269.     }
  270.     return TRUE;
  271.  
  272. } /* SetPlaneSlideX */
  273.  
  274. /*
  275.  * SetPlaneVelX
  276.  */
  277. BOOL SetPlaneVelX( HPLANE *hPlane, LONG xv, POSITION position )
  278. {
  279.     if( position == P_ABSOLUTE )
  280.     {
  281.     hPlane->xv = xv;
  282.     }
  283.     else if( position == P_RELATIVE )
  284.     {
  285.     hPlane->xv += xv;
  286.     }
  287.  
  288.     return TRUE;
  289. } /* SetPlaneVelX */
  290.  
  291. /*
  292.  * SetPlaneIncremX
  293.  */
  294. BOOL SetPlaneIncremX( HPLANE *hPlane, LONG xincrem, POSITION position )
  295. {
  296.     if( position == P_ABSOLUTE )
  297.     {
  298.     hPlane->xincrem = xincrem;
  299.     }
  300.     else if( position == P_RELATIVE )
  301.     {
  302.     hPlane->xincrem += xincrem;
  303.     }
  304.  
  305.     return TRUE;
  306.  
  307. } /* SetPlaneIncremX */
  308.  
  309. /*
  310.  * ScrollPlane
  311.  */
  312. BOOL ScrollPlane( HSPRITE *hSprite )
  313. {
  314.     if( (GetSpriteX(hSprite) <= C_FOX_STARTX) && (GetSpriteVelX(hSprite) < 0) )
  315.     {
  316.     return TRUE;
  317.     }
  318.  
  319.     if( (GetSpriteX(hSprite) >= C_FOX_STARTX) && (GetSpriteVelX(hSprite) > 0) )
  320.     {
  321.     return TRUE;
  322.     }
  323.     return FALSE;
  324.  
  325. } /* ScrollPlane */
  326.  
  327.  
  328. /*
  329.  * DisplayPlane
  330.  */
  331. BOOL DisplayPlane ( GFX_HBM  hBuffer, HPLANE *hPlane )
  332. {
  333.     USHORT      n;
  334.     USHORT      i;
  335.     USHORT      j;
  336.     USHORT      x1;
  337.     USHORT      y1;
  338.     USHORT      x2;
  339.     USHORT      y2;
  340.     USHORT      xmod;
  341.     USHORT      ymod;
  342.     POINT       src;
  343.     RECT        dst;
  344.  
  345.  
  346.     x1 = (hPlane->x >> 16) / C_TILE_W;          
  347.     y1 = (hPlane->y >> 16) / C_TILE_H;
  348.     x2 = x1 + C_SCREEN_W / C_TILE_W;
  349.     y2 = y1 + C_SCREEN_H / C_TILE_H;
  350.     xmod = (hPlane->x >> 16) % C_TILE_W;
  351.     ymod = (hPlane->y >> 16) % C_TILE_H;
  352.  
  353.     for( j = y1; j < y2; ++j )
  354.     {
  355.     for( i = x1; i <= x2; ++i )
  356.     {
  357.         n = (i % hPlane->width) + j * hPlane->width;
  358.         if( hPlane->hBM[n] != NULL )
  359.         {
  360.         if( i == x1 )
  361.         {
  362.             dst.left  = 0;
  363.             dst.right = dst.left + C_TILE_W - xmod;
  364.             src.x     = xmod;
  365.         }
  366.         else if( i == x2 )
  367.         {
  368.             dst.left  = (i - x1) * C_TILE_W - xmod;
  369.             dst.right = dst.left + xmod;
  370.             src.x     = 0;
  371.         } else {
  372.             dst.left  = (i - x1) * C_TILE_W - xmod;
  373.             dst.right = dst.left + C_TILE_W;
  374.             src.x     = 0;
  375.         }
  376.     
  377.         if( j == y1 )
  378.         {
  379.             dst.top    = 0;
  380.             dst.bottom = dst.top + C_TILE_H - ymod;
  381.             src.y      = ymod;
  382.         }
  383.         else if( j == y2 )
  384.         {
  385.             dst.top    = (j - y1) * C_TILE_H - ymod;
  386.             dst.bottom = dst.top + ymod;
  387.             src.y      = 0;
  388.         } else {
  389.             dst.top    = (j - y1) * C_TILE_H - ymod;
  390.             dst.bottom = dst.top + C_TILE_H;
  391.             src.y      = 0;
  392.         }
  393.      
  394.         gfxBlt(&dst,hPlane->hBM[n],&src);
  395.         }
  396.     }
  397.     }
  398.  
  399.     return TRUE;
  400.  
  401. } /* DisplayPlane */
  402.  
  403. /*
  404.  * DestroyPlane
  405.  */
  406. BOOL DestroyPlane ( HPLANE *hPlane )
  407. {
  408.     if( hPlane == NULL )
  409.     {
  410.     ErrorMessage( "hPlane in DestroyPlane" );
  411.     }
  412.  
  413.     if( hPlane->hBM == NULL )
  414.     {
  415.     ErrorMessage( "hPlane->hBM in DestroyPlane" );
  416.     }
  417.  
  418.     MemFree( hPlane->hBM );
  419.     MemFree( hPlane );
  420.  
  421.     return TRUE;
  422.  
  423. } /* DestroyPlane */
  424.