Detect executable folder from SDL - c++

I am creating a C++ SDL game engine, and it is relevant to know the executable path since images and other resources are not stored within the executable - they are in a separated folder("res/").
Under Linux, I am using a shell script "rungame.sh" that cd's to the executable path and then runs the executable(using then "./" to reference the executable folder).
However, I believe this is an "ugly" approach and I want it to be Windows-compatible.
Also, the current approach is not very good as it may change the meaning of some command line arguments.
I want a cross-platform(*NIX, Windows and Mac OS X, if possible) solution to get the current path of the executable. The game path/executable name may change.
What is the cleanest way to solve my problem (preferrably using std::string and as few platform-dependent APIs as possible)?

Since SDL 2.0.1 there's SDL_GetBasePath.

I strongly suggest searching before posting: How do I get the directory that a program is running from?

physicsfs has PHYSFS_getBaseDir():
Get the path where the application resides.
Helper function.
Get the "base dir". This is the directory where the application was
run from, which is probably the installation directory, and may or may
not be the process's current working directory.
You should probably use the base dir in your search path.

Related

how to designate the working directory at run time in C++ in basis of cross platform?

I am confused how I can designate the working directory of "./" at run time.
So sometimes it will be the directory of the project file, sometimes it is the directory of the executable. All dependents how you execute the program.
How should I designate this working directory "./" as I want it always be? I mean I want to keep it always the directory of the executable for example.
And this implementation should be platform independent.(I think it will be rather big hard, so you can ignore this if it's not possible).
If you have a compiler that supports the filesystem Technical Specification or a C++17 compiler you can use the new soon-to-be standard filesystem library
However I don't know of a catch-all way to discover the executable directory. One way that I think should work on Windows and Linux is to use argv[0] which contains the launch command and find its canonical path:
std::error_code ec;
// find the executable directory
auto exe_folder = fs::canonical(fs::path(argv[0]), ec).parent_path();
// set the current directory to the executable directory
fs::current_path(exe_folder);
Currently GCC v6 has filesystem as the Technical Specification otherwise you can also use boost::filesystem on-which it is based.
Typically programs don't have write permissions to the directory that contains the executable.
myself found a solution if anyone else is interested. you have to have Qt on your computer at first.
you can change the working directory at run time with:
QDir::setCurrent(qApp->applicationDirPath());
This will change the working directory at run time to that of the executable.
But it does not look very neat cause you have to have Qt.
The best solution I've come up with (without requiring a C++17 compiler) is if you can guarantee a data-driven file is going to reside next to your executable, you can place a similarly named file in your project directory.
"./[your file].txt" is always going to exist, and you can use the content of these files as an indicator of where your current working directory is.

How to specify a standard directory in a Qt project file

I have developed an application that I plan to deploy on Windows, Mac, and Linux. The program requires access to some files (scripts and the like) at run-time.
The installation process should install the files to a location that my application can later determine without user-intervention (or perhaps just a prompt allowing the user to change the location, if desired).
What is the best way to achieve this? I can't seem to find any way to:
1. Use a "standardized path" variable in the project file's INSTALLS statement. (e.g., my application could use QStandardPaths to initialize the location, but I can't figure out how to access this path from the INSTALLS statement)
2. Save the path to my project's QSettings (.plist, registry, whatever) for later retrieval
That leaves me with creating a custom project file and INSTALLS command for each environment, and then I still can't install to the user's directory because I don't know the user's name when I deploy the make command. It seems as if there must be a better way, but I can't seem to find any documentation for this. Am I just using the wrong keywords in my searches? Thanks in advance!
What standard directory? What type of getting that standard directory?
For instance, you can put such thing in your windows branch of .pro file:
win32 {
APPDATA_DIR = $$system(echo %APPDATA%) # should be %LOCALAPPDATA% as requested
message($$APPDATA_DIR)
}
Just unsure of what exact kind of standartized path you are talking about. QStandardPaths knows many. It makes sense to be more concrete to find the correspondence with concrete OS.
Also somewhat relative reply on mine, on how to check the correspondence with certain variable, etc: Qt .pro file - how to add conditioning on OSX version?
Maybe this class will help you
QStandardPaths documentation
But your problem is still little bit unclear for me.

Running a process inside a virtual file system?

