/src/TileSet.cpp

http://github.com/clintbellanger/flare · C++ · 106 lines · 65 code · 18 blank · 23 comment · 18 complexity · 8d6dc2ab675cbd6f54b2da84fc6481c3 MD5 · raw file

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