home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GAPBILExample / CWorld.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-09  |  2.6 KB  |  121 lines

  1. //GAPBILExample
  2. //Copyright John Manslow
  3. //29/09/2001
  4.  
  5. ////////////////////////////////////////////////////
  6. //Remove this include if not compiling under Windows
  7. #include "stdafx.h"
  8. #define new DEBUG_NEW
  9. ////////////////////////////////////////////////////
  10.  
  11. #include "CWorld.h"
  12. #include "assert.h"
  13.  
  14. CWorld::CWorld(const unsigned long ulNewWorldSizeX,
  15.                             const unsigned long ulNewWorldSizeY)
  16. {
  17.     unsigned long i;
  18.     ulWorldSizeX=ulNewWorldSizeX;
  19.     ulWorldSizeY=ulNewWorldSizeY;
  20.     ppnWorld=new int*[ulWorldSizeX];
  21.     for(i=0;i<ulWorldSizeX;i++)
  22.     {
  23.         ppnWorld[i]=new int[ulWorldSizeY];
  24.     }
  25.     Initialise();
  26. }
  27.  
  28. CWorld::~CWorld()
  29. {
  30.     unsigned long i;
  31.     for(i=0;i<ulWorldSizeX;i++)
  32.     {
  33.         delete []ppnWorld[i];
  34.     }
  35.     delete []ppnWorld;
  36. }
  37.  
  38. void CWorld::Initialise(void)
  39. {
  40.     unsigned long i,j;
  41.     for(i=0;i<ulWorldSizeX;i++)
  42.     {
  43.         for(j=0;j<ulWorldSizeY;j++)
  44.         {
  45.             ppnWorld[i][j]=0;
  46.         }
  47.     }
  48.     for(i=0;i<ulWorldSizeX;i++)
  49.     {
  50.         ppnWorld[i][0]=2;
  51.         ppnWorld[i][ulWorldSizeY-1]=2;
  52.     }
  53.     for(i=0;i<ulWorldSizeY;i++)
  54.     {
  55.         ppnWorld[0][i]=2;
  56.         ppnWorld[ulWorldSizeX-1][i]=2;
  57.     }
  58.     unsigned long ulx,uly;
  59.     for(i=0;i<600;i++)
  60.     {
  61.         ulx=rand()%(ulWorldSizeX-2)+1;
  62.         uly=rand()%(ulWorldSizeY-2)+1;
  63.         ppnWorld[ulx][uly]=1;
  64.     }
  65.     for(i=0;i< 400;i++)
  66.     {
  67.         ulx=rand()%(ulWorldSizeX-2)+1;
  68.         uly=rand()%(ulWorldSizeY-2)+1;
  69.         ppnWorld[ulx][uly]=3;
  70.     }
  71.     ulCharacterLocationX=rand()%(ulWorldSizeX-2)+1;
  72.     ulCharacterLocationY=rand()%(ulWorldSizeY-2)+1;
  73. }
  74.  
  75. void CWorld::Draw(CDC *pDC)
  76. {
  77.     unsigned long i,j;
  78.     CBrush *pOldBrush=(CBrush*)pDC->SelectStockObject(BLACK_BRUSH);
  79.     CPen *pOldPen=(CPen*)pDC->SelectStockObject(NULL_PEN);
  80.     CBrush *pBrush;
  81.     double dScale=400.0/double(ulWorldSizeX);
  82.     for(i=0;i<ulWorldSizeX;i++)
  83.     {
  84.         for(j=0;j<ulWorldSizeY;j++)
  85.         {
  86.             pBrush=NULL;
  87.             if(ppnWorld[i][j]==0)
  88.             {
  89.                 pBrush=new CBrush(RGB(255,255,255));
  90.             }
  91.             if(ppnWorld[i][j]==2)
  92.             {
  93.                 pBrush=new CBrush(RGB(0,0,0));
  94.             }
  95.             if(ppnWorld[i][j]==1)
  96.             {
  97.                 pBrush=new CBrush(RGB(0,200,0));
  98.             }
  99.             if(ppnWorld[i][j]==3)
  100.             {
  101.                 pBrush=new CBrush(RGB(255,0,0));
  102.             }
  103.             assert(pBrush);
  104.             pDC->SelectObject(pBrush);
  105.             pDC->Rectangle(int(i*dScale),int(j*dScale),int((i+1)*dScale+1),int((j+1)*dScale+1));
  106.             pDC->SelectObject(pOldBrush);
  107.             delete pBrush;
  108.         }
  109.     }
  110.     pBrush=new CBrush(RGB(0,0,255));
  111.     pDC->SelectObject(pBrush);
  112.     pDC->Rectangle(
  113.                                     int(ulCharacterLocationX*dScale),
  114.                                     int(ulCharacterLocationY*dScale),
  115.                                     int((ulCharacterLocationX+1)*dScale+1),
  116.                                     int((ulCharacterLocationY+1)*dScale+1));
  117.     pDC->SelectObject(pOldBrush);
  118.     pDC->SelectObject(pOldPen);
  119.     delete pBrush;
  120. }
  121.