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"
//TileSet::~TileSet() {
// // @Todo remove all tiles from std::vector<sf::Texture*> tile;
//}
Map::Map()
{
void TileSet::Clear() {
while (this->tile.size() > 0) {
delete this->tile[this->tile.size() - 1];
this->tile.pop_back();
}
}
TileSet::~TileSet() {
this->Clear();
}
Map::~Map()
Map::Map()
{
delete this->tileSet;
delete this->layer1;
this->tileSet = new TileSet();
this->layer1 = new Layer();
}
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)
//{
// 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 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)
// {
// y = 0;
// }
// for (int x = minWidth; x < maxWidth; x++)
// for (int x = minWidth; x < maxWidth && x < map->width; x++)
// {
// if (x < 0)
// {

6
src/map.h

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

46
src/mapload.cpp

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

Loading…
Cancel
Save