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
|
#if defined(TILES)
#include "sdl_wrappers.h"
#include <ostream>
#include <stdexcept>
#include <string>
#include "cata_assert.h"
#include "debug.h"
#include "point.h"
#if defined(_MSC_VER) && defined(USE_VCPKG)
# include <SDL2/SDL_image.h>
#else
# include <SDL_image.h>
#endif
#define dbg(x) DebugLog((x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "
bool printErrorIf( const bool condition, const char *const message )
{
if( !condition ) {
return false;
}
dbg( D_ERROR ) << message << ": " << SDL_GetError();
return true;
}
void throwErrorIf( const bool condition, const char *const message )
{
if( !condition ) {
return;
}
throw std::runtime_error( std::string( message ) + ": " + SDL_GetError() );
}
void RenderCopy( const SDL_Renderer_Ptr &renderer, const SDL_Texture_Ptr &texture,
const SDL_Rect *srcrect, const SDL_Rect *dstrect )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to render to a null renderer";
return;
}
if( !texture ) {
dbg( D_ERROR ) << "Tried to render a null texture";
return;
}
printErrorIf( SDL_RenderCopy( renderer.get(), texture.get(), srcrect, dstrect ) != 0,
"SDL_RenderCopy failed" );
}
SDL_Texture_Ptr CreateTexture( const SDL_Renderer_Ptr &renderer, Uint32 format, int access,
int w, int h )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to create texture with a null renderer";
return SDL_Texture_Ptr();
}
SDL_Texture_Ptr result( SDL_CreateTexture( renderer.get(), format, access, w, h ) );
printErrorIf( !result, "SDL_CreateTexture failed" );
return result;
}
SDL_Texture_Ptr CreateTextureFromSurface( const SDL_Renderer_Ptr &renderer,
const SDL_Surface_Ptr &surface )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to create texture with a null renderer";
return SDL_Texture_Ptr();
}
if( !surface ) {
dbg( D_ERROR ) << "Tried to create texture from a null surface";
return SDL_Texture_Ptr();
}
SDL_Texture_Ptr result( SDL_CreateTextureFromSurface( renderer.get(), surface.get() ) );
printErrorIf( !result, "SDL_CreateTextureFromSurface failed" );
return result;
}
void SetRenderDrawColor( const SDL_Renderer_Ptr &renderer, const Uint8 r, const Uint8 g,
const Uint8 b, const Uint8 a )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to use a null renderer";
return;
}
printErrorIf( SDL_SetRenderDrawColor( renderer.get(), r, g, b, a ) != 0,
"SDL_SetRenderDrawColor failed" );
}
void RenderDrawPoint( const SDL_Renderer_Ptr &renderer, const point &p )
{
printErrorIf( SDL_RenderDrawPoint( renderer.get(), p.x, p.y ) != 0, "SDL_RenderDrawPoint failed" );
}
void RenderFillRect( const SDL_Renderer_Ptr &renderer, const SDL_Rect *const rect )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to use a null renderer";
return;
}
printErrorIf( SDL_RenderFillRect( renderer.get(), rect ) != 0, "SDL_RenderFillRect failed" );
}
void FillRect( const SDL_Surface_Ptr &surface, const SDL_Rect *const rect, Uint32 color )
{
if( !surface ) {
dbg( D_ERROR ) << "Tried to use a null surface";
return;
}
printErrorIf( SDL_FillRect( surface.get(), rect, color ) != 0, "SDL_FillRect failed" );
}
void SetTextureBlendMode( const SDL_Texture_Ptr &texture, SDL_BlendMode blendMode )
{
if( !texture ) {
dbg( D_ERROR ) << "Tried to use a null texture";
}
throwErrorIf( SDL_SetTextureBlendMode( texture.get(), blendMode ) != 0,
"SDL_SetTextureBlendMode failed" );
}
bool SetTextureColorMod( const SDL_Texture_Ptr &texture, Uint32 r, Uint32 g, Uint32 b )
{
if( !texture ) {
dbg( D_ERROR ) << "Tried to use a null texture";
return true;
}
return printErrorIf( SDL_SetTextureColorMod( texture.get(), r, g, b ) != 0,
"SDL_SetTextureColorMod failed" );
}
void SetRenderDrawBlendMode( const SDL_Renderer_Ptr &renderer, const SDL_BlendMode blendMode )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to use a null renderer";
return;
}
printErrorIf( SDL_SetRenderDrawBlendMode( renderer.get(), blendMode ) != 0,
"SDL_SetRenderDrawBlendMode failed" );
}
void GetRenderDrawBlendMode( const SDL_Renderer_Ptr &renderer, SDL_BlendMode &blend_mode )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to use a null renderer";
return;
}
printErrorIf( SDL_GetRenderDrawBlendMode( renderer.get(), &blend_mode ) != 0,
"SDL_GetRenderDrawBlendMode failed" );
}
SDL_Surface_Ptr load_image( const char *const path )
{
cata_assert( path );
SDL_Surface_Ptr result( IMG_Load( path ) );
if( !result ) {
throw std::runtime_error( "Could not load image \"" + std::string( path ) + "\": " +
IMG_GetError() );
}
return result;
}
void SetRenderTarget( const SDL_Renderer_Ptr &renderer, const SDL_Texture_Ptr &texture )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to use a null renderer";
return;
}
// a null texture is fine for SDL
printErrorIf( SDL_SetRenderTarget( renderer.get(), texture.get() ) != 0,
"SDL_SetRenderTarget failed" );
}
void RenderClear( const SDL_Renderer_Ptr &renderer )
{
if( !renderer ) {
dbg( D_ERROR ) << "Tried to use a null renderer";
return;
}
printErrorIf( SDL_RenderClear( renderer.get() ) != 0, "SDL_RenderClear failed" );
}
SDL_Surface_Ptr CreateRGBSurface( const Uint32 flags, const int width, const int height,
const int depth, const Uint32 Rmask, const Uint32 Gmask, const Uint32 Bmask, const Uint32 Amask )
{
SDL_Surface_Ptr surface( SDL_CreateRGBSurface( flags, width, height, depth, Rmask, Gmask, Bmask,
Amask ) );
throwErrorIf( !surface, "Failed to create surface" );
return surface;
}
#endif
|