Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DynamicTileArray.h"
- using namespace std;
- DynamicTileArray::DynamicTileArray() : width(10), height(10) {
- cout << "DynamicTileArray::init" << endl;
- Tile** myArray = (Tile**)malloc(sizeof(Tile**)*10);
- //allocate the second dimension of the array
- for(int i = 0; i < 10; i++) {
- myArray[i] = (Tile*)malloc(sizeof(Tile*)*10);
- }
- tiles = myArray;
- cout << "DynamicTileArray::init tiles = " << tiles << endl;
- }
- DynamicTileArray::DynamicTileArray(int width, int height) : width(width), height(height) {
- Tile** myArray = (Tile**)malloc(sizeof(Tile**)*this->width);
- //allocate the second dimension of the array
- for(int i = 0; i < this->width; i++) {
- myArray[i] = (Tile*)malloc(sizeof(Tile*)*this->height);
- }
- tiles = myArray;
- }
- DynamicTileArray::~DynamicTileArray() {
- for(int i = 0; i < width; i++) {
- free(tiles[i]);
- }
- free(tiles);
- }
- void DynamicTileArray::allocateSpace(int nwidth, int nheight) {
- //allocate the first dimension of the array
- Tile** myArray = (Tile**)malloc(sizeof(Tile**)*nwidth);
- cout << "DynamicTileArray::allocateSpace myArray = " << myArray << endl;
- cout << "DynamicTileArray::allocateSpace tiles = " << tiles << endl;
- cout << "DynamicTileArray::allocateSpace tiles size: " << width << ", " << height << endl;
- //allocate the second dimension of the array
- for(int i = 0; i < nwidth; i++) {
- myArray[i] = (Tile*)malloc(sizeof(Tile*)*nheight);
- }
- cout << "DynamicTileArray::allocateSpace copying existing tiles" << endl;
- //copy over existing tiles from the previous array
- for(int i = 0; i < width; i++) {
- for(int j = 0; j < height; j++) {
- cout << "DynamicTileArray::allocateSpace copying: " << i << ", " << j << endl;
- //it dies here (t1 and t2 are just here to see what is broken)
- Tile t1 = myArray[i][j];
- cout << "DynamicTileArray::allocateSpace test2" << endl;
- Tile t2 = tiles[i][j];
- myArray[i][j] = tiles[i][j];
- }
- }
- cout << "DynamicTileArray::allocateSpace freeing previous array" << endl;
- //free the second dimension of the previous array
- for(int i = 0; i < width; i++) {
- free(tiles[i]);
- }
- free(tiles);
- tiles = myArray;
- width = nwidth;
- height = nheight;
- }
- Tile DynamicTileArray::getTile(int x, int y) {
- if(x > width || y > height || x < 0 || y < 0) return Tile();
- return tiles[x][y];
- }
- void DynamicTileArray::setTile(Tile tile, int x, int y) {
- cout << "DynamicTileArray::setTile (" << x << ", " << y << ")" << endl;
- if(x >= width || y >= height) {
- int diffX = x-(width-1);
- int diffY = y-(height-1);
- int nWidth = diffX+width;
- int nHeight = diffY+height;
- cout << "DynamicTileArray::setTile, too big, resizing to: " << nWidth << ", " << nHeight << endl;
- allocateSpace(nWidth, nHeight);
- }
- if(x < 0 || y < 0) {
- return; //do nothing
- }
- tiles[x][y] = tile;
- }
- int DynamicTileArray::getWidth() {
- return width;
- }
- int DynamicTileArray::getHeight() {
- return height;
- }
Advertisement
Add Comment
Please, Sign In to add comment