1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-03 14:52:11 +02:00

Added Color class to replace SDL_Color

This commit is contained in:
Ivan Savenko 2023-01-30 00:12:43 +02:00
parent 07dc2e1c48
commit 42df5626d9
23 changed files with 137 additions and 68 deletions

View File

@ -63,7 +63,6 @@
#include "../lib/CPathfinder.h" #include "../lib/CPathfinder.h"
#include "../lib/RoadHandler.h" #include "../lib/RoadHandler.h"
#include "../lib/TerrainHandler.h" #include "../lib/TerrainHandler.h"
#include <SDL_timer.h>
#include "CServerHandler.h" #include "CServerHandler.h"
// FIXME: only needed for CGameState::mutex // FIXME: only needed for CGameState::mutex
#include "../lib/CGameState.h" #include "../lib/CGameState.h"
@ -342,7 +341,7 @@ void CPlayerInterface::heroMoved(const TryMoveHero & details, bool verbose)
auto unlockPim = vstd::makeUnlockGuard(*pim); auto unlockPim = vstd::makeUnlockGuard(*pim);
while(frameNumber == GH.mainFPSmng->getFrameNumber()) while(frameNumber == GH.mainFPSmng->getFrameNumber())
SDL_Delay(5); boost::this_thread::sleep(boost::posix_time::milliseconds(5));
}; };
//first initializing done //first initializing done
@ -1514,7 +1513,7 @@ void CPlayerInterface::centerView (int3 pos, int focusTime)
{ {
auto unlockPim = vstd::makeUnlockGuard(*pim); auto unlockPim = vstd::makeUnlockGuard(*pim);
IgnoreEvents ignore(*this); IgnoreEvents ignore(*this);
SDL_Delay(focusTime); boost::this_thread::sleep(boost::posix_time::milliseconds(focusTime));
} }
} }
CCS->curh->show(); CCS->curh->show();
@ -2276,7 +2275,7 @@ void CPlayerInterface::waitForAllDialogs(bool unlockPim)
while(!dialogs.empty()) while(!dialogs.empty())
{ {
auto unlock = vstd::makeUnlockGuardIf(*pim, unlockPim); auto unlock = vstd::makeUnlockGuardIf(*pim, unlockPim);
SDL_Delay(5); boost::this_thread::sleep(boost::posix_time::milliseconds(5));
} }
waitWhileDialog(unlockPim); waitWhileDialog(unlockPim);
} }

View File

@ -15,6 +15,8 @@
#include "CPlayerInterface.h" #include "CPlayerInterface.h"
#include "../lib/filesystem/Filesystem.h" #include "../lib/filesystem/Filesystem.h"
#include <SDL_render.h>
extern CGuiHandler GH; //global gui handler extern CGuiHandler GH; //global gui handler
#ifndef DISABLE_VIDEO #ifndef DISABLE_VIDEO

View File

@ -116,10 +116,7 @@ void ProjectileRay::show(Canvas & canvas)
for (size_t i = 0; i < rayConfig.size(); ++i) for (size_t i = 0; i < rayConfig.size(); ++i)
{ {
auto ray = rayConfig[i]; auto ray = rayConfig[i];
SDL_Color beginColor{ ray.r1, ray.g1, ray.b1, ray.a1}; canvas.drawLine(Point(x1, y1 + i), Point(x2, y2+i), ray.start, ray.end);
SDL_Color endColor { ray.r2, ray.g2, ray.b2, ray.a2};
canvas.drawLine(Point(x1, y1 + i), Point(x2, y2+i), beginColor, endColor);
} }
} }
else // draw in vertical axis else // draw in vertical axis
@ -133,10 +130,8 @@ void ProjectileRay::show(Canvas & canvas)
for (size_t i = 0; i < rayConfig.size(); ++i) for (size_t i = 0; i < rayConfig.size(); ++i)
{ {
auto ray = rayConfig[i]; auto ray = rayConfig[i];
SDL_Color beginColor{ ray.r1, ray.g1, ray.b1, ray.a1};
SDL_Color endColor { ray.r2, ray.g2, ray.b2, ray.a2};
canvas.drawLine(Point(x1 + i, y1), Point(x2 + i, y2), beginColor, endColor); canvas.drawLine(Point(x1 + i, y1), Point(x2 + i, y2), ray.start, ray.end);
} }
} }

