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

Author Topic: Checking when window is ready for drawing  (Read 2373 times)

0 Members and 1 Guest are viewing this topic.

KilledWhale

  • Newbie
  • *
  • Posts: 4
    • View Profile
Checking when window is ready for drawing
« on: January 07, 2012, 04:50:11 pm »
Following code draws the line as it should but without the sleep(1); it doesn't display a thing. Not even a black background.

Is some part of window initialization done asynchronously?

Happens at least on linux x64.

I wonder if there's any way to check if the window's ready for drawing or not because this solution seems kinda hacky.

Discovered the issue when I was trying to draw sierpinski triangle line-by-line without clearing the screen between each line drawing.

Code: [Select]
#include <stdint.h>
#include <iostream>
#include <SFML/Graphics.hpp>

int main() {
  sf::RenderWindow window(sf::VideoMode(800, 800), "");

  sleep(1);
 
  sf::Vertex points[2];
  points[0].Position.y = 50;
  points[0].Position.x = 50;
  points[1].Position.y = 400;
  points[1].Position.x = 500;

  window.Draw(points, 2, sf::Lines);
  window.Display();
 
  sleep(500);

  return 0;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6274
  • Thor Developer
    • View Profile
    • Bromeon
Checking when window is ready for drawing
« Reply #1 on: January 07, 2012, 04:51:51 pm »
I believe you must call Clear(). I recently tried to omit it because I filled the whole background with an image, which resulted in strange display bugs.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

KilledWhale

  • Newbie
  • *
  • Posts: 4
    • View Profile
Checking when window is ready for drawing
« Reply #2 on: January 07, 2012, 04:55:28 pm »
Quote from: "Nexus"
I believe you must call Clear(). I recently tried to omit it because I filled the whole background with an image, which resulted in strange display bugs.

Calling Clear() doesn't help. Also it would make no sense as the program works if I use the sleep() to wait after creation  :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Checking when window is ready for drawing
« Reply #3 on: January 07, 2012, 05:44:22 pm »
You probably need to handle your window's events. If you don't, the OS events will never reach your SFML window and it will remain frozen/inactive.

Quote
I believe you must call Clear(). I recently tried to omit it because I filled the whole background with an image, which resulted in strange display bugs.

This is strange, Clear is definitely not needed if you fill the entire target.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6274
  • Thor Developer
    • View Profile
    • Bromeon
Checking when window is ready for drawing
« Reply #4 on: January 07, 2012, 07:04:01 pm »
Ok, it was another issue... The bugs appeared because I used a repeated texture without enlarging the sf::Sprite's texture rect. With Clear(), the background was just not drawn for some time. Without Clear(), the effect was much more eye-catching.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

KilledWhale

  • Newbie
  • *
  • Posts: 4
    • View Profile
Checking when window is ready for drawing
« Reply #5 on: January 08, 2012, 01:26:53 am »
Quote from: "Laurent"
You probably need to handle your window's events. If you don't, the OS events will never reach your SFML window and it will remain frozen/inactive.

Quote
I believe you must call Clear(). I recently tried to omit it because I filled the whole background with an image, which resulted in strange display bugs.

This is strange, Clear is definitely not needed if you fill the entire target.

But that still doens't explain why it works with sleep() but not without it :/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Checking when window is ready for drawing
« Reply #6 on: January 08, 2012, 09:44:40 am »
Have you tried to add event handling or not?
Laurent Gomila - SFML developer

KilledWhale

  • Newbie
  • *
  • Posts: 4
    • View Profile
Checking when window is ready for drawing
« Reply #7 on: January 08, 2012, 10:22:57 am »
Quote from: "Laurent"
Have you tried to add event handling or not?

I tried adding event handling like this but no difference.
Code: [Select]
#include <stdint.h>
#include <iostream>
#include <SFML/Graphics.hpp>

int main() {
sf::RenderWindow window(sf::VideoMode(800, 800), "");

sf::Vertex points[2];
points[0].Position.y = 50;
points[0].Position.x = 50;
points[1].Position.y = 400;
points[1].Position.x = 500;

window.Draw(points, 2, sf::Lines);
window.Display();

sf::Event ev;

while(window.WaitEvent(ev)) {
if (ev.Type == sf::Event::Closed) break;
if (ev.Type == sf::Event::KeyPressed) break;
}

return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Checking when window is ready for drawing
« Reply #8 on: January 08, 2012, 10:48:36 am »
I mean, between creating the window and drawing.

But this way of displaying things doesn't make sense anyway, so I don't know if it's worth solving this "problem".
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Checking when window is ready for drawing
« Reply #9 on: January 08, 2012, 01:00:09 pm »
retep998 explained why (and how) you should do here.
SFML / OS X developer

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Checking when window is ready for drawing
« Reply #10 on: January 12, 2012, 05:22:10 pm »
Would not calling Clear() improve performance, and if so, by how much?

Ed
SFML 2.1

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Checking when window is ready for drawing
« Reply #11 on: January 12, 2012, 05:34:53 pm »
Quote
Would not calling Clear() improve performance, and if so, by how much?

It would, but the difference would be so tiny that you would most likely not see it.
Laurent Gomila - SFML developer

 

anything