Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/Shapes.cpp
/* Copyright (C) 2015 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
/* | |||||
Shapes.cpp | |||||
*/ | |||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include <string> | |||||
#include "Shapes.h" | #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() : | CRect::CRect() : | ||||
left(0.f), top(0.f), right(0.f), bottom(0.f) | left(0.f), top(0.f), right(0.f), bottom(0.f) | ||||
{ | { | ||||
} | } | ||||
CRect::CRect(const CPos &pos) : | CRect::CRect(const CPos &pos) : | ||||
left(pos.x), top(pos.y), right(pos.x), bottom(pos.y) | left(pos.x), top(pos.y), right(pos.x), bottom(pos.y) | ||||
▲ Show 20 Lines • Show All 402 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator