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

Author Topic: The sprite is moving very slow and with bugs.  (Read 4637 times)

0 Members and 1 Guest are viewing this topic.

DoubleNy

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
The sprite is moving very slow and with bugs.
« on: December 28, 2016, 01:06:23 am »
Hi , I am trying to learn SFML, but all I've done is a skeleton of a game.
1. I created a sprite and added it to window.
2. I create a map(tiled map) and added it, too.
But, when i move my sprite it is very slow and has a lot of lags, i don't now why...or i do, I think this is caused because I draw my map and sprite always after moving sprite. How can I change this thing? 'cause if I remove draw.map, the sprite is very smooth and fast.

This is a part of my code :
while (window.isOpen()){
                // handle events
                sf::Event event;
                while (window.pollEvent(event)){
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) {
                                x = 68;
                                player.move(0.0, -20.0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) {
                                x = 0;
                                player.move(0.0, 20.0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
                                x = 3 * 70;
                                player.move(-20.0, 0.0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) {
                                x = 2 * 68;
                                player.move(20.0, 0.0);
                        }
                //texture.loadFromFile("tank.png", sf::IntRect(x, 0, 80, 65));
                //changed to
               player.setTextureRect(sf::IntRect(x, 0, 79, 63));
                window.clear();
                window.draw(map);
                window.draw(player);
                window.display();
}
 
« Last Edit: December 28, 2016, 11:32:42 am by DoubleNy »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: The sprite is moving very slow and with bugs.
« Reply #1 on: December 28, 2016, 01:26:23 am »
Don't load textures inside your main loop, it's useless and slow. Load them only once before.

DoubleNy

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: The sprite is moving very slow and with bugs.
« Reply #2 on: December 28, 2016, 11:13:49 am »
even if I don't load texture in main loop the sprite is moving slow. Only if I erase "window.clear(); && window.draw(map);" it is moving smooth and fast, but in such case there remain parts of sprite in the back (smth like this : http://cdn2.theweek.co.uk/sites/theweek/files/styles/gallery_adv/public/2015/07/555.jpg?itok=Z4_O-uNe ). How can i draw only the part which will be fulfilled by sprite after his moving ?

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
I would like a spanish/latin community...
Problems building for Android? Look here

DoubleNy

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: The sprite is moving very slow and with bugs.
« Reply #4 on: December 29, 2016, 12:48:34 am »
I have already read this part , but this doesn't work : (( 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
The sprite is moving very slow and with bugs.
« Reply #5 on: December 29, 2016, 09:44:46 am »
What GPU do you have?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

DoubleNy

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: The sprite is moving very slow and with bugs.
« Reply #6 on: December 29, 2016, 11:53:19 am »
Mobile Intel(R) 45 Express Chipset Family(MSC - WDDM 1.1)

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: The sprite is moving very slow and with bugs.
« Reply #7 on: December 29, 2016, 12:10:04 pm »
Would you provide MCVE where you reproduce this problem so we don't have to guess where it went wrong?

DoubleNy

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: The sprite is moving very slow and with bugs.
« Reply #8 on: December 29, 2016, 12:42:59 pm »
I think the problem is caused by drawing "window.draw(map);", because if i don't draw the map, and only draw the player inside loop, fps are like >1000, if they both are drawn fps don't exceed 18. I create the map as it is done in this tutorial http://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php#example-tile-map.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
The sprite is moving very slow and with bugs.
« Reply #9 on: December 29, 2016, 03:18:14 pm »
And how large is your map. But as has been said, without a compilable and minimal example all we can do is make random guesses.

Might add though, that your graphics card is rather old and not perform well even in simple scenarios. How many games can you run fluently on your PC?
« Last Edit: December 29, 2016, 03:20:32 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

DoubleNy

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: The sprite is moving very slow and with bugs.
« Reply #10 on: December 29, 2016, 03:20:20 pm »
you can see the code on https://github.com/DoubleNy/Demokratie-bringer, it's not a bunch of code.
p.s look at game.cpp
p.p.s I don't play games,I've played only cs 1.6  :D 3-4 years ago.
« Last Edit: December 29, 2016, 03:25:31 pm by DoubleNy »

 

anything