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.
Related
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.
I am running Xcode 11.5 on my mac OS X Catalina 10.15.3. I did all the steps to set up SFML in Xcode, and created a project with the standard SFML app template, using SFML from dylibs instead of frameworks (I couldn't get the app to build when I created it with frameworks). I haven't edited the code from the template, which contains this is main.cpp:
//
// Disclaimer:
// ----------
//
// This code will work only if you selected window, graphics and audio.
//
// Note that the "Run Script" build phase will copy the required frameworks
// or dylibs to your application bundle so you can execute it on any OS X
// computer.
//
// Your resource files (images, sounds, fonts, ...) are also copied to your
// application bundle. To get the path to these resources, use the helper
// function `resourcePath()` from ResourcePath.hpp
//
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
// Here is a small helper for you! Have a look.
#include "ResourcePath.hpp"
int main(int, char const**)
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "icon.png")) {
return EXIT_FAILURE;
}
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return EXIT_FAILURE;
}
sf::Sprite sprite(texture);
// Create a graphical text to display
sf::Font font;
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
return EXIT_FAILURE;
}
sf::Text text("Hello SFML", font, 50);
text.setFillColor(sf::Color::Black);
// Load a music to play
sf::Music music;
if (!music.openFromFile(resourcePath() + "nice_music.ogg")) {
return EXIT_FAILURE;
}
// Play the music
music.play();
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite);
// Draw the string
window.draw(text);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
The app successfully builds, but the window won't open and I get following in my Threads tab:
I've read that SIGABRT can be caused by naming errors and such. I'm not that familiar with assembly so I was wondering if anyone can see the mistake here.
I am trying a simple SFML project and it is giving me an error saying that it cannot load the image file. Here is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
int main() {
RenderWindow window(VideoMode(600, 600), "Chess");
system("dir");
Texture texture;
texture.loadFromFile("board.png");
Sprite s(texture);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//draw
window.clear();
window.draw(s);
window.display();
}
}
I have tried every guide and fix for this problem I could find and not a single one of them has worked. Would anyone be able to help me? And yes, my image files are in the project directory. If it helps, I am using VS 2013.
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>
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...