game/sdlpp/sdlpp_common.cpp

123 lines
3.4 KiB
C++
Raw Normal View History

2020-11-21 19:57:40 +00:00
#include "sdlpp_common.hpp"
#ifdef _WIN32
#include "SDL2/SDL_image.h"
#else
#include <SDL2/SDL_image.h>
#endif
#include <iostream>
int SDLPP::hex2num( char c ) {
if ( c <= '9' )
return c - '0';
switch ( c ) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
default:
return 15;
}
}
bool SDLPP::init() {
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
if ( TTF_Init() == -1 ) {
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: "
<< TTF_GetError() << std::endl;
return false;
}
return true;
}
bool SDLPP::init( uint32_t SDL_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
return true;
}
bool SDLPP::init( uint32_t SDL_OPTIONS, int IMAGE_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if ( IMG_Init( IMAGE_OPTIONS ) != IMAGE_OPTIONS ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
return true;
}
std::tuple< int, int, int, int >
SDLPP::getColorsHEX( const std::string &color ) {
int red = 0, green = 0, blue = 0, alpha = 255;
const char *color_ptr = color.c_str();
if ( color_ptr[0] == '#' )
color_ptr++;
red = hex2num( color_ptr[0] ) * 16 + hex2num( color_ptr[1] );
green = hex2num( color_ptr[2] ) * 16 + hex2num( color_ptr[3] );
blue = hex2num( color_ptr[4] ) * 16 + hex2num( color_ptr[5] );
if ( color_ptr[6] != '\0' )
alpha = hex2num( color_ptr[6] ) * 16 + hex2num( color_ptr[7] );
return { red, green, blue, alpha };
}
SDL_Color
SDLPP::getSDLColorTuple( const std::tuple< int, int, int, int > &tuple ) {
SDL_Color ret_color{};
ret_color.r = std::get< 0 >( tuple );
ret_color.g = std::get< 1 >( tuple );
ret_color.b = std::get< 2 >( tuple );
ret_color.a = std::get< 3 >( tuple );
return ret_color;
}
SDL_Color SDLPP::getSDLColorHEX( const std::string &color ) {
auto color_tuple = SDLPP::getColorsHEX( color );
return getSDLColorTuple( color_tuple );
}
std::tuple< int, int, int, int >
SDLPP::getColorsSDLColor( const SDL_Color &color ) {
return { color.r, color.g, color.b, color.a };
}
bool SDLPP::getSDLEvent( SDL_Event &e ) {
if ( SDL_PeepEvents( &e, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT,
SDL_LASTEVENT ) == 1 ) {
SDL_PeepEvents( &e, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT );
return true;
}
return false;
}