SFML event::TextEntered not working properly - c++

I'm making a school project about image modifying and I'm using sfml to make something like a console but a bit more "beautiful".
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
}
if (event.type == sf::Event::TextEntered)
{
//display text on screen
}
}
The problem is that when i press a certain key shortly on the display it appears like I've hold the key for a long period of time...
Example: pressing G, expecting to see only 1 G on the screen but on the screens there appears like 20 G.

Your if belongs inside the event polling loop:
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
if (event.type == sf::Event::TextEntered)
{
//display text on screen
}
}
}
From your description, there is probably more going wrong, but you need to post more code to see that.

I think this is because when pressing a key that signal is always transmitted, so
try this :
sf::RenderWindow window(...);
window.setKeyRepeatEnabled(false);
while(...)
{
stuff...
}

Related

Sfml lostFocus / gainedFocus cpu usage

Why when I run this simple code and just doing nothing CPU usage by this app is around 0.4%
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 800), "hi", sf::Style::Close | sf::Style::Titlebar);
window.setFramerateLimit(60);
sf::Event event;
bool lostFocus = false;
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::LostFocus)
lostFocus = true;
else if (event.type == sf::Event::GainedFocus)
lostFocus = false;
}
if (!lostFocus)
{
window.clear(sf::Color::White);
window.display();
}
}
}
But when I click on other app, CPU usage by this app increases to 12%.
Does GainedFocus actually eat so much cpu power or what?
(I just wanted to make a simple pause of game and saw this cpu usage)
As already mentioned, when your window doesn't have focus and you're not updating the window, the loop goes into a hot spin where it's continuously checking for events. When you've lost focus, you instead want to block and wait for a GainedFocus event to occur. As a very simple example, you could do something like
void block_until_gained_focus(sf::Window& window) {
sf::Event event;
while (true) {
if (window.waitEvent(event) && event.type == sf::Event::GainedFocus) {
return;
}
}
}
Then your main game loop could look something like
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::LostFocus)
wait_until_gained_focus(window);
}
window.clear(sf::Color::White);
window.display();
}
Now if you wanted to do additional work while focus is lost you'll need a different implementation, but the key here is to use waitEvent instead of pollEvent.

SFML Window Resizing is very ugly

When I resize my sfml window, when I cut resize to make it smaller and resize to make it larger, it gives you a really weird effect.
How do I make the resizing more prettier? The code is from the installation tutorial for code::blocks.
Code (same as the code in the installation tutorial for code::blocks on the sfml website):
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
You need to manage the resize of the window. Otherwise the coordinates are wrong. Here is an excerpt of your code with the solution. Credits go to the author of this forum post, this is where I once found it when I was looking for a solution: https://en.sfml-dev.org/forums/index.php?topic=17747.0
Additionally you can set the new coordinates based on the new size. The link gives you more information.
// create own view
sf::View view = window.getDefaultView();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::Resized) {
// resize my view
view.setSize({
static_cast<float>(event.size.width),
static_cast<float>(event.size.height)
});
window.setView(view);
// and align shape
}
}

SFML mouse button released is spamming

I am using SFML and C++ and I am getting an odd problem,
Here is my main game update method
while (renderService.Window.isOpen())
{
//Poll events
sf::Event event;
while (renderService.Window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
renderService.Window.close();
running = false;
}
MouseMovment(event);
MouseClick(event);
Update();
Draw();
}
and here is my MouseClick method
void Game::MouseClick(sf::Event event)
{
sf::Vector2i position = sf::Mouse::getPosition(renderService.Window);
if (event.mouseButton.button == sf::Mouse::Left && event.type == sf::Event::MouseButtonReleased)
{
std::cout << "Mouse released" << std::endl;
}
}
now here is the weird part, in my console sometimes my cout will be spammed like 10/20 times, but sometimes it will work perfectly, am I calling the event wrongly?
You're doing it wrong, suppose that a MouseButtonReleased event is fired and your polling function grabs it (follows the numbers in the comments):
while (renderService.Window.isOpen()) // 4) The loop starts again
{
//Poll events
sf::Event event;
while (renderService.Window.pollEvent(event)) // 1) Grabs the event // 5) No more events
{
if (event.type == sf::Event::Closed) // 2) Nope, it's not this one
renderService.Window.close();
running = false;
}
MouseMovment(event);
MouseClick(event); // 3) Yes, handle it // 6) Uses the unmodified event variable - undefined behavior
Update();
Draw();
}
you should rather do something like:
sf::Event event;
// while there are pending events...
while (window.pollEvent(event))
{
// check the type of the event...
switch (event.type)
{
// window closed
case sf::Event::Closed:
...
break;
// mouse button released
case sf::Event::MouseButtonReleased:
{
if (event.mouseButton.button == sf::Mouse::Left)
...
} break;
// we don't process other types of events
default:
break;
}
}

