diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-01-13 10:42:55 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-01-13 10:42:55 -0500 |
commit | f91e95eed85bdd74b219b20a435fa6f297069da1 (patch) | |
tree | bb22c40e618932c3aaef5b8d62e7305d4895a998 /include | |
parent | 620311fb15953984c2fe37917d678f9b3aaa00b6 (diff) |
vector2 stuff, other things
Diffstat (limited to 'include')
-rw-r--r-- | include/common.hpp | 112 | ||||
-rw-r--r-- | include/render.hpp | 2 | ||||
-rw-r--r-- | include/texture.hpp | 2 | ||||
-rw-r--r-- | include/vector2.hpp | 80 | ||||
-rw-r--r-- | include/world.hpp | 6 |
5 files changed, 95 insertions, 107 deletions
diff --git a/include/common.hpp b/include/common.hpp index abfd28b..1244464 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -55,73 +55,10 @@ using uint = unsigned int; #define coalesce(v1, v2) ((v1 != nullptr) ? v1 : v2) -/** - * Creates a coordinate of integers. - */ -typedef struct { - int x; /**< The x coordinate */ - int y; /**< The y coordinate */ -} ivec2; +#include <vector2.hpp> -/** - * A pair of x and y for dimensions (i.e. width and height). - */ -typedef ivec2 dim2; - -/** - * Creates a coordinate out of floating point integers. - */ -struct vec2 { - float x; - float y; - - vec2(float _x = 0.0f, float _y = 0.0f) - : x(_x), y(_y) {} - - bool operator==(const vec2 &v) const { - return (x == v.x) && (y == v.y); - } - - template<typename T> - const vec2 operator=(const T &n) { - x = y = n; - return *this; - } - - template<typename T> - vec2 operator+(T n) const { - return vec2 (x + n, y + n); - } - - vec2 operator+(const vec2 &v) { - return vec2 (x + v.x, y + v.y); - } - - vec2 operator*(const float&n) const { - return vec2 (x * n, y * n); - } - - vec2 operator/(const float& n) const { - return vec2 (x / n, y / n); - } - - // std::swap can't work due to being packed - - inline void swapX(vec2 &v) { - float t = x; - x = v.x, v.x = t; - } - - inline void swapY(vec2 &v) { - float t = y; - y = v.y, v.y = t; - } - - std::string toString(void) const { - return "(" + std::to_string(x) + ", " + std::to_string(y) + ")"; - } - -} __attribute__ ((packed)); +using vec2 = vector2<float>; +using dim2 = vector2<int>; /** * A structure for three-dimensional points. @@ -133,8 +70,7 @@ struct vec3 { vec3(float _x = 0.0f, float _y = 0.0f, float _z = 1.0f) : x(_x), y(_y), z(_z) {} - -} __attribute__ ((packed)); +}; /** * This structure contains two sets of coordinates for ray drawing. @@ -155,35 +91,15 @@ public: float blue; /**< The amount of blue */ float alpha; /**< Transparency */ - Color() { - red = green = blue = alpha = 0.0f; - } + Color(float r = 0, float g = 0, float b = 0, float a = 255) + : red(r), green(g), blue(b), alpha(a) {} - Color(float r, float g ,float b) { - red = r; - green = g; - blue = b; - alpha = 255; + Color operator-(const float& a) { + return Color(red - a, green - a, blue - a, alpha); } - Color(float r, float g, float b, float a) { - red = r; - green = g; - blue = b; - alpha = a; - } - - Color operator-=(float a) { - red -= a; - green -= a; - blue -= a; - return{red+a,green+a,blue+a}; - } - Color operator+=(float a) { - return{red+a,green+a,blue+a}; - } - Color operator=(float a) { - return{red=a,green=a,blue=a}; + Color operator+(const float& a) { + return Color(red + a, green + a, blue + a, alpha); } }; @@ -207,14 +123,6 @@ constexpr float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC; std::vector<std::string> StringTokenizer(const std::string& str, char delim); /** - * Seperates a string like, "23,12" to a vec2. - * - * @param s the string to parse - * @return the vec2 of the values passed in the string - */ -vec2 str2coord(std::string s); - -/** * Returns a measurement in HLINEs * * @param the number of HLINEs, integer or decimal diff --git a/include/render.hpp b/include/render.hpp index cf58519..997d7d0 100644 --- a/include/render.hpp +++ b/include/render.hpp @@ -69,8 +69,6 @@ namespace Render { extern Shader worldShader; extern Shader textShader; - void initShaders(void); - void useShader(Shader *s); void drawRect(vec2 ll, vec2 ur, float z); diff --git a/include/texture.hpp b/include/texture.hpp index 3cb8d1f..43db1b6 100644 --- a/include/texture.hpp +++ b/include/texture.hpp @@ -93,6 +93,8 @@ namespace Colors { extern ColorTex red; /**< A solid red texture. */ extern ColorTex blue; /**< A solid blue texture. */ + extern GLfloat texCoord[12]; + /** * Creates the colors. */ diff --git a/include/vector2.hpp b/include/vector2.hpp new file mode 100644 index 0000000..debaee9 --- /dev/null +++ b/include/vector2.hpp @@ -0,0 +1,80 @@ +#ifndef VECTOR2_HPP_ +#define VECTOR2_HPP_ + +#include <string> +#include <type_traits> + +template<typename T> +struct vector2 { + static_assert(std::is_arithmetic<T>::value, "vector2 members must be an arithmetic type (i.e. numbers)"); + + T x, y; + + vector2(T _x = 0, T _y = 0) + : x(_x), y(_y) {} + + // format: "3, 5" + vector2(const std::string& s) { + *this = s; + } + + vector2<T>& operator=(const T& value) { + x = y = value; + return *this; + } + + vector2<T>& operator=(const std::string& s) { + auto comma = s.find(','); + x = std::stoi(s.substr(0, comma)); + y = std::stoi(s.substr(comma + 1)); + return *this; + } + + // addition + vector2<T> operator+(const vector2<T>& v) const { + return vector2<T>(x + v.x, y + v.y); + } + + vector2<T> operator+(const T& n) const { + return vector2<T>(x + n, y + n); + } + + // subtraction + vector2<T> operator-(const vector2<T>& v) const { + return vector2<T>(x - v.x, y - v.y); + } + + vector2<T> operator-(const T& n) const { + return vector2<T>(x - n, y - n); + } + + // multiplication + vector2<T> operator*(const vector2<T>& v) const { + return vector2<T>(x * v.x, y * v.y); + } + + vector2<T> operator*(const T& n) const { + return vector2<T>(x * n, y * n); + } + + // division + vector2<T> operator/(const vector2<T>& v) const { + return vector2<T>(x / v.x, y / v.y); + } + + vector2<T> operator/(const T& n) const { + return vector2<T>(x / n, y / n); + } + + // compare + bool operator==(const vector2<T>& v) const { + return (x == v.x) && (y == v.y); + } + + // other functions + std::string toString(void) const { + return "(" + std::to_string(x) + ", " + std::to_string(y) + ")"; + } +}; + +#endif // VECTOR2_HPP_ diff --git a/include/world.hpp b/include/world.hpp index 14c64d0..9b68314 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -37,7 +37,7 @@ struct WorldData { float grassHeight[2]; /**< height of the two grass blades */ float groundHeight; /**< height of the 'line' */ unsigned char groundColor; /**< a value that affects the ground's color */ -} __attribute__ ((packed)); +}; /** * Defines how many game ticks it takes to go from day to night or vice versa. @@ -171,8 +171,8 @@ public: WorldData2 worldData; void generate(int width = 0); - void addHole(const unsigned int& start, const unsigned int& end); - void addHill(const ivec2& peak, const unsigned int& width); + //void addHole(const unsigned int& start, const unsigned int& end); + //void addHill(const ivec2& peak, const unsigned int& width); bool save(void); void load(const std::string& file); |