Support-routines for the SDL-graphics-library. More...
#include "sdl/utils.hpp"
#include "sdl/rect.hpp"
#include "color.hpp"
#include "xBRZ/xbrz.hpp"
#include "video.hpp"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <boost/circular_buffer.hpp>
#include <boost/math/constants/constants.hpp>
Go to the source code of this file.
Functions | |
version_info | sdl_get_version () |
surface | stretch_surface_horizontal (const surface &surf, const unsigned w) |
Stretches a surface in the horizontal direction. More... | |
surface | stretch_surface_vertical (const surface &surf, const unsigned h) |
Stretches a surface in the vertical direction. More... | |
surface | scale_surface_xbrz (const surface &surf, std::size_t z) |
Scale a surface using xBRZ algorithm. More... | |
surface | scale_surface_nn (const surface &surf, int w, int h) |
Scale a surface using the nearest neighbor algorithm (provided by xBRZ lib) More... | |
surface | scale_surface (const surface &surf, int w, int h) |
Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gradients, for example in some portraits. More... | |
surface | scale_surface_legacy (const surface &surf, int w, int h) |
Scale a surface using simple bilinear filtering (discarding rgb from source pixels with 0 alpha) More... | |
surface | scale_surface_sharp (const surface &surf, int w, int h) |
Scale a surface using modified nearest neighbour algorithm. More... | |
surface | tile_surface (const surface &surf, int w, int h, bool centered) |
Tile a surface. More... | |
surface | adjust_surface_color (const surface &surf, int red, int green, int blue) |
surface | greyscale_image (const surface &surf) |
surface | monochrome_image (const surface &surf, const int threshold) |
surface | sepia_image (const surface &surf) |
surface | negative_image (const surface &surf, const int thresholdR, const int thresholdG, const int thresholdB) |
surface | alpha_to_greyscale (const surface &surf) |
surface | wipe_alpha (const surface &surf) |
surface | shadow_image (const surface &surf) |
create an heavy shadow of the image, by blurring, increasing alpha and darkening More... | |
surface | swap_channels_image (const surface &surf, channel r, channel g, channel b, channel a) |
surface | recolor_image (surface surf, const color_range_map &map_rgb) |
Recolors a surface using a map with source and converted palette values. More... | |
surface | brighten_image (const surface &surf, int32_t amount) |
void | adjust_surface_alpha (surface &surf, int32_t amount) |
surface | adjust_surface_alpha_add (const surface &surf, int amount) |
surface | mask_surface (const surface &surf, const surface &mask, bool *empty_result, const std::string &filename) |
Applies a mask on a surface. More... | |
bool | in_mask_surface (const surface &surf, const surface &mask) |
Check if a surface fit into a mask. More... | |
surface | submerge_alpha (const surface &surf, int depth, float alpha_base, float alpha_delta) |
Progressively reduce alpha of bottom part of the surface. More... | |
surface | light_surface (const surface &surf, const surface &lightmap) |
Light surf using lightmap. More... | |
surface | blur_surface (const surface &surf, int depth) |
Cross-fades a surface. More... | |
void | blur_surface (surface &surf, SDL_Rect rect, int depth) |
Cross-fades a surface in place. More... | |
surface | blur_alpha_surface (const surface &surf, int depth) |
Cross-fades a surface with alpha channel. More... | |
surface | cut_surface (const surface &surf, const SDL_Rect &r) |
Cuts a rectangle from a surface. More... | |
surface | blend_surface (const surface &surf, const double amount, const color_t color) |
Blends a surface with a color. More... | |
surface | rotate_any_surface (const surface &surf, float angle, int zoom, int offset) |
Rotates a surface by any degrees. More... | |
void | put_pixel (const surface &surf, surface_lock &surf_lock, int x, int y, uint32_t pixel) |
Helper methods for setting/getting a single pixel in an image. More... | |
uint32_t | get_pixel (const surface &surf, const const_surface_lock &surf_lock, int x, int y) |
surface | rotate_180_surface (const surface &surf) |
Rotates a surface 180 degrees. More... | |
surface | rotate_90_surface (const surface &surf, bool clockwise) |
Rotates a surface 90 degrees. More... | |
surface | flip_surface (const surface &surf) |
surface | flop_surface (const surface &surf) |
void | blit_surface (const surface &surf, const SDL_Rect *srcrect, surface &dst, const SDL_Rect *dstrect) |
Replacement for sdl_blit. More... | |
surface | get_surface_portion (const surface &src, SDL_Rect &area) |
Get a portion of the screen. More... | |
SDL_Rect | get_non_transparent_portion (const surface &surf) |
void | draw_centered_on_background (surface &surf, const SDL_Rect &rect, const color_t &color, CVideo &video) |
Support-routines for the SDL-graphics-library.
Definition in file utils.cpp.
void adjust_surface_alpha | ( | surface & | surf, |
int32_t | amount | ||
) |
Definition at line 1088 of file utils.cpp.
Referenced by gui::textbox::append_text(), font::floating_label::create_texture(), gui::textbox::draw_contents(), game_display::new_turn(), display::render_image(), editor::mouse_action_item::set_item_mouse_overlay(), editor::mouse_action_village::set_mouse_overlay(), editor::mouse_action_map_label::set_mouse_overlay(), editor::mouse_action_select::set_mouse_overlay(), editor::mouse_action_paste::set_mouse_overlay(), editor::mouse_action_starting_position::set_mouse_overlay(), editor::mouse_action::set_terrain_mouse_overlay(), and editor::mouse_action_unit::set_unit_mouse_overlay().
Definition at line 1097 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Definition at line 597 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Referenced by image::apply_light(), image::get_tod_colored(), image::getMinimap(), image::cs_modification::operator()(), and sdl_copy_portion().
Definition at line 812 of file utils.cpp.
References surface::clone(), and surface_locker< T >::pixels().
Referenced by image::plot_alpha_modification::operator()(), and sdl_copy_portion().
Blends a surface with a color.
Every pixel in the surface will be blended with the color
given. The final color of a pixel is amount * color
+ (1 - amount) * original.
surf | The surface to blend. |
amount | The amount of the new color is determined by color . Must be a number in the range [0, 1]. |
color | The color to blend width, note its alpha channel is ignored. |
Definition at line 1694 of file utils.cpp.
References a, color_t::b, b, surface::clone(), g, color_t::g, surface_locker< T >::pixels(), and color_t::r.
Referenced by image::blend_modification::operator()(), and display::render_image().
void blit_surface | ( | const surface & | src, |
const SDL_Rect * | srcrect, | ||
surface & | dst, | ||
const SDL_Rect * | dstrect | ||
) |
Replacement for sdl_blit.
sdl_blit has problems with blitting partly transparent surfaces so this is a replacement. It ignores the SDL_SRCALPHA and SDL_SRCCOLORKEY flags. src and dst will have the SDL_RLEACCEL flag removed. The return value of SDL_BlistSurface is normally ignored so no return value. The rectangles are const and will not be modified.
src
contains a valid canvas. dst
contains a valid neutral canvas. src
fits on the dst
.src | The surface to blit. |
srcrect | The region of the surface to blit |
dst | The surface to blit on. |
dstrect | The offset to blit the surface on, only x and y are used. |
Definition at line 1987 of file utils.cpp.
References surface::is_neutral(), surface_locker< T >::pixels(), and w.
Referenced by CVideo::current_refresh_rate(), gui2::image_shape::draw(), and gui2::text_shape::draw().
Cross-fades a surface with alpha channel.
surf | The source surface. |
depth | The depth of the blurring. |
Definition at line 1489 of file utils.cpp.
References surface::clone(), p, and surface_locker< T >::pixels().
Referenced by image::bl_modification::operator()(), and shadow_image().
Cross-fades a surface.
surf | The source surface. |
depth | The depth of the blurring. |
Definition at line 1354 of file utils.cpp.
References blur_surface(), and surface::clone().
Referenced by gui2::canvas::blit(), blur_surface(), and gui::dialog_frame::draw_background().
void blur_surface | ( | surface & | surf, |
SDL_Rect | rect, | ||
int | depth = 1 |
||
) |
Cross-fades a surface in place.
surf | The surface to blur, must have 32 bits per pixel. |
rect | The part of the surface to blur. |
depth | The depth of the blurring. |
Definition at line 1373 of file utils.cpp.
References p, and surface_locker< T >::pixels().
Definition at line 1046 of file utils.cpp.
References b, surface::clone(), fixed_point_multiply(), g, and surface_locker< T >::pixels().
Referenced by image::get_brightened(), and display::render_image().
Cuts a rectangle from a surface.
Definition at line 1636 of file utils.cpp.
References surface_locker< T >::pixels(), and utf8::size().
Referenced by image::load_image_sub_file(), and image::crop_modification::operator()().
void draw_centered_on_background | ( | surface & | surf, |
const SDL_Rect & | rect, | ||
const color_t & | color, | ||
CVideo & | video | ||
) |
Definition at line 2250 of file utils.cpp.
References color_t::a, color_t::b, CVideo::blit_surface(), CVideo::fill(), color_t::g, color_t::r, and CVideo::set_clip().
Referenced by display::draw_minimap().
Definition at line 1929 of file utils.cpp.
References surface::clone(), surface_locker< T >::pixels(), and swap().
Referenced by gui2::image_shape::draw(), image::fl_modification::operator()(), and image::reverse_image().
Definition at line 1958 of file utils.cpp.
References surface::clone(), surface_locker< T >::pixels(), and swap().
Referenced by image::fl_modification::operator()(), halo::halo_impl::effect::render(), and display::render_image().
SDL_Rect get_non_transparent_portion | ( | const surface & | surf | ) |
Definition at line 2181 of file utils.cpp.
References surface::clone(), n, and surface_locker< T >::pixels().
Referenced by display::draw_image_for_report().
uint32_t get_pixel | ( | const surface & | surf, |
const const_surface_lock & | surf_lock, | ||
int | x, | ||
int | y | ||
) |
Definition at line 1829 of file utils.cpp.
References surface_locker< T >::pixels().
Referenced by rotate_any_surface().
Get a portion of the screen.
Send nullptr if the portion is outside of the screen.
surf | The source surface. |
rect | The portion of the source surface to copy. |
0 | if error or the portion is outside of the surface. |
Definition at line 2135 of file utils.cpp.
References sdl_copy_portion().
Referenced by display::draw_image_for_report().
Definition at line 642 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Referenced by image::gs_modification::operator()(), display::render_image(), and sdl_copy_portion().
Check if a surface fit into a mask.
Definition at line 1204 of file utils.cpp.
References surface::clone(), and surface_locker< T >::pixels().
Referenced by image::is_in_hex().
Light surf using lightmap.
surf | The source surface. |
lightmap | add/subtract this color to surf but RGB values are converted to (X-128)*2 to cover the full (-256,256) spectrum. Should already be neutral |
Definition at line 1288 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Referenced by image::apply_light(), and image::light_modification::operator()().
surface mask_surface | ( | const surface & | surf, |
const surface & | mask, | ||
bool * | empty_result = nullptr , |
||
const std::string & | filename = std::string() |
||
) |
Applies a mask on a surface.
Definition at line 1135 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Referenced by image::get_hexed(), image::is_empty_hex(), image::load_image_sub_file(), image::mask_modification::operator()(), and editor::mouse_action::set_terrain_mouse_overlay().
Definition at line 687 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Referenced by image::bw_modification::operator()(), and sdl_copy_portion().
surface negative_image | ( | const surface & | surf, |
const int | thresholdR, | ||
const int | thresholdG, | ||
const int | thresholdB | ||
) |
Definition at line 769 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Referenced by image::negative_modification::operator()(), and sdl_copy_portion().
void put_pixel | ( | const surface & | surf, |
surface_lock & | surf_lock, | ||
int | x, | ||
int | y, | ||
uint32_t | pixel | ||
) |
Helper methods for setting/getting a single pixel in an image.
Lifted from http://sdl.beuc.net/sdl.wiki/Pixel_Access
surf | The image to get or receive the pixel from. |
surf_lock | The locked surface to make sure the pointers are valid. |
x | The position in the row of the pixel. |
y | The row of the pixel. |
pixel | The pixel value. |
Definition at line 1798 of file utils.cpp.
References surface_locker< T >::pixels().
Referenced by rotate_any_surface().
surface recolor_image | ( | surface | surf, |
const color_range_map & | map_rgb | ||
) |
Recolors a surface using a map with source and converted palette values.
This is most often used for team-coloring.
surf | The source surface. |
map_rgb | Map of color values, with the keys corresponding to the source palette, and the values to the recolored palette. |
Definition at line 1007 of file utils.cpp.
References surface::clone(), color_t::from_argb_bytes(), i, and surface_locker< T >::pixels().
Referenced by image::rc_modification::operator()().
Rotates a surface 180 degrees.
surf | The surface to rotate. |
Definition at line 1852 of file utils.cpp.
References surface::clone(), is_odd(), surface_locker< T >::pixels(), and swap().
Referenced by image::fl_modification::operator()(), and image::rotate_modification::operator()().
Rotates a surface 90 degrees.
surf | The surface to rotate. |
clockwise | Whether the rotation should be clockwise (true) or counter-clockwise (false). |
Definition at line 1894 of file utils.cpp.
References surface_locker< T >::pixels().
Referenced by image::rotate_modification::operator()().
Rotates a surface by any degrees.
zoom
>= offset
Otherwise offset
> 0 Otherwise the procedure will not return.surf | The surface to rotate. |
angle | The angle of rotation. |
zoom | Which zoom level to use for calculating the result. |
offset | Pixel offset when scanning the zoomed source. |
Definition at line 1741 of file utils.cpp.
References get_pixel(), put_pixel(), xbrz::scale(), and scale_surface().
Referenced by image::rotate_modification::operator()().
Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gradients, for example in some portraits.
surf | The source surface. |
w | The width of the resulting surface. |
h | The height of the resulting surface. |
0 | Returned upon error. |
surf | Returned if w == surf->w and h == surf->h. |
Definition at line 198 of file utils.cpp.
References a, b, e, fixed_point_divide(), fixed_point_to_int(), g, h, n, surface_locker< T >::pixels(), s, and w.
Referenced by gui::dialog_frame::draw_border(), gui::scrollbar::draw_contents(), gui::tristate_button::draw_contents(), display::draw_image_for_report(), editor::item_palette::draw_item(), editor::unit_palette::draw_item(), editor::terrain_palette::draw_item(), draw_label(), gui::menu::imgsel_style::draw_row_bg(), gui::menu::style::get_item_image(), gui::button::load_images(), image::light_modification::operator()(), CVideo::read_pixels_low_res(), rotate_any_surface(), sdl_copy_portion(), editor::mouse_action_item::set_item_mouse_overlay(), editor::mouse_action_village::set_mouse_overlay(), editor::mouse_action_map_label::set_mouse_overlay(), editor::mouse_action_select::set_mouse_overlay(), editor::mouse_action_paste::set_mouse_overlay(), editor::mouse_action_starting_position::set_mouse_overlay(), editor::mouse_action::set_terrain_mouse_overlay(), and editor::mouse_action_unit::set_unit_mouse_overlay().
Scale a surface using simple bilinear filtering (discarding rgb from source pixels with 0 alpha)
surf | The source surface. |
w | The width of the resulting surface. |
h | The height of the resulting surface. |
0 | Returned upon error. |
surf | Returned if w == surf->w and h == surf->h. |
Definition at line 327 of file utils.cpp.
References a, b, fixed_point_divide(), fixed_point_to_int(), g, h, surface_locker< T >::pixels(), and w.
Referenced by gui2::image_shape::draw(), image::scale_modification::operator()(), and sdl_copy_portion().
Scale a surface using the nearest neighbor algorithm (provided by xBRZ lib)
surf | The sources surface |
w | The width of the resulting surface. |
h | The height of the resulting surface. |
Definition at line 161 of file utils.cpp.
References h, xbrz::nearestNeighborScale(), surface_locker< T >::pixels(), and w.
Referenced by image::get_lighted_image(), image::get_scaled_to_hex(), image::get_scaled_to_zoom(), and sdl_copy_portion().
Scale a surface using modified nearest neighbour algorithm.
Use only if preserving sharp edges is a priority (e.g. minimap).
surf | The source surface. |
w | The width of the resulting surface. |
h | The height of the resulting surface. |
0 | Returned upon error. |
surf | Returned if w == surf->w and h == surf->h. |
Definition at line 464 of file utils.cpp.
References a, b, g, h, surface_locker< T >::pixels(), and w.
Referenced by gui2::image_shape::draw(), image::getMinimap(), image::scale_modification::operator()(), and sdl_copy_portion().
Scale a surface using xBRZ algorithm.
surf | The source surface |
z | The scaling factor. Should be an integer 2-5 (1 is tolerated). |
Definition at line 124 of file utils.cpp.
References surface_locker< T >::pixels(), and xbrz::scale().
Referenced by image::xbrz_modification::operator()(), and sdl_copy_portion().
version_info sdl_get_version | ( | ) |
Definition at line 35 of file utils.cpp.
Referenced by CVideo::get_dpi(), and CVideo::supports_vsync().
Definition at line 727 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Referenced by image::sepia_modification::operator()(), and sdl_copy_portion().
create an heavy shadow of the image, by blurring, increasing alpha and darkening
Definition at line 868 of file utils.cpp.
References blur_alpha_surface(), and surface_locker< T >::pixels().
Referenced by font::floating_label::create_texture(), and sdl_copy_portion().
Stretches a surface in the horizontal direction.
The stretches a surface it uses the first pixel in the horizontal direction of the original surface and copies that to the destination. This means only the first column of the original is used for the destination.
surf | The source surface. |
w | The width of the resulting surface. |
0 | Returned upon error. |
surf | Returned if w == surf->w. |
Definition at line 42 of file utils.cpp.
References surface_locker< T >::pixels(), and w.
Referenced by gui2::image_shape::draw(), and sdl_copy_portion().
Stretches a surface in the vertical direction.
The stretches a surface it uses the first pixel in the vertical direction of the original surface and copies that to the destination. This means only the first row of the original is used for the destination.
surf | The source surface. |
h | The height of the resulting surface. |
surf | Returned if h == surf->h. |
Definition at line 84 of file utils.cpp.
References h, and surface_locker< T >::pixels().
Referenced by gui2::image_shape::draw(), and sdl_copy_portion().
Progressively reduce alpha of bottom part of the surface.
surf | The source surface. |
depth | The height of the bottom part in pixels |
alpha_base | The alpha adjustment at the interface |
alpha_delta | The alpha adjustment reduction rate by pixel depth |
Definition at line 1250 of file utils.cpp.
References a, b, surface::clone(), d, fixed_point_multiply(), floating_to_fixed_point(), g, and surface_locker< T >::pixels().
Referenced by display::render_image().
Definition at line 905 of file utils.cpp.
References ALPHA, BLUE, surface::clone(), GREEN, surface_locker< T >::pixels(), and RED.
Referenced by image::swap_modification::operator()().
Tile a surface.
surf | The source surface. |
w | The width of the resulting surface. |
h | The height of the resulting surface. |
centered | Whether to tile from the center outwards or from the top left (origin). |
0 | Returned upon error |
surf | Returned if w == surf->w and h == surf->h. |
Definition at line 554 of file utils.cpp.
References h, i, surface_locker< T >::pixels(), sw, and w.
Referenced by gui2::image_shape::draw(), draw_panel(), and sdl_copy_portion().
Definition at line 840 of file utils.cpp.
References surface::clone(), and surface_locker< T >::pixels().
Referenced by image::wipe_alpha_modification::operator()(), and sdl_copy_portion().