-
Hi. I'm using Arch with Nvidia's latest stable driver.
This is my code:
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
int main(){
RenderWindow window(VideoMode(800, 600), "Flappy Bird");
std::cout << "\nWindow created.\n";
std::cout << "\nCreating texture...\n";
Texture texture;
if (!texture.loadFromFile("/assets/textures/image.jpg"))
std::cout << "\nFailed loading texture.\n";
return EXIT_FAILURE;
Sprite sprite(texture);
std::cout << "\nTexture created.\n";
std::cout << "\nCreating font...\n";
Font font;
if (!font.loadFromFile("assets/fonts/arial.ttf"))
std::cout << "\nFailed loading font.\n";
return EXIT_FAILURE;
Text text("Hello SFML world!", font, 50);
std::cout << "\nFont created.\n";
std::cout << "\nCreating music...\n";
Music music;
if (!music.openFromFile("assets/sounds/background.mp3"))
std::cout << "\nFailed loading music.\n";
return EXIT_FAILURE;
std::cout << "\nMusic created...\n";
music.play();
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case Event::Closed:
window.close();
break;
default:
break;
}
}
window.clear();
window.draw(sprite);
window.draw(text);
window.display();
}
// Exit the program successfully...
return EXIT_SUCCESS;
}
When I run this, I get the following output:
Window created.
Creating texture...
Failed to load image "/assets/textures/image.jpg". Reason: Unable to open file
Failed loading texture.
Any ideas? :-\
p.s.: This is my first time posting here, sorry if the formatting is trash.
-
You most likely don't want to use /assets as this will look for a folder in the root directory of you system called assets. For a relative path, you can just use assets/textures/image.jpg.
Then make sure the assets directory in your working directory.
p.s.: This is my first time posting here, sorry if the formatting is trash.
You can use the [code=cpp][/code] tags for code formatting.
-
You most likely don't want to use /assets as this will look for a folder in the root directory of you system called assets. For a relative path, you can just use assets/textures/image.jpg.
Then make sure the assets directory in your working directory.
p.s.: This is my first time posting here, sorry if the formatting is trash.
You can use the [code=cpp][/code] tags for code formatting.
Changing /assets to assets doesn't make any difference, except it now doesn't even output an error for some reason.
this is the output:
Window created.
Creating texture...
%
What does that "%" at the end even mean?
-
Note that this code is wrong:
if (!texture.loadFromFile("/assets/textures/image.jpg"))
std::cout << "\nFailed loading texture.\n";
return EXIT_FAILURE;
without indentation it means:
if (!texture.loadFromFile("/assets/textures/image.jpg"))
std::cout << "\nFailed loading texture.\n";
return EXIT_FAILURE;
So it would also just exit your application.
I'm personally a fan of using curly braces at all times, even or especially for single line statements:
if (!texture.loadFromFile("/assets/textures/image.jpg"))
{
std::cout << "\nFailed loading texture.\n";
return EXIT_FAILURE;
}
Note the same issue for the music
-
Note that this code is wrong:
if (!texture.loadFromFile("/assets/textures/image.jpg"))
std::cout << "\nFailed loading texture.\n";
return EXIT_FAILURE;
without indentation it means:
if (!texture.loadFromFile("/assets/textures/image.jpg"))
std::cout << "\nFailed loading texture.\n";
return EXIT_FAILURE;
So it would also just exit your application.
I'm personally a fan of using curly braces at all times, even or especially for single line statements:
if (!texture.loadFromFile("/assets/textures/image.jpg"))
{
std::cout << "\nFailed loading texture.\n";
return EXIT_FAILURE;
}
Note the same issue for the music
Yes! this fixed my issue; thanks. it's weird how I had never noticed this in C++, I guess because I also usually go with curly braces.
std::cout << "\nCreating texture...\n";
Texture texture;
if (!texture.loadFromFile("assets/textures/image.jpg"))
{
std::cout << "\nFailed loading texture.\n";
return EXIT_FAILURE;
}
Sprite sprite(texture);
std::cout << "\nTexture created.\n";
std::cout << "\nCreating font...\n";
Font font;
if (!font.loadFromFile("assets/fonts/arial.ttf"))
{
std::cout << "\nFailed loading font.\n";
return EXIT_FAILURE;
}
Text text("Hello SFML world!", font, 50);
std::cout << "\nFont created.\n";
std::cout << "\nCreating music...\n";
Music music;
if (!music.openFromFile("assets/sounds/background.mp3"))
{
std::cout << "\nFailed loading music.\n";
return EXIT_FAILURE;
}
std::cout << "\nMusic created...\n";