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

Author Topic: Why doesn't the window display function work the way I intended?  (Read 3286 times)

0 Members and 1 Guest are viewing this topic.

kaiserMKII

  • Newbie
  • *
  • Posts: 6
    • View Profile
Why doesn't the window display function work the way I intended?
« on: November 25, 2017, 02:30:46 pm »
Hello!

As the title suggests I want to know how does window display function actually work?
I mean I did read the guide but still I don't get it.
I know it's a form double buffering but still I don't get it.

I was playing with the window and basically I wanted to redraw not resize the rectangles I had drawn everytime I resize the window.And it works!
But the thing that inspired me to ask this question is after resizing I decided to flip the Window and to my suprise the previous drawing from the previous buffer is not present after flipping the window.
My question is why?

Here's the code
#include <SFML/Graphics.hpp>
#include <time.h>
#include <stdlib.h>
#include <iostream>

const float width=1024,height=768;
const float pi=3.1415926;
const int k=6;
int i=1;

int main()
{
    sf::RenderWindow window(sf::VideoMode(width,height), "SFML works!");
    const sf::Vector2f rectsize(25.f,100.f);
    sf::RectangleShape rect[k];
    window.clear();
    for(int i=0;i<k;i++)
    {
        rect[i].setSize(rectsize);
        switch(i)
        {
        case 0:
            rect[i].setFillColor(sf::Color::Magenta);
            break;
        case 1:
            rect[i].setFillColor(sf::Color::Cyan);
            break;
        case 2:
            rect[i].setFillColor(sf::Color::Yellow);
            break;
        case 3:
            rect[i].setFillColor(sf::Color::Red);
            break;
        case 4:
            rect[i].setFillColor(sf::Color::Green);
            break;
        case 5:
            rect[i].setFillColor(sf::Color::Blue);
            break;
        }
        rect[i].setPosition(10+i*rectsize.x,10);
        if(i>2)
        {
            rect[i].setPosition(10+((i-k/2)%k)*rectsize.x,10+rectsize.y);
        }
        window.draw(rect[i]);
    }

window.display();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            switch(event.type)
            {
            case sf::Event::Closed:
                window.close();
                break;
            case sf::Event::Resized:
                    for(int i=0;i<k;i++)
    {
        rect[i].setSize(rectsize);
        switch(i)
        {
        case 0:
            rect[i].setFillColor(sf::Color::Magenta);
            break;
        case 1:
            rect[i].setFillColor(sf::Color::Cyan);
            break;
        case 2:
            rect[i].setFillColor(sf::Color::Yellow);
            break;
        case 3:
            rect[i].setFillColor(sf::Color::Red);
            break;
        case 4:
            rect[i].setFillColor(sf::Color::Green);
            break;
        case 5:
            rect[i].setFillColor(sf::Color::Blue);
            break;
        }
        rect[i].setPosition(10+i*rectsize.x,10);
        if(i>2)
        {
            rect[i].setPosition(10+((i-k/2)%k)*rectsize.x,10+rectsize.y);
        }
        window.draw(rect[i]);
    }
    window.display();
    break;
        case sf::Event::KeyPressed:
            if(event.key.code==sf::Keyboard::F) window.display();
            break;
            }

        }
    }

    return 0;
}

 

I know it's a messy code and I'm sorry I tried to make it more tighty.But I am exploring the library and all of it's content and trying to figure out what most the things do on my own and try not ask questions.
Sadly in this case I can't find an answer.
Thank you for your time and answers!
« Last Edit: November 25, 2017, 06:20:42 pm by Laurent »

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Why doesn't the window display function work the way I intended?
« Reply #1 on: November 25, 2017, 02:41:12 pm »
you should draw and display inside the main loop every time, not only in events handling.

kaiserMKII

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Why doesn't the window display function work the way I intended?
« Reply #2 on: November 25, 2017, 02:49:59 pm »
Thank you for the advice.I guess?
But I already knew that and I'm currently studying this library.
And how does that answer my question?

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Why doesn't the window display function work the way I intended?
« Reply #3 on: November 25, 2017, 05:44:35 pm »
Thank you for the advice.I guess?
But I already knew that and I'm currently studying this library.
And how does that answer my question?

