Day 44/100 100 Days of Code

Day 44/100 100 Days of Code

Rebuild Back Better

I focused on working on the main menu today and some window settings.

Full-Screen Window

The application now starts on full screen. I did it, by adding SDL_WINDOW_FULLSCREEN as the last argument of the SDL_CreateWindow() function.

window = SDL_CreateWindow("Rebuild Back Better",
                          SCREEN_WIDTH,
                          SCREEN_HEIGHT,
                          SDL_WINDOW_FULLSCREEN);

Getting the width and height of the window

Getting the size of the window is quite easy. The function SDL_GetWindowSize() can be used to get the values. The variables that hold the values of the window's size are passed as arguments with their memory address location to assign a value to them directly.

SDL_GetWindowSize(window, &windowWidth, &windowHeight);

Loading Fonts Function

I used a similar technique to the SDL_GetWindowsize to apply a value to a bool variable. The variable is used to check if loading the font is successful. The application exits if it fails. The second argument is a string with the URL to the TTF font file and the last argument is the font's size.

TTF_Font *Game::LoadFont(bool *hasFontLoaded, std::string urlToFont, unsigned int fontSize)
{
    TTF_Font *font = TTF_OpenFont(urlToFont.c_str(), fontSize);
    if (font == nullptr)
    {
        std::cout << "Failed to create font ";
        std::cout << SDL_GetError() << std::endl;
        *hasFontLoaded = false;
        return nullptr;
    }
    *hasFontLoaded = true;
    return font;
}

Main Menu Selection

The color of the menu options should be changed based on the current selection and if the continue option is available. I created an enumerator to handle the states and an integer variable to hold its value.

enum MenuOptionSelection
{
    ContinueSelected = 0,
    StartSelected,
    ExitSelected
};

static int currentMainMenuSelection;

It is important to remember that I will need to use the cursor for the menu selection.

Game State Switch

On the Main_Menu, I load the menu's fonts first and then, run the MainMenu() method to build the menu.

if (!hasLoadedTitleFont)
{
        titleFont = LoadFont(&hasLoadedTitleFont, 
                             "/Users/chrisd/Desktop/Rebuild Back Better/fonts/ArianaVioleta-dz2K.ttf",
                             100);    

        if (!hasLoadedTitleFont)
        {
            std::cout << "Title font has not loaded" << std::endl;
            exit(-1);
        }
}

if (!hasLoadedMenuFont)
{
    menuFont = LoadFont(&hasLoadedMenuFont,
                        "/Users/chrisd/Desktop/Rebuild Back Better/fonts/CfArpineDemoRegular-q2Zr2.ttf",
                        60);

    if (!hasLoadedMenuFont)
    {
        std::cout << "Menu font has not loaded" << std::endl;
        exit(-1);
    }
}

LoadMainMenu();

MainMenu() Method

I changed the game's title color, added the color values that are going to be used for the options, and created a switch to set the color of each option accordingly. It will be also used to handle selections.

void Game::LoadMainMenu()
{
    SDL_Color titleTextcolor = {0xE0, 0xAA, 0x95};

    // Title
    titleTextSurface = TTF_RenderText_Solid(titleFont,
                                            "Rebuild Back Better",
                                            titleTextcolor);

    if (titleTextSurface == nullptr)
    {
        std::cout << "Failed to create title texture surface ";
        std::cout << SDL_GetError() << std::endl;
        exit(-1);
    }

    titleTextTexture = SDL_CreateTextureFromSurface(renderer, titleTextSurface);

    const SDL_FRect titleHolder = {static_cast<int>((float)SCREEN_WIDTH * 0.27),
                            static_cast<int>((float)SCREEN_HEIGHT * 0.05),
                            static_cast<float>(titleTextSurface->w),
                            static_cast<float>(titleTextSurface->h)};

    SDL_RenderTexture(renderer, titleTextTexture, nullptr,
                      &titleHolder);

    SDL_DestroySurface(titleTextSurface);
    titleTextTexture = nullptr;

    // Menu Options
    SDL_Color notSelected = {0xff, 0xff, 0xff};
    SDL_Color disabledOption = {0x10, 0x10, 0x10};
    SDL_Color selectedOption = {0xE0, 0xAA, 0x95};

    switch (currentMainMenuSelection)
    {
        case ContinueSelected:
            break;

        case StartSelected:
            break;

        case ExitSelected:
            break;
    }
}

Tomorrow I will use the window width and height values to place the title and finish the menu options.