diff --git a/CMakeLists.txt b/CMakeLists.txt index ac59ae0..3a287eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,8 +56,10 @@ add_executable(${PROJECT_NAME} src/sizeratio.cpp src/sizenotifiable.cpp src/platform.cpp + src/vectormath.cpp src/platformsystem.cpp src/movers/moverworld.cpp + src/line.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/src/CloonelJumpConfig.h.in b/src/CloonelJumpConfig.h.in index 7f1c57d..f753f1e 100644 --- a/src/CloonelJumpConfig.h.in +++ b/src/CloonelJumpConfig.h.in @@ -31,6 +31,7 @@ #define DEF_RANDOM_SEED 1984 #define MAX_PLATFORMS_ON_SCREEN 8 +/* TODO: make this path relative */ #define GAME_BASE_PATH "@CMAKE_SOURCE_DIR@" #endif diff --git a/src/line.cpp b/src/line.cpp new file mode 100644 index 0000000..08734f0 --- /dev/null +++ b/src/line.cpp @@ -0,0 +1,24 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +#include "line.hpp" +#include "line_helpers.hpp" + +namespace cloonel { +} //namespace cloonel diff --git a/src/line.hpp b/src/line.hpp new file mode 100644 index 0000000..e8e1559 --- /dev/null +++ b/src/line.hpp @@ -0,0 +1,72 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +#ifndef id56F112C6551D44039D0C0270F573B35B +#define id56F112C6551D44039D0C0270F573B35B + +#include "vector.hpp" +#include "vectormath.hpp" + +namespace cloonel { + template + class Line { + public: + typedef Vector Point; + typedef T Scalar; + + Line ( void ) = default; + Line ( const Line& parOther ); + Line ( const Point& parStart, const Point& parEnd ); + Line ( const Point& parStart, const Point& parDirection, Scalar parLength ) : Line(parStart, parDirection * parLength) { } + ~Line ( void ) noexcept = default; + + Point& Start ( void ) { return m_points.x(); } + Point& End ( void ) { return m_points.y(); } + const Point& Start ( void ) const { return m_points.x(); } + const Point& End ( void ) const { return m_points.y(); } + + Line& operator+= ( const Point& parRhs ) __attribute__((flatten)); + Line& operator-= ( const Point& parRhs ) __attribute__((flatten)); + + private: + Vector m_points; + }; + + template + Line operator+ ( Line parLhs, const Vector& parRhs ) __attribute__((pure)) __attribute__((flatten)); + template + Line operator- ( Line parLhs, const Vector& parRhs ) __attribute__((pure)) __attribute__((flatten)); + template + Line operator+ ( const Vector& parLhs, Line parRhs ) __attribute__((pure)) __attribute__((flatten)); + template + Line operator- ( const Vector& parLhs, Line parRhs ) __attribute__((pure)) __attribute__((flatten)); + + template + bool operator> ( const Vector& parLhs, const Line& parRhs ) __attribute__((pure)); + template + bool operator< ( const Vector& parLhs, const Line& parRhs ) __attribute__((pure)); + template + bool operator>= ( const Vector& parLhs, const Line& parRhs ) __attribute__((pure)); + template + bool operator<= ( const Vector& parLhs, const Line& parRhs ) __attribute__((pure)); +} //namespace cloonel + +#include "line.inl" + +#endif diff --git a/src/line.inl b/src/line.inl new file mode 100644 index 0000000..91e6929 --- /dev/null +++ b/src/line.inl @@ -0,0 +1,112 @@ +namespace cloonel { + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Line::Line (const Line& parOther) : + m_points(parOther.m_points) + { + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Line::Line (const Point& parStart, const Point& parEnd) : + m_points(parStart, parEnd) + { + } + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Line& Line::operator+= (const Point& parRhs) { + for (uint32_t z = 0; z < S; ++z) { + m_points[z] += parRhs; + } + return *this; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Line& Line::operator-= (const Point& parRhs) { + for (uint32_t z = 0; z < S; ++z) { + m_points[z] -= parRhs; + } + return *this; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Line operator+ ( const Vector& parLhs, Line parRhs ) { + parRhs += parLhs; + return parRhs; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Line operator- ( const Vector& parLhs, Line parRhs ) { + parRhs -= parLhs; + return parRhs; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Line operator+ ( Line parLhs, const Vector& parRhs ) { + parLhs += parRhs; + return parLhs; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Line operator- ( Line parLhs, const Vector& parRhs ) { + parLhs -= parRhs; + return parLhs; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + bool operator> (const Vector& parLhs, const Line& parRhs) { + const Vector line(parRhs.End() - parRhs.Start()); + const Vector linePerp(line.y(), -line.x()); + const Vector pt(parLhs - parRhs.Start()); + const T dotproduct = dot(linePerp, pt); + return (dotproduct > T(0)); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + bool operator< (const Vector& parLhs, const Line& parRhs) { + const Vector line(parRhs.End() - parRhs.Start()); + const Vector linePerp(line.y(), -line.x()); + const Vector pt(parLhs - parRhs.Start()); + const T dotproduct = dot(linePerp, pt); + return (dotproduct < T(0)); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + bool operator>= (const Vector& parLhs, const Line& parRhs) { + const Vector line(parRhs.End() - parRhs.Start()); + const Vector linePerp(line.y(), -line.x()); + const Vector pt(parLhs - parRhs.Start()); + const T dotproduct = dot(linePerp, pt); + return (dotproduct >= T(0)); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + bool operator<= (const Vector& parLhs, const Line& parRhs) { + const Vector line(parRhs.End() - parRhs.Start()); + const Vector linePerp(line.y(), -line.x()); + const Vector pt(parLhs - parRhs.Start()); + const T dotproduct = dot(linePerp, pt); + return (dotproduct <= T(0)); + } +} //namespace cloonel diff --git a/src/line_helpers.hpp b/src/line_helpers.hpp new file mode 100644 index 0000000..7278c9f --- /dev/null +++ b/src/line_helpers.hpp @@ -0,0 +1,37 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +#ifndef idDF0B3D1FA4714EF3AB1241C3DA0D4E3D +#define idDF0B3D1FA4714EF3AB1241C3DA0D4E3D + +#include "vector.hpp" +#include "line.hpp" +#include + +namespace cloonel { + template + T len ( const Line& parLine ) __attribute__((pure)); + + template + T len_sq ( const Line& parLine ) __attribute__((pure)); +} //namespace cloonel + +#include "line_helpers.inl" + +#endif diff --git a/src/line_helpers.inl b/src/line_helpers.inl new file mode 100644 index 0000000..dd2a3ea --- /dev/null +++ b/src/line_helpers.inl @@ -0,0 +1,42 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +namespace cloonel { + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + T len (const Line& parLine) { + return std::sqrt(len_sq(parLine)); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + T len_sq (const Line& parLine) { + T res(0); + const typename Line::Point& start = parLine.Start(); + const typename Line::Point& end = parLine.End(); + + for (uint32_t z = 0; z < S; ++z) { + const T diff(end[z] - start[z]); + res += diff * diff; + } + return res; + } +} //namespace cloonel diff --git a/src/vectormath.cpp b/src/vectormath.cpp new file mode 100644 index 0000000..1eed710 --- /dev/null +++ b/src/vectormath.cpp @@ -0,0 +1,20 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +#include "vectormath.hpp" diff --git a/src/vectormath.hpp b/src/vectormath.hpp new file mode 100644 index 0000000..c7193ba --- /dev/null +++ b/src/vectormath.hpp @@ -0,0 +1,43 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +#ifndef id5DB098DAA5534A0A869DEF23888F96B0 +#define id5DB098DAA5534A0A869DEF23888F96B0 + +#include "vector.hpp" +#include +#include +#include + +namespace cloonel { + template + typename std::common_type::type dot ( const Vector& parA, const Vector& parB ) __attribute__((pure)); + template + Vector::type, 3> cross ( const Vector& parA, const Vector& parB ) __attribute__((pure)); + template + T len ( const Vector& parVector ) __attribute__((pure)); + template + T len_sq ( const Vector& parVector ) __attribute__((pure)); + template + Vector normalized ( const Vector& parVector ) __attribute__((pure)); +} //namespace cloonel + +#include "vectormath.inl" + +#endif diff --git a/src/vectormath.inl b/src/vectormath.inl new file mode 100644 index 0000000..a8ddca2 --- /dev/null +++ b/src/vectormath.inl @@ -0,0 +1,63 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +namespace cloonel { + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + typename std::common_type::type dot (const Vector& parA, const Vector& parB) { + typename std::common_type::type retVal(0); + for (uint32_t z = 0; z < S; ++z) { + retVal += parA[z] * parB[z]; + } + return retVal; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Vector::type, 3> cross (const Vector& parA, const Vector& parB) { + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + T len (const Vector& parVector) { + return std::sqrt(len_sq(parVector)); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + T len_sq (const Vector& parVector) { + T retVal(0); + for (uint32_t z = 0; z < S; ++z) { + retVal += parVector[z] * parVector[z]; + } + return retVal; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + Vector normalized (const Vector& parVector) { + return parVector / len(parVector); + } + +} //namespace cloonel