Last active
April 10, 2016 01:36
-
-
Save xaxxon/a636703c758543c3d8e1ec45ecd73472 to your computer and use it in GitHub Desktop.
why does this turn blue when the if block runs?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TextureData Graphics::load_font_texture(const std::string & message, int max_width, int max_height) | |
{ | |
SDL_Color color = {255,255,255,255}; | |
SDL_Surface * rendered_message = TTF_RenderText_Blended(Graphics::font, message.c_str(), color); | |
if (rendered_message->w > max_width || | |
rendered_message->h > max_height) { | |
const float horizontal_scaling = rendered_message->w / (float)max_width; | |
const float vertical_scaling = rendered_message->h / (float)max_height; | |
const float final_scaling = horizontal_scaling > vertical_scaling ? horizontal_scaling : vertical_scaling; | |
SDL_Surface * scaled_surface = SDL_CreateRGBSurface(0, max_width / final_scaling, max_height / final_scaling, | |
32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); | |
auto blit_result = SDL_BlitScaled(rendered_message, nullptr, scaled_surface, nullptr); | |
assert(blit_result == 0); | |
SDL_FreeSurface(rendered_message); | |
rendered_message = scaled_surface; | |
} | |
TextureData texture; | |
texture.height = rendered_message->h; | |
texture.width = rendered_message->w; | |
ssize_t data_size = rendered_message->format->BytesPerPixel * rendered_message->w * rendered_message->h; | |
texture.data.resize(data_size); | |
memcpy(&texture.data[0], rendered_message->pixels, data_size); | |
SDL_FreeSurface(rendered_message); | |
return texture; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment