Day 52/100 100 Days of Code

Day 52/100 100 Days of Code

Rebuild Back Better

Hooray for weekends! I made some good progress today.

I implemented a new method to destroy the main menu, included the future library for asynchronous asset loading, removed some old code because it was not needed anymore, installed the sdl_mixer library, and learned something new about templates.


The DestroyMainMenu method transitions the game state from the main menu to the gameplay. Methods make the code easier to read and easier to handle.

void Game::DestroyMainMenu()
    currentGameState = GAMEPLAY;
    menuTitle = nullptr;
    menuStart = nullptr;
    menuContinue = nullptr;
    menuExit = nullptr;
    menuFontsLoaded = false;
    haveElementsLoaded = false;

I included the future library to use std::async to load assets. Using multiple threads will make asset loading much faster.

To use std::async is pretty easy. First, we create a std::future variable and call the async method. Then, we need to call the method wait() of the future variable and then we can use the get() method to get the returning value of the function.

2 policies can be used when launching a function with std::async. The first one is std::launch::deferred which doesn't run on a new thread. The other one is std::launch::async which runs on a new thread. Both can be used with the wait() and get() methods.

if (!menuFontsLoaded)
    auto getTitleFont = std::async(std::launch::async, LoadFont, 
                                   execpath + std::string("/Contents/Resources/fonts/ArianaVioleta-dz2K.ttf"),

    auto getMenuFont = std::async(std::launch::async, LoadFont, 
                                  execpath + std::string("/Contents/Resources/fonts/CfArpineDemoRegular-q2Zr2.ttf"),

    titleFont = getTitleFont.get();
    menuFont = getMenuFont.get();

    if (titleFont == nullptr)
        std::cout << "Title font has not loaded" << std::endl;

    if (menuFont == nullptr)
        std::cout << "Menu font has not loaded" << std::endl;

    menuFontsLoaded = true;

Then, I removed a lot of variables that were not needed anymore and added the SDL_mixer library to handle audio. I need to add the image library to handle the game's graphics.

What I learned

I tried removing the template from the MenuElement struct because I thought it might still work without it. Of course, it didn't work because I have not included the SDL3 library in the file where the MenuElement struct is defined. This means that the compiler won't be able to find the SDL code definitions.

That's for now! I should add some sounds and install the image library next. I should also create the menu's background image.