Jumping Ball Progress

Jumping Ball Progress

Blog Post 2

I finally figured out how to fix the font path crash. Using const wasn't enough, as many crashes suddenly appeared. Using a char array fixed the font path problem and also resolved a random segmentation fault I was experiencing.

Also, I discovered a really cool tool for dmg file creation. I updated the CMakeLists.txt file to move the dynamic libraries to the .app bundle and added an icon too!

The problem with char* is that it points to the address of the first character and then the rest of the string can by found by going through each address after the first one until it meets a termination character. What does this might mean?

Since, the char* variable is not an array and points to the first character, the rest of the string can be mutated without a reason. I converted the const char* location to char location[1024] array. I created a temporary char* to assign the path that the FindResource returns and then run it through a for loop to copy it.

// Resource struct
typedef struct Resource
{
   char location[1024];
}Resource;

// Init app function

char *temp = "/Contents/Resources/fonts/Montserrat-VariableFont_wght.ttf";

for (int i = 0; i < strlen(temp); i++)
{
    fontResource.location[i] = temp[i]; 
}

// The for loop doesn't add the termination character
// I had to add it "manually"
fontResource.location[strlen(temp)] = '\0';        

To move the dylib files of each library inside the .app Frameworks folder, I used the same technique that I used for the assets that are being copied inside the .app bundle. The application does not search for the libraries inside the .app bundle. I need to find a way to fix this.

file(GLOB_RECURSE SDL_LIB_FILES "./modules/SDL/build/*.dylib")
file(GLOB_RECURSE SDL_TTF_LIB_FILES "./modules/SDL_ttf/build/*.dylib")
file(GLOB_RECURSE LIBLOCATE_LIB_FILES "./modules/cpplocate/build/*.dylib")

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
                ./src/results.h ./src/results.c ./src/textbox.h 
                ./src/textbox.c ./src/ball.h ./src/ball.c 
                ./src/button.h ./src/button.c ${APP_FONT} ${MACOS_ICON}
                ${SDL_LIB_FILES} ${SDL_TTF_LIB_FILES} ${LIBLOCATE_LIB_FILES})

# include SDL library to the bundle
foreach(RESOURCE ${SDL_LIB_FILES})
    file(RELATIVE_PATH RES_PATH_NAME "${CMAKE_CURRENT_SOURCE_DIR}" ${RESOURCE})
    get_filename_component(NEW_FILE_PATH_NAME ${RES_PATH_NAME} DIRECTORY)
    set_property(SOURCE ${RESOURCE} PROPERTY MACOSX_PACKAGE_LOCATION "Frameworks/${NEW_FILE_PATH_NAME}")
endforeach()
# include SDL ttf library to the bundle
foreach(RESOURCE ${SDL_TTF_LIB_FILES})
    file(RELATIVE_PATH RES_PATH_NAME "${CMAKE_CURRENT_SOURCE_DIR}" ${RESOURCE})
    get_filename_component(NEW_FILE_PATH_NAME ${RES_PATH_NAME} DIRECTORY)
    set_property(SOURCE ${RESOURCE} PROPERTY MACOSX_PACKAGE_LOCATION "Frameworks/${NEW_FILE_PATH_NAME}")
endforeach()
# include cpplocate library to the bundle
foreach(RESOURCE ${LIBLOCATE_LIB_FILES})
    file(RELATIVE_PATH RES_PATH_NAME "${CMAKE_CURRENT_SOURCE_DIR}" ${RESOURCE})
    get_filename_component(NEW_FILE_PATH_NAME ${RES_PATH_NAME} DIRECTORY)
    set_property(SOURCE ${RESOURCE} PROPERTY MACOSX_PACKAGE_LOCATION "Frameworks/${NEW_FILE_PATH_NAME}")
endforeach()        

For the icon, I simply copied the resource to the Resources folder and added the icon as a property using CMakeLists.txt

file(GLOB MACOS_ICON "./packaging assets/img/Jumping_Ball_Logo.icns")

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
                ./src/results.h ./src/results.c ./src/textbox.h 
                ./src/textbox.c ./src/ball.h ./src/ball.c 
                ./src/button.h ./src/button.c ${APP_FONT} ${MACOS_ICON}
                ${SDL_LIB_FILES} ${SDL_TTF_LIB_FILES} ${LIBLOCATE_LIB_FILES})

# Set macos Icon
file(RELATIVE_PATH RES_PATH_NAME "${CMAKE_CURRENT_SOURCE_DIR}" ${MACOS_ICON})
get_filename_component(NEW_FILE_PATH_NAME ${RES_PATH_NAME} DIRECTORY)
set_property(SOURCE ${MACOS_ICON} PROPERTY MACOSX_PACKAGE_LOCATION "Resources/${NEW_FILE_PATH_NAME}")
set_target_properties(${PROJECT_NAME} PROPERTIES MACOSX_BUNDLE_ICON_FILE "Contents/Resources/packaging assets/img/Jumping_Ball_Logo.icns")        

For the dmg creation, I used a really cool that I found on GitHub. Special thanks to LinusU for the awesome tool!

Demonstration


要查看或添加评论,请登录

Chris Douris的更多文章

  • Jumping Ball Progress Report 4

    Jumping Ball Progress Report 4

    Completing the Windows version Creating an installer for my program was elementary. I only needed to install an…

  • MotionMedia - An Introduction

    MotionMedia - An Introduction

    What is MotionMedia? MotionMedia is a command line application made with C++ for the Raspberry Pi. The program will use…

  • Jumping Ball - Progress Report 3

    Jumping Ball - Progress Report 3

    Working on MacOS and Windows Versions I updated the MacOS source to code use char arrays instead of char* to fix memory…

  • Jumping Ball - Progress Report 2

    Jumping Ball - Progress Report 2

    Progress with the windows version I worked on the completing the input logic for the Jumping Ball application. Figuring…

  • Jumping Ball

    Jumping Ball

    Progress Report 1 Working on the windows version Just as expected. More weird character string memory problems.

  • Rebuild Back Better - Progress Report 1

    Rebuild Back Better - Progress Report 1

    Upgrade Buttons Upgrades are a very important feature for any clicker game that exists. They give a sense of…

  • Rebuild Back Better

    Rebuild Back Better

    Progress Report 1 I am happy to start working on RBB again. This time, I added the background image to the Main Menu…

  • Jumping Ball Progress

    Jumping Ball Progress

    Blog Post 3 I need to post more updates on my progress. I learned how to use CMake's install features to create an…

  • Jumping Ball Progress

    Jumping Ball Progress

    Blog Post 1 It took me quite a bit to figure out how to handle the ball's behaviour. Using print statements and…

  • Day 100/100 100 Days of Code

    Day 100/100 100 Days of Code

    This is the last day of 100 days of code. For the last day I watched tutorials and read about multithreading with C…

社区洞察

其他会员也浏览了