1
General / Re: Nested pollEvent spikes CPU
« on: June 16, 2015, 01:36:17 am »
Ah ha! I think I have the solution you pointed me to. The code below behaves the same as the code in my original post (as far as I can tell), but without the CPU spike:
This code has less "x-mas trees" too (I haven't seen that term before). I'm curious about that. Is the issue with too many "x-mas trees" that it looks ugly and it's easy to make mistakes in, or is there something else that causes this to be an indicator of a problem?
I appreciate all the help. Thank you!
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
int main() {
sf::RenderWindow window(sf::VideoMode(480, 320), "SFML Window");
window.setFramerateLimit(20);
sf::Font font;
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
return 0;
}
sf::Text text;
text.setFont(font);
text.setCharacterSize(50);
text.setStyle(sf::Text::Regular);
text.setColor(sf::Color::Blue);
while (window.isOpen()) {
text.setString("Hello SFML");
window.clear();
window.draw(text);
window.display();
bool spacePressed = false;
sf::Event event;
while (window.pollEvent(event) || spacePressed == true) {
if (event.type == sf::Event::Closed) {
window.close();
exit(0);
}
if (event.type == sf::Event::KeyPressed || spacePressed == true) {
if (event.key.code == sf::Keyboard::Space || spacePressed == true) {
spacePressed = true;
text.setString("You pressed space");
window.clear();
window.draw(text);
window.display();
}
if (event.key.code == sf::Keyboard::Escape) {
spacePressed = false;
}
}
}
}
return 0;
}
#include "ResourcePath.hpp"
int main() {
sf::RenderWindow window(sf::VideoMode(480, 320), "SFML Window");
window.setFramerateLimit(20);
sf::Font font;
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
return 0;
}
sf::Text text;
text.setFont(font);
text.setCharacterSize(50);
text.setStyle(sf::Text::Regular);
text.setColor(sf::Color::Blue);
while (window.isOpen()) {
text.setString("Hello SFML");
window.clear();
window.draw(text);
window.display();
bool spacePressed = false;
sf::Event event;
while (window.pollEvent(event) || spacePressed == true) {
if (event.type == sf::Event::Closed) {
window.close();
exit(0);
}
if (event.type == sf::Event::KeyPressed || spacePressed == true) {
if (event.key.code == sf::Keyboard::Space || spacePressed == true) {
spacePressed = true;
text.setString("You pressed space");
window.clear();
window.draw(text);
window.display();
}
if (event.key.code == sf::Keyboard::Escape) {
spacePressed = false;
}
}
}
}
return 0;
}
This code has less "x-mas trees" too (I haven't seen that term before). I'm curious about that. Is the issue with too many "x-mas trees" that it looks ugly and it's easy to make mistakes in, or is there something else that causes this to be an indicator of a problem?
I appreciate all the help. Thank you!