My project has gotten kind of big now but so I'm trying to post as little code as I can. It seems like the center of my problem is that I have a map for a game, at the start of this game the map is created and is constantly drawn on the screen. The map(class Map) is made up of many regions(class Region) and when the map is told to draw itself the map turns around and tells all the regions that would appear on the screen to draw themselves, and all the rest to free up their memory for storing the tiles that represent them. The problem I am having is that it seems like once the constructor for Map finishes all the Regions that were created expire. The 2d vector I made to contain them can be traversed and the draw function for each Region can be called but most of the time I get a Segmentation Fault on the first line in the Region draw function.
cout << x << ", " << y << '\n'; //x and y are the coordinates of the region on the map.
Sometimes I don't get a Segmentation Fault and I get output like this, note, I'm trying to make a 100 x 100 game grid.
6391408, 0
As I said I think this is a problem of the Regions simply ceasing to exist after the constructor completes but I don't know how to fix this problem. I've tried several versions of the code below but the problem never stopped.
This is the header Map.h that has the prototypes for the Map and Region classes
#ifndef MAP_H
#define MAP_H
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL.h"
#include "Standard.h"
#include "Graphics.h"
#include "SDL/SDL_rotozoom.h"
#include <vector>
#include <iostream>
#include <sstream>
#define TILE_HEIGHT 100
#define TILE_WIDTH 200
class Region;
class Map
{
private:
vector< vector<Region> > regions;
Graphics* graphics;
SDL_Surface* screen;
double scale;
int screenX, screenY, space, w, h;
bool created;
public:
Map();
Map(int w, int h, SDL_Surface* screen, int space, Graphics* graphics);
void draw();
bool isCreated();
};
class Region
{
private:
SDL_Surface* screen;
SDL_Surface* tile;
SDL_Surface* tempTile;
Graphics* graphics;
int x, y;
bool created;
public:
void draw(int x, int y, double scale, bool onMonitor);
bool isCreated();
Region();
Region(SDL_Surface* screen, Graphics* graphics, Map* world, int x, int y);
};
#endif
This is the constructor for the Map class
Map::Map(int w, int h, SDL_Surface* screen, int space, Graphics* graphics)
{
this->screen = screen;
this->graphics = graphics;
this->space = space;
screenX = 0;
screenY = 0;
this->w = w;
this->h = h;
this->scale = 0.5;
int x, y;
for(x = 0; x < w;x++)
{
regions.push_back(vector<Region>());
for(y = 0; y < h;y++)
{
regions[x].push_back(Region(screen, graphics, this, x, y));
}
}
created = true;
}
Constructor for the Region class
Region::Region(SDL_Surface* screen, Graphics* graphics, Map* world, int x, int y)
{
this->x = x;
this->y = y;
this->screen = screen;
this->graphics = graphics;
tile = load_image("tile.png");
tempTile = NULL;
if(tile == NULL)
{
printf("Region surface failed!\n");
created = false;
}
cout << x << ", " << y << '\n';
created = true;
}
The draw function for the Region class which the Map class calls for each region in it's draw function.
void Region::draw(int screenX, int screenY, double scale, bool onMonitor)
{
cout << x << ", " << y << '\n';
if(onMonitor)
{
tempTile = load_image("tile.png");
tile = rotozoomSurface(tempTile, 0, scale, 0);
apply_surface((scale * TILE_WIDTH) * x - screenX, (scale * TILE_HEIGHT) * y - screenY, tile, screen, NULL);
}
else
{
SDL_FreeSurface(tile);
}
}
bool Region::isCreated()
{
return created;
}