Support-routines for the SDL-graphics-library. More...
#include "sdl/utils.hpp"
#include "sdl/rect.hpp"
#include "color.hpp"
#include "xBRZ/xbrz.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, fixed_t amount) |
void | adjust_surface_alpha (surface &surf, fixed_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, surface target) |
Support-routines for the SDL-graphics-library.
Definition in file utils.cpp.
Definition at line 1086 of file utils.cpp.
Referenced by gui::textbox::append_text(), font::floating_label::create_surface(), 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 1095 of file utils.cpp.
References b, surface::clone(), g, and surface_locker< T >::pixels().
Referenced by font::floating_label::get_surface().
Definition at line 595 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 810 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 1716 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 2009 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 1511 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 1376 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 1395 of file utils.cpp.
References p, and surface_locker< T >::pixels().
Definition at line 1044 of file utils.cpp.
References b, surface::clone(), fxpmult, g, and surface_locker< T >::pixels().
Referenced by image::get_brightened(), and display::render_image().
Cuts a rectangle from a surface.
Definition at line 1658 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, | ||
surface | target | ||
) |
Definition at line 2272 of file utils.cpp.
References color_t::a, color_t::b, sdl::fill_surface_rect(), color_t::g, color_t::r, and sdl_blit().
Referenced by display::draw_minimap().
Definition at line 1951 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 1980 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 2203 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 1851 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 2157 of file utils.cpp.
References sdl_copy_portion().
Referenced by gui2::canvas::blit(), gui2::window::draw(), gui::dialog_frame::draw_background(), display::draw_image_for_report(), display::refresh_report(), halo::halo_impl::effect::render(), and surface_restorer::update().
Definition at line 640 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 1202 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 1310 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 1133 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 685 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 767 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 1820 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 1005 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 1874 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 1916 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 1763 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 196 of file utils.cpp.
References a, b, e, ftofxp, fxpdiv, fxptoi, 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()(), 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 325 of file utils.cpp.
References a, b, ftofxp, fxpdiv, fxptoi, 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 159 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 462 of file utils.cpp.
References a, b, g, h, surface_locker< T >::pixels(), and w.
Referenced by 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 122 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 33 of file utils.cpp.
Referenced by surface::free_surface(), CVideo::update_framebuffer(), and CVideo::~CVideo().
Definition at line 725 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 866 of file utils.cpp.
References blur_alpha_surface(), and surface_locker< T >::pixels().
Referenced by font::floating_label::create_surface(), 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 40 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 82 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 1248 of file utils.cpp.
References a, b, surface::clone(), d, ftofxp, fxpmult, g, and surface_locker< T >::pixels().
Referenced by display::render_image().
Definition at line 903 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 552 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 838 of file utils.cpp.
References surface::clone(), and surface_locker< T >::pixels().
Referenced by image::wipe_alpha_modification::operator()(), and sdl_copy_portion().