2

I'd like to use Meson to build a little game in C++. Let's say that theses are my file:

.
├── img
│   └── img.png
├── meson.buid
└── src
    ├── main.cpp
    └── meson.build

Here are the meson.buid files:

# meson.build
project('mygame', 'cpp')
subdir('src')
pkgdatadir = join_paths(get_option('datadir'), 'mygame')
install_subdir('img', install_dir : join_paths([pkgdatadir, 'img']))

And the second file:

# src/meson.build
executable('mygame', 'main.cpp', install : true)

In my C++ code, what path should I use to load in a portable (relative ?) way (Windows, OS X, Linux) the assets files, given that I may have created a bundled application or installed a (deb) package in the system file hierarchy ?

I would also like the file paths to work when I build with ninja in the build directory without having to install at all the game data.

I thought of adding a define DATA_PREFIX set at compile time, or using an environment variable.

See http://mesonbuild.com/Installing.html and http://mesonbuild.com/Reference-manual.html#install_data.

Thank you.

Antonin Décimo
  • 499
  • 3
  • 17

1 Answers1

1

I thought of adding a define DATA_PREFIX set at compile time

That is the method I would recommend. You can then use configure_file() to output a header containing it:

conf = configuration_data()
conf.set_quoted('PACKAGE_DATADIR', join_paths(get_option('prefix'), pkgdatadir))
configure_file(
  output: 'config.h',
  configuration: conf
)

Then just include config.h in your source.

TingPing
  • 2,129
  • 1
  • 12
  • 15
  • > I would also like the file paths to work when I build with ninja in the build directory without having to install at all the game data. How would I do this ? alter the prefix ? how ? – Antonin Décimo Oct 02 '17 at 15:30
  • 1
    You use a `run_target()` and then in your code check for the env vars `MESON_SOURCE_ROOT` and `MESON_BUILD_ROOT` – TingPing Oct 03 '17 at 17:37