Browse Source

Introduce SDLWrap::CreateRGBSurface

pull/2638/head
Vladimir Olteanu 5 years ago committed by Anders Jenbo
parent
commit
5d54f8d322
  1. 5
      Source/DiabloUI/ttf_render_wrapped.cpp
  2. 13
      Source/dx.cpp
  3. 15
      Source/utils/display.cpp
  4. 28
      Source/utils/sdl_wrap.h

5
Source/DiabloUI/ttf_render_wrapped.cpp

@ -14,6 +14,7 @@
#include "utils/log.hpp"
#include "utils/sdl_compat.h"
#include "utils/sdl_wrap.h"
namespace devilution {
@ -107,9 +108,7 @@ SDLSurfaceUniquePtr RenderUTF8_Solid_Wrapped(TTF_Font *font, const char *text, S
return SDLSurfaceUniquePtr{ TTF_RenderText_Solid(font, text, fg) };
/* Create the target surface */
SDLSurfaceUniquePtr textbuf { SDL_CreateRGBSurface(SDL_SWSURFACE, (strLines.size() > 1) ? wrapLength : width, height * strLines.size() + (lineSpace * (strLines.size() - 1)), 8, 0, 0, 0, 0) };
if (textbuf == nullptr)
return {};
auto textbuf = SDLWrap::CreateRGBSurface(SDL_SWSURFACE, (strLines.size() > 1) ? wrapLength : width, height * strLines.size() + (lineSpace * (strLines.size() - 1)), 8, 0, 0, 0, 0);
/* Fill the palette with the foreground color */
SDL_Palette *palette = textbuf->format->palette;

13
Source/dx.cpp

@ -13,6 +13,7 @@
#include "utils/display.h"
#include "utils/log.hpp"
#include "utils/sdl_mutex.h"
#include "utils/sdl_wrap.h"
#ifdef __3DS__
#include <3ds.h>
@ -279,18 +280,16 @@ void Blit(SDL_Surface *src, SDL_Rect *srcRect, SDL_Rect *dstRect)
// If the surface has a color key, we must stretch first and can then call BlitSurface.
if (SDL_HasColorKey(src)) {
SDL_Surface *stretched = SDL_CreateRGBSurface(SDL_SWSURFACE, dstRect->w, dstRect->h, src->format->BitsPerPixel,
SDLSurfaceUniquePtr stretched = SDLWrap::CreateRGBSurface(SDL_SWSURFACE, dstRect->w, dstRect->h, src->format->BitsPerPixel,
src->format->Rmask, src->format->Gmask, src->format->BitsPerPixel, src->format->Amask);
SDL_SetColorKey(stretched, SDL_SRCCOLORKEY, src->format->colorkey);
SDL_SetColorKey(stretched.get(), SDL_SRCCOLORKEY, src->format->colorkey);
if (src->format->palette != NULL)
SDL_SetPalette(stretched, SDL_LOGPAL, src->format->palette->colors, 0, src->format->palette->ncolors);
SDL_SetPalette(stretched.get(), SDL_LOGPAL, src->format->palette->colors, 0, src->format->palette->ncolors);
SDL_Rect stretched_rect = { 0, 0, dstRect->w, dstRect->h };
if (SDL_SoftStretch(src, srcRect, stretched, &stretched_rect) < 0
|| SDL_BlitSurface(stretched, &stretched_rect, dst, dstRect) < 0) {
SDL_FreeSurface(stretched);
if (SDL_SoftStretch(src, srcRect, stretched.get(), &stretched_rect) < 0
|| SDL_BlitSurface(stretched.get(), &stretched_rect, dst, dstRect) < 0) {
ErrSdl();
}
SDL_FreeSurface(stretched);
return;
}

15
Source/utils/display.cpp

@ -17,6 +17,7 @@
#include "controls/game_controls.h"
#include "options.h"
#include "utils/log.hpp"
#include "utils/sdl_wrap.h"
#ifdef USE_SDL1
#ifndef SDL1_VIDEO_MODE_BPP
@ -290,22 +291,20 @@ void ScaleOutputRect(SDL_Rect *rect)
#ifdef USE_SDL1
namespace {
SDL_Surface *CreateScaledSurface(SDL_Surface *src)
SDLSurfaceUniquePtr CreateScaledSurface(SDL_Surface *src)
{
SDL_Rect stretched_rect = { 0, 0, static_cast<Uint16>(src->w), static_cast<Uint16>(src->h) };
ScaleOutputRect(&stretched_rect);
SDL_Surface *stretched = SDL_CreateRGBSurface(
SDLSurfaceUniquePtr stretched = SDLWrap::CreateRGBSurface(
SDL_SWSURFACE, stretched_rect.w, stretched_rect.h, src->format->BitsPerPixel,
src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask);
if (SDL_HasColorKey(src)) {
SDL_SetColorKey(stretched, SDL_SRCCOLORKEY, src->format->colorkey);
SDL_SetColorKey(stretched.get(), SDL_SRCCOLORKEY, src->format->colorkey);
if (src->format->palette != NULL)
SDL_SetPalette(stretched, SDL_LOGPAL, src->format->palette->colors, 0, src->format->palette->ncolors);
SDL_SetPalette(stretched.get(), SDL_LOGPAL, src->format->palette->colors, 0, src->format->palette->ncolors);
}
if (SDL_SoftStretch((src), NULL, stretched, &stretched_rect) < 0) {
SDL_FreeSurface(stretched);
if (SDL_SoftStretch((src), NULL, stretched.get(), &stretched_rect) < 0)
ErrSdl();
}
return stretched;
}
@ -316,7 +315,7 @@ SDLSurfaceUniquePtr ScaleSurfaceToOutput(SDLSurfaceUniquePtr surface)
{
#ifdef USE_SDL1
if (OutputRequiresScaling())
return SDLSurfaceUniquePtr { CreateScaledSurface(surface.get()) };
return CreateScaledSurface(surface.get());
#endif
return surface;
}

28
Source/utils/sdl_wrap.h

@ -0,0 +1,28 @@
#pragma once
#include <SDL.h>
#ifdef USE_SDL1
#include "utils/sdl2_to_1_2_backports.h"
#else
#include "utils/sdl2_backports.h"
#endif
#include "appfat.h"
#include "utils/sdl_ptrs.h"
namespace devilution {
namespace SDLWrap {
inline SDLSurfaceUniquePtr CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
{
SDLSurfaceUniquePtr ret { SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask) };
if (ret == nullptr)
ErrSdl();
return ret;
}
} //namespace SDLWrap
} //namespace devilution
Loading…
Cancel
Save