2009-04-15 17:03:31 +03:00
/*
* SDL_Extensions . h , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
2009-04-16 14:14:13 +03:00
*/
2023-01-17 22:01:35 +02:00
2014-05-23 13:51:38 +03:00
# pragma once
# include <SDL_render.h>
2014-07-15 10:14:49 +03:00
# include <SDL_events.h>
# include "../../lib/GameConstants.h"
2023-01-18 15:50:52 +02:00
# include "../../lib/Rect.h"
2014-05-23 13:51:38 +03:00
2023-01-17 22:01:35 +02:00
struct SDL_Window ;
struct SDL_Renderer ;
struct SDL_Texture ;
struct SDL_Surface ;
struct SDL_Color ;
2009-04-16 14:14:13 +03:00
2014-05-21 19:04:34 +03:00
extern SDL_Window * mainWindow ;
extern SDL_Renderer * mainRenderer ;
extern SDL_Texture * screenTexture ;
2009-05-25 02:21:55 +03:00
extern SDL_Surface * screen , * screen2 , * screenBuf ;
2023-01-17 22:01:35 +02:00
class Rect ;
2023-01-18 15:50:52 +02:00
class Point ;
2009-04-17 17:01:22 +03:00
2012-11-11 15:23:31 +03:00
/**
* The colors class defines color constants of type SDL_Color .
*/
class Colors
2011-12-22 16:05:19 +03:00
{
2012-11-11 15:23:31 +03:00
public :
/** the h3 yellow color, typically used for headlines */
static const SDL_Color YELLOW ;
/** the standard h3 white color */
static const SDL_Color WHITE ;
2012-11-20 20:53:45 +03:00
/** the metallic gold color used mostly as a border around buttons */
static const SDL_Color METALLIC_GOLD ;
2012-12-19 20:24:53 +03:00
/** green color used for in-game console */
static const SDL_Color GREEN ;
2016-11-07 23:19:53 +02:00
2018-01-05 19:21:07 +02:00
/** the h3 orange color, used for blocked buttons */
static const SDL_Color ORANGE ;
/** the h3 bright yellow color, used for selection border */
static const SDL_Color BRIGHT_YELLOW ;
2014-07-02 21:20:54 +03:00
/** default key color for all 8 & 24 bit graphics */
static const SDL_Color DEFAULT_KEY_COLOR ;
2023-01-17 22:01:35 +02:00
/// Selected creature card
static const SDL_Color RED ;
/// Minimap border
static const SDL_Color PURPLE ;
static const SDL_Color BLACK ;
2023-01-18 16:54:53 +02:00
static const SDL_Color TRANSPARENCY ;
2012-11-11 15:23:31 +03:00
} ;
2011-12-22 16:05:19 +03:00
2023-01-17 22:01:35 +02:00
namespace CSDL_Ext
2012-07-21 00:42:25 +03:00
{
2023-01-17 22:01:35 +02:00
2023-01-18 15:50:52 +02:00
/// creates Point using provided event
Point fromSDL ( const SDL_MouseMotionEvent & motion ) ;
/// creates Rect using provided rect
Rect fromSDL ( const SDL_Rect & rect ) ;
/// creates SDL_Rect using provided rect
SDL_Rect toSDL ( const Rect & rect ) ;
2023-01-17 22:01:35 +02:00
void setColors ( SDL_Surface * surface , SDL_Color * colors , int firstcolor , int ncolors ) ;
void warpMouse ( int x , int y ) ;
bool isCtrlKeyDown ( ) ;
bool isAltKeyDown ( ) ;
bool isShiftKeyDown ( ) ;
void setAlpha ( SDL_Surface * bg , int value ) ;
2012-07-21 00:42:25 +03:00
2009-06-26 18:41:19 +03:00
template < typename IntType >
2014-07-04 12:48:09 +03:00
std : : string makeNumberShort ( IntType number , IntType maxLength = 3 ) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k)
2009-06-26 18:41:19 +03:00
{
2014-07-04 12:48:09 +03:00
IntType max = pow ( 10 , maxLength ) ;
2015-10-12 16:08:18 +02:00
if ( std : : abs ( number ) < max )
2012-07-19 21:52:44 +03:00
return boost : : lexical_cast < std : : string > ( number ) ;
2009-06-26 18:41:19 +03:00
2014-07-04 12:48:09 +03:00
std : : string symbols = " kMGTPE " ;
2012-07-19 21:52:44 +03:00
auto iter = symbols . begin ( ) ;
2009-06-26 18:41:19 +03:00
2014-07-04 12:48:09 +03:00
while ( number > = max )
2009-06-26 18:41:19 +03:00
{
2012-07-19 21:52:44 +03:00
number / = 1000 ;
iter + + ;
2009-06-26 18:41:19 +03:00
2012-07-19 21:52:44 +03:00
assert ( iter ! = symbols . end ( ) ) ; //should be enough even for int64
}
return boost : : lexical_cast < std : : string > ( number ) + * iter ;
2009-06-26 18:41:19 +03:00
}
2023-01-18 15:50:52 +02:00
Rect genRect ( const int & hh , const int & ww , const int & xx , const int & yy ) ;
2010-08-05 14:02:18 +03:00
2023-01-17 22:01:35 +02:00
typedef void ( * TColorPutter ) ( Uint8 * & ptr , const Uint8 & R , const Uint8 & G , const Uint8 & B ) ;
typedef void ( * TColorPutterAlpha ) ( Uint8 * & ptr , const Uint8 & R , const Uint8 & G , const Uint8 & B , const Uint8 & A ) ;
2010-08-12 08:22:48 +03:00
2023-01-17 22:01:35 +02:00
void blitAt ( SDL_Surface * src , int x , int y , SDL_Surface * dst = screen ) ;
void blitAt ( SDL_Surface * src , const Rect & pos , SDL_Surface * dst = screen ) ;
2010-08-05 14:02:18 +03:00
2023-01-17 22:01:35 +02:00
void setClipRect ( SDL_Surface * src , const Rect & other ) ;
void getClipRect ( SDL_Surface * src , Rect & other ) ;
2012-07-21 23:16:54 +03:00
2023-01-17 22:01:35 +02:00
void blitSurface ( SDL_Surface * src , const Rect & srcRect , SDL_Surface * dst , const Point & dest ) ;
void blitSurface ( SDL_Surface * src , SDL_Surface * dst , const Point & dest ) ;
2012-07-21 23:16:54 +03:00
2023-01-17 22:01:35 +02:00
void fillSurface ( SDL_Surface * dst , const SDL_Color & color ) ;
void fillRect ( SDL_Surface * dst , const Rect & dstrect , const SDL_Color & color ) ;
2012-07-21 23:16:54 +03:00
//fill dest image with source texture.
void fillTexture ( SDL_Surface * dst , SDL_Surface * sourceTexture ) ;
2009-04-16 14:14:13 +03:00
2023-01-17 22:01:35 +02:00
void updateRect ( SDL_Surface * surface , const Rect & rect ) ;
void putPixelWithoutRefresh ( SDL_Surface * ekran , const int & x , const int & y , const Uint8 & R , const Uint8 & G , const Uint8 & B , Uint8 A = 255 ) ;
void putPixelWithoutRefreshIfInSurf ( SDL_Surface * ekran , const int & x , const int & y , const Uint8 & R , const Uint8 & G , const Uint8 & B , Uint8 A = 255 ) ;
2009-04-16 14:14:13 +03:00
2013-11-06 16:42:58 +03:00
SDL_Surface * verticalFlip ( SDL_Surface * toRot ) ; //vertical flip
SDL_Surface * horizontalFlip ( SDL_Surface * toRot ) ; //horizontal flip
2023-01-17 22:01:35 +02:00
Uint32 getPixel ( SDL_Surface * surface , const int & x , const int & y , bool colorByte = false ) ;
2010-02-18 14:44:59 +02:00
bool isTransparent ( SDL_Surface * srf , int x , int y ) ; //checks if surface is transparent at given position
2009-12-22 23:53:50 +02:00
2012-05-19 19:22:34 +03:00
Uint8 * getPxPtr ( const SDL_Surface * const & srf , const int x , const int y ) ;
2012-05-28 22:29:32 +03:00
TColorPutter getPutterFor ( SDL_Surface * const & dest , int incrementing ) ; //incrementing: -1, 0, 1
TColorPutterAlpha getPutterAlphaFor ( SDL_Surface * const & dest , int incrementing ) ; //incrementing: -1, 0, 1
2009-12-22 23:53:50 +02:00
2010-08-12 08:22:48 +03:00
template < int bpp >
2023-01-17 22:01:35 +02:00
int blit8bppAlphaTo24bppT ( const SDL_Surface * src , const Rect & srcRect , SDL_Surface * dst , const Point & dstPoint ) ; //blits 8 bpp surface with alpha channel to 24 bpp surface
int blit8bppAlphaTo24bpp ( const SDL_Surface * src , const Rect & srcRect , SDL_Surface * dst , const Point & dstPoint ) ; //blits 8 bpp surface with alpha channel to 24 bpp surface
2009-04-16 14:14:13 +03:00
Uint32 colorToUint32 ( const SDL_Color * color ) ; //little endian only
2013-07-07 11:27:27 +03:00
SDL_Color makeColor ( ui8 r , ui8 g , ui8 b , ui8 a ) ;
2009-08-17 11:50:31 +03:00
2009-04-16 14:14:13 +03:00
void update ( SDL_Surface * what = screen ) ; //updates whole surface (default - main screen)
2022-11-15 21:42:16 +02:00
void drawLine ( SDL_Surface * sur , int x1 , int y1 , int x2 , int y2 , const SDL_Color & color1 , const SDL_Color & color2 ) ;
2023-01-17 22:01:35 +02:00
void drawBorder ( SDL_Surface * sur , int x , int y , int w , int h , const SDL_Color & color ) ;
void drawBorder ( SDL_Surface * sur , const Rect & r , const SDL_Color & color ) ;
void drawDashedBorder ( SDL_Surface * sur , const Rect & r , const SDL_Color & color ) ;
2013-03-03 20:06:03 +03:00
void setPlayerColor ( SDL_Surface * sur , PlayerColor player ) ; //sets correct color of flags; -1 for neutral
2009-04-16 14:14:13 +03:00
std : : string processStr ( std : : string str , std : : vector < std : : string > & tor ) ; //replaces %s in string
SDL_Surface * newSurface ( int w , int h , SDL_Surface * mod = screen ) ; //creates new surface, with flags/format same as in surface given
SDL_Surface * copySurface ( SDL_Surface * mod ) ; //returns copy of given surface
2012-05-18 20:35:46 +03:00
template < int bpp >
SDL_Surface * createSurfaceWithBpp ( int width , int height ) ; //create surface with give bits per pixels value
2010-07-15 20:13:17 +03:00
void VflipSurf ( SDL_Surface * surf ) ; //fluipis given surface by vertical axis
2012-05-17 13:44:48 +03:00
2012-06-15 20:08:19 +03:00
//scale surface to required size.
//nearest neighbour algorithm
SDL_Surface * scaleSurfaceFast ( SDL_Surface * surf , int width , int height ) ;
// bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
2012-05-20 00:38:01 +03:00
SDL_Surface * scaleSurface ( SDL_Surface * surf , int width , int height ) ;
2012-05-17 13:44:48 +03:00
template < int bpp >
2023-01-17 22:01:35 +02:00
void applyEffectBpp ( SDL_Surface * surf , const Rect & rect , int mode ) ;
void applyEffect ( SDL_Surface * surf , const Rect & rect , int mode ) ; //mode: 0 - sepia, 1 - grayscale
2016-11-07 23:19:53 +02:00
2023-01-17 22:01:35 +02:00
void startTextInput ( const Rect & where ) ;
2014-07-02 18:41:30 +03:00
void stopTextInput ( ) ;
2016-11-07 23:19:53 +02:00
2014-07-02 21:20:54 +03:00
void setColorKey ( SDL_Surface * surface , SDL_Color color ) ;
2017-09-08 13:25:12 +02:00
2014-07-02 21:20:54 +03:00
///set key-color to 0,255,255
void setDefaultColorKey ( SDL_Surface * surface ) ;
2017-09-08 13:25:12 +02:00
2014-07-02 21:20:54 +03:00
///set key-color to 0,255,255 only if it exactly mapped
void setDefaultColorKeyPresize ( SDL_Surface * surface ) ;
2023-01-17 22:01:35 +02:00
/// helper that will safely set and un-set ClipRect for SDL_Surface
class CClipRectGuard
{
SDL_Surface * surf ;
Rect oldRect ;
public :
CClipRectGuard ( SDL_Surface * surface , const Rect & rect ) :
surf ( surface )
{
CSDL_Ext : : getClipRect ( surf , oldRect ) ;
CSDL_Ext : : setClipRect ( surf , rect ) ;
}
~ CClipRectGuard ( )
{
CSDL_Ext : : setClipRect ( surf , oldRect ) ;
}
} ;
2012-09-15 22:16:16 +03:00
}