Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How do Games Manage Resource Files?  (Read 6988 times)

0 Members and 2 Guests are viewing this topic.

arnavb

  • Newbie
  • *
  • Posts: 18
    • View Profile
How do Games Manage Resource Files?
« on: July 05, 2018, 01:14:13 am »
TBH, this question doesn't necessarily to SFML specifically, but to any program that requires a resource file.

Say I have a game, compiled into a program game.exe. Said game has a line in its code:

std::ifstream ifs{ "important_data.lua" };

Now obviously, in order for the game to work, this executable needs the resource file important_data.lua in the same directory as the executable. This is quite easy to manage on Windows and MacOSX, where applications are bundled so that they have a folder dedicated to their own applications. (C:\Program Files\program_name\... and on OSX executable/Contents/...). However, I don't understand how this works on Linux. Now obviously, the executable gets installed to /usr/local/bin or /usr/bin. Now where does the resource file go? Would it go to bin, where any other application in the directory has access to it, or somewhere else? I would think it would go /usr/share, but then the application wouldn't run because ifstream would be unable to access it. Where should this resource file be placed for my game?

If It's relevant, I'm using CMake to manage everything related to my game.
« Last Edit: July 05, 2018, 01:16:24 am by arnavb »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How do Games Manage Resource Files?
« Reply #1 on: July 05, 2018, 11:06:54 am »
If it's just about the location, you can prefix all paths with a configurable location - macOS already requires you (afaik) to specify a ResourcePath variable/class, so in theory that could be reused for all platforms.

Then all you need to handle OS specific is the location of the config file.
I'm not a big Linux user, so I'm unsure what the file philosophy is, but it certainly isn't to put it in the bin directory. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

arnavb

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How do Games Manage Resource Files?
« Reply #2 on: July 05, 2018, 02:36:13 pm »
Thanks for the reply, but unfortunately I still don't have a complete understanding of how this works.... ;(


I did some research, and found this: https://askubuntu.com/a/27257
I guess that answers the question about where these things are stored on Linux.

But a question still remains: How does an application "configure" the ResourcePath, as you mentioned?

Also, on another side note: I've installed various high-end games on my PC before. I've gone into C:\Program Files\Application before. How come I don't see hundreds of sprites and sound files and script files in these directories? I doubt these games don't need external files, but they must be stored _somewhere_, right? So, how do video games specifically manage their resource files?


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How do Games Manage Resource Files?
« Reply #3 on: July 05, 2018, 03:06:34 pm »
Most games package their resources into one file often of a proprietary format. For example you might find huge files, those contain sounds, textures and more.

The resource path could simply be a string, so for Windows you use "./" for relative paths, for macOS ypu can provide the path needed for the bundle and for Linux you can use the path mentioned in the linked QA.
The path differences are achived with different configuration files per platform. As such you only have to "hardcode" where the config file is found on each platform.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Daid

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: How do Games Manage Resource Files?
« Reply #4 on: July 05, 2018, 10:45:51 pm »
A lot of applications handles this by having a compile-time prefix before all the resource file names. Empty on Windows, but /usr/share/application_name/ on Linux.

Not sure for OSX, stuff there changes from time to time, and there are different methods used. The linux method used to work, not sure if it still does.



However, I use a different method. My engine has something called "resource providers". You can register multiple of them, and if you request a filename, it goes trough all the providers and find the one path that can provide this resource.
Example with a lot of paths registered:
https://github.com/daid/EmptyEpsilon/blob/master/src/main.cpp#L130
So I can search relative to the current working directory, and in a fixed location at the same time. I have even an implementation that can get resources from a zip file without the user of the code noticing.

arnavb

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How do Games Manage Resource Files?
« Reply #5 on: July 06, 2018, 01:43:43 am »
@Daid, that is somewhat helpful, but I'm still lost on how to set RESOURCE_BASE_DIR. I was looking in your CMakeLists.txt, and I see this:

Code: [Select]
# install resources, scripts, and packs to app bundle instead of system dir.
if(APPLE)
set(CMAKE_INSTALL_PREFIX "./")
elseif(UNIX)
# Set RESOURCE_BASE_DIR on Unix so the built binary is able to find resources
add_definitions(-DRESOURCE_BASE_DIR="${CMAKE_INSTALL_PREFIX}/share/emptyepsilon/")
endif()

Now this seems fine at first glance, but I don't think this would work.
Suppose I had an source directory with a CMake file, and I did mkdir build, cd build, and cmake .. on a unix system (A basic out-of-source build). And now I wanted to run this app. However, since the RESOURCE_BASE_DIR is set to /usr/share/emptyepsilon, the game is going to look for resources there, when they aren't there in the first place. How owuld that work?

Daid

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: How do Games Manage Resource Files?
« Reply #6 on: July 06, 2018, 08:57:23 pm »
If you would run "make install", then the resources are placed in /usr/share/emptyepsilon/

If you only want to run an out-of-source build, then you would do it like this (I do this all the time):
mkdir _build
cd _build
cmake ..
make -j 5
cd ..
./_build/EmptyEpsilon
 

As my resourceprovider system looks in both the compiled prefixed path AND the current working directory, it can find the files due to the current working directory.

arnavb

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How do Games Manage Resource Files?
« Reply #7 on: July 09, 2018, 02:45:48 pm »
Sorry, this must be me being naive, but I _still_ don't understand how this works. I can't even find the definition for DirectoryResourceProvider. Daid, is it possible you could provide me with a minimal CMake script that configures RESOURCE_BASE_DIR like for your project?

Daid

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: How do Games Manage Resource Files?
« Reply #8 on: July 19, 2018, 06:06:23 pm »
The define is configured as quoted above.
The ResourceProvider code is at https://github.com/daid/SeriousProton/blob/master/src/resources.h https://github.com/daid/SeriousProton/blob/master/src/resources.cpp

It's not documented. But it also isn't that complex if you combine it with the other link to the main function I provided before. It pretty much gives a method of looking for files in multiple locations, without a lot of fuzz for the user of the code.