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

Author Topic: [SOLVED] Black window after clear/draw/display - draw called from an object.  (Read 887 times)

0 Members and 1 Guest are viewing this topic.

Toiwat

  • Newbie
  • *
  • Posts: 3
    • View Profile
Here's a small mock up/example of what I have:

class MainObject
{
private:
        sf::RenderWindow window;
       
        Map map;
public:
        //Constructor/destructor
        void Draw()
        {
                window.clear();
                map.Draw(&window);
                window.display();
        };
}

class Map:
{
private:
        sf::Texture texture;

        std::vector<sf::Sprite*> spv;

        sf::Sprite testsp; //Test case.
public:
        //Constructor and destructor that load a texture and setup sprites - These work! Really, they do!
        void Draw(sf::RenderWindow* window);
        {
                for (std::vector<int>::size_type tile = 0; tile != mapTiles.size(); ++tile)
                {
                                window->draw(*spv[tile]);
                }

                window->draw(testsp); //I thought I made a mistake in the vector or iteration but even this fails.
        };
}

This makes sense, right? I'm trying to draw a sprite (more accurately, a set of sprites) on the RenderWindow passed by reference from inside a child object. My problem is that nothing gets drawn on the screen.

Formerly, MainObject itself held the texture and sprite:

class MainObject
{
private:
        sf::RenderWindow window;
        sf::Texture texture;
        sf::Sprite sprite;

public:
        //Constructor/destructor
        void Draw()
        {
                window.clear();
                window.draw(sprite);
                window.display();
        };
}
 
This worked.



Clear() works because I can change the color the window refreshes to. I tried moving the whole clear/draw/display cycle inside of Map but had no success.

I realize this might be very little to work on. Shall I post a more in-depth description with parts from my actual code?
« Last Edit: May 16, 2013, 02:51:39 pm by Toiwat »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How is the "map" variable initialized in MainObject?
Laurent Gomila - SFML developer

Toiwat

  • Newbie
  • *
  • Posts: 3
    • View Profile
Via it's constructor. Here's the actual snippet of code:

GameMap::GameMap()
{
        textureMapTiles.loadFromFile("resources\\art\\map_tiles_generic.png");

        //Setup basic sprite templates. Furter istances wil copy from these.
        mapTile_4.setTexture(textureMapTiles);
        mapTile_4.setTextureRect(sf::IntRect(0, 0, 300, 300));

        mapTile_3.setTexture(textureMapTiles);
        mapTile_3.setTextureRect(sf::IntRect(301, 0, 300, 300));

        mapTile_2s.setTexture(textureMapTiles);
        mapTile_2s.setTextureRect(sf::IntRect(0, 301, 300, 300));

        mapTile_2b.setTexture(textureMapTiles);
        mapTile_2b.setTextureRect(sf::IntRect(301, 301, 300, 300));
}

This gets called - I know it does, I got paranoid at one point and started placing std::printf everywhere.
SFML doesn't give any error message on loading the texture, plus I can load and display it fine outside of Map.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Quote
How is the "map" variable initialized in MainObject?
Laurent Gomila - SFML developer

Toiwat

  • Newbie
  • *
  • Posts: 3
    • View Profile
Apologies,  like this:
MainGameScreen::MainGameScreen(void) : map()
{
}

To be more specific, the code from before is slightly inaccurate because I forgot it was an older version I had saved on my laptop. It's actually:
GameMap::GameMap() {
        GameMap::GameMap("resources\\art\\map_tiles_generic.png");
}

GameMap::GameMap(std::string filename)
{
        textureMapTiles.loadFromFile(filename);

/*snip*/
}

And with this I realize my mistake - you can't call a constructor from another in C++, can you? I tried just now and just calling the one with the filename parameter works as intended.

Damn. Thank you for your time, I don't think I'd have caught this by myself.

 

anything