Browse Source

Fix memory leak on Map load

master
Riyyi 9 years ago
parent
commit
12dc5f9dc5
  1. 30
      src/map.cpp
  2. 6
      src/map.h
  3. 46
      src/mapload.cpp

30
src/map.cpp

@ -1,18 +1,20 @@
#include "map.h" #include "map.h"
//TileSet::~TileSet() { void TileSet::Clear() {
// // @Todo remove all tiles from std::vector<sf::Texture*> tile; while (this->tile.size() > 0) {
//} delete this->tile[this->tile.size() - 1];
this->tile.pop_back();
Map::Map() }
{ }
TileSet::~TileSet() {
this->Clear();
} }
Map::~Map() Map::Map()
{ {
delete this->tileSet; this->tileSet = new TileSet();
delete this->layer1; this->layer1 = new Layer();
} }
void Map::Render(sf::RenderWindow* window) { void Map::Render(sf::RenderWindow* window) {
@ -32,6 +34,12 @@ void Map::Render(sf::RenderWindow* window) {
} }
} }
Map::~Map()
{
delete this->tileSet;
delete this->layer1;
}
//void drawMap(sf::RenderWindow* window) //void drawMap(sf::RenderWindow* window)
//{ //{
// sf::Sprite tile; // sf::Sprite tile;
@ -42,13 +50,13 @@ void Map::Render(sf::RenderWindow* window) {
// int maxHeight = nearbyint((view.getCenter().y + view.getSize().y / 2) / map->tileSet->tileHeight) + 1; // int maxHeight = nearbyint((view.getCenter().y + view.getSize().y / 2) / map->tileSet->tileHeight) + 1;
// int minHeight = nearbyint((view.getCenter().y - view.getSize().y / 2) / map->tileSet->tileHeight) - 1; // int minHeight = nearbyint((view.getCenter().y - view.getSize().y / 2) / map->tileSet->tileHeight) - 1;
// for (int y = minHeight; y < maxHeight; y++) // for (int y = minHeight; y < maxHeight && y < map->height; y++)
// { // {
// if (y < 0) // if (y < 0)
// { // {
// y = 0; // y = 0;
// } // }
// for (int x = minWidth; x < maxWidth; x++) // for (int x = minWidth; x < maxWidth && x < map->width; x++)
// { // {
// if (x < 0) // if (x < 0)
// { // {

6
src/map.h

@ -26,7 +26,8 @@ class TileSet
{ {
public: public:
//TileSet(); //TileSet();
//~TileSet(); void Clear();
~TileSet();
// "firstgid":1, // "firstgid":1,
std::string image; std::string image;
@ -51,8 +52,9 @@ class Map
{ {
public: public:
Map(); Map();
~Map();
void Render(sf::RenderWindow* window); void Render(sf::RenderWindow* window);
~Map();
// Global // Global
int height; int height;

46
src/mapload.cpp

@ -37,22 +37,22 @@ void MapLoad::Parser(std::string* content, Map* map) {
// Process map // Process map
// TILESET // TILESET
TileSet* tileSet = new TileSet(); map->tileSet->Clear();
tileSet->image = document["tilesets"][0]["image"].GetString(); map->tileSet->image = document["tilesets"][0]["image"].GetString();
tileSet->imageHeight = document["tilesets"][0]["imageheight"].GetInt(); map->tileSet->imageHeight = document["tilesets"][0]["imageheight"].GetInt();
tileSet->imageWidth = document["tilesets"][0]["imagewidth"].GetInt(); map->tileSet->imageWidth = document["tilesets"][0]["imagewidth"].GetInt();
tileSet->tileHeight = document["tilesets"][0]["tileheight"].GetInt(); map->tileSet->tileHeight = document["tilesets"][0]["tileheight"].GetInt();
tileSet->tileWidth = document["tilesets"][0]["tilewidth"].GetInt(); map->tileSet->tileWidth = document["tilesets"][0]["tilewidth"].GetInt();
int height = tileSet->imageHeight / tileSet->tileHeight; int height = map->tileSet->imageHeight / map->tileSet->tileHeight;
int width = tileSet->imageWidth / tileSet->tileWidth; int width = map->tileSet->imageWidth / map->tileSet->tileWidth;
// Add NULL texture to fill position '0' // Add NULL texture to fill position '0'
sf::Texture* texture = new sf::Texture(); sf::Texture* texture = new sf::Texture();
tileSet->tile.push_back(texture); map->tileSet->tile.push_back(texture);
// Load map tilesheet // Load map tilesheet
std::string tileSheetLocation = tileSet->image; std::string tileSheetLocation = map->tileSet->image;
tileSheetLocation.replace(0, 2, "data"); tileSheetLocation.replace(0, 2, "data");
sf::Image tileSheet; sf::Image tileSheet;
tileSheet.loadFromFile(tileSheetLocation); tileSheet.loadFromFile(tileSheetLocation);
@ -60,17 +60,19 @@ void MapLoad::Parser(std::string* content, Map* map) {
for(int y = 0; y < height; y++) { for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) { for(int x = 0; x < width; x++) {
sf::Texture* texture = new sf::Texture(); sf::Texture* texture = new sf::Texture();
texture->loadFromImage(tileSheet, sf::IntRect(x * tileSet->tileWidth, y * tileSet->tileHeight, tileSet->tileWidth, tileSet->tileHeight)); texture->loadFromImage(tileSheet, sf::IntRect(x * map->tileSet->tileWidth,
tileSet->tile.push_back(texture); y * map->tileSet->tileHeight,
map->tileSet->tileWidth,
map->tileSet->tileHeight));
map->tileSet->tile.push_back(texture);
} }
} }
// LAYER 1 // LAYER 1
map->layer1->data.clear();
height = document["layers"][0]["height"].GetInt(); height = document["layers"][0]["height"].GetInt();
width = document["layers"][0]["width"].GetInt(); width = document["layers"][0]["width"].GetInt();
Layer* layer = new Layer();
const rapidjson::Value& mapArray = document["layers"][0]["data"]; const rapidjson::Value& mapArray = document["layers"][0]["data"];
assert(mapArray.IsArray()); assert(mapArray.IsArray());
@ -83,20 +85,20 @@ void MapLoad::Parser(std::string* content, Map* map) {
itr++; itr++;
} }
layer->data.push_back(row); map->layer1->data.push_back(row);
} }
layer->height = height; map->layer1->height = height;
layer->visible = document["layers"][0]["visible"].GetBool(); map->layer1->visible = document["layers"][0]["visible"].GetBool();
layer->width = width; map->layer1->width = width;
layer->x = document["layers"][0]["x"].GetInt(); map->layer1->x = document["layers"][0]["x"].GetInt();
layer->y = document["layers"][0]["y"].GetInt(); map->layer1->y = document["layers"][0]["y"].GetInt();
// MAP // MAP
map->height = document["layers"][0]["height"].GetInt(); map->height = document["layers"][0]["height"].GetInt();
map->layer1 = layer; // Layer
map->tileheight = document["tileheight"].GetInt(); map->tileheight = document["tileheight"].GetInt();
map->tileSet = tileSet; // TileSet
map->tilewidth = document["tilewidth"].GetInt(); map->tilewidth = document["tilewidth"].GetInt();
map->width = document["layers"][0]["width"].GetInt(); map->width = document["layers"][0]["width"].GetInt();

Loading…
Cancel
Save