SFML textEntered not working properly - c++

so today I started looking into SFML and I found it quite interesting so decided to learn how to play with it, but I am already hitting some problems, I am trying to use textEntered event, but it is not working properly, it shows complete nonsense and text writes itself even without me pressing any key. Heres link
Code
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!");
std::string display;
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text;
text.setFont(font);
text.setCharacterSize(30);
text.setStyle(sf::Text::Bold);
text.setColor(sf::Color::Red);
text.setPosition(50, 50);
while (window.isOpen())
{
sf::Event Revent;
while (window.pollEvent(Revent))
{
if (sf::Event::TextEntered)
{
std::cout << static_cast<char>(Revent.text.unicode);
//text.setString(display);
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
window.clear();
//window.draw(text);
window.display();
}
return 0;
}

You wrote if (sf::Event::TextEntered), which evaluates to true (since it's not equal to 0).
You probably meant
if (Revent.type == sf::Event::TextEntered).
Using Revent.text is undefined behavior in this case (when you're not sure what type of event Revent contains) because sf::Event is an union, so only one of its members is usable at a time. You can read more about SFML events here.

Related

how do you use a draw function in a different function to the one in which the window was opened?

I am working on a basic game in C++ using SFML for graphics. It is designed to use a grid system with functions to determine what should be displayed in each square. However, the compiler won't recognize the references to the window in the functions.
To keep it easier to expand there are functions for each type of terrain to be displayed, taking the coordinates as inputs (possibly not the correct term).
Good Question. SFML recommends that you create your window in the main function, but, if you want to modify it, you can simply pass it by reference. EX:
#include <SFML/Graphics.hpp>
void doSomething(sf::RenderWindow& window) {
sf::RectangleShape shape(sf::Vector2f(100, 100));
shape.setFillColor(sf::Color::Green);
shape.setPosition(50, 50);
window.draw(shape);
window.display();
}
int main() {
sf::RenderWindow window(sf::VideoMode(500, 500), "Test", sf::Style::Close);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
//draw here
doSomething(window);
}
return 0;
}
You should take a look at https://www.sfml-dev.org/tutorials/2.5/. Specifically I would look at https://www.sfml-dev.org/tutorials/2.5/window-window.php

Trouble condensing code in C++ - SFML

This is probably going to sound like a stupid question...
I have this piece of code in C++, that uses the SFML library:
#include <SFML/Graphics.hpp>
#include <string>
void textDisplay(sf::Text& display_text, std::string display_str, sf::Font display_font, int char_size) {
display_text.setString(display_str);
display_text.setFont(display_font);
display_text.setCharacterSize(char_size);
display_text.setStyle(sf::Text::Regular);
}
int main() {
sf::RenderWindow window(sf::VideoMode(400, 400), "Game");
sf::Font arial;
if (!arial.loadFromFile("fonts/arial/arial.ttf")) {
return 0;
}
sf::Text text;
textDisplay(text, "Hello, World!", arial, 30);
sf::CircleShape shape(100.f, 10);
shape.setFillColor(sf::Color::Yellow);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
window.draw(text);
window.display();
}
return 0;
}
Because I'm going to repeat the same four lines of code over and over when I'm trying to display the text onto an SFML screen, rather than repeating the code, I created a function (textDisplay) that takes in four arguments (the Text object itself, a string, a font, and the character size) and initializes the Text object for me.
For some reason, the text doesn't display - is there anything I'm doing wrong?
Some stuff:
The window is called window.
There's no error when compiled, so I have no idea where the error is.
The board itself is just black, there's no text on window.
If the error has to do with the function itself, can you recommend an alternative to typing the same four lines over and over again?
Found it, you were setting your text with a temporary font, since its a normal variable and not a reference/pointer:
void textDisplay(sf::Text& display_text, std::string display_str, sf::Font& display_font, int char_size) { //Just make it a reference and its done! :)
display_text.setString(display_str);
display_text.setFont(display_font);
display_text.setCharacterSize(char_size);
display_text.setStyle(sf::Text::Regular);
}

SFML window.draw(); only shows up for a small time

I'm attempting to get a picture to display by using SFML (just a test run). The program can find the picture, and open a new window, but when it opens the window it only pops up for half a second then returns with 1. Here is the code (which is just their example that I tweaked):
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
sf::Texture Texture;
sf::Sprite Sprite;
if(!Texture.loadFromFile("resources/pepe.png"));
return 1;
Sprite.setTexture(Texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(Sprite);
window.display();
}
return 0;
}
I am assuming the error is coming from the return 1; after the loading, but I don't see what is wrong. Can someone post something that worked for them or give me tips on what may be going wrong?
Your code works just fine, except for the ; after the texture loading from a file, making your program always return 1, whatever was happening before.
It's a good idea to add error messages to know what's going wrong.
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
sf::Texture Texture;
sf::Sprite Sprite;
if(!Texture.loadFromFile("resources/pepe.png")){ // there was a ; here.
// making the code below always run.
std::cerr << "Error loading my texture" << std::endl;
return 1;
}
Sprite.setTexture(Texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed){
window.close();
}
// you only get here when there is at least one event.
}
// but you always want to display to the screen.
window.clear();
window.draw(Sprite);
window.display();
}
return 0;
}
My rule of thumb is to always enclose code blocks with curly braces so you never make these kind of mistakes (or someone else changing your code is less prone to make that mistake).

