Day 49/100 100 Days of Code

Day 49/100 100 Days of Code

Rebuild Back Better

Today I worked on the main menu.

I added all the menu options. The application now destroys all of its components on exit. I removed 2 variables that were not needed. I must find a new font for the main menu's options and create a new method because there is repeating code in the MainMenu method.

Repeating Code

There is repeating code in the method that builds the main menu. I should build another one that adds text elements to the page.

    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);

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

    const SDL_FRect titleHolder = {static_cast<float>(windowWidth * 0.31),
                            static_cast<float>(windowHeight * 0.05),
                            static_cast<float>(titleTextSurface->w),
                            static_cast<float>(titleTextSurface->h)};

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

    SDL_DestroySurface(titleTextSurface);
    titleTextTexture = nullptr;
    SDL_DestroyTexture(titleTextTexture);
    titleTextTexture = nullptr;

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

    continueGameSurface = TTF_RenderText_Solid(menuFont,
                                              "Continue",
                                              disabledOption);
    if (continueGameSurface == nullptr)
    {
        std::cout << "Failed to create continue game text surface ";
        std::cout << SDL_GetError() << std::endl;
        exit(-1);
    }

    continueGameTexture = SDL_CreateTextureFromSurface(renderer, continueGameSurface);

    if (continueGameTexture == nullptr)
    {
        std::cout << "Failed to create continue game texture ";
        std::cout << SDL_GetError() << std::endl;
        exit(-1);
    }

    const SDL_FRect continueTextHolder = {static_cast<float>(windowWidth * 0.4),
                                          static_cast<float>(windowHeight * 0.4),
                                          static_cast<float>(continueGameSurface->w),
                                          static_cast<float>(continueGameSurface->h)};

    SDL_RenderTexture(renderer, continueGameTexture, nullptr, &continueTextHolder);

    SDL_DestroySurface(continueGameSurface);
    continueGameSurface = nullptr;
    SDL_DestroyTexture(continueGameTexture);
    continueGameTexture = nullptr;

    startGameSurface = TTF_RenderText_Solid(menuFont,
                                            "Start",
                                            notSelected);
    if (startGameSurface == nullptr)
    {
        std::cout << "Failed to create start game surface ";
        std::cout << SDL_GetError() << std::endl;
        exit(-1);
    }

    startGameTexture = SDL_CreateTextureFromSurface(renderer, startGameSurface);

    if (startGameTexture == nullptr)
    {
        std::cout << "Failed to create start game texture ";
        std::cout << SDL_GetError() << std::endl;
        exit(-1);
    }

    const SDL_FRect startTextHolder = {static_cast<float>(windowWidth * 0.4),
                                       static_cast<float>(windowHeight * 0.5),
                                       static_cast<float>(startGameSurface->w),
                                       static_cast<float>(startGameSurface->h)};     

    SDL_RenderTexture(renderer, startGameTexture, nullptr, &startTextHolder);        

    SDL_DestroySurface(startGameSurface);
    startGameSurface = nullptr;
    SDL_DestroyTexture(startGameTexture);
    startGameTexture = nullptr;

    exitGameSurface = TTF_RenderText_Solid(menuFont,
                                            "Exit",
                                            notSelected);
    if (exitGameSurface == nullptr)
    {
        std::cout << "Failed to create exit game surface ";
        std::cout << SDL_GetError() << std::endl;
        exit(-1);
    }

    exitGameTexture = SDL_CreateTextureFromSurface(renderer, exitGameSurface);

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

    const SDL_FRect exitTextHolder = {static_cast<float>(windowWidth * 0.405),
                                       static_cast<float>(windowHeight * 0.6),
                                       static_cast<float>(exitGameSurface->w),
                                       static_cast<float>(exitGameSurface->h)};     

    SDL_RenderTexture(renderer, exitGameTexture, nullptr, &exitTextHolder);        

    SDL_DestroySurface(exitGameSurface);
    exitGameSurface = nullptr;
    SDL_DestroyTexture(exitGameTexture);
    exitGameTexture = nullptr;

Yikes