View File

@ -13,6 +13,8 @@
#include "../widgets/Images.h" #include "../widgets/Images.h"
#include "../gui/CAnimation.h" #include "../gui/CAnimation.h"
#include <SDL_render.h>
class CIntObject; class CIntObject;
class CreatureAnimation; class CreatureAnimation;
class Canvas; class Canvas;

View File

@ -17,10 +17,11 @@
#include "../CBitmapHandler.h" #include "../CBitmapHandler.h"
#include "../Graphics.h" #include "../Graphics.h"
#include "../lib/filesystem/Filesystem.h" #include "../../lib/filesystem/Filesystem.h"
#include "../lib/filesystem/ISimpleResourceLoader.h" #include "../../lib/filesystem/ISimpleResourceLoader.h"
#include "../lib/JsonNode.h" #include "../../lib/JsonNode.h"
#include "../lib/CRandomGenerator.h" #include "../../lib/CRandomGenerator.h"
#include "../../lib/vcmi_endian.h"
class SDLImageLoader; class SDLImageLoader;

View File

@ -9,7 +9,6 @@
*/ */
#pragma once #pragma once
#include "../../lib/vcmi_endian.h"
#include "../../lib/GameConstants.h" #include "../../lib/GameConstants.h"
#ifdef IN #ifdef IN

View File

@ -11,8 +11,6 @@
#include "CGuiHandler.h" #include "CGuiHandler.h"
#include "../lib/CondSh.h" #include "../lib/CondSh.h"
#include <SDL_timer.h>
#include "CIntObject.h" #include "CIntObject.h"
#include "CursorHandler.h" #include "CursorHandler.h"
#include "SDL_Extensions.h" #include "SDL_Extensions.h"
@ -24,6 +22,9 @@
#include "../CPlayerInterface.h" #include "../CPlayerInterface.h"
#include "../battle/BattleInterface.h" #include "../battle/BattleInterface.h"
#include <SDL_render.h>
#include <SDL_timer.h>
extern std::queue<SDL_Event> SDLEventsQueue; extern std::queue<SDL_Event> SDLEventsQueue;
extern boost::mutex eventsM; extern boost::mutex eventsM;
@ -641,12 +642,11 @@ const Point & CGuiHandler::getCursorPosition() const
void CGuiHandler::drawFPSCounter() void CGuiHandler::drawFPSCounter()
{ {
const static SDL_Color yellow = {255, 255, 0, 0};
static SDL_Rect overlay = { 0, 0, 64, 32}; static SDL_Rect overlay = { 0, 0, 64, 32};
Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10); Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
SDL_FillRect(screen, &overlay, black); SDL_FillRect(screen, &overlay, black);
std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps); std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, yellow, Point(10, 10)); graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, Colors::YELLOW, Point(10, 10));
} }
SDL_Keycode CGuiHandler::arrowToNum(SDL_Keycode key) SDL_Keycode CGuiHandler::arrowToNum(SDL_Keycode key)
@ -751,7 +751,8 @@ void CFramerateManager::framerateDelay()
// FPS is higher than it should be, then wait some time // FPS is higher than it should be, then wait some time
if(timeElapsed < rateticks) if(timeElapsed < rateticks)
{ {
SDL_Delay((Uint32)ceil(this->rateticks) - timeElapsed); int timeToSleep = (Uint32)ceil(this->rateticks) - timeElapsed;
boost::this_thread::sleep(boost::posix_time::milliseconds(timeToSleep));
} }
currentTicks = SDL_GetTicks(); currentTicks = SDL_GetTicks();

View File

