-
Hello! I have recently started using SFML 2.0 with Thor 2.0. I made an example program to learn how the library works and I have correctly running it on my Ubuntu x64 PC in Eclipse:
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <Thor/Animation.hpp>
#include <Thor/Particles.hpp>
#include <iostream>
int main()
{
bool moveText = false;
// Create the main window
sf::RenderWindow Ventana(sf::VideoMode(800, 600), "Prueba Juego con SFML");
// Create a graphical string to display
sf::Font font;
if(!font.loadFromFile("gkfont.ttf")) return EXIT_FAILURE;
sf::Text mText("Hello!! :D", font, 50);
sf::Music miMusica;
if (!miMusica.openFromFile("musica.ogg")) return EXIT_FAILURE;
Ventana.setFramerateLimit(60);
Ventana.clear(sf::Color::Black);
// Start the game loop
while (Ventana.isOpen()){
// Process events
sf::Event Event;
while (Ventana.pollEvent(Event)){
// Close window : exit
if (Event.type == sf::Event::Closed) Ventana.close();
if (Event.type == sf::Event::MouseButtonPressed){
mText.setPosition(Event.mouseButton.x, Event.mouseButton.y);
Ventana.draw(mText);
moveText = true;
}
if(Event.type == sf::Event::MouseMoved && moveText == true){
Ventana.clear(sf::Color::Black);
mText.setPosition(Event.mouseMove.x, Event.mouseMove.y);
Ventana.draw(mText);
}
if(Event.type == sf::Event::MouseButtonReleased){
Ventana.clear(sf::Color::Black);
moveText = false;
}
if(Event.type == sf::Event::KeyPressed && moveText){
if(Event.key.code == sf::Keyboard::A) mText.setCharacterSize(mText.getCharacterSize()-2);
if(Event.key.code == sf::Keyboard::S) mText.setCharacterSize(mText.getCharacterSize()+2);
Ventana.clear(sf::Color::Black);
Ventana.draw(mText);
}
if(Event.type == sf::Event::KeyPressed){
if(Event.key.code == sf::Keyboard::P) miMusica.pause();
if(Event.key.code == sf::Keyboard::O) miMusica.play();
}
}
// Update the window
Ventana.display();
}
return EXIT_SUCCESS;
}
The program simply shows a text where the mouse pointer is, you can change its size pressing keys A and S and play music pressing O and P, everything ok.
The problem is when I try to run it on my netbook with Windows XP x86, Intel GMA3150, 1Gb RAM and Intel Athom even this very simple program is very slow and choppy:
- I can see the window refreshing (the window flashs white)
- Text movement is slow and not smooth as in my other PC
- Even if I don't show any text screen flash unless I delete Ventana.display() from the loop
The graphics card have OpenGL v1.4 I think it should be ok to display a simple text. I already updated the drivers to the latest version. Hope you can help me :)
-
I'm not sure if this will help solve the problem, but I wouldn't put the Ventana.clear(sf::Color::Black) nor the Ventana.draw(mText) lines inside your events like that. Mixing event code with drawing might be what's causing the issues. What you would probably want is something along these lines.
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <Thor/Animation.hpp>
#include <Thor/Particles.hpp>
#include <iostream>
int main()
{
bool moveText = false;
// Create the main window
sf::RenderWindow Ventana(sf::VideoMode(800, 600), "Prueba Juego con SFML");
// Create a graphical string to display
sf::Font font;
if(!font.loadFromFile("gkfont.ttf")) return EXIT_FAILURE;
sf::Text mText("Hello!! :D", font, 50);
sf::Music miMusica;
if (!miMusica.openFromFile("musica.ogg")) return EXIT_FAILURE;
Ventana.setFramerateLimit(60);
Ventana.clear(sf::Color::Black);
// Start the game loop
while (Ventana.isOpen()){
// Process events
sf::Event Event;
while (Ventana.pollEvent(Event)){
// Close window : exit
if (Event.type == sf::Event::Closed) Ventana.close();
if (Event.type == sf::Event::MouseButtonPressed){
mText.setPosition(Event.mouseButton.x, Event.mouseButton.y);
moveText = true;
}
if(Event.type == sf::Event::MouseMoved && moveText == true){
mText.setPosition(Event.mouseMove.x, Event.mouseMove.y);
}
if(Event.type == sf::Event::MouseButtonReleased){
moveText = false;
}
if(Event.type == sf::Event::KeyPressed && moveText){
if(Event.key.code == sf::Keyboard::A) mText.setCharacterSize(mText.getCharacterSize()-2);
if(Event.key.code == sf::Keyboard::S) mText.setCharacterSize(mText.getCharacterSize()+2);
}
if(Event.type == sf::Event::KeyPressed){
if(Event.key.code == sf::Keyboard::P) miMusica.pause();
if(Event.key.code == sf::Keyboard::O) miMusica.play();
}
}
// Update the window
Ventana.clear(sf::Color::Black);
Ventana.draw(mText);
Ventana.display();
}
return EXIT_SUCCESS;
}
-
Nice! Thank you! The flashing screen has disappeared and the text is being rendered correctly I can read it now :D
But still it is very slow, the movement is not smooth at all! I am not an expert on graphics but how can't a simple text move along a screen but if I run Warcraft 3 on OpenGL mode runs smooth (on low graphics)
-
What about using the realtime class sf::Mouse:
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <Thor/Animation.hpp>
#include <Thor/Particles.hpp>
#include <iostream>
int main()
{
bool moveText = false;
// Create the main window
sf::RenderWindow Ventana(sf::VideoMode(800, 600), "Prueba Juego con SFML");
// Create a graphical string to display
sf::Font font;
if(!font.loadFromFile("gkfont.ttf")) return EXIT_FAILURE;
sf::Text mText("Hello!! :D", font, 50);
sf::Music miMusica;
if (!miMusica.openFromFile("musica.ogg")) return EXIT_FAILURE;
Ventana.setFramerateLimit(60);
Ventana.clear(sf::Color::Black);
// Start the game loop
while (Ventana.isOpen()){
// Process events
sf::Event Event;
while (Ventana.pollEvent(Event)){
// Close window : exit
if (Event.type == sf::Event::Closed) Ventana.close();
else if(Event.type == sf::Event::KeyPressed && moveText){
if(Event.key.code == sf::Keyboard::A) mText.setCharacterSize(mText.getCharacterSize()-2);
if(Event.key.code == sf::Keyboard::S) mText.setCharacterSize(mText.getCharacterSize()+2);
}
else if(Event.type == sf::Event::KeyPressed){
if(Event.key.code == sf::Keyboard::P) miMusica.pause();
if(Event.key.code == sf::Keyboard::O) miMusica.play();
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
mText.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(Ventana)));
// Update the window
Ventana.clear(sf::Color::Black);
Ventana.draw(mText);
Ventana.display();
}
}
-
It works slightly better but still slow, here is the code, I added the color transparent to hide the text when I release the mouse button:
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <Thor/Animation.hpp>
#include <Thor/Particles.hpp>
#include <iostream>
int main()
{
bool moveText = false;
// Create the main window
sf::RenderWindow Ventana(sf::VideoMode(800, 600), "Prueba Juego con SFML");
// Create a graphical string to display
sf::Font font;
if(!font.loadFromFile("gkfont.ttf")) return EXIT_FAILURE;
sf::Text mText("Hello!! :D", font, 50);
sf::Music miMusica;
if (!miMusica.openFromFile("musica.ogg")) return EXIT_FAILURE;
Ventana.setFramerateLimit(60);
mText.setColor(sf::Color::Transparent);
// Start the game loop
while (Ventana.isOpen()){
// Process events
sf::Event Event;
while (Ventana.pollEvent(Event)){
// Close window : exit
if (Event.type == sf::Event::Closed) Ventana.close();
else if(Event.type == sf::Event::KeyPressed && moveText){
if(Event.key.code == sf::Keyboard::A) mText.setCharacterSize(mText.getCharacterSize()-2);
if(Event.key.code == sf::Keyboard::S) mText.setCharacterSize(mText.getCharacterSize()+2);
}
else if(Event.type == sf::Event::KeyPressed){
if(Event.key.code == sf::Keyboard::P) miMusica.pause();
if(Event.key.code == sf::Keyboard::O) miMusica.play();
}
else if(Event.type == sf::Event::MouseButtonReleased) mText.setColor(sf::Color::Transparent);
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
mText.setColor(sf::Color::White);
mText.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(Ventana)));
}
// Update the window
Ventana.clear(sf::Color::Black);
Ventana.draw(mText);
Ventana.display();
}
}
-
Ok I think the problem is related to Events Polling because if I draw simple graphics moving around the screen they move smooth.
-
Is it also slow, when you don't move your mouse over the window? If not, what mouse are you using?
-
Is it also slow, when you don't move your mouse over the window? If not, what mouse are you using?
I need to move the mouse in order to move my text around the screen. But I did a simple pong program who hits the screen limits and it is smooth and I don't use Events and it is more graphics intensive than this program writing text.