5 * Copyright (C) 2010-2011 Scott Smith
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 void CBase::Log( const char* output, ... )
38 va_start( fmtargs, output );
39 vsnprintf( buffer, sizeof(buffer)-1, output, fmtargs );
42 fprintf( stdout, "%s", buffer );
45 FILE* fout = fopen( "log.txt", "a" );
48 printf( "Failed to open logfile\n" );
56 uint32_t CBase::CheckRange( int32_t value, int32_t size )
58 if (value >= 0 && value < size)
68 uint32_t CBase::rgb_to_int( SDL_Color color )
70 return SDL_MapRGB(SDL_GetVideoSurface()->format, color.r, color.g, color.b);
73 int32_t CBase::a_to_i( const string& line )
77 stringstream ss( line.c_str() );
78 ss >> number; // Convert string to integer
82 string CBase::i_to_a( const int16_t num )
88 ss >> str; // Convert string to integer
92 string CBase::lowercase( string text )
94 transform( text.begin(), text.end(), text.begin(), (int (*)(int))tolower );
98 SDL_Surface* CBase::LoadImage( const string& filename )
100 SDL_Surface* loaded_image = NULL; // The mpImage that's loaded
101 SDL_Surface* optimized_image = NULL; // The optimized surface that will be used
103 loaded_image = IMG_Load( filename.c_str() ); //Load the mpImage
105 // If the mpImage loaded
106 if (NULL != loaded_image)
108 optimized_image = SDL_DisplayFormatAlpha( loaded_image ); // Create an optimized surface
109 SDL_FreeSurface(loaded_image); // Free the old surface
112 // If the surface was optimized
113 if (NULL != optimized_image)
116 SDL_SetColorKey( optimized_image, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimized_image->format, 0xFF, 0, 0xFF ) );
121 //Log( "LoadImage -> Could not load image: %s at path='%s'\n", IMG_GetError(), filename.c_str() );
125 //Return the optimized surface
126 return optimized_image;
129 void CBase::ApplyImage( int16_t x, int16_t y, SDL_Surface* src, SDL_Surface* dst, SDL_Rect* clip )
139 SDL_BlitSurface( src, clip, dst, &offset );
142 bool CBase::CheckRectCollision( SDL_Rect* boxA, SDL_Rect* boxB )
144 // The sides of the SDL_Rects
145 int16_t leftA, leftB;
146 int16_t rightA, rightB;
148 int16_t bottomA, bottomB;
150 // Calculate the sides of rec mCollisionbox
152 rightA = boxB->x + boxB->w;
154 bottomA = boxB->y + boxB->h;
156 // Calculate the sides of rec box
158 rightB = boxA->x + boxA->w;
160 bottomB = boxA->y + boxA->h;
162 // If any of the sides from mCollisionbox are outside of box
163 if (bottomA <= topB) return false;
164 if (topA >= bottomB) return false;
165 if (rightA <= leftB) return false;
166 if (leftA >= rightB) return false;
167 // If none of the sides from mCollisionbox are outside box
168 return true; // Collision has occured
171 void CBase::SplitString( const std::string& delimiter, const std::string& text, vector<string>& array )
173 string::size_type pos1, pos2;
178 pos1 = text.find( delimiter, 0 );
179 if (pos1 == string::npos)
181 pos1 = text.length();
183 value = text.substr( 0, pos1 );
184 array.push_back(value);
189 pos1 = text.find( delimiter, pos2 );
191 if (pos1 != string::npos)
193 pos2 = text.find( delimiter, pos1+1 );
194 if (pos2 == string::npos)
196 pos2 = text.length();
199 value = text.substr( pos1+1, pos2-pos1-1 );
200 array.push_back(value);
202 } while (pos1 != string::npos);
205 bool CBase::UnprefixString( string& result, const string& line, const string& prefix )
207 string::size_type pos;
209 pos = line.find(prefix, 0);
213 pos = line.find("=", 0) + 1;
214 result = line.substr(pos, line.length()-pos);
215 // Remove any comments
216 pos = result.find("#", 0);
217 result = result.substr(0, pos);
218 // Trim left and right white spaces
219 result.erase( result.begin(), std::find_if(result.begin(), result.end(), std::not1(std::ptr_fun<int, int>(std::isspace))) );
220 result.erase( std::find_if(result.rbegin(), result.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), result.end() );
227 int16_t CBase::CheckExtension( const string& filename, const string& ext )
230 string::size_type pos;
232 pos = filename.length() - ext.length();
233 result = lowercase(filename).find( lowercase(ext), pos);
238 void CBase::CheckPath( string& path )
242 if (path.at(path.length()-1) != '/')
249 Log( "Warning: CheckPath path too short\n" );
253 string CBase::cmdclean( string& cmdline )
255 string spchars = "\\`$();|{}&'\"*?<>[]!^~-#\n\r ";
256 for (uint i=0; i<spchars.length(); i++)
258 string curchar = spchars.substr(i,1);
259 cmdline = strreplace(cmdline, curchar, "\\"+curchar);
264 string CBase::strreplace( string& orig, const string& search, const string& replace )
266 string::size_type pos = orig.find( search, 0 );
267 while (pos != string::npos)
269 orig.replace(pos,search.length(),replace);
270 pos = orig.find( search, pos+replace.length() );
275 SDL_Surface* CBase::ScaleSurface( SDL_Surface *surface, uint16_t width, uint16_t height )
277 if(!surface || !width || !height)
280 SDL_Surface *_ret = SDL_CreateRGBSurface(surface->flags, width, height, surface->format->BitsPerPixel,
281 surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
283 double _stretch_factor_x = ( static_cast<double>(width) / static_cast<double>(surface->w) ),
284 _stretch_factor_y = ( static_cast<double>(height) / static_cast<double>(surface->h) );
286 for (int32_t y = 0; y < surface->h; y++)
287 for (int32_t x = 0; x < surface->w; x++)
288 for (int32_t o_y = 0; o_y < _stretch_factor_y; ++o_y)
289 for (int32_t o_x = 0; o_x < _stretch_factor_x; ++o_x)
290 putpixel( _ret, static_cast<int32_t>(_stretch_factor_x * x) + o_x,
291 static_cast<int32_t>(_stretch_factor_y * y) + o_y, getpixel(surface, x, y) );
296 uint32_t CBase::getpixel( SDL_Surface *surface, int16_t x, int16_t y )
298 int16_t bpp = surface->format->BytesPerPixel;
299 /* Here p is the address to the pixel we want to retrieve */
300 uint8_t *p = (uint8_t *)surface->pixels + y * surface->pitch + x * bpp;
308 return *(uint16_t *)p;
312 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
313 return p[0] << 16 | p[1] << 8 | p[2];
315 return p[0] | p[1] << 8 | p[2] << 16;
319 return *(uint32_t *)p;
323 return 0; /* shouldn't happen, but avoids warnings */
327 void CBase::putpixel( SDL_Surface *surface, int16_t x, int16_t y, uint32_t pixel )
329 int16_t bpp = surface->format->BytesPerPixel;
330 /* Here p is the address to the pixel we want to set */
331 uint8_t *p = (uint8_t *)surface->pixels + y * surface->pitch + x * bpp;
339 *(uint16_t *)p = pixel;
343 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
344 p[0] = (pixel >> 16) & 0xff;
345 p[1] = (pixel >> 8) & 0xff;
349 p[1] = (pixel >> 8) & 0xff;
350 p[2] = (pixel >> 16) & 0xff;
356 *(uint32_t *)p = pixel;