So if you already knew that - why don't you do that? You call display only on getting resize event

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why doesn't the window display function work the way I intended?
« Reply #4 on: November 25, 2017, 06:26:44 pm »
Quote
to my suprise the previous drawing from the previous buffer is not present after flipping the window
What do you expect to see, and what are you seeing instead?

And you should really use consistent indentation, your code is so hard to read.
Laurent Gomila - SFML developer

kaiserMKII

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Why doesn't the window display function work the way I intended?
« Reply #5 on: November 25, 2017, 07:08:24 pm »
Quote
to my suprise the previous drawing from the previous buffer is not present after flipping the window
What do you expect to see, and what are you seeing instead?

And you should really use consistent indentation, your code is so hard to read.

Well it's a double buffer correct?So what ever I'm drawing it is drawn at the back buffer and when I call display it switches the back buffer with the front buffer.So after resizing the window I get resized drawing and whatever was left is now at the back waiting to be cleared and re-drawn.

Instead I only see the resized drawing and a blank buffer when I press F.

And my question is why does that happen?

Quote
So if you already knew that - why don't you do that? You call display only on getting resize event

Curiosity and studying how the library works.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why doesn't the window display function work the way I intended?
« Reply #6 on: November 25, 2017, 09:25:22 pm »
Try this simpler code to see double buffering in action:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML double buffer");

    window.clear(sf::Color::Green);
    window.display();
    window.clear(sf::Color::Red);
    window.display();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::KeyPressed)
                window.display();
    }

    return 0;
}

And, while it's always a good thing to understand what's behind the scenes, keep in mind that it is an implementation detail that may not be used on all platforms.
« Last Edit: November 25, 2017, 09:27:14 pm by Laurent »
Laurent Gomila - SFML developer

kaiserMKII

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Why doesn't the window display function work the way I intended?
« Reply #7 on: November 25, 2017, 11:22:54 pm »
Try this simpler code to see double buffering in action:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML double buffer");

    window.clear(sf::Color::Green);
    window.display();
    window.clear(sf::Color::Red);
    window.display();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::KeyPressed)
                window.display();
    }

    return 0;
}

And, while it's always a good thing to understand what's behind the scenes, keep in mind that it is an implementation detail that may not be used on all platforms.

Yes this is the result I was and still am trying to achieve.
but what is the difference between mine and yours?
I mean shouldn't mine worked the same way as yours?
If not then where's my mistake?
Thanks!
« Last Edit: November 26, 2017, 01:47:38 am by kaiserMKII »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why doesn't the window display function work the way I intended?
« Reply #8 on: November 26, 2017, 01:40:29 pm »
I don't know, but since your example is over-complicated for what you want to demonstrate, I'm not sure I want to spend more time trying to figure out what you did wrong. I let you play with your own code :D
Laurent Gomila - SFML developer

kaiserMKII

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Why doesn't the window display function work the way I intended?
« Reply #9 on: November 26, 2017, 03:04:39 pm »
But you're the admin and why won't you help?At least tell so I can can be at peace  and figure it on my own :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why doesn't the window display function work the way I intended?
« Reply #10 on: November 26, 2017, 03:27:00 pm »
It's not that I don't want to help, it's just that I have no idea what's wrong, and don't want to setup a project, copy your code and start debugging it. No time for doing this kind of funny things, unfortunately. Sorry.

But here is how I would proceed: start with the code that works (mine) and then transform it to the code that doesn't work (yours), step by step, testing the result after each step. And remove all the unneeded stuff (why so many rectangles?), and please indent your code correctly ;D
« Last Edit: November 26, 2017, 03:29:04 pm by Laurent »
Laurent Gomila - SFML developer

kaiserMKII

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Why doesn't the window display function work the way I intended?
« Reply #11 on: November 26, 2017, 04:09:04 pm »
Well I said I was studying the library so I didn't want to lose time deleting the all the rectangles(Yes I know it's not hard sue me :D)I thought I would just go with what I made previously :D and get on with it :D.Yes I know it's a messy code(I did say it at the beginning) because I'm challenging myself everytime I complete a task and use what I made from that task for my next challenge and on and on :D.
Still thank you for your honesty you made me laugh with that :D.
I will take your advice.