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.
Related
I tried to upload the image via sfml probably in all possible ways, but I got an error in the console
My code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
sf::Texture texture;
int i=0;
if (!texture.loadFromFile("box.jpg"))
{
std::cout << "Error!";
window.close();
return 1;
}
sf::Sprite sprite;
sprite.setTexture(texture);
sf::Event event=sf::Event();
while (window.isOpen())
{
if (event.type == sf::Event::Closed)
window.close();
window.draw(sprite);
window.clear();
window.display();
}
return 0;
}
Maybe I'm doing something wrong, I'm a beginner c++ and sfml developer.
My settings:
https://i.stack.imgur.com/OF9FA.png
https://i.stack.imgur.com/u2ZSC.png
The file with the picture is in all folders starting from the repos
I dragged the file over all folders in the solution folder, starting from the very first folder ending with the x64 folder, all without success, I searched the question on the Internet, I did not find. \
I tried to move the image to the C folder and specified the path to the image C:\\box.jpg, I got an error in the console:Failed to load image "╨°│╕;☻C:\box.jpg
‼b√⌂Г0b√⌂9Ц ☻ў⌂ ♥X☻ ў⌂Я0b√⌂И·/WJИ·/WJ¶·/WJж╬▀pp↓ut ☻ў⌂)w ☻ў⌂☺p↓│▼√⌂╜О ☻ў⌂☺P?Ь╡;☻└ЙЪ╡;☻`t ☻ў⌂╬u ☻ў⌂Ё ☻ў⌂ Є ☻ў⌂аK♥ў⌂ИK♥ў⌂Оt ☻ў⌂╛w ☻ў⌂¶v╞р√⌂#¶WJб&╢с√⌂0√ ш♦0√ ╨♦↓
You can start from here.
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
sf::Texture texture;
int i = 0;
if (!texture.loadFromFile("box.jpg")) // check file path or you can execute the program from command line to make sure
{
std::cout << "Error!";
window.close();
return 1;
}
sf::Sprite sprite;
sprite.setTexture(texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event)) // poll event
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(); // clear first
window.draw(sprite);
window.display();
}
return 0;
}
If you don't use the absolute file path the folder tree is as follows:
C:\...\SFML
├───SFML // the image file needs to be here to run from IDE
│ └───x64
│ └───Debug
│
└───x64
└───Debug // the image file needs to be here to run from command line
└───Release // the image file needs to be here to run from command line
Who has the same problem go to the project -> properties -> configuration properties -> advanced -> use debugging libraries, by default it is yes, you need to set it to default or "no".
Translated through a translator, the translation may not be accurate
(Sorry for my English)
Make sure the image you want to load is in the same folder as the c++ project file. it doesn't need to be anywhere else but there.
Also make sure your file is named as it is in your program (box.jpg).
You should also move window.clear(); to above window.draw(sprite); or the image wont be displayed.
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.
I am trying to load my texture onto my window.... however when I compile in visual studio on windows 10 laptop It says "sf has no member texture" I'm honestly not sure what on earth is going on! for whatever reason its not loading the sf! if you have any ideas please let me know... thanks so much it means a great deal to me! (I have also tried to use "#include" and it has not worked out for me unfortunately)
#include <SFML/Window.hpp>
int main()
{
sf::Texture texture;
if (!texture.loadFromFile("sheet_1.png"))
{
// error...
}
sf::Window window(sf::VideoMode(800, 600), "My window");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
}
return 0;
}
Since sf::Texture is not included in Window.hpp you need to also include Graphics.hpp in your project.
#include <SFML/Graphics.hpp>
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>
#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;
}
This is directly from the tutorial's page, and I've checked and rechecked, so I know this is not a syntax or linker error.
When I run this program, I get a window whose contents are the same as whatever the place it is located in. Yet, if I remove the window.draw(shape) command, I see a black window, like I should.
I'm compiling on Windows 7 (32 bit) using mingw32-g++.exe (4.7.1). Oh, and it's the same if I compile debug or release and static or dynamic, so that's not the problem either.
Your code is correct, but to me it looks like you need to set the position of your circle. I maybe wrong here but the position could be any value and that is why it isn't displaying.
Could you try updating your code to this please.
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
shape.setPosition(50f, 50f);
shape.setScale(2,2); // You can remove this line if you want to, I just put it there for debugging
while (window.isOpen())
{
...
Here is the SFML documentation for setPosition (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Transformable.php#a4dbfb1a7c80688b0b4c477d706550208)
Here is the SFML documentation for setScale (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Transformable.php#aaec50b46b3f41b054763304d1e727471)
Let me know if that fixes your problem.