Day 79/100 100 Days of Code

Chris DourisChris Douris
2 min read

This session was all about creating a Mac executable file and move the resources inside it.

I updated the CMakeLists.txt file to create a MacOS bundle file during the build process and copy the app's resources into the container.

add_subdirectory("${PROJECT_SOURCE_DIR}/modules/SDL")
add_subdirectory("${PROJECT_SOURCE_DIR}/modules/SDL_ttf")
add_subdirectory("${PROJECT_SOURCE_DIR}/modules/cpplocate")

find_package(SDL3_ttf REQUIRED CONFIG REQUIRED COMPONENTS SDL3_ttf)
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
find_package(liblocate REQUIRED CONFIG REQUIRED COMPONENTS liblocate)

file(GLOB_RECURSE MINI_APP "./modules/jumpcalculations/*")
file(GLOB APP_FONT "./fonts/Montserrat-VariableFont_wght.ttf")

add_executable(    ${PROJECT_NAME} MACOSX_BUNDLE ./src/main.c
                ./src/app.h ./src/app.c ./src/resourcelocator.h
                ./src/resourcelocator.c ./src/text.h ./src/text.c
                ${MINI_APP} ${APP_FONT})

foreach(APPFILE ${MINI_APP})

    # Get the folder and the file name
    file(RELATIVE_PATH RES_PATH_NAME "${CMAKE_CURRENT_SOURCE_DIR}" ${APPFILE})

    # keep only the name of the folder
    get_filename_component(NEW_FILE_PATH_NAME ${RES_PATH_NAME} DIRECTORY)

    # Add current file and directory to the resources folder
    set_property(SOURCE ${APPFILE} PROPERTY MACOSX_PACKAGE_LOCATION "Resources/${NEW_FILE_PATH_NAME}")

endforeach(APPFILE)

# Get the folder and the file name
file(RELATIVE_PATH RES_PATH_NAME "${CMAKE_CURRENT_SOURCE_DIR}" ${APP_FONT})

# keep only the name of the folder
get_filename_component(NEW_FILE_PATH_NAME ${RES_PATH_NAME} DIRECTORY)

# Add current file and directory to the resources folder
set_property(SOURCE ${APP_FONT} PROPERTY MACOSX_PACKAGE_LOCATION "Resources/${NEW_FILE_PATH_NAME}")

I added one more conditional statement check for the Fortran mini-app submodule.

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/modules/jumpcalculations/app/main.f90")
    message(FATAL_ERROR "Jump calculations mini app was not downloaded properly. GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

I then modified the FindResource function to locate the bundle path rather than the executable path.

char* FindResource(char *fileLocation)
{
    char *execPath; 
    unsigned int lengthValue = 100;
    getBundlePath(&execPath, &lengthValue);
    printf("Path %s\n", execPath);

    strcat(execPath, fileLocation);
    return execPath;
}

The application now finds the font inside the application successfully with a minor change.

// This is the path from the bundle file to the font file
fontResource.location = "/Contents/Resources/fonts/Montserrat-VariableFont_wght.ttf";
0
Subscribe to my newsletter

Read articles from Chris Douris directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chris Douris
Chris Douris

AKA Chris, is a software developer from Athens, Greece. He started programming with basic when he was very young. He lost interest in programming during school years but after an unsuccessful career in audio, he decided focus on what he really loves which is technology. He loves working with older languages like C and wants to start programming electronics and microcontrollers because he wants to get into embedded systems programming.