diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index 2076695b7..bb7cbd2e7 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -63,7 +63,6 @@ #include "../lib/CPathfinder.h" #include "../lib/RoadHandler.h" #include "../lib/TerrainHandler.h" -#include #include "CServerHandler.h" // FIXME: only needed for CGameState::mutex #include "../lib/CGameState.h" @@ -342,7 +341,7 @@ void CPlayerInterface::heroMoved(const TryMoveHero & details, bool verbose) auto unlockPim = vstd::makeUnlockGuard(*pim); while(frameNumber == GH.mainFPSmng->getFrameNumber()) - SDL_Delay(5); + boost::this_thread::sleep(boost::posix_time::milliseconds(5)); }; //first initializing done @@ -1514,7 +1513,7 @@ void CPlayerInterface::centerView (int3 pos, int focusTime) { auto unlockPim = vstd::makeUnlockGuard(*pim); IgnoreEvents ignore(*this); - SDL_Delay(focusTime); + boost::this_thread::sleep(boost::posix_time::milliseconds(focusTime)); } } CCS->curh->show(); @@ -2276,7 +2275,7 @@ void CPlayerInterface::waitForAllDialogs(bool unlockPim) while(!dialogs.empty()) { auto unlock = vstd::makeUnlockGuardIf(*pim, unlockPim); - SDL_Delay(5); + boost::this_thread::sleep(boost::posix_time::milliseconds(5)); } waitWhileDialog(unlockPim); } diff --git a/client/CVideoHandler.cpp b/client/CVideoHandler.cpp index b3dbe9563..8d3d37ad5 100644 --- a/client/CVideoHandler.cpp +++ b/client/CVideoHandler.cpp @@ -15,6 +15,8 @@ #include "CPlayerInterface.h" #include "../lib/filesystem/Filesystem.h" +#include + extern CGuiHandler GH; //global gui handler #ifndef DISABLE_VIDEO diff --git a/client/battle/BattleProjectileController.cpp b/client/battle/BattleProjectileController.cpp index 429d11fc1..c71d09ee7 100644 --- a/client/battle/BattleProjectileController.cpp +++ b/client/battle/BattleProjectileController.cpp @@ -116,10 +116,7 @@ void ProjectileRay::show(Canvas & canvas) for (size_t i = 0; i < rayConfig.size(); ++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, y1 + i), Point(x2, y2+i), beginColor, endColor); + canvas.drawLine(Point(x1, y1 + i), Point(x2, y2+i), ray.start, ray.end); } } else // draw in vertical axis @@ -133,10 +130,8 @@ void ProjectileRay::show(Canvas & canvas) for (size_t i = 0; i < rayConfig.size(); ++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); } } diff --git a/client/battle/CreatureAnimation.h b/client/battle/CreatureAnimation.h index cdead9c79..482b12b87 100644 --- a/client/battle/CreatureAnimation.h +++ b/client/battle/CreatureAnimation.h @@ -13,6 +13,8 @@ #include "../widgets/Images.h" #include "../gui/CAnimation.h" +#include + class CIntObject; class CreatureAnimation; class Canvas; diff --git a/client/gui/CAnimation.cpp b/client/gui/CAnimation.cpp index 50052fe48..448f0b389 100644 --- a/client/gui/CAnimation.cpp +++ b/client/gui/CAnimation.cpp @@ -17,10 +17,11 @@ #include "../CBitmapHandler.h" #include "../Graphics.h" -#include "../lib/filesystem/Filesystem.h" -#include "../lib/filesystem/ISimpleResourceLoader.h" -#include "../lib/JsonNode.h" -#include "../lib/CRandomGenerator.h" +#include "../../lib/filesystem/Filesystem.h" +#include "../../lib/filesystem/ISimpleResourceLoader.h" +#include "../../lib/JsonNode.h" +#include "../../lib/CRandomGenerator.h" +#include "../../lib/vcmi_endian.h" class SDLImageLoader; diff --git a/client/gui/CAnimation.h b/client/gui/CAnimation.h index d0e4ab84c..62793903d 100644 --- a/client/gui/CAnimation.h +++ b/client/gui/CAnimation.h @@ -9,7 +9,6 @@ */ #pragma once -#include "../../lib/vcmi_endian.h" #include "../../lib/GameConstants.h" #ifdef IN diff --git a/client/gui/CGuiHandler.cpp b/client/gui/CGuiHandler.cpp index ced56145f..a2ea6c00a 100644 --- a/client/gui/CGuiHandler.cpp +++ b/client/gui/CGuiHandler.cpp @@ -11,8 +11,6 @@ #include "CGuiHandler.h" #include "../lib/CondSh.h" -#include - #include "CIntObject.h" #include "CursorHandler.h" #include "SDL_Extensions.h" @@ -24,6 +22,9 @@ #include "../CPlayerInterface.h" #include "../battle/BattleInterface.h" +#include +#include + extern std::queue SDLEventsQueue; extern boost::mutex eventsM; @@ -641,12 +642,11 @@ const Point & CGuiHandler::getCursorPosition() const void CGuiHandler::drawFPSCounter() { - const static SDL_Color yellow = {255, 255, 0, 0}; static SDL_Rect overlay = { 0, 0, 64, 32}; Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10); SDL_FillRect(screen, &overlay, black); std::string fps = boost::lexical_cast(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) @@ -751,7 +751,8 @@ void CFramerateManager::framerateDelay() // FPS is higher than it should be, then wait some time 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(); diff --git a/client/gui/Canvas.cpp b/client/gui/Canvas.cpp index adc443924..95d2f555c 100644 --- a/client/gui/Canvas.cpp +++ b/client/gui/Canvas.cpp @@ -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); } -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 ) diff --git a/client/gui/Canvas.h b/client/gui/Canvas.h index d4fcc1619..39a4df9fa 100644 --- a/client/gui/Canvas.h +++ b/client/gui/Canvas.h @@ -11,6 +11,7 @@ #include "TextAlignment.h" #include "../../lib/Rect.h" +#include "../../lib/Color.h" struct SDL_Color; struct SDL_Surface; @@ -56,7 +57,7 @@ public: void draw(Canvas & image, const Point & pos); /// 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 void drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text ); diff --git a/client/gui/CursorHandler.cpp b/client/gui/CursorHandler.cpp index 39c19b860..b7392ff14 100644 --- a/client/gui/CursorHandler.cpp +++ b/client/gui/CursorHandler.cpp @@ -16,6 +16,8 @@ #include "CAnimation.h" #include "../../lib/CConfigHandler.h" +#include + #ifdef VCMI_APPLE #include #endif diff --git a/client/gui/Fonts.cpp b/client/gui/Fonts.cpp index 277a5bd51..6b46818c1 100644 --- a/client/gui/Fonts.cpp +++ b/client/gui/Fonts.cpp @@ -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 { if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow - { - SDL_Color black = { 0, 0, 0, SDL_ALPHA_OPAQUE}; - renderText(surface, data, black, pos + Point(1,1)); - } + renderText(surface, data, Colors::BLACK, pos + Point(1,1)); if (!data.empty()) { diff --git a/client/gui/SDL_Extensions.cpp b/client/gui/SDL_Extensions.cpp index 0d6a41e2e..62c2c5450 100644 --- a/client/gui/SDL_Extensions.cpp +++ b/client/gui/SDL_Extensions.cpp @@ -66,6 +66,22 @@ SDL_Rect CSDL_Ext::toSDL(const Rect & rect) 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) { SDL_SetPaletteColors(surface->format->palette,colors,firstcolor,ncolors); diff --git a/client/gui/SDL_Extensions.h b/client/gui/SDL_Extensions.h index 8d5ff8859..4dbec2382 100644 --- a/client/gui/SDL_Extensions.h +++ b/client/gui/SDL_Extensions.h @@ -9,10 +9,10 @@ */ #pragma once -#include #include #include "../../lib/GameConstants.h" #include "../../lib/Rect.h" +#include "../../lib/Color.h" struct SDL_Window; struct SDL_Renderer; @@ -79,6 +79,12 @@ Rect fromSDL(const SDL_Rect & rect); /// creates SDL_Rect using provided 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 warpMouse(int x, int y); bool isCtrlKeyDown(); diff --git a/client/widgets/AdventureMapClasses.cpp b/client/widgets/AdventureMapClasses.cpp index e507e99ff..1b9b828b3 100644 --- a/client/widgets/AdventureMapClasses.cpp +++ b/client/widgets/AdventureMapClasses.cpp @@ -355,13 +355,11 @@ void CTownList::update(const CGTownInstance *) 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); // if tile is not visible it will be black on minimap if(!tile) - return fogOfWar; + return Colors::BLACK; // if object at tile is owned - it will be colored as its owner for (const CGObjectInstance *obj : tile->blockingObjects) @@ -494,21 +492,8 @@ std::map > CMinimap::loadColors() for(const auto & terrain : CGI->terrainTypeHandler->objects) { - SDL_Color normal = - { - 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) - }; + SDL_Color normal = CSDL_Ext::toSDL(terrain->minimapUnblocked); + SDL_Color blocked = CSDL_Ext::toSDL(terrain->minimapBlocked); ret[terrain->getId()] = std::make_pair(normal, blocked); } diff --git a/client/widgets/Images.h b/client/widgets/Images.h index 2b4b1bd24..626f1fb34 100644 --- a/client/widgets/Images.h +++ b/client/widgets/Images.h @@ -12,13 +12,12 @@ #include "../gui/CIntObject.h" #include "../battle/BattleConstants.h" -#include - VCMI_LIB_NAMESPACE_BEGIN class Rect; VCMI_LIB_NAMESPACE_END struct SDL_Surface; +struct SDL_Color; class CAnimImage; class CLabel; class CAnimation; diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 1dafce97f..37025192c 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -192,9 +192,9 @@ void CBuildingRect::show(SDL_Surface * to) if(border->format->palette != nullptr) { // key colors in glowing border - SDL_Color c1 = {200, 200, 200, 255}; - SDL_Color c2 = {120, 100, 60, 255}; - SDL_Color c3 = {200, 180, 110, 255}; + SDL_Color c1 = {200, 200, 200, 255}; // x2 + SDL_Color c2 = {120, 100, 60, 255}; // x0.5 + SDL_Color c3 = {210, 180, 110, 255}; // x1 ui32 colorID = SDL_MapRGB(border->format, c3.r, c3.g, c3.b); SDL_Color oldColor = border->format->palette->colors[colorID]; diff --git a/client/windows/CSpellWindow.cpp b/client/windows/CSpellWindow.cpp index f18dadaaa..c68e70746 100644 --- a/client/windows/CSpellWindow.cpp +++ b/client/windows/CSpellWindow.cpp @@ -640,9 +640,8 @@ void CSpellWindow::SpellArea::setSpell(const CSpell * spell) SDL_Color firstLineColor, secondLineColor; if(spellCost > owner->myHero->mana) //hero cannot cast this spell { - static const SDL_Color unavailableSpell = {239, 189, 33, 0}; firstLineColor = Colors::WHITE; - secondLineColor = unavailableSpell; + secondLineColor = Colors::ORANGE; } else { diff --git a/cmake_modules/VCMI_lib.cmake b/cmake_modules/VCMI_lib.cmake index 0190221ac..dfdb844f6 100644 --- a/cmake_modules/VCMI_lib.cmake +++ b/cmake_modules/VCMI_lib.cmake @@ -414,6 +414,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/CModHandler.h ${MAIN_LIB_DIR}/CondSh.h ${MAIN_LIB_DIR}/ConstTransitivePtr.h + ${MAIN_LIB_DIR}/Color.h ${MAIN_LIB_DIR}/CPathfinder.h ${MAIN_LIB_DIR}/CPlayerState.h ${MAIN_LIB_DIR}/CRandomGenerator.h diff --git a/lib/CCreatureHandler.cpp b/lib/CCreatureHandler.cpp index f5e256301..15d54d74c 100644 --- a/lib/CCreatureHandler.cpp +++ b/lib/CCreatureHandler.cpp @@ -927,15 +927,15 @@ void CCreatureHandler::loadCreatureJson(CCreature * creature, const JsonNode & c { CCreature::CreatureAnimation::RayColor color; - color.r1 = value["start"].Vector()[0].Integer(); - color.g1 = value["start"].Vector()[1].Integer(); - color.b1 = value["start"].Vector()[2].Integer(); - color.a1 = value["start"].Vector()[3].Integer(); + color.start.r = value["start"].Vector()[0].Integer(); + color.start.g = value["start"].Vector()[1].Integer(); + color.start.b = value["start"].Vector()[2].Integer(); + color.start.a = value["start"].Vector()[3].Integer(); - color.r2 = value["end"].Vector()[0].Integer(); - color.g2 = value["end"].Vector()[1].Integer(); - color.b2 = value["end"].Vector()[2].Integer(); - color.a2 = value["end"].Vector()[3].Integer(); + color.end.r = value["end"].Vector()[0].Integer(); + color.end.g = value["end"].Vector()[1].Integer(); + color.end.b = value["end"].Vector()[2].Integer(); + color.end.a = value["end"].Vector()[3].Integer(); creature->animation.projectileRay.push_back(color); } diff --git a/lib/CCreatureHandler.h b/lib/CCreatureHandler.h index 0958c6eea..a61fb5568 100644 --- a/lib/CCreatureHandler.h +++ b/lib/CCreatureHandler.h @@ -19,6 +19,7 @@ #include "JsonNode.h" #include "IHandlerBase.h" #include "CRandomGenerator.h" +#include "Color.h" VCMI_LIB_NAMESPACE_BEGIN @@ -63,12 +64,12 @@ public: struct CreatureAnimation { struct RayColor { - uint8_t r1, g1, b1, a1; - uint8_t r2, g2, b2, a2; + ColorRGBA start; + ColorRGBA end; template void serialize(Handler &h, const int version) { - h & r1 & g1 & b1 & a1 & r2 & g2 & b2 & a2; + h & start & end; } }; diff --git a/lib/Color.h b/lib/Color.h new file mode 100644 index 000000000..f41f53d46 --- /dev/null +++ b/lib/Color.h @@ -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 + void serialize(Handler &h, const int version) + { + h & r; + h & g; + h & b; + h & a; + } +}; + +VCMI_LIB_NAMESPACE_END diff --git a/lib/TerrainHandler.h b/lib/TerrainHandler.h index 648775b42..5d7ba3e1f 100644 --- a/lib/TerrainHandler.h +++ b/lib/TerrainHandler.h @@ -14,6 +14,7 @@ #include #include "GameConstants.h" #include "IHandlerBase.h" +#include "Color.h" VCMI_LIB_NAMESPACE_BEGIN @@ -46,8 +47,8 @@ public: std::vector battleFields; std::vector prohibitTransitions; - std::array minimapBlocked; - std::array minimapUnblocked; + ColorRGBA minimapBlocked; + ColorRGBA minimapUnblocked; std::string shortIdentifier; std::string musicFilename; std::string tilesFilename; diff --git a/mapeditor/maphandler.cpp b/mapeditor/maphandler.cpp index b9799eb0e..0df7b091a 100644 --- a/mapeditor/maphandler.cpp +++ b/mapeditor/maphandler.cpp @@ -433,7 +433,7 @@ QRgb MapHandler::getTileColor(int x, int y, int z) if (tile.blocked && (!tile.visitable)) 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)