浏览代码

Added Color class to replace SDL_Color

Ivan Savenko 2 年之前
父节点
当前提交
42df5626d9

+ 3 - 4
client/CPlayerInterface.cpp

@@ -63,7 +63,6 @@
 #include "../lib/CPathfinder.h"
 #include "../lib/RoadHandler.h"
 #include "../lib/TerrainHandler.h"
-#include <SDL_timer.h>
 #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);
 }

+ 2 - 0
client/CVideoHandler.cpp

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

+ 2 - 7
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);
 		}
 	}
 

+ 2 - 0
client/battle/CreatureAnimation.h

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

+ 5 - 4
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;
 

+ 0 - 1
client/gui/CAnimation.h

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

+ 6 - 5
client/gui/CGuiHandler.cpp

@@ -11,8 +11,6 @@
 #include "CGuiHandler.h"
 #include "../lib/CondSh.h"
 
-#include <SDL_timer.h>
-
 #include "CIntObject.h"
 #include "CursorHandler.h"
 #include "SDL_Extensions.h"
@@ -24,6 +22,9 @@
 #include "../CPlayerInterface.h"
 #include "../battle/BattleInterface.h"
 
+#include <SDL_render.h>
+#include <SDL_timer.h>
+
 extern std::queue<SDL_Event> 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<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)
@@ -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();

+ 2 - 2
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 )

+ 2 - 1
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 );

+ 2 - 0
client/gui/CursorHandler.cpp

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

+ 1 - 4
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())
 	{

+ 16 - 0
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);

+ 7 - 1
client/gui/SDL_Extensions.h

@@ -9,10 +9,10 @@
  */
 
 #pragma once
-#include <SDL_render.h>
 #include <SDL_events.h>
 #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();

+ 3 - 18
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<TerrainId, std::pair<SDL_Color, SDL_Color> > 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);
 	}

+ 1 - 2
client/widgets/Images.h

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

+ 3 - 3
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];

+ 1 - 2
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
 		{

+ 1 - 0
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

+ 9 - 9
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.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.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.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);
 	}

+ 4 - 3
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 <typename Handler> void serialize(Handler &h, const int version)
 			{
-				h & r1 & g1 & b1 & a1 & r2 & g2 & b2 & a2;
+				h & start & end;
 			}
 		};
 

+ 62 - 0
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 <typename Handler>
+	void serialize(Handler &h, const int version)
+	{
+		h & r;
+		h & g;
+		h & b;
+		h & a;
+	}
+};
+
+VCMI_LIB_NAMESPACE_END

+ 3 - 2
lib/TerrainHandler.h

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

+ 1 - 1
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)