1
0
forked from 0ad/0ad

Split CColor from Shapes.

Reviewed By: wraitii
Tested By: Stan
Differential Revision: https://code.wildfiregames.com/D1515
This was SVN commit r22051.
This commit is contained in:
Vladislav Belov 2019-01-13 15:38:41 +00:00
parent 43a291a071
commit 651cf8b364
23 changed files with 150 additions and 166 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -19,8 +19,8 @@
#define INCLUDED_CINEMAMANAGER
#include "lib/input.h" // InReaction - can't forward-declare enum
#include "graphics/Color.h"
#include "ps/CStr.h"
#include "ps/Shapes.h"
#include "simulation2/helpers/CinemaPath.h"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2009 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -15,16 +15,14 @@
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Convert float RGB(A) colors to unsigned byte
*/
#include "precompiled.h"
#include "graphics/Color.h"
#include "maths/MathUtil.h"
#include "graphics/SColor.h"
#include "maths/MathUtil.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
#if HAVE_SSE
# include <xmmintrin.h>
@ -34,10 +32,10 @@
static SColor4ub fallback_ConvertRGBColorTo4ub(const RGBColor& src)
{
SColor4ub result;
result.R=clamp(int(src.X*255),0,255);
result.G=clamp(int(src.Y*255),0,255);
result.B=clamp(int(src.Z*255),0,255);
result.A=0xff;
result.R = clamp(static_cast<int>(src.X * 255), 0, 255);
result.G = clamp(static_cast<int>(src.Y * 255), 0, 255);
result.B = clamp(static_cast<int>(src.Z * 255), 0, 255);
result.A = 255;
return result;
}
@ -93,3 +91,57 @@ void ColorActivateFastImpl()
debug_printf("No SSE available. Slow fallback routines will be used.\n");
}
}
bool CColor::ParseString(const CStr8& value, int defaultAlpha)
{
const size_t NUM_VALS = 4;
int values[NUM_VALS] = { 0, 0, 0, defaultAlpha };
std::stringstream stream;
stream.str(value);
// Parse each value
size_t i;
for (i = 0; i < NUM_VALS; ++i)
{
if (stream.eof())
break;
stream >> values[i];
if ((stream.rdstate() & std::stringstream::failbit) != 0)
{
LOGWARNING("Unable to parse CColor parameters. Your input: '%s'", value.c_str());
return false;
}
if (values[i] < 0 || values[i] > 255)
{
LOGWARNING("Invalid value (<0 or >255) when parsing CColor parameters. Your input: '%s'", value.c_str());
return false;
}
}
if (i < 3)
{
LOGWARNING("Not enough parameters when parsing as CColor. Your input: '%s'", value.c_str());
return false;
}
if (!stream.eof())
{
LOGWARNING("Too many parameters when parsing as CColor. Your input: '%s'", value.c_str());
return false;
}
r = values[0] / 255.f;
g = values[1] / 255.f;
b = values[2] / 255.f;
a = values[3] / 255.f;
return true;
}
bool CColor::operator==(const CColor& color) const
{
return
r == color.r &&
g == color.g &&
b == color.b &&
a == color.a;
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2009 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -15,23 +15,20 @@
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Convert float RGB(A) colors to unsigned byte
*/
#ifndef INCLUDED_COLOR
#define INCLUDED_COLOR
#include "SColor.h"
#include "graphics/SColor.h"
#include "maths/Vector3D.h"
#include "maths/Vector4D.h"
// simple defines for 3 and 4 component floating point colors - just map to
// corresponding vector types
// Simple defines for 3 and 4 component floating point colors - just map to
// corresponding vector types.
typedef CVector3D RGBColor;
typedef CVector4D RGBAColor;
// exposed as function pointer because it is set at init-time to
// Convert float RGB(A) colors to unsigned byte.
// Exposed as function pointer because it is set at init-time to
// one of several implementations depending on CPU caps.
extern SColor4ub (*ConvertRGBColorTo4ub)(const RGBColor& src);
@ -39,4 +36,41 @@ extern SColor4ub (*ConvertRGBColorTo4ub)(const RGBColor& src);
// possible codepath.
extern void ColorActivateFastImpl();
#endif
class CStr8;
struct CColor
{
CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {}
CColor(float cr, float cg, float cb, float ca) : r(cr), g(cg), b(cb), a(ca) {}
/**
* Try to parse @p Value as a color. Returns true on success, false otherwise.
* @param value Should be "r g b" or "r g b a" where each value is an integer in [0,255].
* @param defaultAlpha The alpha value that is used if the format of @p Value is "r g b".
*/
bool ParseString(const CStr8& value, int defaultAlpha = 255);
bool operator==(const CColor& color) const;
bool operator!=(const CColor& color) const
{
return !(*this == color);
}
// For passing to glColor[34]fv:
const float* FloatArray() const { return &r; }
// For passing to CRenderer:
SColor4ub AsSColor4ub() const
{
return SColor4ub(
static_cast<u8>(r * 255.f),
static_cast<u8>(g * 255.f),
static_cast<u8>(b * 255.f),
static_cast<u8>(a * 255.f)
);
}
float r, g, b, a;
};
#endif // INCLUDED_COLOR

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -18,11 +18,11 @@
#ifndef INCLUDED_MATERIAL
#define INCLUDED_MATERIAL
#include "graphics/Color.h"
#include "graphics/ShaderDefines.h"
#include "graphics/Texture.h"
#include "ps/CStr.h"
#include "ps/CStrIntern.h"
#include "ps/Shapes.h"
#include "simulation2/helpers/Player.h"
class CMaterial

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2011 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -18,9 +18,9 @@
#ifndef INCLUDED_MODELABSTRACT
#define INCLUDED_MODELABSTRACT
#include "maths/BoundingBoxOriented.h"
#include "graphics/Color.h"
#include "graphics/RenderableObject.h"
#include "ps/Shapes.h"
#include "maths/BoundingBoxOriented.h"
#include "simulation2/helpers/Player.h"
class CModel;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2016 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -29,9 +29,9 @@ struct SPropPoint;
#include <set>
#include <vector>
#include "graphics/Color.h"
#include "lib/file/vfs/vfs_path.h"
#include "ps/CStr.h"
#include "ps/Shapes.h"
#include "graphics/ObjectBase.h"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -18,12 +18,12 @@
#ifndef INCLUDED_GRAPHICS_OVERLAY
#define INCLUDED_GRAPHICS_OVERLAY
#include "graphics/Color.h"
#include "graphics/Texture.h"
#include "maths/Vector2D.h"
#include "maths/Vector3D.h"
#include "maths/FixedVector3D.h"
#include "ps/CStrIntern.h"
#include "ps/Shapes.h"
class CTerrain;
class CSimContext;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -19,6 +19,7 @@
#include "ShaderProgram.h"
#include "graphics/Color.h"
#include "graphics/ShaderManager.h"
#include "graphics/TextureManager.h"
#include "lib/res/graphics/ogl_tex.h"
@ -27,7 +28,6 @@
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/PreprocessorWrapper.h"
#include "ps/Shapes.h"
#if !CONFIG2_GLES

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -23,11 +23,11 @@
#include <boost/tokenizer.hpp>
#include "TerrainTextureManager.h"
#include "graphics/Color.h"
#include "graphics/TerrainTextureManager.h"
#include "maths/MathUtil.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/Shapes.h"
#include "ps/XML/XeroXMB.h"
#include "ps/XML/Xeromyces.h"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -19,10 +19,10 @@
#include "TerritoryTexture.h"
#include "graphics/Color.h"
#include "graphics/Terrain.h"
#include "lib/bits.h"
#include "ps/Profile.h"
#include "ps/Shapes.h"
#include "renderer/Renderer.h"
#include "simulation2/Simulation2.h"
#include "simulation2/helpers/Pathfinding.h"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -18,6 +18,7 @@
#ifndef INCLUDED_TEXTRENDERER
#define INCLUDED_TEXTRENDERER
#include "graphics/Color.h"
#include "graphics/ShaderProgramPtr.h"
#include "maths/Matrix3D.h"
#include "ps/CStrIntern.h"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -18,6 +18,7 @@
#ifndef INCLUDED_GUIRENDERER
#define INCLUDED_GUIRENDERER
#include "graphics/Color.h"
#include "graphics/ShaderTechnique.h"
#include "graphics/Texture.h"
#include "lib/res/handle.h"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -35,6 +35,7 @@ GUI Core, stuff that the whole GUI uses
#include <map>
#include <vector>
#include "graphics/Color.h"
#include "ps/CStr.h"
#include "ps/Errors.h"
// I would like to just forward declare CSize, but it doesn't

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -15,80 +15,9 @@
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Shapes.cpp
*/
#include "precompiled.h"
#include <string>
#include "Shapes.h"
#include "CLogger.h"
#include "CStr.h"
/**
* Try to parse @p Value as a color. Returns true on success, false otherwise.
* @param Value Should be "r g b" or "r g b a" where each value is an integer in [0,255].
* @param DefaultAlpha The alpha value that is used if the format of @p Value is "r g b".
*/
bool CColor::ParseString(const CStr8& Value, int DefaultAlpha)
{
const unsigned int NUM_VALS = 4;
int values[NUM_VALS] = { 0, 0, 0, DefaultAlpha };
std::stringstream stream;
stream.str(Value);
// Parse each value
size_t i;
for (i = 0; i < NUM_VALS; ++i)
{
if (stream.eof())
break;
stream >> values[i];
if ((stream.rdstate() & std::stringstream::failbit) != 0)
{
LOGWARNING("Unable to parse CColor parameters. Your input: '%s'", Value.c_str());
return false;
}
if (values[i] < 0 || values[i] > 255)
{
LOGWARNING("Invalid value (<0 or >255) when parsing CColor parameters. Your input: '%s'", Value.c_str());
return false;
}
}
if (i < 3)
{
LOGWARNING("Not enough parameters when parsing as CColor. Your input: '%s'", Value.c_str());
return false;
}
if (!stream.eof())
{
LOGWARNING("Too many parameters when parsing as CColor. Your input: '%s'", Value.c_str());
return false;
}
r = values[0]/255.f;
g = values[1]/255.f;
b = values[2]/255.f;
a = values[3]/255.f;
return true;
}
/*************************************************************************/
bool CColor::operator == (const CColor &color) const
{
return r==color.r &&
g==color.g &&
b==color.b &&
a==color.a;
}
/*************************************************************************/
CRect::CRect() :
left(0.f), top(0.f), right(0.f), bottom(0.f)

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -16,49 +16,15 @@
*/
/*
Shapes.h
--Overview--
Classes mostly useful for representing 2D screen overlays;
includes functionality for overlay position, color, texture and borders.
CColor is used more widely for various game systems.
*/
#ifndef INCLUDED_SHAPES
#define INCLUDED_SHAPES
#include "graphics/SColor.h"
class CStr8;
struct CColor
{
CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {}
CColor(float cr,float cg,float cb,float ca) : r(cr), g(cg), b(cb), a(ca) {}
bool ParseString(const CStr8& Value, int DefaultAlpha = 255);
bool operator == (const CColor &color) const;
bool operator != (const CColor &color) const
{
return !(*this==color);
}
// For passing to glColor[34]fv:
const float* FloatArray() const { return &r; }
// For passing to CRenderer:
SColor4ub AsSColor4ub() const
{
return SColor4ub((u8)(r*255.0), (u8)(g*255.0), (u8)(b*255.0), (u8)(a*255.0));
}
float r, g, b, a;
};
class CPos;
class CSize;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -17,8 +17,8 @@
#include "lib/self_test.h"
#include "graphics/Color.h"
#include "ps/CLogger.h"
#include "ps/Shapes.h"
class TestCColor : public CxxTest::TestSuite
{

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -19,13 +19,13 @@
#include "TerrainOverlay.h"
#include "graphics/Color.h"
#include "graphics/Terrain.h"
#include "lib/bits.h"
#include "lib/ogl.h"
#include "maths/MathUtil.h"
#include "ps/Game.h"
#include "ps/Profile.h"
#include "ps/Shapes.h"
#include "ps/World.h"
#include "renderer/Renderer.h"
#include "renderer/TerrainRenderer.h"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -22,11 +22,11 @@
#ifndef INCLUDED_WATERMANAGER
#define INCLUDED_WATERMANAGER
#include "graphics/Color.h"
#include "graphics/Texture.h"
#include "lib/ogl.h"
#include "maths/Matrix3D.h"
#include "maths/Vector2D.h"
#include "ps/Shapes.h"
#include "renderer/VertexBufferManager.h"
class CSimulation2;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -25,7 +25,7 @@
#include "simulation2/components/ICmpOwnership.h"
#include "simulation2/MessageTypes.h"
#include "ps/Shapes.h"
#include "graphics/Color.h"
class CCmpMinimap : public ICmpMinimap
{

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -19,12 +19,11 @@
#include "ICmpPlayer.h"
#include "graphics/Color.h"
#include "maths/FixedVector3D.h"
#include "simulation2/system/InterfaceScripted.h"
#include "simulation2/scripting/ScriptComponent.h"
#include "ps/Shapes.h"
BEGIN_INTERFACE_WRAPPER(Player)
END_INTERFACE_WRAPPER(Player)

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -21,7 +21,7 @@
#include "simulation2/system/InterfaceScripted.h"
#include "ps/Shapes.h"
#include "graphics/Color.h"
BEGIN_INTERFACE_WRAPPER(Selectable)
DEFINE_INTERFACE_METHOD_2("SetSelectionHighlight", void, ICmpSelectable, SetSelectionHighlight, CColor, bool)

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -19,6 +19,7 @@
#include "scriptinterface/ScriptConversions.h"
#include "graphics/Color.h"
#include "maths/Fixed.h"
#include "maths/FixedVector2D.h"
#include "maths/FixedVector3D.h"

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2009 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -19,12 +19,12 @@
#include "Brushes.h"
#include "ps/Game.h"
#include "ps/Shapes.h"
#include "ps/World.h"
#include "graphics/Color.h"
#include "graphics/Terrain.h"
#include "lib/ogl.h"
#include "maths/MathUtil.h"
#include "ps/Game.h"
#include "ps/World.h"
#include "renderer/TerrainOverlay.h"
#include "simulation2/Simulation2.h"
#include "simulation2/system/SimContext.h"