src/fade.c
author Tampax86
Thu, 17 Jul 2025 22:03:19 -0400
changeset 0 b2e3aa63e96c
permissions -rw-r--r--
First commit

/*
Copyright (c) 2023 MCL Software

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "common.h"

Uint8 alpha;
bool fadeInProgress = 0;
SDL_TimerID fadeTimer = 0;

Uint32 fadeIn(Uint32 interval)
{
    alpha -= 1;
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, alpha);

    if(alpha == 0)
    {
        SDL_RemoveTimer(fadeTimer);
        return 0;
    }

    return interval;
}

Uint32 fadeOut(Uint32 interval)
{
    alpha += 1;
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, alpha);

    if(alpha == 255)
    {
        SDL_RemoveTimer(fadeTimer);
        return 0;
    }

    return interval;
}

bool ISOL_FadeIn(int interval)
{
    if(fadeInProgress && alpha > 0)
    {
        SDL_RenderFillRect(renderer, &(SDL_Rect){0, 0, SCREEN_WIDTH, SCREEN_HEIGHT});
        //SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
        return 0;
    }

    else if(fadeInProgress)
    {
        fadeInProgress = 0;
        return 1;
    }

    else
    {
        fadeInProgress = 1;
        alpha = 255;
        fadeTimer = SDL_AddTimer(interval, fadeIn, 0);
        return 0;
    }
}

bool ISOL_FadeOut(int interval)
{
    if(fadeInProgress && alpha < 255)
    {
        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
        SDL_RenderFillRect(renderer, &(SDL_Rect){0, 0, SCREEN_WIDTH, SCREEN_HEIGHT});
        return 0;
    }

    else if(fadeInProgress)
    {
        fadeInProgress = 0;
        return 1;
    }

    else
    {
        fadeInProgress = 1;
        alpha = 0;
        fadeTimer = SDL_AddTimer(interval, fadeOut, 0);
        return 0;
    }
}