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

Author Topic: [2.0]Drawing Sprite AND Shape  (Read 5365 times)

0 Members and 1 Guest are viewing this topic.

isReady

  • Newbie
  • *
  • Posts: 13
    • View Profile
[2.0]Drawing Sprite AND Shape
« on: January 10, 2013, 05:35:34 pm »
When I want to draw a shape and a sprite on my window there appear strange bugs.
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;

int main() {
        sf::RenderWindow window;
        sf::Event myEvent;
        sf::Texture texture;
        sf::Sprite sprite;
        sf::CircleShape circle;
        sf::RectangleShape rect;
        //=========================>
        if (!texture.loadFromFile("gothic.bmp"))
                return -1;
        sprite.setTexture(texture);
        sprite.setScale(0.5, 0.5);

        circle.setPosition(100, 100);
        circle.setRadius(100);
        circle.setOutlineColor(sf::Color::Red);

        rect.setPosition(500, 300);
        rect.setSize(sf::Vector2f(20, 20));

        window.create(sf::VideoMode(800, 600), "MyWindow");
        while (window.isOpen()) {
                while(window.pollEvent(myEvent)) {
                        if (myEvent.type == sf::Event::Closed) {
                                window.close();
                        }

                        window.clear();
                        window.draw(circle);
                        window.draw(rect); //evil line. as soon as i have deleted this line everything works
                        window.draw(sprite);
                        window.display();
                }
        }

        return 0;
}

Result: ust the rectangle is drawed to the window.

If I change the order of the draw-calls and comment the "draw(rect)"-line out, everything works:
                        window.clear();
                        window.draw(sprite);
                        window.draw(circle);
                        //window.draw(rect); //evil line
                        window.display();

But this does not work.
window.clear();
                        window.draw(circle);
                        //window.draw(rect); //evil line. as soon as i have deleted this line everything works
                        window.draw(sprite);
                        window.display();
//Only the sprite is drawed

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: [2.0]Drawing Sprite AND Shape
« Reply #1 on: January 10, 2013, 05:48:29 pm »
Could you please specify what 'strange bugs' you're getting?
On my end everything works as expected. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cooldog99

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
    • Perilous Game Studios
Re: [2.0]Drawing Sprite AND Shape
« Reply #2 on: January 10, 2013, 05:51:59 pm »
you have your clearing/displaying in the event loop, this is usually a no-no because it'll only clear/display and draw when an event is fired, and will do this more times than needed, if there are many events. it can also cause unexpected behavior.

instead, try this:

    while (window.isOpen()) {
        while(window.pollEvent(myEvent)) {
                 if (myEvent.type == sf::Event::Closed) {
                     window.close();
                 }
            }
            window.clear();
            window.draw(circle);
            window.draw(rect); //evil line. as soon as i have deleted this line everything works
            window.draw(sprite);
            window.display();
       
    }
 

If that doesn't fix it, I don't think it's the code, everything seems fine, besides what I pointed out.
« Last Edit: January 10, 2013, 05:53:40 pm by cooldog99 »

isReady

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [2.0]Drawing Sprite AND Shape
« Reply #3 on: January 10, 2013, 06:08:46 pm »
Thanks for the fast reply. Unfortunately my mistake with the event loop was not the cause of the displaying problems.
This code:
window.clear();
                        window.draw(sprite);
                        window.draw(circle);
                        window.draw(rect);
                        window.display();

produces this output:



and this code:
window.clear();
                        window.draw(circle);
                        window.draw(rect);
                        window.draw(sprite);
                        window.display();

produces this output, although I expected to see a picture.




This is my whole code:
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;

int main() {
        sf::RenderWindow window;
        sf::Event myEvent;
        sf::Texture texture;
        sf::Sprite sprite;
        sf::CircleShape circle;
        sf::RectangleShape rect;
        //=========================>
        if (!texture.loadFromFile("gothic.bmp"))
                return -1;
        sprite.setPosition(0, 0);
        sprite.setTexture(texture);
        sprite.setScale(0.5, 0.5);

        circle.setPosition(100, 100);
        circle.setRadius(10);
        circle.setOutlineColor(sf::Color::Red);

        rect.setPosition(500, 300);
        rect.setSize(sf::Vector2f(20, 20));

        window.create(sf::VideoMode(800, 600), "MyWindow");
        while (window.isOpen()) {
                while(window.pollEvent(myEvent)) {
                                if (myEvent.type == sf::Event::Closed) {
                                        window.close();
                                }
                        }

                        window.clear();
                        window.draw(sprite);
                        window.draw(circle);
                        window.draw(rect);
                        window.display();
        }

        return 0;
}

This is the picture I want to display [it's not for a special purpose].


For your information: I have an AMD graphics card. Maybe thats important to know for you because SFML 1.6 didn't work with it.

[attachment deleted by admin]

Solluxx

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: [2.0]Drawing Sprite AND Shape
« Reply #4 on: January 10, 2013, 06:58:28 pm »
It looks to me like the sprite IS being drawn but there is nothing on it, and so a big black box is drawn over the circle and half the square...

When you comment out just the rect part though the picture actually shows up?

what happens when you just comment out rect and don't change the order of the draws?

isReady

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [2.0]Drawing Sprite AND Shape
« Reply #5 on: January 10, 2013, 07:05:31 pm »
window.clear();
                        window.draw(sprite);
                        window.draw(circle);
                        //window.draw(rect);
                        window.display();

ouput:

LuxusElg

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: [2.0]Drawing Sprite AND Shape
« Reply #6 on: January 14, 2013, 05:22:39 pm »
I had the exact same problem, where if I drew a Sprite and a RectangleShape, the Sprite would be filled with a single color. (Actually the color of its top-left pixel, I surmised.)
Without the RectangleShape, the Sprite rendered just fine.

After trying it out on a couple of different computers, I tried updating my GPU drivers (I also have an ATI Graphics card), and after updating, it rendered fine even with the RectangleShape, so I would suggest you try updating your drivers as well.

Update your gfx drivers, worked for me. YMMV.

Solluxx

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: [2.0]Drawing Sprite AND Shape
« Reply #7 on: January 21, 2013, 01:37:28 am »
Haha after posting a while ago I just found I got the same problem. For me whatever sprite is drawn right after the Rectangle draw just gets screwed and doesn't show up. Hopefully someone can squash this bug. Updating my drivers now hopefully it'll fix it.