Cast vectors explicitly using vector_cast.

This commit is contained in:
King_DuckZ 2016-11-02 18:31:59 +01:00
parent 650f8d6de1
commit 68d179975e
11 changed files with 41 additions and 39 deletions

View file

@ -47,7 +47,7 @@ namespace cloonel {
m_bounceCallback(&DoNothing), m_bounceCallback(&DoNothing),
m_texture(new Texture(parPath, parMain, false)) m_texture(new Texture(parPath, parMain, false))
#if defined(WITH_DEBUG_VISUALS) #if defined(WITH_DEBUG_VISUALS)
, m_bottomBarDrawable(parMain, Colour(250, 5, 1), static_cast<short2>(m_bottomBar.From()), static_cast<short2>(m_bottomBar.To())) , m_bottomBarDrawable(parMain, Colour(250, 5, 1), vector_cast<short2>(m_bottomBar.From()), vector_cast<short2>(m_bottomBar.To()))
#endif #endif
{ {
assert(parMain); assert(parMain);
@ -68,7 +68,7 @@ namespace cloonel {
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
void Character::Prepare() { void Character::Prepare() {
const SDLMain* const sdlmain = m_texture->SDLObject(); const SDLMain* const sdlmain = m_texture->SDLObject();
const int2 screensize(sdlmain->WidthHeight()); const int2 screensize(vector_cast<int2>(sdlmain->WidthHeight()));
m_texture->Reload(); m_texture->Reload();
} }

View file

@ -34,7 +34,7 @@ namespace cloonel {
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
void ClipLine (const SDL_Rect& parArea, Line<int16_t, 2>& parLine) { void ClipLine (const SDL_Rect& parArea, Line<int16_t, 2>& parLine) {
Line<float, 2> line(static_cast<float2>(parLine[0]), static_cast<float2>(parLine[1])); Line<float, 2> line(vector_cast<float2>(parLine[0]), vector_cast<float2>(parLine[1]));
const float al = static_cast<float>(parArea.x); const float al = static_cast<float>(parArea.x);
const float at = static_cast<float>(parArea.y); const float at = static_cast<float>(parArea.y);
const float ar = static_cast<float>(parArea.x + parArea.w); const float ar = static_cast<float>(parArea.x + parArea.w);
@ -110,9 +110,9 @@ namespace cloonel {
SDL_SetRenderDrawColor(m_sdlmain->GetRenderer(), m_colour.r, m_colour.g, m_colour.b, m_colour.a); SDL_SetRenderDrawColor(m_sdlmain->GetRenderer(), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
LineBase scaledLine(*this); LineBase scaledLine(*this);
scaledLine += static_cast<short2>(parPos); scaledLine += vector_cast<short2>(parPos);
scaledLine *= parScaling; scaledLine *= parScaling;
const short2 wh(static_cast<short2>(m_sdlmain->WidthHeight())); const short2 wh(vector_cast<short2>(m_sdlmain->WidthHeight()));
{ {
SDL_Rect screen; SDL_Rect screen;
screen.x = screen.y = 0; screen.x = screen.y = 0;

View file

@ -26,6 +26,18 @@
#include <ciso646> #include <ciso646>
#include <cstdint> #include <cstdint>
namespace vwr {
template <typename T>
struct VectorWrapperInfo<std::array<cloonel::Vector<T, 2>, 2>> {
enum { dimensions = 2 };
typedef cloonel::Vector<T, 2> scalar_type;
typedef std::array<scalar_type, dimensions> vector_type;
static scalar_type& get_at (uint32_t parIndex, vector_type& parVector) {
return parVector[parIndex];
}
};
} //namespace vwr
namespace cloonel { namespace cloonel {
template <typename T, uint32_t S> template <typename T, uint32_t S>
class Line { class Line {

View file

@ -54,8 +54,11 @@ namespace cloonel {
template <typename T, uint32_t S> template <typename T, uint32_t S>
template <typename U> template <typename U>
Line<T, S>& Line<T, S>::operator*= (const Vector<U, S>& parRhs) { Line<T, S>& Line<T, S>::operator*= (const Vector<U, S>& parRhs) {
m_points.x() = static_cast<Point>(m_points.x() * parRhs); typedef typename std::common_type<U, T>::type CommonType;
m_points.y() = static_cast<Point>(m_points.y() * parRhs); typedef Vector<CommonType, S> CommVecType;
m_points.x() = vector_cast<Point>(vector_cast<CommVecType>(m_points.x()) * vector_cast<CommVecType>(parRhs));
m_points.y() = vector_cast<Point>(vector_cast<CommVecType>(m_points.y()) * vector_cast<CommVecType>(parRhs));
return *this; return *this;
} }

View file

@ -33,7 +33,7 @@ namespace cloonel {
m_collisionTop(new HorzCollisionBar(parPos, parSize.x())), m_collisionTop(new HorzCollisionBar(parPos, parSize.x())),
m_surface(parTexture) m_surface(parTexture)
#if defined(WITH_DEBUG_VISUALS) #if defined(WITH_DEBUG_VISUALS)
, m_collisionTopDrawable(parSdlMain, Colour(215, 181, 3), static_cast<short2>(parPos), static_cast<short2>(parPos + float2(parSize.x(), 0.0f))) , m_collisionTopDrawable(parSdlMain, Colour(215, 181, 3), vector_cast<short2>(parPos), vector_cast<short2>(parPos + float2(parSize.x(), 0.0f)))
#endif #endif
{ {
assert(m_surface); assert(m_surface);

View file

@ -89,7 +89,7 @@ namespace cloonel {
m_gameName(parGameName), m_gameName(parGameName),
m_localData(new LocalData) m_localData(new LocalData)
{ {
m_localData->sizeratio.SetOriginal(static_cast<float2>(parReferenceRes), static_cast<float2>(parRes)); m_localData->sizeratio.SetOriginal(vector_cast<float2>(parReferenceRes), vector_cast<float2>(parRes));
m_localData->initialized = false; m_localData->initialized = false;
#if defined(RASPBERRY_PI) #if defined(RASPBERRY_PI)
m_localData->bcmInitialized = false; m_localData->bcmInitialized = false;
@ -178,7 +178,7 @@ namespace cloonel {
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
void SDLMain::SetResolution (ushort2 parRes) { void SDLMain::SetResolution (ushort2 parRes) {
m_localData->sizeratio.UpdateResolution(static_cast<float2>(parRes)); m_localData->sizeratio.UpdateResolution(vector_cast<float2>(parRes));
{ {
SDL_Renderer* const renderer = GetRenderer(); SDL_Renderer* const renderer = GetRenderer();
assert(renderer); assert(renderer);
@ -225,7 +225,7 @@ namespace cloonel {
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
ushort2 SDLMain::WidthHeight() const noexcept { ushort2 SDLMain::WidthHeight() const noexcept {
return static_cast<ushort2>(m_localData->sizeratio.Resolution()); return vector_cast<ushort2>(m_localData->sizeratio.Resolution());
} }
///------------------------------------------------------------------------ ///------------------------------------------------------------------------

View file

@ -354,7 +354,7 @@ namespace cloonel {
if (m_texture) { if (m_texture) {
int2 wh; int2 wh;
SDL_QueryTexture(m_texture, nullptr, nullptr, &wh.x(), &wh.y()); SDL_QueryTexture(m_texture, nullptr, nullptr, &wh.x(), &wh.y());
m_size = static_cast<float2>(wh); m_size = vector_cast<float2>(wh);
} }
} }
@ -369,14 +369,14 @@ namespace cloonel {
RectFloat src(float2(0.0f), m_size); RectFloat src(float2(0.0f), m_size);
if (parClip) { if (parClip) {
const RectFloat clip(float2(0.0f), static_cast<float2>(m_sdlmain->WidthHeight())); const RectFloat clip(float2(0.0f), vector_cast<float2>(m_sdlmain->WidthHeight()));
const bool visible = ClipRect(src, dest, clip); const bool visible = ClipRect(src, dest, clip);
if (not visible) if (not visible)
return; return;
} }
#if !defined(NDEBUG) #if !defined(NDEBUG)
else { else {
const RectFloat clip(float2(0.0f), static_cast<float2>(m_sdlmain->WidthHeight())); const RectFloat clip(float2(0.0f), vector_cast<float2>(m_sdlmain->WidthHeight()));
assert(IsRectCompletelyInsideRect(dest, clip)); assert(IsRectCompletelyInsideRect(dest, clip));
} }
#endif #endif

View file

@ -33,7 +33,7 @@ namespace cloonel {
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
float2 CountTilesInScreen (const ushort2& parScreenSize, const ushort2& parTileSize) { float2 CountTilesInScreen (const ushort2& parScreenSize, const ushort2& parTileSize) {
assert(ushort2(0) != parTileSize); assert(ushort2(0) != parTileSize);
return static_cast<float2>((parTileSize - 1 + parScreenSize) / parTileSize); return vector_cast<float2>((parTileSize - 1 + parScreenSize) / parTileSize);
} }
} //unnamed namespace } //unnamed namespace
@ -73,7 +73,7 @@ namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
void TiledWallpaper::Draw() const { void TiledWallpaper::Draw() const {
const ushort2 grid(static_cast<ushort2>(m_tileCount.tileCount())); const ushort2 grid(vector_cast<ushort2>(m_tileCount.tileCount()));
const float2& sz = m_tileCount.tileSize(); const float2& sz = m_tileCount.tileSize();
//TODO: add code to tell the renderer if the current tile will need clipping or not //TODO: add code to tell the renderer if the current tile will need clipping or not
@ -100,7 +100,7 @@ namespace cloonel {
TiledWallpaper::TileCountNotifiable::TileCountNotifiable (SDLMain* parMain, const ushort2& parTileSize) : TiledWallpaper::TileCountNotifiable::TileCountNotifiable (SDLMain* parMain, const ushort2& parTileSize) :
BaseClass(parMain), BaseClass(parMain),
m_tileCount(CountTilesInScreen(parMain->WidthHeight(), parTileSize)), m_tileCount(CountTilesInScreen(parMain->WidthHeight(), parTileSize)),
m_tileSize(static_cast<float2>(parTileSize)) m_tileSize(vector_cast<float2>(parTileSize))
#if defined(WITH_DEBUG_VISUALS) #if defined(WITH_DEBUG_VISUALS)
, m_screenRes(1.0f) , m_screenRes(1.0f)
#endif #endif
@ -114,16 +114,16 @@ namespace cloonel {
void TiledWallpaper::TileCountNotifiable::NotifyResChanged (const SizeRatio& parSize) { void TiledWallpaper::TileCountNotifiable::NotifyResChanged (const SizeRatio& parSize) {
std::cout << "Autoregistering" << std::endl; std::cout << "Autoregistering" << std::endl;
BaseClass::NotifyResChanged(parSize); BaseClass::NotifyResChanged(parSize);
m_tileCount = CountTilesInScreen(static_cast<ushort2>(parSize.Resolution()), static_cast<ushort2>(m_tileSize)); m_tileCount = CountTilesInScreen(vector_cast<ushort2>(parSize.Resolution()), vector_cast<ushort2>(m_tileSize));
#if defined(WITH_DEBUG_VISUALS) #if defined(WITH_DEBUG_VISUALS)
m_screenRes = parSize.Resolution(); m_screenRes = parSize.Resolution();
#endif #endif
#if !defined(NDEBUG) #if !defined(NDEBUG)
{ {
m_screenRes = parSize.Resolution(); m_screenRes = parSize.Resolution();
const ushort2 tileSize(static_cast<ushort2>(m_tileSize)); const ushort2 tileSize(vector_cast<ushort2>(m_tileSize));
const ushort2 screenRes(static_cast<ushort2>(parSize.Resolution())); const ushort2 screenRes(vector_cast<ushort2>(parSize.Resolution()));
const ushort2 tileCount(static_cast<ushort2>(m_tileCount)); const ushort2 tileCount(vector_cast<ushort2>(m_tileCount));
assert(tileCount * tileSize < screenRes + tileSize); assert(tileCount * tileSize < screenRes + tileSize);
} }
#endif #endif

View file

@ -28,8 +28,6 @@
#include <cassert> #include <cassert>
namespace cloonel { namespace cloonel {
template <typename T, typename U, uint32_t S>
typename std::common_type<T, U>::type dot ( const Vector<T, S>& parA, const Vector<U, S>& parB ) a_pure;
template <typename T, typename U> template <typename T, typename U>
Vector<typename std::common_type<T, U>::type, 3> cross ( const Vector<T, 3>& parA, const Vector<U, 3>& parB ) a_pure; Vector<typename std::common_type<T, U>::type, 3> cross ( const Vector<T, 3>& parA, const Vector<U, 3>& parB ) a_pure;
template <typename T, uint32_t S> template <typename T, uint32_t S>

View file

@ -18,17 +18,6 @@
*/ */
namespace cloonel { namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, typename U, uint32_t S>
typename std::common_type<T, U>::type dot (const Vector<T, S>& parA, const Vector<U, S>& parB) {
typename std::common_type<T, U>::type retVal(0);
for (uint32_t z = 0; z < S; ++z) {
retVal += parA[z] * parB[z];
}
return retVal;
}
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
template <typename T, typename U> template <typename T, typename U>

View file

@ -128,14 +128,14 @@ namespace vwr {
//make stuff from CloonelJump compile happily //make stuff from CloonelJump compile happily
namespace cloonel { namespace cloonel {
template <typename T, uint32_t S> template <typename T, uint32_t S>
using Vect = ::vwr::Vec<std::array<T, std::size_t(S)>, S>; using Vector = ::vwr::Vec<std::array<T, std::size_t(S)>, S>;
using ushort2 = Vect<uint16_t, 2>; using ushort2 = Vector<uint16_t, 2>;
#if !defined(NDEBUG) #if !defined(NDEBUG)
using short2 = Vect<int16_t, 2>; using short2 = Vector<int16_t, 2>;
#endif #endif
using float2 = Vect<float, 2>; using float2 = Vector<float, 2>;
using int2 = Vect<int32_t, 2>; using int2 = Vector<int32_t, 2>;
using vwr::vector_cast; using vwr::vector_cast;
} //namespace cloonel } //namespace cloonel