@ -76,9 +76,9 @@ void Canvas::draw(Canvas & image, const Point & pos)
CSDL_Ext::blitAt(image.surface, renderOffset.x + pos.x, renderOffset.y + pos.y, surface); CSDL_Ext::blitAt(image.surface, renderOffset.x + pos.x, renderOffset.y + pos.y, surface);
} }
void Canvas::drawLine(const Point & from, const Point & dest, const SDL_Color & colorFrom, const SDL_Color & colorDest) void Canvas::drawLine(const Point & from, const Point & dest, const ColorRGBA & colorFrom, const ColorRGBA & colorDest)
{ {
CSDL_Ext::drawLine(surface, renderOffset.x + from.x, renderOffset.y + from.y, renderOffset.x + dest.x, renderOffset.y + dest.y, colorFrom, colorDest); CSDL_Ext::drawLine(surface, renderOffset.x + from.x, renderOffset.y + from.y, renderOffset.x + dest.x, renderOffset.y + dest.y, CSDL_Ext::toSDL(colorFrom), CSDL_Ext::toSDL(colorDest));
} }
void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text ) void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text )

View File

@ -11,6 +11,7 @@
#include "TextAlignment.h" #include "TextAlignment.h"
#include "../../lib/Rect.h" #include "../../lib/Rect.h"
#include "../../lib/Color.h"
struct SDL_Color; struct SDL_Color;
struct SDL_Surface; struct SDL_Surface;
@ -56,7 +57,7 @@ public:
void draw(Canvas & image, const Point & pos); void draw(Canvas & image, const Point & pos);
/// renders continuous, 1-pixel wide line with color gradient /// renders continuous, 1-pixel wide line with color gradient
void drawLine(const Point & from, const Point & dest, const SDL_Color & colorFrom, const SDL_Color & colorDest); void drawLine(const Point & from, const Point & dest, const ColorRGBA & colorFrom, const ColorRGBA & colorDest);
/// renders single line of text with specified parameters /// renders single line of text with specified parameters
void drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text ); void drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text );

View File

@ -16,6 +16,8 @@
#include "CAnimation.h" #include "CAnimation.h"
#include "../../lib/CConfigHandler.h" #include "../../lib/CConfigHandler.h"
#include <SDL_render.h>
#ifdef VCMI_APPLE #ifdef VCMI_APPLE
#include <dispatch/dispatch.h> #include <dispatch/dispatch.h>
#endif #endif

View File