SFML 2.1 is key pressed

In SFML 2.1 on c++, is there a sf::Keyboard function that checks if any letter in the alphabet being pressed?
I know how to check if a certain key is pressed, like this:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key))
Do I have to type all this:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)&&sf::Keyboard::isKeyPressed(sf::Keyboard::B)&&sf::Keyboard::isKeyPressed(sf::Keyboard::C))
...all the way to sf::Keyboard::Z,
Or is there an easier way?
From the SFML website:
Many programmers use the KeyPressed event to get user input, and start to implement crazy algorithms that try to interpret all the possible key combinations to produce correct characters. Don't do that!
sf::Keyboard::isKeyPressed is for checking whether a key is pressed, which is good for things like movement: as long as the player is holding the 'left' key down, we wish to keep moving their character left. A KeyPressed event is for checking which key is pressed at the time it is pressed, which is good for things that happen only once: if the player jumps with the spacebar, we typically don't want them to keep jumping as long as the spacebar is held down.
When it comes to checking entered text, though, neither of these solutions are very good. In the official tutorials, we are advised against using these for checking text input - we have actually been given a different event for exactly this type of situation. The appropriate thing to do is to get the text-based contents of the key via the TextEntered event as described here. Get the text from the event then apply the logic you need to ensure its validity, or (if possible) use the text from the event directly.
Here's a solution with a counter increased every time when a letter key is pressed. window.setKeyRepeatEnabled() must be set to false to avoid getting multiple events while pressing a key:
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML");
int keyCounter = 0;
window.setKeyRepeatEnabled(false);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
else if(event.type == sf::Event::KeyPressed) {
if(event.key.code >= sf::Keyboard::A && event.key.code <= sf::Keyboard::Z) {
keyCounter++;
}
} else if(event.type == sf::Event::KeyReleased) {
if(event.key.code >= sf::Keyboard::A && event.key.code <= sf::Keyboard::Z) {
keyCounter--;
}
} else if(event.type == sf::Event::MouseMoved) {
/* move cursor inside the window to observe how the counter
behaves while pressing the keys */
std::cout << keyCounter << std::endl;
}
}
window.clear();
window.display();
}
return 0;
}
Alternative approach - iterating over enum:
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
} else if(event.type == sf::Event::MouseButtonPressed) {
/* click inside window to see if there is key pressed */
bool isp = false;
for (int i = sf::Keyboard::A; i <= sf::Keyboard::Z; ++i ) {
if(sf::Keyboard::isKeyPressed(static_cast<sf::Keyboard::Key>(i))) {
isp = true;
break;
}
}
std::cout << isp << std::endl;
}
}
window.clear();
window.display();
}
return 0;
}

KeyRelease Event not fired in fullscreen mode

Just like the title says, KeyRelease events aren't fired at all in fullscreen mode on Mac OS X (haven't tested Linux/Windows, may be broken as well).
Here's the code:
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "My Game", sf::Style::Fullscreen, settings);
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
window.setMouseCursorVisible(false);
while (window.isOpen())
{
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
switch (event.key.code) {
case sf::Keyboard::Escape: // Escape pressed : exit
window.close();
break;
default:
game->handleKeyCode(event.key.code);
break;
}
} else if(event.type == sf::Event::KeyReleased) {
printf("KeyRelease Fired!\n");
}
}
}
If I don't use fullscreen mode, and instead initialize the window like so, the KeyRelease event works just fine:
sf::RenderWindow window(sf::VideoMode(1400, 950), "My Game", sf::Style::Default, settings);
A recent commit fix this. Compiling SFML from its git should solve this issue. See the official tutorial for more details about the compilation.