Day 64/100 100 Days of Code

Day 64/100 100 Days of Code

Rebuild Back Better

I spent the session fixing all the errors that occurred by introducing the AudioPlayer object to the project.

The AudioPlayer class doesn't have a variadic template anymore because I do not need to use 2 different functions to Play an audio file.

I initially thought it would be a good idea to have one function that can fade in an audio clip and another that doesn't offer this feature. However, this isn't necessary because the fade-in length can simply be set to 0!

Additionally, I included a conditional statement to prevent the audio from playing again if it is already playing.

// FROM THIS
void AudioPlayer<Mix_Chunk>::PlayAudio(T *audioFile, Types... types)
{
    constexpr std::size_t getSize = sizeof...(types);
    va_list args;
    va_start(args, types);

    switch(getSize)
    {
        case 1:
            result =  Mix_PlayChannel(-1,    audioFile,    va_arg(args, 0));    

            if (result    ==    -1)
            {
                exit(-1);
            }

            break;
        case 2:
            result = Mix_FadeInChannel(-1,    audioFile,     va_arg(args, 0), 
                                                        va_arg(args, 1));

            if (result == -1)
            {
                exit(-1);
            }
            break;

        default:
            break;
    }
}

// TO THIS

template<>
void AudioPlayer<Mix_Chunk>::PlayAudio(int loops, int fadeInTime)
{
    if (Mix_Playing(result) == 0)
    {
        result = Mix_FadeInChannel(-1,    audioFile, loops, fadeInTime);
    }
}

Then, I instantiated an AudioPlayer Object in the TextElement class to add audio capabilities to the TextElement class.

template<typename S, typename T, typename C, typename F, typename R, typename AUD>
struct TextElement
{
    enum ElementType
    {
        TITLE = 0,
        CONTINUE,
        START,
        EXIT
    };

    float x, y;
    int currentState;
    float width, height;
    bool isEnabled;
    bool hasSoundPlayed;
    std::string content;
    F *font;
    R *renderer;
    C notHovered = {0xbb, 0xbb, 0xbb};
    C disabledOption = {0xAA, 0xAA, 0xAA};
    C hoverOption = {0xE0, 0xAA, 0x95};
    C titleColor = {0xE0, 0xAA, 0x95};
    AUD *sfxPlayer;

    TextElement(float inputX, float inputY, F *inputFont, R *inputRender, std::string getAudioPath);
    TextElement(float inputX, float inputY, F *inputFont, R *inputRender);
    void CreateTextElement(std::string content, int elementType);
    void CreateTextElement(std::string content, float getMouseX, float getMouseY, int elementType);
    bool IsMouseHovering(float inputMouseX, float inputMouseY);
};

I overloaded the TextElement constructor to have the option to not play a sound if the Text Element is static and not interactive. The constructor now assigns an AudioPlayer class to the sfxPlayer if there's a sound to play when the text is hovered on the other side, a nullptr is assigned if there is no sound to be played.

template<>
TextElement<SDL_Surface, SDL_Texture, SDL_Color, TTF_Font, SDL_Renderer, AudioPlayer<Mix_Chunk>>::TextElement(float inputX, float inputY,
                                                                        TTF_Font *inputFont, SDL_Renderer *inputRender,
                                                                        std::string getAudioPath): sfxPlayer {new AudioPlayer<Mix_Chunk>(getAudioPath)}
{
    x = inputX;
    y = inputY;
    font = inputFont;
    renderer = inputRender;
    hasSoundPlayed = false;
}

template<>
TextElement<SDL_Surface, SDL_Texture, SDL_Color, TTF_Font, SDL_Renderer, AudioPlayer<Mix_Chunk>>::TextElement(float inputX, float inputY,
                                                                        TTF_Font *inputFont, SDL_Renderer *inputRender) : sfxPlayer {nullptr}
{
    x = inputX;
    y = inputY;
    font = inputFont;
    renderer = inputRender;
}

Demonstration