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

Author Topic: What's better - drawing every interval that the window updates, or....  (Read 2745 times)

0 Members and 1 Guest are viewing this topic.

the__cows

  • Newbie
  • *
  • Posts: 7
    • View Profile
So what's better?

In case the title is a bit confusing I mean:

1) Drawing every window update interval. For example, for a 60FPS window, every 17 milliseconds. For example:

int main() {
sf::RenderWindow window(sf::VideoMode(800, 608), "Example Window For Forums");
window.setFramerateLimit(60); //Update every 17 milliseconds for 60FPS
while(window.isOpen()) {
window.clear();
window.draw(sf::Sprite()); //Imagine we're drawing a RenderTexture here
window.display();
}
}
Or

2) Drawing when you need to (for example, when a sprite moves) and displaying it straight after. For example:

void func(sf::RenderWindow& window) {
window.clear();
window.draw(sf::Sprite()); //Whatever you're drawing
window.display();
}
Or is there a much better way?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Just do the clear/draw/display dance every frame.
See also the big red box in http://www.sfml-dev.org/tutorials/2.3/graphics-draw.php

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
  • Don't count on your loop running exactly at 60 FPS
  • Framerate limits and VSync will not guarantee exact timing
  • Redraw everything every frame - clear, draw, and display each time
  • Implement a fixed time step to update your game objects
  • Read: http://gafferongames.com/game-physics/fix-your-timestep/
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Draw every frame. Frame limit or syncronise using v-sync if required.
You can update as much as necessary (from zero to multiple times) each frame.

You should be drawing each frame for a number of reason but one to keep in mind is that the window is interacting with the OS as well and the OS may change some of what is currently visible.

p.s. Jesper and zsbzsb beat me to it  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

the__cows

  • Newbie
  • *
  • Posts: 7
    • View Profile
Just do the clear/draw/display dance every frame.
See also the big red box in http://www.sfml-dev.org/tutorials/2.3/graphics-draw.php
  • Don't count on your loop running exactly at 60 FPS
  • Framerate limits and VSync will not guarantee exact timing
  • Redraw everything every frame - clear, draw, and display each time
  • Implement a fixed time step to update your game objects
  • Read: http://gafferongames.com/game-physics/fix-your-timestep/
Draw every frame. Frame limit or syncronise using v-sync if required.
You can update as much as necessary (from zero to multiple times) each frame.

You should be drawing each frame for a number of reason but one to keep in mind is that the window is interacting with the OS as well and the OS may change some of what is currently visible.

p.s. Jesper and zsbzsb beat me to it  ;D

Thanks all! Much more helpful than the GameDev StackExchange haha. I was confused at first; drawing it when needed seemed fine in my current project, but when I was looking at different code samples involving many different libraries and languages, they all had the one thing in common - drawing every interval. This, and the realisation that making things in this way would make the game asynchronous (you can, for example, move a texture up slowly on the screen without, for example, affecting the player input (Think how the town names show up in the Pokemon games)) made me question whether my way was the best way to do things. Also, the three of you all saying the same thing proved it for sure, if that makes sense haha.

p.s. Jesper and zsbzsb beat me to it  ;D
Not a problem! Like I said before, the fact that you all said it solidifies it for me :)

Had a quick scan over it; complicated but doable. Thanks for the link - I'll implement it into my projects for sure :)

Thanks all! :)
« Last Edit: July 24, 2015, 07:40:06 pm by the__cows »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Drawing only when needed is actually great if/while there aren't any animations, especially on mobile platforms, since it will help you saving energy. Your operating system/window manager is possibly doing the same thing (you just won't notice it).

Theoretically you could combine both strategies based on what happens on screen, just keep in mind things will get more complicated and there might be unexpected cases (like the mentioned driver overrides).