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

Author Topic: [Solved] Problem with derived class of sf::RenderWindow  (Read 1499 times)

0 Members and 1 Guest are viewing this topic.

wofo

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] Problem with derived class of sf::RenderWindow
« on: April 06, 2013, 08:15:41 pm »
I have already solved de problem, thanks to this link: https://github.com/SFML/SFML/wiki/FAQ#wiki-graphics-white-rect

Thanks anyway.

__________
Original post:

I am facing the following problem. I want to create a subclass of sf::RenderWindow but it doesn't display the images that I load, but just a white square of the same dimensions.

I post first a normal code (not object oriented), which shows the image that I load without any problem.
 #include <SFML/Audio.hpp>
 #include <SFML/Graphics.hpp>
 
 int main()
 {
     // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
         window.setVerticalSyncEnabled(true);
 
     // Load a sprite to display
     sf::Texture texture;
     if (!texture.loadFromFile("image.png"))
         return EXIT_FAILURE;
     sf::Sprite sprite;
         sprite.setTexture(texture);
 
     // Start the game loop
     while ( true )
     {
 
         // Clear screen
         window.clear();
 
         // Draw the sprite
         window.draw(sprite);
 
         // Update the window
         window.display();
     }
 
     return EXIT_SUCCESS;
 }

I post a second code, in which I use my subclass. The problem is that instead of showing the image that I load, it shows a white square of the same dimensions.
 #include <SFML/Audio.hpp>
 #include <SFML/Graphics.hpp>
 
 class CustomWindow : public sf::RenderWindow {
        private:       
        sf::Sprite sprite;
       
        public:
        CustomWindow();
        void drawSprite();
 };
 
 CustomWindow::CustomWindow() : sf::RenderWindow(sf::VideoMode(800, 600), "SFML window") {
        setVerticalSyncEnabled(true);
        sf::Texture texture;
        texture.loadFromFile("image.png");
        sprite.setTexture(texture);
 }
 
 void CustomWindow::drawSprite() {
        draw(sprite);
 }
 
 int main()
 {
     // Create the main window
     CustomWindow window;
 
     // Start the game loop
     while ( true )
     {
 
         // Clear screen
         window.clear();
 
         // Draw the sprite
                 window.drawSprite();
               
         // Update the window
         window.display();
     }
 
     return EXIT_SUCCESS;
 }

I think that this is a very strange issue. Please help if you know something about it.

Thanks in advance,
Wofo.
« Last Edit: April 06, 2013, 08:17:40 pm by wofo »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Solved] Problem with derived class of sf::RenderWindow
« Reply #1 on: April 06, 2013, 08:19:15 pm »
Why do you derive from sf::RenderWindow? It is not designed for such uses.

Keep in mind, that code isn't automatically "more object-oriented" the more inheritance it uses. The original code (without CustomWindow) was actually better. If you write your own classes, prefer composition:  Declare the sf::RenderWindow as a member.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

wofo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: [Solved] Problem with derived class of sf::RenderWindow
« Reply #2 on: April 06, 2013, 09:34:17 pm »
Thank you. I am going to correct it.

 

anything