C++ SFML Font loading problems - c++

I am having problems with loading my miranda.ttf file. I have the file in the same directory as main.cc and I am sure I am typing it the right way, but it still doesn't work for some reason. I have tried multiple other fonts without luck.
#include <SFML/Graphics.hpp>
#include "core/headers/util.hpp"
#include "core/headers/ui.hpp"
#include <time.h>
int main(void) {
srand((unsigned)time(NULL));
sf::RenderWindow window(sf::VideoMode(1000, 600, 32U), L"Kolonie Mravenců", sf::Style::Titlebar | sf::Style::Close);
sf::CircleShape circle;
circle.setFillColor(sf::Color::Black);
circle.setPosition(10, 10);
circle.setRadius(6);
sf::Font font;
if (!font.loadFromFile("miranda.ttf")){
return EXIT_FAILURE;
}
sf::Text text;
text.setFont(font);
text.setPosition(sf::Vector2<float>(200.0f, 600.0f / 2.0f));
text.setFillColor(sf::Color(0, 0, 0));
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(227, 227, 227));
window.draw(text);
window.display();
}
return 0;
}
This is the code of main.cc.
I am executing the code using terminal in VS Code Insiders on a Macbook Air.

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.

Visual Studio error in SFML: <Information not available, no symbols loaded for sfml-audio-d-2.dll>

When I run the code, everything works except that the sound does not play. The link is fine, when I put a breakpoint and look at the sound object, it displays . I have added sfml-audio-d.lib and sfml-audio.lib in the right places. Any suggestions welcome.
#include <SFML/Graphics.hpp>
#include "SFML/Audio.hpp"
int main() {
sf::RenderWindow window(sf::VideoMode(600, 600), "SFML Application");
sf::SoundBuffer buffer;
buffer.loadFromFile("magicsound.mp3");
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();
//
sf::Sprite background;
sf::Texture texture;
texture.loadFromFile("crash.jpg");
background.setTexture(texture);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::Resized)
{
sf::FloatRect visibleAre(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleAre)); //background stays default size
//window can be resized without affecting background
}
}
window.clear();
window.draw(background);
window.display();
}
}
When you use quotes for including, ide looks for the library in your project's directory. If your project directory have own sfml library in itself I dont know; but it is like a typo to me.
So try this:
"SFML/Audio.hpp" --> <SFML/Audio.hpp>
and result is:
#include <SFML/Audio.hpp>

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

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

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

SFML Crash using Clear function

I'm new to SFML and am currently reading through the documentation. For some reason, however, my "program" crashes whenever I call the Clear() function. My program code is as follows:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <iostream>
void HandleEvents(sf::RenderWindow &App)
{
sf::Event Event;
while (App.GetEvent(Event)) {
if (Event.Type == sf::Event::Closed) {
App.Close();
exit(0);
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) {
App.Close();
exit(0);
}
}
}
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 640, 32), "SFML Graphics");
App.SetFramerateLimit(60); // cap the framerate at 60 FPS
while (App.IsOpened()) {
HandleEvents(App); // handle events... duh
App.Clear(); // When I remove this line, the program doesn't crash....
App.Display();
}
return EXIT_SUCCESS;
}
So do any SFML gurus have advice for me? I've tried researching this problem but the only other person I've seen who tried to ask this had a broken link (because of the new SFML forums).
Thanks.