4

I am trying to make a little game for practice in C++. I am using the Clion IDEA which uses CMake for it's build system.

So I have this project set up which uses the Allegro game library and I can get it working fine for now with me CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(my-game)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)

# Set executeable
add_executable(game ${SOURCE_FILES})

#Include Allegro
include_directories(/usr/include/allegro5/)
link_directories(/usr/include/allegro5/)

#connect all the libraries you need
set(game_LIBS
        liballegro.so
        liballegro_dialog.so
        liballegro_image.so
        liballegro_font.so
        liballegro_ttf.so
)
target_link_libraries(game ${game_LIBS})

Now the problem is that in my project root folder I have this folder called assets and inside of it I would like to store my assets. For now there is a fonts folder in there which contains a arial.ttf file which I would like to use to display some text in my game.

Now the line I would use to get this asset and use it in my application should be something similar:

ALLEGRO_FONT *font24 = al_load_ttf_font("/assets/fonts/arial.ttf", 24, 0);

But for some reason when I launch the application and I start to use that font24 pointer, I get this error:

game: /build/buildd/allegro5-5.0.10/addons/font/text.c:77: al_draw_ustr: Assertion `font' failed.

Now if I use this line instead:

ALLEGRO_FONT *font24 = al_load_ttf_font("/home/kaspar/Documents/Coding-projects/ClionProjects/my-game/assets/fonts/arial.ttf", 24, 0);

Then the application will be able to print the text with the desired font just fine.

So what must I do so that I would be able to reference my assets in the project more easily? I imagine that pointing to a file with it's full system path is not really a healthy practice..

starball
  • 20,030
  • 7
  • 43
  • 238
Kaspar
  • 1,600
  • 4
  • 24
  • 46
  • On an unrelated note, if you plan to distribute this, consider the copyright of the font. Many fonts cannot be distributed due to copyright. – Steve Oct 05 '15 at 20:26
  • It will be used just for learning :) But thanks for the warning! – Kaspar Oct 05 '15 at 20:28
  • 2
    Forward slash represents the [root directory](http://www.linfo.org/root_directory.html). `/assets` is likely looking for assets in the root of your computer. You will need to use relative paths and make sure the assets are copied with the bundle. – Joe Oct 05 '15 at 20:32
  • How would I go about copying the assets into my bundle? – Kaspar Oct 05 '15 at 20:35
  • 2
    Just assume that they are present in your application's working directory : `ALLEGRO_FONT *font24 = al_load_ttf_font("assets/fonts/arial.ttf", 24, 0);`, e.g. without the first `/`. If this file doesn't exist, it means that the installation is somehow incomplete and you can't continue with loading the program. – Daniel Kamil Kozar Oct 05 '15 at 20:50
  • Okay, If I moved the application into my root project folder then it worked fine now. But when I compile to program, all my output is created into a seperate folder called Debug. Is there a way to have CMake copy the assets there when making the compilation? – Kaspar Oct 05 '15 at 20:57
  • Check out this repo, you can use this as a reference https://github.com/SasLuca/raylib-cmake-template – Cyber Avater Apr 14 '22 at 07:55

0 Answers0