Guest User

Tiled Map Loader sfml 2

a guest
Sep 9th, 2012
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /*********************************************************************
  2. Quinn Schwab
  3. 16/08/2010
  5. SFML Tiled Map Loader
  7. The zlib license has been used to make this software fully compatible
  8. with SFML. See
  10. This software is provided 'as-is', without any express or
  11. implied warranty. In no event will the authors be held
  12. liable for any damages arising from the use of this software.
  14. Permission is granted to anyone to use this software for any purpose,
  15. including commercial applications, and to alter it and redistribute
  16. it freely, subject to the following restrictions:
  18. 1. The origin of this software must not be misrepresented;
  19. you must not claim that you wrote the original software.
  20. If you use this software in a product, an acknowledgment
  21. in the product documentation would be appreciated but
  22. is not required.
  24. 2. Altered source versions must be plainly marked as such,
  25. and must not be misrepresented as being the original software.
  27. 3. This notice may not be removed or altered from any
  28. source distribution.
  29. *********************************************************************/
  31. #ifndef LEVEL_H
  32. #define LEVEL_H
  34. #include <string>
  35. #include <vector>
  36. #include <map>
  37. #include <SFML/System.hpp>
  38. #include <SFML/Graphics.hpp>
  40. //Object class. Everything is kept public in case you want to get a different type to the supported ones.
  41. //Also you will be able to modify things. For example doors/changing walls etc (Just give the door a static type and a unique name)
  42. //Kept in same files as level just for convenience. (only have to add two files to your project)
  43. class Object{
  44. public:
  45. int GetPropertyInt(std::string name);
  46. float GetPropertyFloat(std::string name);
  47. std::string GetPropertyString(std::string name);
  48. std::string name;
  49. std::string type;
  50. sf::Rect <int> rect;
  51. std::map <std::string, std::string> properties;//All properties of the object. Values are stored as strings and mapped by strings(names provided in editor).
  52. };
  54. struct Layer{
  55. int opacity;
  56. std::vector <sf::Sprite> tiles;
  57. };
  59. class Level
  60. {
  61. public:
  62. Level();
  63. virtual ~Level();
  64. //Loads the map. Returns false if it fails.
  65. bool LoadFromFile(std::string filename);
  66. //Returns the first object found with the given name. This is why we need unique names :)
  67. Object GetObject(std::string name);
  68. //Returns true if the given pixel is solid.
  69. bool IsSolidPixel(int x, int y);
  70. //Returns true if the given tile is solid. DO WE NEED THIS?
  71. bool IsSolidTile(int x, int y);
  72. //Moves the map. Although I would recommend using a view instead in most cases.
  73. void Move(int xStep, int yStep);
  74. //Set the area to draw. This rect is usually provided directly from the view you are using.
  75. void SetDrawingBounds(sf::Rect<float> bounds);
  76. //Draws the map to the provided window.
  77. void Draw(sf::RenderWindow &window);
  79. private:
  80. //Width(tiles), height(tiles), tile width(pixels), tile height(pixels) of the map.
  81. int width, height, tileWidth, tileHeight, spacing, margin;
  82. //Used to offset the tile number.
  83. int firstTileID;
  84. //Used to clip anything off screen.
  85. sf::Rect <float> drawingBounds;
  86. //The tileset image.
  87. sf::Image tilesetImage;
  88. //The tileset texture.
  89. sf::Texture tilesetTexture;
  90. //This stores all the solid areas (objects with type 'solid'). This gets checked by the IsSolid function.
  91. std::vector <sf::Rect <int> > solidObjects;
  92. //This stores all objects (including 'solid' types)
  93. std::vector <Object> objects;
  94. //This stores each layer of sprites/tiles so they can be drawn in order.
  95. std::vector <Layer> layers;
  96. };
  98. #endif // LEVEL_H
Add Comment
Please, Sign In to add comment