@ -268,10 +268,7 @@ size_t CTrueTypeFont::getStringWidth(const std::string & data) const
void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
{ {
if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
{ renderText(surface, data, Colors::BLACK, pos + Point(1,1));
SDL_Color black = { 0, 0, 0, SDL_ALPHA_OPAQUE};
renderText(surface, data, black, pos + Point(1,1));
}
if (!data.empty()) if (!data.empty())
{ {

View File

@ -66,6 +66,22 @@ SDL_Rect CSDL_Ext::toSDL(const Rect & rect)
return result; return result;
} }
ColorRGBA CSDL_Ext::fromSDL(const SDL_Color & color)
{
return { color.r, color.g, color.b, color.a };
}
SDL_Color CSDL_Ext::toSDL(const ColorRGBA & color)
{
SDL_Color result;
result.r = color.r;
result.g = color.g;
result.b = color.b;
result.a = color.a;
return result;
}
void CSDL_Ext::setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors) void CSDL_Ext::setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
{ {
SDL_SetPaletteColors(surface->format->palette,colors,firstcolor,ncolors); SDL_SetPaletteColors(surface->format->palette,colors,firstcolor,ncolors);

View File

@ -9,10 +9,10 @@
*/ */
#pragma once #pragma once
#include <SDL_render.h>
#include <SDL_events.h> #include <SDL_events.h>
#include "../../lib/GameConstants.h" #include "../../lib/GameConstants.h"
#include "../../lib/Rect.h" #include "../../lib/Rect.h"
#include "../../lib/Color.h"
struct SDL_Window; struct SDL_Window;
struct SDL_Renderer; struct SDL_Renderer;
@ -79,6 +79,12 @@ Rect fromSDL(const SDL_Rect & rect);
/// creates SDL_Rect using provided rect /// creates SDL_Rect using provided rect
SDL_Rect toSDL(const Rect & rect); SDL_Rect toSDL(const Rect & rect);
/// creates Color using provided SDL_Color
ColorRGBA fromSDL(const SDL_Color & color);
/// creates SDL_Color using provided Color
SDL_Color toSDL(const ColorRGBA & color);
void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors); void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors);
void warpMouse(int x, int y); void warpMouse(int x, int y);
bool isCtrlKeyDown(); bool isCtrlKeyDown();

View File

@ -355,13 +355,11 @@ void CTownList::update(const CGTownInstance *)
const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos) const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
{ {
static const SDL_Color fogOfWar = {0, 0, 0, 255};
const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false); const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
// if tile is not visible it will be black on minimap // if tile is not visible it will be black on minimap
if(!tile) if(!tile)
return fogOfWar; return Colors::BLACK;
// if object at tile is owned - it will be colored as its owner // if object at tile is owned - it will be colored as its owner
for (const CGObjectInstance *obj : tile->blockingObjects) for (const CGObjectInstance *obj : tile->blockingObjects)
@ -494,21 +492,8 @@ std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors()
for(const auto & terrain : CGI->terrainTypeHandler->objects) for(const auto & terrain : CGI->terrainTypeHandler->objects)
{ {
SDL_Color normal = SDL_Color normal = CSDL_Ext::toSDL(terrain->minimapUnblocked);
{ SDL_Color blocked = CSDL_Ext::toSDL(terrain->minimapBlocked);
ui8(terrain->minimapUnblocked[0]),
ui8(terrain->minimapUnblocked[1]),
ui8(terrain->minimapUnblocked[2]),
ui8(255)
};
SDL_Color blocked =
{
ui8(terrain->minimapBlocked[0]),
ui8(terrain->minimapBlocked[1]),
ui8(terrain->minimapBlocked[2]),
ui8(255)
};
ret[terrain->getId()] = std::make_pair(normal, blocked); ret[terrain->getId()] = std::make_pair(normal, blocked);
} }

View File

@ -12,13 +12,12 @@
#include "../gui/CIntObject.h" #include "../gui/CIntObject.h"
#include "../battle/BattleConstants.h" #include "../battle/BattleConstants.h"
#include <SDL_render.h>
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
class Rect; class Rect;
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END
struct SDL_Surface; struct SDL_Surface;
struct SDL_Color;
class CAnimImage; class CAnimImage;
class CLabel; class CLabel;
class CAnimation; class CAnimation;

View File

@ -192,9 +192,9 @@ void CBuildingRect::show(SDL_Surface * to)
if(border->format->palette != nullptr) if(border->format->palette != nullptr)
{ {
// key colors in glowing border // key colors in glowing border
SDL_Color c1 = {200, 200, 200, 255}; SDL_Color c1 = {200, 200, 200, 255}; // x2
SDL_Color c2 = {120, 100, 60, 255}; SDL_Color c2 = {120, 100, 60, 255}; // x0.5
SDL_Color c3 = {200, 180, 110, 255}; SDL_Color c3 = {210, 180, 110, 255}; // x1
ui32 colorID = SDL_MapRGB(border->format, c3.r, c3.g, c3.b); ui32 colorID = SDL_MapRGB(border->format, c3.r, c3.g, c3.b);
SDL_Color oldColor = border->format->palette->colors[colorID]; SDL_Color oldColor = border->format->palette->colors[colorID];

View File

@ -640,9 +640,8 @@ void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
SDL_Color firstLineColor, secondLineColor; SDL_Color firstLineColor, secondLineColor;
if(spellCost > owner->myHero->mana) //hero cannot cast this spell if(spellCost > owner->myHero->mana) //hero cannot cast this spell
{ {
static const SDL_Color unavailableSpell = {239, 189, 33, 0};
firstLineColor = Colors::WHITE; firstLineColor = Colors::WHITE;
secondLineColor = unavailableSpell; secondLineColor = Colors::ORANGE;
} }
else else
{ {

View File

@ -414,6 +414,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
${MAIN_LIB_DIR}/CModHandler.h ${MAIN_LIB_DIR}/CModHandler.h
${MAIN_LIB_DIR}/CondSh.h ${MAIN_LIB_DIR}/CondSh.h
${MAIN_LIB_DIR}/ConstTransitivePtr.h ${MAIN_LIB_DIR}/ConstTransitivePtr.h
${MAIN_LIB_DIR}/Color.h
${MAIN_LIB_DIR}/CPathfinder.h ${MAIN_LIB_DIR}/CPathfinder.h
${MAIN_LIB_DIR}/CPlayerState.h ${MAIN_LIB_DIR}/CPlayerState.h
${MAIN_LIB_DIR}/CRandomGenerator.h ${MAIN_LIB_DIR}/CRandomGenerator.h

View File

@ -927,15 +927,15 @@ void CCreatureHandler::loadCreatureJson(CCreature * creature, const JsonNode & c
{ {
CCreature::CreatureAnimation::RayColor color; CCreature::CreatureAnimation::RayColor color;
color.r1 = value["start"].Vector()[0].Integer(); color.start.r = value["start"].Vector()[0].Integer();
color.g1 = value["start"].Vector()[1].Integer(); color.start.g = value["start"].Vector()[1].Integer();
color.b1 = value["start"].Vector()[2].Integer(); color.start.b = value["start"].Vector()[2].Integer();
color.a1 = value["start"].Vector()[3].Integer(); color.start.a = value["start"].Vector()[3].Integer();
color.r2 = value["end"].Vector()[0].Integer(); color.end.r = value["end"].Vector()[0].Integer();
color.g2 = value["end"].Vector()[1].Integer(); color.end.g = value["end"].Vector()[1].Integer();
color.b2 = value["end"].Vector()[2].Integer(); color.end.b = value["end"].Vector()[2].Integer();
color.a2 = value["end"].Vector()[3].Integer(); color.end.a = value["end"].Vector()[3].Integer();
creature->animation.projectileRay.push_back(color); creature->animation.projectileRay.push_back(color);
} }

View File

@ -19,6 +19,7 @@
#include "JsonNode.h" #include "JsonNode.h"
#include "IHandlerBase.h" #include "IHandlerBase.h"
#include "CRandomGenerator.h" #include "CRandomGenerator.h"
#include "Color.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@ -63,12 +64,12 @@ public:
struct CreatureAnimation struct CreatureAnimation
{ {
struct RayColor { struct RayColor {
uint8_t r1, g1, b1, a1; ColorRGBA start;
uint8_t r2, g2, b2, a2; ColorRGBA end;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {
h & r1 & g1 & b1 & a1 & r2 & g2 & b2 & a2; h & start & end;
} }
}; };

62
lib/Color.h Normal file
View File

@ -0,0 +1,62 @@
/*
* Color.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
VCMI_LIB_NAMESPACE_BEGIN
/// An object that represents RGBA color
class ColorRGBA
{
public:
enum : uint8_t
{
ALPHA_OPAQUE = 255,
ALPHA_TRANSPARENT = 0,
};
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
//constructors
ColorRGBA()
:r(0)
,g(0)
,b(0)
,a(0)
{
}
ColorRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
: r(r)
, g(g)
, b(b)
, a(a)
{}
ColorRGBA(uint8_t r, uint8_t g, uint8_t b)
: r(r)
, g(g)
, b(b)
, a(ALPHA_OPAQUE)
{}
template <typename Handler>
void serialize(Handler &h, const int version)
{
h & r;
h & g;
h & b;
h & a;
}
};
VCMI_LIB_NAMESPACE_END

View File

@ -14,6 +14,7 @@
#include <vcmi/Entity.h> #include <vcmi/Entity.h>
#include "GameConstants.h" #include "GameConstants.h"
#include "IHandlerBase.h" #include "IHandlerBase.h"
#include "Color.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@ -46,8 +47,8 @@ public:
std::vector<BattleField> battleFields; std::vector<BattleField> battleFields;
std::vector<TerrainId> prohibitTransitions; std::vector<TerrainId> prohibitTransitions;
std::array<int, 3> minimapBlocked; ColorRGBA minimapBlocked;
std::array<int, 3> minimapUnblocked; ColorRGBA minimapUnblocked;
std::string shortIdentifier; std::string shortIdentifier;
std::string musicFilename; std::string musicFilename;
std::string tilesFilename; std::string tilesFilename;

View File

@ -433,7 +433,7 @@ QRgb MapHandler::getTileColor(int x, int y, int z)
if (tile.blocked && (!tile.visitable)) if (tile.blocked && (!tile.visitable))
color = tile.terType->minimapBlocked; color = tile.terType->minimapBlocked;
return qRgb(color[0], color[1], color[2]); return qRgb(color.r, color.g, color.b);
} }
void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z) void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z)