aboutsummaryrefslogtreecommitdiff
path: root/modules/librender/g_fade.c
blob: 1fcafbe8b96c19d5c86222c439b2fa9e8d1aeaa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 *  Copyright � 2006-2016 SplinterGU (Fenix/Bennugd)
 *  Copyright � 2002-2006 Fenix Team (Fenix)
 *  Copyright � 1999-2002 Jos� Luis Cebri�n Pag�e (Fenix)
 *
 *  This file is part of Bennu - Game Development
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty. In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *     1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *     2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *     3. This notice may not be removed or altered from any source
 *     distribution.
 *
 */

#include <string.h>
#include <stdlib.h>

#include "librender.h"

/* -------------------------------------------------------------------------- */

SDL_Color vpalette[ 256 ] ;

int fade_inc = 0 ;
int fade_on = 0 ;
int fade_set = 0 ;
int fade_step = 64 ;

SDL_Color fade_from ;
SDL_Color fade_to ;
SDL_Color fade_pos = { 100, 100, 100 };

/* -------------------------------------------------------------------------- */

static void activate_vpalette()
{
    int n ;

    if ( !screen ) return ;

    if (( sys_pixel_format ) && ( sys_pixel_format->palette ) )
    {
        if ( sys_pixel_format->depth > 8 )
        {
            for ( n = 0 ; n < 256 ; n++ )
                sys_pixel_format->palette->colorequiv[ n ] = SDL_MapRGB( screen->format, sys_pixel_format->palette->rgb[ n ].r, sys_pixel_format->palette->rgb[ n ].g, sys_pixel_format->palette->rgb[ n ].b ) ;
        }
        else
        {
            for ( n = 0 ; n < 256 ; n++ )
            {
                if ( fade_pos.r <= 100 )
                    vpalette[ n ].r = sys_pixel_format->palette->rgb[ n ].r * fade_pos.r / 100;
                else
                    vpalette[ n ].r = sys_pixel_format->palette->rgb[ n ].r + ( 255 - sys_pixel_format->palette->rgb[ n ].r ) * ( fade_pos.r - 100 ) / 100;

                if ( fade_pos.g <= 100 )
                    vpalette[ n ].g = sys_pixel_format->palette->rgb[ n ].g * fade_pos.g / 100;
                else
                    vpalette[ n ].g = sys_pixel_format->palette->rgb[ n ].g + ( 255 - sys_pixel_format->palette->rgb[ n ].g ) * ( fade_pos.g - 100 ) / 100;

                if ( fade_pos.b <= 100 )
                    vpalette[ n ].b = sys_pixel_format->palette->rgb[ n ].b * fade_pos.b / 100;
                else
                    vpalette[ n ].b = sys_pixel_format->palette->rgb[ n ].b + ( 255 - sys_pixel_format->palette->rgb[ n ].b ) * ( fade_pos.b - 100 ) / 100;
            }

            if ( scale_screen )
                SDL_SetColors( scale_screen, vpalette, 0, 256 ) ;
            else
                SDL_SetColors( screen, vpalette, 0, 256 ) ;
        }
    }
}

/* -------------------------------------------------------------------------- */

void gr_fade_init( int r, int g, int b, int speed )
{
    if ( fade_pos.r == r && fade_pos.g == g && fade_pos.b == b )
    {
        GLODWORD( librender, FADING ) = 0 ;
        fade_on = 0 ;
        return ;
    }

    fade_inc = speed;
    fade_step = 0;
    fade_on = 1 ;
    fade_from = fade_pos;
    fade_to.r = ( r > 200 ) ? 200 : r ;
    fade_to.g = ( g > 200 ) ? 200 : g ;
    fade_to.b = ( b > 200 ) ? 200 : b ;

    GLODWORD( librender, FADING ) = 1 ;
}

/* -------------------------------------------------------------------------- */

void gr_fade_step()
{
    if ( fade_on )
    {
        fade_set = 1 ;
        GLODWORD( librender, FADING ) = 1 ;

        fade_step += fade_inc ;
        if ( fade_step < 0 )
        {
            GLODWORD( librender, FADING ) = 0 ;
            fade_step = 0 ;
            fade_on = 0 ;
        }

        if ( fade_step >= 64 )
        {
            GLODWORD( librender, FADING ) = 0 ;
            fade_step = 64 ;
            fade_on = 0 ;
        }

        fade_pos.r = ( fade_to.r * fade_step + fade_from.r * ( 64 - fade_step ) ) / 64;
        fade_pos.g = ( fade_to.g * fade_step + fade_from.g * ( 64 - fade_step ) ) / 64;
        fade_pos.b = ( fade_to.b * fade_step + fade_from.b * ( 64 - fade_step ) ) / 64;

        if (
            ( fade_step + fade_inc < 0 || fade_step + fade_inc > 64 ) &&
            ( fade_pos.r == 100 && fade_pos.g == 100 && fade_pos.b == 100 ) )
        {
            GLODWORD( librender, FADING ) = 0 ;
            fade_step = 100 ;
            fade_on = 0;
        }
    }

    if ( fade_set )
    {
        if ( !fade_on && fade_to.r == 100 && fade_to.g == 100 && fade_to.b == 100 ) fade_set = 0;

        activate_vpalette() ;

        if ( scrbitmap->format->depth > 8 )
        {
            gr_fade16( scrbitmap, fade_pos.r, fade_pos.g, fade_pos.b );
        }
    }
}

/* -------------------------------------------------------------------------- */