forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_utils.cpp
219 lines (180 loc) · 6.67 KB
/
sdl_utils.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#if defined(TILES)
#include "sdl_utils.h"
#include <array>
#include <utility>
#include "color.h"
#include "color_loader.h"
#include "cursesport.h"
#include "debug.h"
#include "sdltiles.h"
color_pixel_function_map builtin_color_pixel_functions = {
{ "color_pixel_none", nullptr },
{ "color_pixel_darken", color_pixel_darken },
{ "color_pixel_sepia", color_pixel_sepia },
{ "color_pixel_grayscale", color_pixel_grayscale },
{ "color_pixel_nightvision", color_pixel_nightvision },
{ "color_pixel_overexposed", color_pixel_overexposed },
{ "color_pixel_zoverlay", color_pixel_z_overlay },
};
color_pixel_function_pointer get_color_pixel_function( const std::string &name )
{
const auto iter = builtin_color_pixel_functions.find( name );
if( iter == builtin_color_pixel_functions.end() ) {
debugmsg( "no color pixel function with name %s", name );
return nullptr;
}
return iter->second;
}
SDL_Color adjust_color_brightness( const SDL_Color &color, int percent )
{
if( percent <= 0 ) {
return { 0x00, 0x00, 0x00, color.a };
}
if( percent == 100 ) {
return color;
}
return SDL_Color{
static_cast<Uint8>( std::min( color.r *percent / 100, 0xFF ) ),
static_cast<Uint8>( std::min( color.g *percent / 100, 0xFF ) ),
static_cast<Uint8>( std::min( color.b *percent / 100, 0xFF ) ),
color.a
};
}
SDL_Color mix_colors( const SDL_Color &first, const SDL_Color &second, int second_percent )
{
if( second_percent <= 0 ) {
return first;
}
if( second_percent >= 100 ) {
return second;
}
const int first_percent = 100 - second_percent;
return SDL_Color{
static_cast<Uint8>( std::min( ( first.r * first_percent + second.r * second_percent ) / 100, 0xFF ) ),
static_cast<Uint8>( std::min( ( first.g * first_percent + second.g * second_percent ) / 100, 0xFF ) ),
static_cast<Uint8>( std::min( ( first.b * first_percent + second.b * second_percent ) / 100, 0xFF ) ),
static_cast<Uint8>( std::min( ( first.a * first_percent + second.a * second_percent ) / 100, 0xFF ) )
};
}
SDL_Color color_pixel_grayscale( const SDL_Color &color )
{
if( is_black( color ) ) {
return color;
}
const Uint8 av = average_pixel_color( color );
const Uint8 result = std::max( av * 5 >> 3, 0x01 );
return { result, result, result, color.a };
}
SDL_Color color_pixel_nightvision( const SDL_Color &color )
{
const Uint8 av = average_pixel_color( color );
const Uint8 result = std::min( ( av * ( ( av * 3 >> 2 ) + 64 ) >> 8 ) + 16, 0xFF );
return {
static_cast<Uint8>( result >> 2 ),
static_cast<Uint8>( result ),
static_cast<Uint8>( result >> 3 ),
color.a
};
}
SDL_Color color_pixel_overexposed( const SDL_Color &color )
{
const Uint8 av = average_pixel_color( color );
const Uint8 result = std::min( 64 + ( av * ( ( av >> 2 ) + 0xC0 ) >> 8 ), 0xFF );
return {
static_cast<Uint8>( result >> 2 ),
static_cast<Uint8>( result ),
static_cast<Uint8>( result >> 3 ),
color.a
};
}
SDL_Color color_pixel_darken( const SDL_Color &color )
{
if( is_black( color ) ) {
return color;
}
// 85/256 ~ 1/3
return {
std::max<Uint8>( 85 * color.r >> 8, 0x01 ),
std::max<Uint8>( 85 * color.g >> 8, 0x01 ),
std::max<Uint8>( 85 * color.b >> 8, 0x01 ),
color.a
};
}
SDL_Color color_pixel_sepia( const SDL_Color &color )
{
if( is_black( color ) ) {
return color;
}
/*
* Objective is to provide a gradient between two color points
* (sepia_dark and sepia_light) based on the grayscale value.
* This presents an effect intended to mimic a faded sepia photograph.
*/
const SDL_Color sepia_dark = { 39, 23, 19, color.a};
const SDL_Color sepia_light = { 241, 220, 163, color.a};
const Uint8 av = average_pixel_color( color );
const float gammav = 1.6;
const float pv = av / 255.0;
const Uint8 finalv =
std::min( static_cast<int>( std::round( std::pow( pv, gammav ) * 150 ) ), 100 );
return mix_colors( sepia_dark, sepia_light, finalv );
}
SDL_Color color_pixel_z_overlay( const SDL_Color &color )
{
if( static_z_effect ) {
return mix_colors( color, {128, 255, 255, color.a}, color.a / 8 );
}
return { 128, 255, 255, color.a };
}
SDL_Color curses_color_to_SDL( const nc_color &color )
{
const int pair_id = color.to_color_pair_index();
const auto pair = cata_cursesport::colorpairs[pair_id];
int palette_index = pair.FG != 0 ? pair.FG : pair.BG;
if( color.is_bold() ) {
palette_index += color_loader<SDL_Color>::COLOR_NAMES_COUNT / 2;
}
return windowsPalette[palette_index];
}
std::vector<SDL_Color> color_linear_interpolate( const SDL_Color &start_color,
const SDL_Color &end_color, const unsigned additional_steps )
{
const unsigned steps = additional_steps + 2;
std::vector<SDL_Color> colors;
colors.reserve( steps );
colors.push_back( start_color );
const float step = 1.0f / ( steps - 1 );
for( unsigned i = 0; i < steps - 2; i++ ) {
const float percent = ( i + 1 ) * step;
const Uint8 r = static_cast<Uint8>( start_color.r + static_cast<Uint8>( percent *
( end_color.r - start_color.r ) ) );
const Uint8 g = static_cast<Uint8>( start_color.g + static_cast<Uint8>( percent *
( end_color.g - start_color.g ) ) );
const Uint8 b = static_cast<Uint8>( start_color.b + static_cast<Uint8>( percent *
( end_color.b - start_color.b ) ) );
colors.push_back( { r, g, b, 255 } );
}
colors.push_back( end_color );
return colors;
}
SDL_Surface_Ptr create_surface_32( int w, int h )
{
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
return CreateRGBSurface( 0, w, h, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF );
#else
return CreateRGBSurface( 0, w, h, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 );
#endif
}
SDL_Rect fit_rect_inside( const SDL_Rect &inner, const SDL_Rect &outer )
{
const float inner_ratio = static_cast<float>( inner.w ) / inner.h;
const float outer_ratio = static_cast<float>( outer.w ) / outer.h;
const float factor = inner_ratio > outer_ratio
? static_cast<float>( outer.w ) / inner.w
: static_cast<float>( outer.h ) / inner.h;
const int w = factor * inner.w;
const int h = factor * inner.h;
const point p( outer.x + ( outer.w - w ) / 2, outer.y + ( outer.h - h ) / 2 );
return SDL_Rect{ p.x, p.y, w, h };
}
#endif // SDL_TILES