C++ SFML Draw to other window

I know how to draw text to window created with sf::RenderWindow(); but I need to draw text to already existing game window. It makes it go black, I don't want to erase whole window, just update text on it. Code:
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Font.hpp>
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
HWND hWindow = FindWindow(0, "Counter-Strike: Global Offensive");
if(!hWindow) exit(0);
else
{
int i=0;
string tmp, str;
sf::RenderWindow window(hWindow);
sf::Font font;
if(!font.loadFromFile("verdana.ttf"))
{
cout << "error";
}
sf::Text text;
text.setFont(font);
text.setCharacterSize(17);
text.setColor(sf::Color::White);
while (window.isOpen())
{
i++;
itoa(i, (char*)tmp.c_str(), 10);
str = tmp.c_str();
text.setString(str);
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
Sleep(1000);
window.clear(sf::Color::Transparent);
window.draw(text);
window.display();
}
return 0;
}
}
Please help me do it :)
I think it won't be as easy as you might thought.
Every program I know which can manipulate the backbuffer of a game hooks the API calls. That isn't very difficult. There are some good librarys out there for this. But the real problem is that nearly every game use DirectX.
SFML use OpenGL. I don't think they can be mixed.
You should use Direct2D or another drawing libary based on DirectX.
There are even tutorials out there which explains how to code a overlay. Just use Google.

C++ sfml receives close event just after window opens

I am trying to follow a SFML tutorial from this link, however, for some reason my application seems to be caling the close event as soon as my program enters into the event loop.
Here is my code.
#include <SFML/Graphics.hpp>
#include <iostream>
// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
int main()
{
sf::RenderWindow Window(sf::VideoMode(500,400),"SFML tutorial");
/* Or we can do this
* sf::RenderWindow Window
* Window.create (sf::VideoMode(800,600),"SFML tutorial");
*/
//Game loop
while(Window.isOpen())
{
sf::Event Event;
while(Window.pollEvent(Event));
{
if(Event.type == sf::Event::Closed)
{
Window.close();
std::cout << "Close" << std::endl;
}
}
}
return 0;
}
For some reason as soon as my program enters the event loop it receives a sf::Event::Closed event. I have done some research and this is not because the window is too big for my screen.
To fix my issue I have had to ad a int variable canClose to 0 then modify the event loop to this.
if(Event.type == sf::Event::Closed)
{
if(canClose == 1)
{
Window.close();
} else {
canClose = 1;
}
}
After I do this the window now opens and runs as expected.
P.S this is made using xcode and the SFML template.
Edit:
Just made a new xcode SFML project and removed some code in it to make a empty window and it seems to whork. Code looks exactly the same. This seams strange because it is exactly the same code.
From what i have seen, do not use an if statement for checking events, use a switch statement. Also Use a while(window.isOpen()) when trying to poll events
Have you read through the tutorials on http://www.sfml-dev.org ? Because i can guarantee that it is the best source for learning sfml. Use it before using another source.
Your problem is that you declared the variable Event Event type, you have to change the name of the variable to lowercase.
in this way
#include <SFML/Graphics.hpp>
#include <iostream>
// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
int main()
{
sf::RenderWindow Window(sf::VideoMode(500,400),"SFML tutorial");
/* Or we can do this
* sf::RenderWindow Window
* Window.create (sf::VideoMode(800,600),"SFML tutorial");
*/
//Game loop
while(Window.isOpen())
{
sf::Event event;
while(Window.pollEvent(event));
{
if(event.type == sf::Event::Closed)
{
Window.close();
std::cout << "Close" << std::endl;
}
}
}
return 0;
that way you will not have any problems