/src/TileSet.cpp
C++ | 106 lines | 65 code | 18 blank | 23 comment | 18 complexity | 8d6dc2ab675cbd6f54b2da84fc6481c3 MD5 | raw file
Possible License(s): GPL-3.0
1/* 2Copyright 2011 Clint Bellanger 3 4This file is part of FLARE. 5 6FLARE is free software: you can redistribute it and/or modify it under the terms 7of the GNU General Public License as published by the Free Software Foundation, 8either version 3 of the License, or (at your option) any later version. 9 10FLARE is distributed in the hope that it will be useful, but WITHOUT ANY 11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 14You should have received a copy of the GNU General Public License along with 15FLARE. If not, see http://www.gnu.org/licenses/ 16*/ 17 18/** 19 * class TileSet 20 * 21 * TileSet storage and file loading 22 */ 23 24#include "TileSet.h" 25#include "UtilsParsing.h" 26#include "SharedResources.h" 27#include "FileParser.h" 28 29#include <cstdio> 30 31using namespace std; 32 33TileSet::TileSet() { 34 alpha_background = false; 35 sprites = NULL; 36 for (int i=0; i<256; i++) { 37 tiles[i].src.x = 0; 38 tiles[i].src.y = 0; 39 tiles[i].src.w = 0; 40 tiles[i].src.h = 0; 41 tiles[i].offset.x = 0; 42 tiles[i].offset.y = 0; 43 } 44} 45 46void TileSet::loadGraphics(const std::string& filename) { 47 if (sprites) SDL_FreeSurface(sprites); 48 49 sprites = IMG_Load((mods->locate("images/tilesets/" + filename)).c_str()); 50 if (!sprites) { 51 fprintf(stderr, "Couldn't load image: %s\n", IMG_GetError()); 52 SDL_Quit(); 53 } 54 55 // only set a color key if the tile set doesn't have an alpha channel 56 if (!alpha_background) { 57 SDL_SetColorKey( sprites, SDL_SRCCOLORKEY, SDL_MapRGB(sprites->format, 255, 0, 255) ); 58 } 59 60 // optimize 61 SDL_Surface *cleanup = sprites; 62 sprites = SDL_DisplayFormatAlpha(sprites); 63 SDL_FreeSurface(cleanup); 64} 65 66void TileSet::load(const std::string& filename) { 67 if (current_map == filename) return; 68 69 alpha_background = false; 70 71 FileParser infile; 72 unsigned short index; 73 string img; 74 75 if (infile.open(mods->locate("tilesetdefs/" + filename))) { 76 while (infile.next()) { 77 if (infile.key == "tile") { 78 79 infile.val = infile.val + ','; 80 index = eatFirstInt(infile.val, ','); 81 tiles[index].src.x = eatFirstInt(infile.val, ','); 82 tiles[index].src.y = eatFirstInt(infile.val, ','); 83 tiles[index].src.w = eatFirstInt(infile.val, ','); 84 tiles[index].src.h = eatFirstInt(infile.val, ','); 85 tiles[index].offset.x = eatFirstInt(infile.val, ','); 86 tiles[index].offset.y = eatFirstInt(infile.val, ','); 87 88 } 89 else if (infile.key == "img") { 90 img = infile.val; 91 } 92 else if (infile.key == "alpha_background") { 93 if (infile.val == "1") alpha_background = true; 94 } 95 96 } 97 infile.close(); 98 loadGraphics(img); 99 } 100 101 current_map = filename; 102} 103 104TileSet::~TileSet() { 105 SDL_FreeSurface(sprites); 106}