What I'm trying to achieve is to run a program, which thinks a folder exists within its own folder while actually the folder is somewhere else on the system.
So my program would launch a process and say to the process: Folder A which is at C:\A is within your own directory at C:\Program Files (x86)\SomeProgram\A
So the "virtual" directory would only be visible to that process.
I'm using Qt to program my program, so if there are any Qt functions I could use that would be great (in relation to portability). However, plan C++ or any windows-bound API's would be fine.
I was thinking about NTFS junctions or symbolic links but I would have no idea how to create either of those in C++, let alone bind them to a specific process.
Thanks in advance!
EDIT:
In relation to the above, I've found this question: https://superuser.com/questions/234422/does-windows7-support-symbolic-links-folder-shortcuts. However, it only shows how to perform the required actions from the command-line and it wouldn't be process bound.
EDIT 2:
Some extra information: I'm trying to create a virtual directory that is made up of a couple of other directories but then merged (I'm using a priority system to decide which files "win" from other files). These merged directories would then appear to the target process as one directory containing the merged files.
I think I'm going to stick with Window's mklink command. It seems to suit my needs the best.
What I'm going to do is use QFile::link() on all operating systems that aren't Windows, and QProcess with mklink on windows. This should work on every operating system.
For a good example look here: https://stackoverflow.com/a/21013935/979732
Such tasks are accomplished by use of a filesystem filter driver. The driver intercepts OS requests going to the filesystem and lets you insert your own virtual files and directories into the existing directory on the disk. Filter driver can be an overkill for your particular task, though.
Detours approach mentioned in comments requires system-wide hooking of file APIs and will slowdown the whole system(filesystem filter driver is attached to one disk and it's a documented approach, so it's faster and more robust).

Good practice for implementing resource directories

I'm not sure if this is too general, so if it is I'll say that I'm on Linux using qmake, but I'd like to be able to switch from Linux to Windows with my project whenever I need to, as well as, possibly other PCs.
In order to do this, I'd like to know how some of the programmers on here have gotten around using resource directories without using absolute path definitions. With Qt, it seems like the runtime working directory is the build directory of the application, and not the source directory.
Ideally, I think the best solution would be to somehow get the Resource directory as it resides in the source directory and copy that to the relative build directory (i.e., Debug or Release, depending on development stage) so that the application can access that via run time.
This can introduce some complication, however (at least, I think it can).
Anyway, what would be a good solution to do this?
If you are using Qt. I would suggest using deploy process.
http://doc.qt.digia.com/qtcreator/creator-building-running.html
Basically, you just need to declare which directories need to be copied.
The qt creator will copy those dirs to build dir(release/debug) after build process is done.Then you simply run the executable.
Here is one of example.
https://github.com/longwei/incubator-cordova-qt.
in the pro file
wwwDir.source = www
xmlDir.source = xml
qmlDir.source = qml
DEPLOYMENTFOLDERS = wwwDir xmlDir qmlDir
second
include(deployment.pri)
qtcAddDeployment()
then it is done.
Its not clear what exactly you're trying to achieve, but perhaps a simple solution would be for the build scripts to pass the necessary path via a compilation definition (-D with gcc). Then depending on if its a Debug, Release, etc build, the definition would be set accordingly, then the corresponding binary would have the correct path.
As a side note, I tried qmake for a while, but found SCons to be much more versatile.

I can't go to a directory with C++

I need to change working directory of my project, so that output files go to a certain folder, not where all the project files are.
I'm using
system("cd secretdir/");
system("ls");
However, what I get, is the list of files in current project directory, not the "secretdir" one.
I'm on Mac OS X 10.6/Qt Creator 4.7 64 bit
Thanks!
You have to change the current working directory
http://www.linuxquestions.org/questions/programming-9/how-to-change-current-working-directory-in-c-550031/
Also, you should consider saving your output files with full path names instead of changing the working directory.
Your current code will spawn a subshell that will change its current directory to ./secretdir, then proceed to exit() without doing anything else.
Only then will ls run in another subshell, whose current directory is, of course, completely independent of what you did during your previous call to system().
That's probably where your problem lies. Are you looking for the chdir() function?
chdir("secretdir");
// From now on, the current directory of the process is `./secretdir`.
system("ls"); // Will probably behave as expected.
edit See Falmarri's response as I glossed over the first sentence of your question.
You can also use chdir
the following is crufty
The first system spawns a new process that does the cd. The second system spawns a completely different process that doesn't know what happened previously.
One thing you could do is:
system("ls secretdir/");
I'd highly recommend checking out QDir, QFile, and QProcess objects in the QT Creator help or online documentation since you are using it. They have very detailed and easy to understand documentation and using the tools available to you in QT should be a primary reason for choosing that tool much of QT rivals boost in portability and usability in my limited experience.
Also there is a great community for QT related questions at QTForum worth bookmarking especially if QT Creator is your primary development environment.
Using system should be avoided as general rule of thumb it is inefficient and insecure in many cases.
EDIT: Sorry I too glossed over your first sentence and jumped to the code bits. You can modify the project settings via the Projects tab in QT Creator to add a Custom Process step to the build where you can specify a working directory and then do a copy command to wherever you would like your output to go. You also may be able to specify a build output option within your .pro file directly ... once again the help and documentation is your friend however.
The function on Mac OSX is chdir("./secretdir"), although since it's a POSIX API it actually works the same on many other platforms as well.
Using system() is not portable so try to avoid to use directly "cd" like that. My advice is to use Boost filesystem.
There is a Two-minutes Tutorial !
Do
system("cd secretdir/; ls");
Or better yet use boost's filesystem library. Maybe just opendir.