SFML Can't Load Image - c++

I am trying to load an image with SFML, here is my code.
// Get the background texture
Texture bg;
if (!bg.loadFromFile("background.jpg"))
{
return EXIT_FAILURE;
}
Sprite background;
background.setTexture(bg);
background.setPosition(width / 2, height / 2);
and in the main loop:
window.draw(background);
Now I have included the background.jpg in every single folder for this solution, starting from C:/users/username/source/repos/thisSolution. However I am still getting the failed to load image error, is there some arbitrary folder where VS2017 would be looking for this file?

As #Arnav Broborah pointed, by default with Visual Studio, resources should be located into your project folder (where .vcxproj file is)
If you have created your project with "Create directory for solution", the
resulting hierarchy will be something like:
C:\Users\username\Documents\Visual Studio 2013\Projects\YourSolution\YourProject
Obviously, those names depends on the drive you installed VS, username, VS version, etc, but your resource files should go into the project folder by default.

You can change it in debug settings

Related

How can I load an image to QPixmap without specifying the full path?

I'm using Qt Creator on Windows and trying to add an image to an icon.
The only way I can get it to work is by specifying the full path:
QPixmap newIcon("C:/Users/slipn/Documents/qt/projetos/exemplo/new.png");
newToolBarAction = toolbar->addAction(QIcon(newIcon), "New File");
I tried adding to the project as shown below:
The image and the code files are in the same directory. I have no clue of what path I should use to make it work. Any hints?
The problem you need to solve, is that your source path and the path where you build or maybe later even install your application are not the same. You need to bundle your image resource with your application, either by copying & installing it to your build/install, or by including it directly in your application binary.
The latter is your easiest bet with qmake. Installing files with qmake is (relatively) easy, but making your build result work without a separate make install step is a different thing.
Your easiest bet to solve both "running your build result directly" and "running the installed application" is to use the Qt Resource System.
Create a .qrc file with File > New File > Files and Classes > Qt > Qt Resource File, add a "prefix" (some name), add your image file there, and then use QPixmap newIcon(":/yourprefix/new.png") to access it in your application.

SFML "failed to create the font face" issue

I'm using SFML in Visual Studio 2015 to make a game that requires me to print text. I try to load fonts and keep getting an error that says "failed to create the font face". I've tried loading several different fonts and none of them work, and they are in the correct directory, which is the folder where my project is located.
This is literally all it is, and it doesn't work:
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
return EXIT_FAILURE;
The ttf file for arial is in the same folder as the project itself, which is what seems to solve the problem for everyone else I find online who has the same issue. Any idea why the font still won't load?
The simple answer is, the file is not in your current working directory when you run your executable.
Try to put in a fully qualified filename. It will work.
Using Visual Studio, most likely this is the directory where your .vcxproj file resides.
If this does not work, you can find out what your current working directory is by checking this post for a generic way how to find out your current directory.
As a quick hack, you could just create a file when your program starts. Start once and check where the file gets created. This is the current directory.
I fixed with this:
sf::Font font;
if (!font.loadFromFile("../arial.ttf")){
return EXIT_FAILURE;
}
I'm using CLion on Ubuntu using SMFL also.
To add to this, I was experiencing this same error when I thought I'd had everything typed properly. What ultimately ended up being the issue was that I had written .tff for the font file rather than .ttf, which, once corrected, fixed the problem.
My TTF file had no permissions once extracted from its archive. Executing chmod 644 font.ttf fixed the problem.

How to add an image to a C++ project in Xcode

I would like to add an image to my C++ project in Xcode so that I can read that image and do something with it. How do I include the image into my project?
tried copy pasting both into my project and into the folder with my c++ source.
In Xcode 10, I can't put the image in the same folder as the executable, and I don't think it's a good idea anyway as this folder gets deleted when a clean is performed.
The solution I found when dealing with "Command Line Tool" projects in Xcode, is to modify or create a new Copy File Phase (and not a Copy Bundle Resources one) in Targets > Build Phases. But the default values should be modified:
Destination: Resources
Subpath: Nothing
Uncheck Copy only when installing
The image should then be added to this phase (using the + button). Beforehand, when adding the image to the project, selecting a target is not necessary, adding it to the Copy Files Phase will do it automatically.
The code will then look like this:
std::string filename("input.bmp");
bitmap_image image(filename);
Put the image in the same folder with the executable i.e. with the file in the Products folder.

Change Xcode c++ application external file location

I am first time using Xcode and C++
I wrote a program which has to load some images. I changed the working directory in the Xcode so the files are located correctly.
But after I build the project and compile it as an application, the external file directory is changed to "usr/[myuser]" (i used char * dir = getcwd(NULL, 0); to get the path)
Does any has an idea how to change the file directory?
Or how can I include these files in the project?
Click on the bar above scheme
Click edit scheme
Checkmark Custom working directory
Choose your directory of choice
Files in this directory are now accessible by the project

Why is SDL not loading my images when main application is accessed via shortcut?

I have recently created a program using C++ and the SDL library. Originally, all the images and DLLs were in the same folder as the main application. I wanted to make the main application easier to find, so I instead moved all the images into their own folder and modified my source code to find them in the correct folder. So far, so good. The main application still remains in the same folder as the DLLs.
I created a shortcut to the main application and put that it's own folder. I changed the target to %windir%\system32\cmd.exe /q/c start "" ""%SystemRoot%\explorer.exe %UserProfile%\Desktop\Cupcake Corner Build 2.0 (exe test)\dependecies\Cupcake Corner.exe" in an attempt to make a universal shortcut, so that the shortcut would work for any user I send the files to.
After all said and done, I've ran into a problem. If I open the main application itself, it will load the images and work perfectly. If I try to the main application via its shortcut, the window will open correctly and display the correct title, although none of the images will load. I tried moving some files around to see if the shortcut would see them if I put the resource folder elsewhere, but so far no luck.
images here -> C:\Users\Admin\Desktop\Cupcake Corner Build 2.0\dependecies\resources
main app/dlls here -> C:\Users\Admin\Desktop\Cupcake Corner Build 2.0\dependecies\
shortcut here -> C:\Users\Admin\Desktop\Cupcake Corner Build 2.0\
I am using Microsoft Visual Studio Express 2010. I think it has something to do with the way I have my project directory set in project options, but I'm honestly baffled at this point. Can anyone point me in the right direction?
Generally, when dealing with files in an application (that the application knows internally, at least), you will want to have absolute paths.
The best way to solve this is by having a (set of) function(s) that add the absolute path (found by GetModuleFilename(0, ...) or from some configuration for example in the registry)
std::string myfiles_root_path = FindMyFilesRootPath();
std::string MakeFullPath(const std::string &filename)
{
return myfiles_root_path + filename;
}
The function FindMyFilesRootPath() uses one of the methods above to find the appropriate path, and filename is the name of a file local to your application.