Changeset View
Changeset View
Standalone View
Standalone View
source/maths/Vector2D.h
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2022 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/>. | |||||||||
*/ | */ | |||||||||
#ifndef INCLUDED_VECTOR2D | #ifndef INCLUDED_VECTOR2D | |||||||||
#define INCLUDED_VECTOR2D | #define INCLUDED_VECTOR2D | |||||||||
#include "ps/containers/Span.h" | ||||||||||
#include <math.h> | #include <math.h> | |||||||||
class CSize2D; | class CSize2D; | |||||||||
/* | /* | |||||||||
* Provides an interface for a vector in R2 and allows vector and | * Provides an interface for a vector in R2 and allows vector and | |||||||||
* scalar operations on it. | * scalar operations on it. | |||||||||
*/ | */ | |||||||||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | public: | |||||||||
} | } | |||||||||
CVector2D operator+(const CSize2D& size) const; | CVector2D operator+(const CSize2D& size) const; | |||||||||
CVector2D operator-(const CSize2D& size) const; | CVector2D operator-(const CSize2D& size) const; | |||||||||
void operator+=(const CSize2D& size); | void operator+=(const CSize2D& size); | |||||||||
void operator-=(const CSize2D& size); | void operator-=(const CSize2D& size); | |||||||||
// Returns 2 element array of floats, e.g. for vec2 uniforms. | ||||||||||
PS::span<const float> AsFloatArray() const | ||||||||||
Stan: Shouldn't this be asfloatspan instead? | ||||||||||
Done Inline ActionsI was thinking about that. Not sure, technically it's still an array view. vladislavbelov: I was thinking about that. Not sure, technically it's still an array view. | ||||||||||
Done Inline ActionsAsFloatArrayView() maybe ? So it's distinguished from the other kinds of AsFloatArray. Stan: AsFloatArrayView() maybe ? So it's distinguished from the other kinds of AsFloatArray. | ||||||||||
Done Inline ActionsCurrently all AsFloatArray return PS::span. vladislavbelov: Currently all `AsFloatArray` return `PS::span`. | ||||||||||
{ | ||||||||||
// Additional check to prevent a weird compiler having a different | ||||||||||
Done Inline Actions
Stan: | ||||||||||
// alignement for an array and a class members. | ||||||||||
static_assert( | ||||||||||
Done Inline ActionsShould we use cassert? Stan: Should we use cassert? | ||||||||||
Done Inline ActionsIn case you don't want to add a readable message you can use cassert. vladislavbelov: In case you don't want to add a readable message you can use cassert. | ||||||||||
Done Inline ActionsI see Stan: I see
| ||||||||||
sizeof(CVector2D) == sizeof(float) * 2u && | ||||||||||
offsetof(CVector2D, X) == 0 && | ||||||||||
offsetof(CVector2D, Y) == sizeof(float), | ||||||||||
"Vector2D should be properly layouted to use AsFloatArray"); | ||||||||||
return PS::span<const float>(&X, 2); | ||||||||||
} | ||||||||||
public: | public: | |||||||||
float X, Y; | float X, Y; | |||||||||
}; | }; | |||||||||
#endif // INCLUDED_VECTOR2D | #endif // INCLUDED_VECTOR2D |
Wildfire Games · Phabricator
Shouldn't this be asfloatspan instead?