C++ SFML library code not working for drawing a text - c++

I am feeling really frustrated with draw() not working in my SFML project. My compiler gives off no errors, my eyes doesn't catch a thing that's off (as a reference I am using official tutorial). The problem is that when window load it doesn't draw a thing. It just stays a white window without any text in it.
Where could be the problem?
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800,600), "Trying to make a game");
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
{
//error
}
sf::Text text;
text.setFont(font);
text.setString("Hello, World!");
text.setCharacterSize(50);
text.setColor(sf::Color::Red);
text.setPosition(10, 50);
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;
}

You're closing the window before drawing the text...

Related

SFML - failed to create the font face

I'm working on a C++ project in WSL environment. The project includes SFML library. I have to use XLaunch to open SFML windows. I'm trying to render text by loading a font from a .ttf file. I get
Failed to load font "Graphics/fonts/fff-forward-regular.ttf" (failed to create the font face)
Here's my code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(1600, 900), "Dungeon Crawler", sf::Style::Close);
sf::RectangleShape quit_button(sf::Vector2f(300, 100));
quit_button.setOrigin(150, 50);
quit_button.setPosition(800, 600);
quit_button.setFillColor(sf::Color::Magenta);
sf::RectangleShape start_button(sf::Vector2f(360, 120));
start_button.setOrigin(180, 60);
start_button.setPosition(800, 300);
start_button.setFillColor(sf::Color::Cyan);
sf::Font pixel_font;
pixel_font.loadFromFile("Graphics/fonts/fff-forward-regular.ttf");
sf::Text quit_text("Quit", pixel_font, 30);
quit_text.setOrigin(quit_text.getLocalBounds().width/2, quit_text.getLocalBounds().height/2);
quit_text.setPosition(quit_button.getPosition());
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(quit_button);
window.draw(start_button);
window.draw(quit_text);
window.display();
}
return 0;
}
The SFML window opens and everything else seems to work just fine, but rendering fonts don't work. I have checked that the path of the .ttf is correct. Can't figure out what's causing this.

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
}
}

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).