00001 /* 00002 Copyright (c) 2009, Dan Hagerstrand and contributors 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, 00009 this list of conditions and the following disclaimer. 00010 00011 * Redistributions in binary form must reproduce the above copyright notice, 00012 this list of conditions and the following disclaimer in the documentation 00013 and/or other materials provided with the distribution. 00014 00015 * Neither the name of Cirrus Creative nor the names of its 00016 contributors may be used to endorse or promote products derived from this 00017 software without specific prior written permission. 00018 00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00022 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00023 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00024 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00025 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 */ 00031 00032 #ifndef BENG_ARRAY2D_H 00033 #define BENG_ARRAY2D_H 00034 00035 #include <cstdlib> 00036 00050 00051 template <typename T> 00052 class BengArray2D { 00053 00054 protected: 00055 00057 unsigned int sizeX; 00059 unsigned int sizeY; 00061 T *tileMap; 00063 T **tileRows; 00064 00065 public: 00066 00069 BengArray2D(unsigned int xsize, unsigned int ysize) 00070 { 00071 this->sizeX = xsize; 00072 this->sizeY = ysize; 00073 this->tileMap = NULL; 00074 this->tileRows = NULL; 00075 00076 // Allocate space for the actual array 00077 this->tileMap = (T*)calloc(ysize * xsize, sizeof(T)); 00078 if (this->tileMap == NULL) 00079 abort(); 00080 //return; 00081 //this->scene_->system_->giveError(BENG_ERROR_FATAL, 00082 //"Could not allocate tilemap for the scene layer!\nYou may be out of memory!"); 00083 00084 // Allocate row pointers to transform the contiguous space into a 2D array 00085 this->tileRows = (T**)malloc(xsize * sizeof(T*)); 00086 if (this->tileRows == NULL) 00087 abort(); 00088 //return; 00089 //this->scene_->system_->giveError(BENG_ERROR_FATAL, 00090 //"Could not allocate tile rows for the scene layer!\nYou may be out of memory!"); 00091 00092 // Adjust the pointers to go to each row. 00093 for (unsigned int i = 0; i < xsize; i++) 00094 { 00095 this->tileRows[i] = this->tileMap + (i * ysize); 00096 } 00097 } 00100 ~BengArray2D(void) 00101 { 00102 if (this->tileRows) 00103 free(this->tileRows); 00104 if (this->tileMap) 00105 free(this->tileMap); 00106 } 00107 00109 void getSize(unsigned int &xOut, unsigned int &yOut) 00110 { 00111 xOut = this->sizeX; 00112 yOut = this->sizeY; 00113 } 00114 00117 void setElement(unsigned int xIn, unsigned int yIn, T data) 00118 { 00119 if (xIn >= 0 && xIn < this->sizeX && yIn >= 0 && yIn < this->sizeY) 00120 this->tileRows_[yIn][xIn] = data; 00121 } 00122 00125 T getElement(unsigned int xIn, unsigned int yIn) 00126 { 00127 if (xIn >= 0 && xIn < this->sizeX && yIn >= 0 && yIn < this->sizeY) 00128 return this->tileRows_[yIn][xIn]; 00129 else 00130 return 0; 00131 } 00132 00137 T **getRawData(void) 00138 { 00139 return this->tileRows; 00140 } 00141 00142 00143 }; 00144 00145 #endif
1.5.8