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

Author Topic: SFML 2.0 flickering textures  (Read 2063 times)

0 Members and 1 Guest are viewing this topic.

acmd

  • Newbie
  • *
  • Posts: 4
    • View Profile
SFML 2.0 flickering textures
« on: April 20, 2011, 06:32:55 pm »
Hi There! I've started using SFML 2.0 a few days ago and realized that it's awesome :D sf::View::SetRotation is also great.
Now I'm trying to create simple flight simulator.
When camera moves with constant speed everything is ok, but when I set speed according to App.GetFrameTime() grass texture starts flickering. Any help?

Code: [Select]

#include <SFML/Graphics.hpp>
#define TEXTURE_WIDTH 1200
#define TEXTTURE_HEIGHT 798
#define PI 3.141592654
#define SPEED 111
int main()
{
sf::VideoMode dm = sf::VideoMode::GetDesktopMode();
sf::RenderWindow App(dm, "SFML Views", sf::Style::Fullscreen);
App.EnableVerticalSync(true);
App.SetFramerateLimit(60);
sf::Image BackgroundImage;
if (!BackgroundImage.LoadFromFile("Grass.jpg" )) //http://www.dizayner.ucoz.com/3ddd/3dmitri/Grass_from_cg_textures.jpg
return 1;
sf::Sprite Background(BackgroundImage);
Background.Resize(TEXTURE_WIDTH , TEXTTURE_HEIGHT);
sf::Vector2f Center(1000, 1000); //init position
sf::Vector2f HalfSize(dm.Width / 2, dm.Height/ 2);
sf::View View(Center, HalfSize);
float angle = 45; // init camera angle
sf::Vector2f position;

position.x = Background.GetPosition().x + Background.GetSize().x / 2;
position.y = Background.GetPosition().y + Background.GetSize().y / 2;
while (App.IsOpened())
{
float time = App.GetFrameTime();
sf::Event Event;
while (App.GetEvent(Event))
{
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}

//flickering

//position.x += SPEED * time * cos((angle - 90) * 2 * PI / 360);
//position.y += SPEED * time * sin((angle - 90) * 2 * PI / 360);

//no flickering
const float MYSPEED = 3; // depends on your hardware
position.x += MYSPEED * cos((angle - 90) * 2 * PI / 360);
position.y += MYSPEED * sin((angle - 90) * 2 * PI / 360);


if (App.GetInput().IsKeyDown(sf::Key::Left)) angle -= 1.5f;
if (App.GetInput().IsKeyDown(sf::Key::Right)) angle += 1.5f;


View.SetCenter(position.x, position.y);
View.SetRotation(angle);
App.SetView(View);
App.Clear();

for(int w = 0; w < 5; w++)
for(int j = 0; j < 5; j++)
{
Background.SetPosition(TEXTURE_WIDTH * w, TEXTTURE_HEIGHT * j);
App.Draw(Background);
}
App.Display();
}
return 0;
}

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
SFML 2.0 flickering textures
« Reply #1 on: April 21, 2011, 04:06:31 am »
Your math is wrong. I printed out the values each frame and sometimes the view is moving backwards when going forward in the same direction.

Note that SPEED * time can give a number less than 1.

This might have something to do with implicit conversions if you truly can't find anything wrong with your math.