Skip to content
This repository has been archived by the owner on Apr 14, 2020. It is now read-only.

Commit

Permalink
Support transparent images
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Feb 20, 2020
1 parent e4084b3 commit 6bf0b64
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
21 changes: 20 additions & 1 deletion sdlutils.cpp
Expand Up @@ -34,6 +34,22 @@ SDL_Surface *SDL_utils::loadImage(const std::string &p_filename)
return l_img2;
}

static void AsciiToLower(std::string *s)
{
for (char &c : *s)
if (c >= 'A' && c <= 'Z')
c -= ('Z' - 'z');
}

static std::string GetFileExtension(const std::string &name) {
const auto dot_pos = name.rfind('.');
if (dot_pos == std::string::npos)
return "";
std::string ext = name.substr(dot_pos + 1);
AsciiToLower(&ext);
return ext;
}

SDL_Surface *SDL_utils::loadImageToFit(const std::string &p_filename, int fit_w, int fit_h)
{
// Load image
Expand All @@ -57,7 +73,10 @@ SDL_Surface *SDL_utils::loadImageToFit(const std::string &p_filename, int fit_w,
target_h *= PPU_Y;
SDL_Surface *l_img2 = zoomSurface(l_img, static_cast<double>(target_w) / l_img->w, static_cast<double>(target_h) / l_img->h, SMOOTHING_ON);
SDL_FreeSurface(l_img);
SDL_Surface *l_img3 = SDL_DisplayFormat(l_img2);

const std::string ext = GetFileExtension(p_filename);
const bool supports_alpha = ext != "xcf" && ext != "jpg" && ext != "jpeg";
SDL_Surface *l_img3 = supports_alpha ? SDL_DisplayFormatAlpha(l_img2) : SDL_DisplayFormat(l_img2);
SDL_FreeSurface(l_img2);
return l_img3;
}
Expand Down
20 changes: 15 additions & 5 deletions viewer.cpp
Expand Up @@ -49,11 +49,7 @@ CViewer::CViewer(const std::string &p_fileName):
// Create background image
m_background = SDL_utils::createImage(SCREEN_WIDTH, SCREEN_HEIGHT * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_BG_1));
{
SDL_Rect l_rect;
l_rect.x = 0;
l_rect.y = 0;
l_rect.w = SCREEN_WIDTH;
l_rect.h = Y_LIST * PPU_Y;
SDL_Rect l_rect = {0, 0, SCREEN_WIDTH, Y_LIST * PPU_Y};
SDL_FillRect(m_background, &l_rect, SDL_MapRGB(m_background->format, COLOR_BORDER));
}
// Print title
Expand All @@ -79,6 +75,20 @@ CViewer::CViewer(const std::string &p_fileName):
if (m_image != nullptr)
{
m_mode = IMAGE;

// Transparency grid background.
constexpr int kTransparentBgRectSize = 10;
const Uint32 colors[2] = {
SDL_MapRGB(m_background->format, 240, 240, 240),
SDL_MapRGB(m_background->format, 155, 155, 155),
};
int i = 0;
for (int y = Y_LIST; y < SCREEN_HEIGHT; y += kTransparentBgRectSize, ++i) {
for (int x = 0; x < SCREEN_WIDTH; x += kTransparentBgRectSize, ++i) {
SDL_Rect rect = {x, y * PPU_Y, kTransparentBgRectSize, kTransparentBgRectSize * PPU_Y};
SDL_FillRect(m_background, &rect, colors[i % 2]);
}
}
}
else
{
Expand Down

0 comments on commit 6bf0b64

Please sign in to comment.