ajtgarber

DynamicTileArray.cpp

Jun 8th, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include "DynamicTileArray.h"
  2.  
  3. using namespace std;
  4.  
  5. DynamicTileArray::DynamicTileArray() : width(10), height(10) {
  6.     cout << "DynamicTileArray::init" << endl;
  7.     Tile** myArray = (Tile**)malloc(sizeof(Tile**)*10);
  8.     //allocate the second dimension of the array
  9.     for(int i = 0; i < 10; i++) {
  10.         myArray[i] = (Tile*)malloc(sizeof(Tile*)*10);
  11.     }
  12.     tiles = myArray;
  13.     cout << "DynamicTileArray::init tiles = " << tiles << endl;
  14. }
  15. DynamicTileArray::DynamicTileArray(int width, int height) : width(width), height(height) {
  16.     Tile** myArray = (Tile**)malloc(sizeof(Tile**)*this->width);
  17.     //allocate the second dimension of the array
  18.     for(int i = 0; i < this->width; i++) {
  19.         myArray[i] = (Tile*)malloc(sizeof(Tile*)*this->height);
  20.     }
  21.     tiles = myArray;
  22. }
  23. DynamicTileArray::~DynamicTileArray() {
  24.     for(int i = 0; i < width; i++) {
  25.         free(tiles[i]);
  26.     }
  27.     free(tiles);
  28. }
  29.  
  30. void DynamicTileArray::allocateSpace(int nwidth, int nheight) {
  31.     //allocate the first dimension of the array
  32.     Tile** myArray = (Tile**)malloc(sizeof(Tile**)*nwidth);
  33.     cout << "DynamicTileArray::allocateSpace myArray = " << myArray << endl;
  34.     cout << "DynamicTileArray::allocateSpace tiles = " << tiles << endl;
  35.     cout << "DynamicTileArray::allocateSpace tiles size: " << width << ", " << height << endl;
  36.     //allocate the second dimension of the array
  37.     for(int i = 0; i < nwidth; i++) {
  38.         myArray[i] = (Tile*)malloc(sizeof(Tile*)*nheight);
  39.     }  
  40.     cout << "DynamicTileArray::allocateSpace copying existing tiles" << endl;
  41.     //copy over existing tiles from the previous array
  42.     for(int i = 0; i < width; i++) {
  43.         for(int j = 0; j < height; j++) {
  44.             cout << "DynamicTileArray::allocateSpace copying: " << i << ", " << j << endl;
  45.             //it dies here (t1 and t2 are just here to see what is broken) 
  46.             Tile t1 = myArray[i][j];
  47.             cout << "DynamicTileArray::allocateSpace test2" << endl;
  48.             Tile t2 = tiles[i][j];
  49.             myArray[i][j] = tiles[i][j];
  50.         }
  51.     }
  52.    
  53.     cout << "DynamicTileArray::allocateSpace freeing previous array" << endl;
  54.     //free the second dimension of the previous array
  55.     for(int i = 0; i < width; i++) {
  56.         free(tiles[i]);
  57.     }
  58.     free(tiles);
  59.     tiles = myArray;
  60.     width = nwidth;
  61.     height = nheight;
  62. }
  63.  
  64. Tile DynamicTileArray::getTile(int x, int y) {
  65.     if(x > width || y > height || x < 0 || y < 0) return Tile();
  66.    
  67.     return tiles[x][y];
  68. }
  69. void DynamicTileArray::setTile(Tile tile, int x, int y) {
  70.     cout << "DynamicTileArray::setTile (" << x << ", " << y << ")" << endl;
  71.     if(x >= width || y >= height) {
  72.         int diffX = x-(width-1);
  73.         int diffY = y-(height-1);
  74.         int nWidth = diffX+width;
  75.         int nHeight = diffY+height;
  76.         cout << "DynamicTileArray::setTile, too big, resizing to: " << nWidth << ", " << nHeight << endl;
  77.         allocateSpace(nWidth, nHeight);
  78.     }
  79.     if(x < 0 || y < 0) {
  80.         return; //do nothing
  81.     }
  82.  
  83.     tiles[x][y] = tile;
  84. }
  85.  
  86. int DynamicTileArray::getWidth() {
  87.     return width;
  88. }
  89. int DynamicTileArray::getHeight() {
  90.     return height;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment