SFML community forums
Help => Graphics => Topic started by: Scipi on November 02, 2011, 09:47:47 pm
-
Hello,
I'm having a small issue with a pointer to a render window. I basically have a vector of a class I wrote where each instance has their own render window, although because render window is noncopyable, I am using a pointer to a render window. Here's the code:
class TileMap
{
public:
sf::RenderWindow* App;
sf::RenderWindow* App2;
}
int main()
{
vector<TileMap> Maps;
Maps.push_back(TileMap());
}
TileMap::TileMap()
{
App = new sf::RenderWindow(sf::VideoMode(1088, 832), MapType + " - " + MapName);
App2 = new sf::RenderWindow(sf::VideoMode(64, 640), "Images");
}
What ends up happening is two white windows flash on the screen then disappear. Then the program crashes when it checks if App is open.
So what am I doing wrong in this? I believe that, somehow, the pointer is being broken somewhere. I tried making the RenderWindows on the heap but I got a whole slew of bugs.
Thanks
-
This code is ok, show us the rest of your code.
-
I actually forgot to change something.
TileMap::TileMap()
{
App = new sf::RenderWindow(sf::VideoMode(1088, 832), MapType + " - " + MapName);
App2 = new sf::RenderWindow(sf::VideoMode(64, 640), "Images");
}
Should be
TileMap::TileMap()
{
App = &sf::RenderWindow(sf::VideoMode(1088, 832), MapType + " - " + MapName);
App2 = &sf::RenderWindow(sf::VideoMode(64, 640), "Images");
}
And that's what I'm getting an error with. The rest of my code(Minus unrelated functions, of course):
class TileMap
{
public:
sf::RenderWindow* App;
sf::RenderWindow* App2;
sf::View View;
vector<int> MapData;
vector<sf::Sprite> Sprites;
vector<sf::Image> Images;
vector<sf::Shape> Shapes;
vector<string> MapTypes;
int MapWidth, MapHeight, ImageSize;
string MapType, MapName;
//void Draw(int, int, int);//Done
void Inputs();
void Inputs(vector<TileMap>&);//Done
void LoadImages();//Done
void LoadSprites();//Done
void SaveFile();//Done
void LoadFile();//Done
void GetEvents();//Done
void Display();
void Display(vector<sf::Sprite>&, vector<int>&);
//void SaveCalc();
//void SendCalc();//This will be the last thing to be implemented
TileMap();//Type of Map
//TileMap(string, string);
//~TileMap();
protected:
private:
int ImgListNum, selectedimage, selectedimage2;
};
int main()
{
vector<TileMap> Maps;
Maps.push_back(TileMap());
while(Maps[0].App -> IsOpened())
{
for(int i = 0; i < Maps.size(); i++)
{
Maps[i].GetEvents();
if(!Maps[i].App -> IsOpened())
Maps.erase(Maps.begin() + i);
Maps[i].Inputs(Maps);
Maps[i].App -> Clear();
Maps[i].App2 -> Clear(sf::Color::White);
if(Maps[i].MapType == "visual")
Maps[i].Display();
else
Maps[i].Display(Maps[0].Sprites, Maps[0].MapData);
}
}
return 0;
}
TileMap::TileMap()
{
bool load = false;
MapTypes.push_back("visual");
MapTypes.push_back("collision");
MapTypes.push_back("transitional");
MapTypes.push_back("events");
MapTypes.push_back("NPC");
int choice = -1;
while(choice < 0 || choice > MapTypes.size())
{
system("cls");
cout << "Map Type";
for(int i = 0; i < MapTypes.size(); i++)
cout << "\n" << i << ":\t" << MapTypes[i];
cout << "\n(1, 2, 3, 4...): ";
cin >> choice;
}
MapType = MapTypes[choice];
cout << "Do you want to load a map? (1/0)";
cin >> load;
if(load)
{
cout << "Name of Map to Load: ";
cin >> MapName;
}
else
{
cout << "Name of New Map: ";
cin >> MapName;
cout << "Input Map Size (X Y): ";
cin >> MapWidth >> MapHeight;
MapData.resize(MapWidth * MapHeight, -1);
}
cout << "Input Image Size: ";
cin >> ImageSize;
App = &sf::RenderWindow(sf::VideoMode(1088, 832), MapType + " - " + MapName);
App2 = &sf::RenderWindow(sf::VideoMode(64, 640), "Images");
App -> UseVerticalSync(true);
App2 -> UseVerticalSync(true);
View.SetFromRect(sf::FloatRect(0,0,1088,832));
App -> SetView(View);
LoadImages();
cout << "Images Loaded\n";
LoadSprites();
cout << "Sprites Loaded\n";
selectedimage = 0;
selectedimage2 = 1;
Shapes.push_back(sf::Shape::Rectangle(0,0,(MapWidth * ImageSize), (MapHeight * ImageSize),sf::Color(255,255,255,0),4,sf::Color(0,0,255,127)));
Shapes.push_back(sf::Shape::Rectangle(0,0,ImageSize,ImageSize,sf::Color(255,255,255,0),4,sf::Color(0,0,255,127)));
Shapes.push_back(sf::Shape::Rectangle(0,0,ImageSize,ImageSize,sf::Color(255,255,255,0),4,sf::Color(0,255,0,127)));
}
-
Oh... then revert your change, your first version was the right one.
-
When you do something like this:
App2 = &sf::RenderWindow(sf::VideoMode(64, 640), "Images");
you create a local variable and takes its address to put on the pointer. This local variable gets destroyes when the function ends, and then you have an invalid address on the pointer. But if you use the new to construct the pointers, they stay on the memory until you destroy them manually.