Skip to content

Commit

Permalink
- Rename toMappedSdlRGBColor/toMappedSdlRGBAColor into `toSdlColo…
Browse files Browse the repository at this point in the history
…r`/`toSdlAlphaColor` and use overload with `SDL_Surface`.

- Use named color at some places.
  • Loading branch information
Jarod42 committed Dec 8, 2023
1 parent 4967142 commit ef3f6b0
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 39 deletions.
10 changes: 5 additions & 5 deletions src/lib/SDLutility/drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
void drawPoint (SDL_Surface& surface, const cPosition& position, const cRgbColor& color)
{
SDL_Rect rect = {Sint16 (position.x()), Sint16 (position.y()), 1, 1};
SDL_FillRect (&surface, &rect, toMappedSdlRGBAColor (color, surface.format));
SDL_FillRect (&surface, &rect, toSdlAlphaColor (color, surface));
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -80,7 +80,7 @@ void drawRectangle (SDL_Surface& surface, const cBox<cPosition>& rectangle, cons

SDL_Rect line_h = {rectangle.getMinCorner().x(), rectangle.getMinCorner().y(), size.x(), thickness};

const auto sdlColor = toMappedSdlRGBAColor (color, surface.format);
const auto sdlColor = toSdlAlphaColor (color, surface);

SDL_FillRect (&surface, &line_h, sdlColor);
line_h.y += size.y() - thickness;
Expand All @@ -96,7 +96,7 @@ void drawSelectionCorner (SDL_Surface& surface, const cBox<cPosition>& rectangle
{
constexpr int selectionCornerLineThickness = 3;
const cPosition size = rectangle.getMaxCorner() - rectangle.getMinCorner();
const auto sdlColor = toMappedSdlRGBAColor (color, surface.format);
const auto sdlColor = toSdlAlphaColor (color, surface);

// cornersize is CellW or CellW*2(largeUnit)/4, or 16 for 32p (largest) cells.
const int t = selectionCornerLineThickness;
Expand Down Expand Up @@ -210,8 +210,8 @@ void putPixel (SDL_Surface& surface, const cPosition& position, Uint32 pixel)
//------------------------------------------------------------------------------
void replaceColor (SDL_Surface& surface, const cRgbColor& sourceColor, const cRgbColor& destinationColor)
{
const auto srcMapped = toMappedSdlRGBAColor (sourceColor, surface.format);
const auto destMapped = toMappedSdlRGBAColor (destinationColor, surface.format);
const auto srcMapped = toSdlAlphaColor (sourceColor, surface);
const auto destMapped = toSdlAlphaColor (destinationColor, surface);

Uint32 key;
const auto hadKey = SDL_GetColorKey (&surface, &key) == 0;
Expand Down
15 changes: 13 additions & 2 deletions src/lib/SDLutility/tosdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,24 @@ SDL_Rect toSdlRect (const cBox<cPosition>& box)
}

//------------------------------------------------------------------------------
Uint32 toMappedSdlRGBColor (const cRgbColor& color, const SDL_PixelFormat* format)
Uint32 toSdlColor (const cRgbColor& color, const SDL_PixelFormat* format)
{
return SDL_MapRGB (format, color.r, color.g, color.b);
}

//------------------------------------------------------------------------------
Uint32 toMappedSdlRGBAColor (const cRgbColor& color, const SDL_PixelFormat* format)
Uint32 toSdlColor (const cRgbColor& color, const SDL_Surface& surface)
{
return toSdlColor (color, surface.format);
}

//------------------------------------------------------------------------------
Uint32 toSdlAlphaColor (const cRgbColor& color, const SDL_PixelFormat* format)
{
return SDL_MapRGBA (format, color.r, color.g, color.b, color.a);
}
//------------------------------------------------------------------------------
Uint32 toSdlAlphaColor (const cRgbColor& color, const SDL_Surface& surface)
{
return toSdlAlphaColor (color, surface.format);
}
6 changes: 4 additions & 2 deletions src/lib/SDLutility/tosdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class cBox;

SDL_Rect toSdlRect (const cBox<cPosition>&);

Uint32 toMappedSdlRGBColor (const cRgbColor&, const SDL_PixelFormat*);
Uint32 toMappedSdlRGBAColor (const cRgbColor&, const SDL_PixelFormat*);
Uint32 toSdlColor (const cRgbColor&, const SDL_PixelFormat*);
Uint32 toSdlColor (const cRgbColor&, const SDL_Surface&);
Uint32 toSdlAlphaColor (const cRgbColor&, const SDL_PixelFormat*);
Uint32 toSdlAlphaColor (const cRgbColor&, const SDL_Surface&);

#endif
5 changes: 3 additions & 2 deletions src/lib/output/video/unifonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ void cUnicodeFont::loadChars (eUnicodeFontCharset charset, eUnicodeFontType font
int currentChar = 0;
int pX = 0;
int pY = 0;
const auto limitColor = SDL_MapRGB (surface->format, 0xFF, 0, 0xFF);

for (int rows = 0; rows < highcount; rows++)
{
Expand All @@ -412,7 +413,7 @@ void cUnicodeFont::loadChars (eUnicodeFontCharset charset, eUnicodeFontType font
pX = (cellW * cols) + pCol;
pY = (cellH * rows) + pRow;

if (getPixel (*surface, cPosition (pX, pY)) != SDL_MapRGB (surface->format, 0xFF, 0, 0xFF))
if (getPixel (*surface, cPosition (pX, pY)) != limitColor)
{
// offset
Rect.x = pX;
Expand All @@ -429,7 +430,7 @@ void cUnicodeFont::loadChars (eUnicodeFontCharset charset, eUnicodeFontType font
pX = (cellW * cols) + pCol_w;
pY = (cellH * rows) + pRow_w;

if (getPixel (*surface, cPosition (pX, pY)) != SDL_MapRGB (surface->format, 0xFF, 0, 0xFF))
if (getPixel (*surface, cPosition (pX, pY)) != limitColor)
{
Rect.w = (pX - Rect.x) + 1;
pCol_w = -1; // break loop
Expand Down
3 changes: 2 additions & 1 deletion src/lib/output/video/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "utility/log.h"
#include "utility/mathtools.h"
#include "utility/os.h"
#include "SDLutility/tosdl.h"
#include "utility/thread/ismainthread.h"

#include <SDL.h>
Expand Down Expand Up @@ -260,7 +261,7 @@ void cVideo::applyWindowMode()

void cVideo::clearBuffer()
{
SDL_FillRect (buffer, nullptr, SDL_MapRGB (buffer->format, 0, 0, 0));
SDL_FillRect (buffer, nullptr, toSdlColor (cRgbColor::black(), *buffer));
}

void cVideo::detectResolutions()
Expand Down
3 changes: 2 additions & 1 deletion src/lib/resources/loaddata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "resources/uidata.h"
#include "resources/vehicleuidata.h"
#include "settings.h"
#include "SDLutility/tosdl.h"
#include "utility/language.h"
#include "utility/listhelpers.h"
#include "utility/log.h"
Expand Down Expand Up @@ -169,7 +170,7 @@ static void createShadowGfx()
{
// TODO: reduce size once we use texture.
GraphicsData.gfx_shadow = AutoSurface (SDL_CreateRGBSurface (0, Video.getResolutionX(), Video.getResolutionY(), Video.getColDepth(), 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000));
SDL_FillRect (GraphicsData.gfx_shadow.get(), nullptr, SDL_MapRGBA (GraphicsData.gfx_shadow->format, 0, 0, 0, 50));
SDL_FillRect (GraphicsData.gfx_shadow.get(), nullptr, toSdlAlphaColor (cRgbColor::black(50), *GraphicsData.gfx_shadow));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/resources/playercolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace
{
auto texture = AutoSurface (SDL_CreateRGBSurface (0, 128, 128, 32, 0, 0, 0, 0));

SDL_FillRect (texture.get(), nullptr, toMappedSdlRGBAColor (color, texture->format));
SDL_FillRect (texture.get(), nullptr, toSdlAlphaColor (color, *texture));

auto hsvColor = color.toHsv();

Expand Down Expand Up @@ -122,7 +122,7 @@ namespace
}
SDL_Rect dest = {xPos, yPos, width, height};

SDL_FillRect (texture.get(), &dest, toMappedSdlRGBAColor (getRandom (randomColors), texture->format));
SDL_FillRect (texture.get(), &dest, toSdlAlphaColor (getRandom (randomColors), *texture));
}
return texture;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ui/graphical/game/temp/drawingcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "resources/uidata.h"
#include "resources/vehicleuidata.h"
#include "settings.h"
#include "SDLutility/tosdl.h"
#include "ui/graphical/game/animations/animationtimer.h"
#include "ui/widgets/framecounter.h"
#include "utility/mathtools.h"
Expand Down Expand Up @@ -91,7 +92,7 @@ void sDrawingCacheEntry::init (const cVehicle& vehicle, const cMapView& map, con
}
surface = AutoSurface (SDL_CreateRGBSurface (0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000));

SDL_FillRect (surface.get(), nullptr, SDL_MapRGBA (surface->format, 0, 0, 0, 0));
SDL_FillRect (surface.get(), nullptr, toSdlAlphaColor (cRgbColor::transparent(), *surface));
}

void sDrawingCacheEntry::init (const cBuilding& building, double zoom_, unsigned long long frameNr)
Expand Down Expand Up @@ -120,7 +121,7 @@ void sDrawingCacheEntry::init (const cBuilding& building, double zoom_, unsigned

surface = AutoSurface (SDL_CreateRGBSurface (0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000));

SDL_FillRect (surface.get(), nullptr, SDL_MapRGBA (surface->format, 0, 0, 0, 0));
SDL_FillRect (surface.get(), nullptr, toSdlAlphaColor(cRgbColor::transparent(), *surface));
}

cDrawingCache::cDrawingCache (std::shared_ptr<const cFrameCounter> frameCounter_) :
Expand Down
15 changes: 9 additions & 6 deletions src/ui/graphical/game/widgets/chatbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,20 @@ void cChatBox<ChatListItemType, PlayerListItemType>::createBackground()
nonFocusBackground = AutoSurface (SDL_CreateRGBSurface (0, size.x(), size.y(), Video.getColDepth(), 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000));
focusBackground = AutoSurface (SDL_CreateRGBSurface (0, size.x(), size.y(), Video.getColDepth(), 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000));

const auto black_50 = toSdlAlphaColor (cRgbColor::black (50), *nonFocusBackground);
const auto black_100 = toSdlAlphaColor (cRgbColor::black (100), *focusBackground);

SDL_Rect chatListBackgroundRect = {chatList->getPosition().x() - getPosition().x(), chatList->getPosition().y() - getPosition().y(), chatList->getSize().x(), chatList->getSize().y()};
SDL_FillRect (nonFocusBackground.get(), &chatListBackgroundRect, SDL_MapRGBA (nonFocusBackground->format, 0, 0, 0, 50));
SDL_FillRect (focusBackground.get(), &chatListBackgroundRect, SDL_MapRGBA (focusBackground->format, 0, 0, 0, 100));
SDL_FillRect (nonFocusBackground.get(), &chatListBackgroundRect, black_50);
SDL_FillRect (focusBackground.get(), &chatListBackgroundRect, black_100);

SDL_Rect chatLineEditBackgroundRect = {chatLineEdit->getPosition().x() - getPosition().x() - 2, chatLineEdit->getPosition().y() - getPosition().y() - 2, chatLineEdit->getSize().x() + 4, chatLineEdit->getSize().y() + 4};
SDL_FillRect (nonFocusBackground.get(), &chatLineEditBackgroundRect, SDL_MapRGBA (nonFocusBackground->format, 0, 0, 0, 50));
SDL_FillRect (focusBackground.get(), &chatLineEditBackgroundRect, SDL_MapRGBA (focusBackground->format, 0, 0, 0, 100));
SDL_FillRect (nonFocusBackground.get(), &chatLineEditBackgroundRect, black_50);
SDL_FillRect (focusBackground.get(), &chatLineEditBackgroundRect, black_100);

SDL_Rect playerListBackgroundRect = {playersList->getPosition().x() - getPosition().x(), playersList->getPosition().y() - getPosition().y(), playersList->getSize().x(), playersList->getSize().y()};
SDL_FillRect (nonFocusBackground.get(), &playerListBackgroundRect, SDL_MapRGBA (nonFocusBackground->format, 0, 0, 0, 50));
SDL_FillRect (focusBackground.get(), &playerListBackgroundRect, SDL_MapRGBA (focusBackground->format, 0, 0, 0, 100));
SDL_FillRect (nonFocusBackground.get(), &playerListBackgroundRect, black_50);
SDL_FillRect (focusBackground.get(), &playerListBackgroundRect, black_100);
}

#endif // ui_graphical_game_widgets_chatboxH
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void cChatBoxPlayerListViewItem::updatePlayerFinishedTurn()
SDL_Rect src = {player->getHasFinishedTurn() ? 10 : 0, 0, 10, 10};

AutoSurface readySurface (SDL_CreateRGBSurface (0, src.w, src.h, Video.getColDepth(), 0, 0, 0, 0));
SDL_SetColorKey (readySurface.get(), SDL_TRUE, toMappedSdlRGBAColor (cRgbColor (0, 1, 0), readySurface->format));
SDL_SetColorKey (readySurface.get(), SDL_TRUE, toSdlAlphaColor (cRgbColor (0, 1, 0), *readySurface));
SDL_BlitSurface (GraphicsData.gfx_player_ready.get(), &src, readySurface.get(), nullptr);

readyImage->setImage (readySurface.get());
Expand Down
6 changes: 3 additions & 3 deletions src/ui/graphical/game/widgets/gamemessagelistviewitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ void cGameMessageListViewItem::createBackground()
{
default:
case eGameMessageListViewItemBackgroundColor::DarkGray:
SDL_FillRect (background.get(), nullptr, SDL_MapRGBA (background->format, 0, 0, 0, alpha));
SDL_FillRect (background.get(), nullptr, toSdlAlphaColor(cRgbColor::black(alpha), *background));
break;
case eGameMessageListViewItemBackgroundColor::LightGray:
SDL_FillRect (background.get(), nullptr, SDL_MapRGBA (background->format, 0xFF, 0xFF, 0xFF, alpha));
SDL_FillRect (background.get(), nullptr, toSdlAlphaColor (cRgbColor::white(alpha), *background));
break;
case eGameMessageListViewItemBackgroundColor::Red:
SDL_FillRect (background.get(), nullptr, SDL_MapRGBA (background->format, 0xFF, 0, 0, alpha));
SDL_FillRect (background.get(), nullptr, toSdlAlphaColor (cRgbColor::red(alpha), *background));
break;
}
}
6 changes: 3 additions & 3 deletions src/ui/graphical/game/widgets/minimapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void cMiniMapWidget::drawUnits()
if (!attackUnitsOnly || building->getStaticUnitData().canAttack)
{
const auto color = building->getOwner() ? building->getOwner()->getColor() : neutralColor;
SDL_FillRect (surface.get(), &rect, toMappedSdlRGBAColor (color, surface->format));
SDL_FillRect (surface.get(), &rect, toSdlAlphaColor (color, *surface));
}
}

Expand All @@ -333,7 +333,7 @@ void cMiniMapWidget::drawUnits()
if (!attackUnitsOnly || vehicle->getStaticUnitData().canAttack)
{
const auto color = vehicle->getOwner() ? vehicle->getOwner()->getColor() : neutralColor;
SDL_FillRect (surface.get(), &rect, toMappedSdlRGBAColor (color, surface->format));
SDL_FillRect (surface.get(), &rect, toSdlAlphaColor (color, *surface));
}
}

Expand All @@ -343,7 +343,7 @@ void cMiniMapWidget::drawUnits()
if (!attackUnitsOnly || vehicle->getStaticUnitData().canAttack)
{
const auto color = vehicle->getOwner() ? vehicle->getOwner()->getColor() : neutralColor;
SDL_FillRect (surface.get(), &rect, toMappedSdlRGBAColor (color, surface->format));
SDL_FillRect (surface.get(), &rect, toSdlAlphaColor (color, *surface));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/graphical/menu/dialogs/dialogcolorpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ AutoSurface cDialogColorPicker::createSelectedColorSurface()
{
AutoSurface surface (SDL_CreateRGBSurface (0, 50, 50, 32, 0, 0, 0, 0));

SDL_FillRect (surface.get(), nullptr, toMappedSdlRGBAColor (colorPicker->getSelectedColor(), surface->format));
SDL_FillRect (surface.get(), nullptr, toSdlAlphaColor (colorPicker->getSelectedColor(), *surface));

return surface;
}
8 changes: 4 additions & 4 deletions src/ui/graphical/menu/widgets/colorpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ AutoSurface cRgbColorPicker::createColorsSurface()
color.s = x * 100 / (size.x() - 1);
color.v = 100 - (y * 100 / (size.y() - 1));

putPixel (*surface, cPosition (x, y), toMappedSdlRGBAColor (color.toRgb(), surface->format));
putPixel (*surface, cPosition (x, y), toSdlAlphaColor (color.toRgb(), surface->format));
}
}

Expand All @@ -118,7 +118,7 @@ AutoSurface cRgbColorPicker::createColorBarSurface()
{
const cPosition position (x, y);

putPixel (*surface, position, toMappedSdlRGBAColor (color.toRgb(), surface->format));
putPixel (*surface, position, toSdlAlphaColor (color.toRgb(), surface->format));
}
}

Expand All @@ -130,7 +130,7 @@ AutoSurface cRgbColorPicker::createColorMarkerSurface()
{
AutoSurface surface (SDL_CreateRGBSurface (0, 3, 3, 32, 0, 0, 0, 0));

SDL_FillRect (surface.get(), nullptr, toMappedSdlRGBColor (cRgbColor::white(), surface->format));
SDL_FillRect (surface.get(), nullptr, toSdlColor (cRgbColor::white(), *surface));
drawPoint (*surface, cPosition (1, 1), cRgbColor (0xFF, 0, 0xFF));

return surface;
Expand All @@ -141,7 +141,7 @@ AutoSurface cRgbColorPicker::createColorHueMarkerSurface()
{
AutoSurface surface (SDL_CreateRGBSurface (0, 15 + 2, 3, 32, 0, 0, 0, 0));

SDL_FillRect (surface.get(), nullptr, toMappedSdlRGBColor (cRgbColor::white(), surface->format));
SDL_FillRect (surface.get(), nullptr, toSdlColor (cRgbColor::white(), *surface));

drawLine (*surface, cPosition (1, 1), cPosition (16, 1), cRgbColor (0xFF, 0, 0xFF));

Expand Down
2 changes: 1 addition & 1 deletion src/ui/graphical/menu/widgets/slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void cSlider::createSurface (eSliderType sliderType)
auto size = getSize();

surface = AutoSurface (SDL_CreateRGBSurface (0, size.x(), size.y(), Video.getColDepth(), 0, 0, 0, 0));
SDL_FillRect (surface.get(), nullptr, toMappedSdlRGBAColor (cRgbColor::black(), surface->format));
SDL_FillRect (surface.get(), nullptr, toSdlAlphaColor (cRgbColor::black(), *surface));

drawLine (*surface, cPosition (0, 0), cPosition (0, size.y()), cRgbColor (140, 102, 61));
drawLine (*surface, cPosition (size.x() - 1, 0), cPosition (size.x() - 1, size.y()), cRgbColor (140, 102, 61));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void cChatBoxLandingPlayerListViewItem::updatePlayerHasSelectedPosition()
SDL_Rect src = {playerLandingStatus.hasSelectedPosition() ? 10 : 0, 0, 10, 10};

AutoSurface readySurface (SDL_CreateRGBSurface (0, src.w, src.h, Video.getColDepth(), 0, 0, 0, 0));
SDL_SetColorKey (readySurface.get(), SDL_TRUE, toMappedSdlRGBAColor (cRgbColor (0, 1, 0), readySurface->format));
SDL_SetColorKey (readySurface.get(), SDL_TRUE, toSdlAlphaColor (cRgbColor (0, 1, 0), *readySurface));
SDL_BlitSurface (GraphicsData.gfx_player_ready.get(), &src, readySurface.get(), nullptr);

readyImage->setImage (readySurface.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ cWindowReports::cWindowReports (const cModel& model,
std::string playerText = player->getName() + lngPack.i18n ("Punctuation~Colon") + lngPack.plural ("Comp~Point(s)", player->getScore (turnClock->getTurn())) + ", " + lngPack.plural ("Comp~EcoSphere(s)", player->getNumEcoSpheres());

AutoSurface colorSurface (SDL_CreateRGBSurface (0, 8, 8, Video.getColDepth(), 0, 0, 0, 0));
SDL_FillRect (colorSurface.get(), nullptr, toMappedSdlRGBAColor (player->getColor(), colorSurface->format));
SDL_FillRect (colorSurface.get(), nullptr, toSdlAlphaColor (player->getColor(), *colorSurface));
scoreFrame->emplaceChild<cImage> (scoreFrame->getPosition() + cPosition (5, 20 + font->getFontHeight() * i), colorSurface.get());

ecosphereLabels.emplace_back (scoreFrame->emplaceChild<cLabel> (cBox<cPosition> (scoreFrame->getPosition() + cPosition (16, 20 + font->getFontHeight() * i), scoreFrame->getPosition() + cPosition (16 + 435, 20 + font->getFontHeight() * (i + 1))), playerText));
Expand Down
2 changes: 1 addition & 1 deletion src/ui/widgets/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void cWindow::draw (SDL_Surface& destination, const cBox<cPosition>& clipRect)
switch (backgroundType)
{
case eWindowBackgrounds::Black:
SDL_FillRect (cVideo::buffer, nullptr, 0xFF000000);
SDL_FillRect (cVideo::buffer, nullptr, toSdlColor (cRgbColor::black(), *cVideo::buffer));
break;
case eWindowBackgrounds::Alpha:
// NOTE: this is not fully robust yet! It will not work if an
Expand Down

0 comments on commit ef3f6b0

Please sign in to comment.