Add text and rendering of color rectangles

This commit is contained in:
2020-08-22 13:17:56 +02:00
parent 618786f885
commit 329ef7f40f
4 changed files with 283 additions and 148 deletions
+27 -3
View File
@@ -12,6 +12,10 @@ bool SDLPP::init() {
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;
}
@@ -132,13 +136,33 @@ int SDLPP::hex2num(char c) {
}
}
std::tuple<int, int, int> SDLPP::getColorsHEX(const std::string &color) {
int red = 0, green = 0, blue = 0;
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]);
return {red, green, blue};
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};
}