Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with sf::RenderWindow*  (Read 2405 times)

0 Members and 1 Guest are viewing this topic.

Scipi

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Help with sf::RenderWindow*
« 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:

Code: [Select]

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
I issa Cat Person =^w^=

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Help with sf::RenderWindow*
« Reply #1 on: November 02, 2011, 09:55:09 pm »
This code is ok, show us the rest of your code.
Laurent Gomila - SFML developer

Scipi

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Help with sf::RenderWindow*
« Reply #2 on: November 02, 2011, 10:18:01 pm »
I actually forgot to change something.

Code: [Select]

TileMap::TileMap()
{
    App = new sf::RenderWindow(sf::VideoMode(1088, 832), MapType + " - " + MapName);
    App2 = new sf::RenderWindow(sf::VideoMode(64, 640), "Images");
}  


Should be

Code: [Select]

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):

Code: [Select]

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)));

}
I issa Cat Person =^w^=

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Help with sf::RenderWindow*
« Reply #3 on: November 02, 2011, 10:21:15 pm »
Oh... then revert your change, your first version was the right one.
Laurent Gomila - SFML developer

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Help with sf::RenderWindow*
« Reply #4 on: November 03, 2011, 04:05:44 am »
When you do something like this:

Code: [Select]
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.

 

anything