aboutsummaryrefslogtreecommitdiffstats
path: root/yage
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-10-12 14:57:26 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-10-12 14:57:26 +0100
commit1bb0ef8960c71ef505a351702bec54c01ba15e22 (patch)
tree365811364237f1e55e66f421b93cad8adcf51eda /yage
parentba733040afb85d4c287a2ec464db05cb86a53fca (diff)
downloadYAGE-1bb0ef8960c71ef505a351702bec54c01ba15e22.tar.gz
YAGE-1bb0ef8960c71ef505a351702bec54c01ba15e22.zip
Fixing spritesheet and fixed #12
Diffstat (limited to 'yage')
-rw-r--r--yage/CMakeLists.txt4
-rw-r--r--yage/base/resourcemanager.h2
-rw-r--r--yage/base/spritebatch.cpp4
-rw-r--r--yage/base/spritebatch.h10
-rw-r--r--yage/base/spritesheet.cpp33
-rw-r--r--yage/base/spritesheet.h6
-rw-r--r--yage/base/texturecache.cpp2
-rw-r--r--yage/base/texturecache.h3
-rw-r--r--yage/base/window.cpp26
-rw-r--r--yage/base/window.h5
-rw-r--r--yage/math/matrix.h174
11 files changed, 186 insertions, 83 deletions
diff --git a/yage/CMakeLists.txt b/yage/CMakeLists.txt
index e9d4071b..d7df4a94 100644
--- a/yage/CMakeLists.txt
+++ b/yage/CMakeLists.txt
@@ -1,9 +1,5 @@
cmake_policy(SET CMP0048 NEW)
-project(yage
- VERSION 0.1.1.0
- LANGUAGES CXX)
-
include(base/CMakeLists.txt)
include(physics/CMakeLists.txt)
include(math/CMakeLists.txt)
diff --git a/yage/base/resourcemanager.h b/yage/base/resourcemanager.h
index 3c5081c4..52d98e12 100644
--- a/yage/base/resourcemanager.h
+++ b/yage/base/resourcemanager.h
@@ -1,3 +1,4 @@
+
/* ----------------------------------------------------------------------------
* resourcemanager.h
*
@@ -24,6 +25,7 @@ private:
public:
static Texture getTexture(const std::string &texture_path);
+
};
} // namespace yage
diff --git a/yage/base/spritebatch.cpp b/yage/base/spritebatch.cpp
index 3aec8153..aaeec32f 100644
--- a/yage/base/spritebatch.cpp
+++ b/yage/base/spritebatch.cpp
@@ -61,8 +61,8 @@ void SpriteBatch::end()
createRenderBatches();
}
-void SpriteBatch::draw(const glm::vec4 &destination_rect,
- const glm::vec4 &uv_rect, GLuint texture,
+void SpriteBatch::draw(const yage::Vector4f &destination_rect,
+ const yage::Vector4f &uv_rect, GLuint texture,
const Color &color, float depth)
{
Vertex top_left, top_right, bottom_right, bottom_left;
diff --git a/yage/base/spritebatch.h b/yage/base/spritebatch.h
index 3b4aca76..953055f6 100644
--- a/yage/base/spritebatch.h
+++ b/yage/base/spritebatch.h
@@ -6,8 +6,8 @@
* ----------------------------------------------------------------------------
*/
-/** @file
- */
+/** @file
+ */
#ifndef YAGE_SPRITE_BATCH_H
#define YAGE_SPRITE_BATCH_H
@@ -16,6 +16,7 @@
#include <glad/glad.h>
#include <glm/glm.hpp>
+#include <yage/math/matrix.h>
#include <vector>
@@ -92,8 +93,9 @@ public:
void begin();
void end();
// adds a sprite to the sprite batch to be rendered later
- void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect,
- GLuint texture, const Color &color, float depth);
+ void draw(const yage::Vector4f &destination_rect,
+ const yage::Vector4f &uv_rect, GLuint texture, const Color &color,
+ float depth);
// render the batch
void render();
diff --git a/yage/base/spritesheet.cpp b/yage/base/spritesheet.cpp
index d53f64aa..5c3499cc 100644
--- a/yage/base/spritesheet.cpp
+++ b/yage/base/spritesheet.cpp
@@ -6,6 +6,9 @@
* ----------------------------------------------------------------------------
*/
+/** @file
+ */
+
#include "spritesheet.h"
#include <cassert>
@@ -25,7 +28,15 @@ namespace yage
SpriteSheet::SpriteSheet(string pngFileName, string jsonFileName)
{
- string fileContents = fileContent(jsonFileName);
+ int jsonWidth, jsonHeight;
+ fileLocations_ =
+ parseJson(jsonWidth, jsonHeight, fileContent(jsonFileName));
+ texture_ = ImageLoader::loadPng(pngFileName);
+
+ if (texture_.width != jsonWidth)
+ throw runtime_error("JSON width does not match texture width");
+ if (texture_.height != jsonHeight)
+ throw runtime_error("JSON height does not match texture height");
}
string SpriteSheet::fileContent(string jsonFileName) const
@@ -38,7 +49,8 @@ string SpriteSheet::fileContent(string jsonFileName) const
return stream.str();
}
-SpriteMap SpriteSheet::parseJson(int &width, int &height, const string &jsonContent) const
+SpriteMap SpriteSheet::parseJson(int &width, int &height,
+ string jsonContent) const
{
SpriteMap spriteMap;
Document jsonAtlas;
@@ -48,10 +60,23 @@ SpriteMap SpriteSheet::parseJson(int &width, int &height, const string &jsonCont
height = jsonAtlas["height"].GetInt();
for (auto &texture : jsonAtlas["sprites"].GetObject()) {
- spriteMap[texture.name.GetString()] = Coordinate();
+ Coordinate coord;
for (auto &value : texture.value.GetObject()) {
- /// @todo add the coordinate to the map
+ string keyName{value.value.GetString()};
+ int keyValue{value.value.GetInt()};
+ if (keyName == "x") {
+ coord.x = keyValue;
+ } else if (keyName == "y") {
+ coord.y = keyValue;
+ } else if (keyName == "width") {
+ coord.width = keyValue;
+ } else if (keyName == "height") {
+ coord.height = keyValue;
+ } else {
+ throw runtime_error("JSON key incorrect: " + keyName);
+ }
}
+ spriteMap[texture.name.GetString()] = coord;
}
return spriteMap;
diff --git a/yage/base/spritesheet.h b/yage/base/spritesheet.h
index d44e1dc0..2ead0ba1 100644
--- a/yage/base/spritesheet.h
+++ b/yage/base/spritesheet.h
@@ -6,6 +6,9 @@
* ----------------------------------------------------------------------------
*/
+/** @file
+ */
+
#ifndef YAGE_SPRITESHEET_H
#define YAGE_SPRITESHEET_H
@@ -52,7 +55,8 @@ private:
Texture texture_;
details::SpriteMap fileLocations_;
- details::SpriteMap parseJson(int &width, int &height, const std::string &jsonContent) const;
+ details::SpriteMap parseJson(int &width, int &height,
+ std::string jsonContent) const;
};
} // namespace yage
diff --git a/yage/base/texturecache.cpp b/yage/base/texturecache.cpp
index 5d2950a3..628f3604 100644
--- a/yage/base/texturecache.cpp
+++ b/yage/base/texturecache.cpp
@@ -12,8 +12,6 @@
namespace yage
{
-TextureCache::TextureCache() = default;
-
Texture TextureCache::getTexture(const std::string &texture_path)
{
auto itr = texture_map_.find(texture_path);
diff --git a/yage/base/texturecache.h b/yage/base/texturecache.h
index 414c9ec3..b28349ec 100644
--- a/yage/base/texturecache.h
+++ b/yage/base/texturecache.h
@@ -22,9 +22,10 @@ private:
std::unordered_map<std::string, Texture> texture_map_;
public:
- TextureCache();
+ TextureCache() = default;
Texture getTexture(const std::string &texture_path);
+ Texture getTextureFromSpriteSheet();
};
} // namespace yage
diff --git a/yage/base/window.cpp b/yage/base/window.cpp
index 38056b14..5ac2d8dc 100644
--- a/yage/base/window.cpp
+++ b/yage/base/window.cpp
@@ -18,13 +18,17 @@ Window::Window() = default;
Window::~Window()
{
glfwDestroyWindow(window_);
+ glfwTerminate();
}
-void Window::create(const std::string &window_name, int width, int height,
- unsigned)
+void Window::create(std::string window_name, int width, int height)
{
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
+ if(glfwInit() == GLFW_FALSE) {
+ throw std::runtime_error("GLFW Initialisation failed");
+ }
+
+ glfwWindowHint(GLFW_VERSION_MAJOR, 4);
+ glfwWindowHint(GLFW_VERSION_MINOR, 5);
window_ =
glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr);
@@ -46,6 +50,8 @@ void Window::create(const std::string &window_name, int width, int height,
// set alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ // set the clear depth
+ glClearDepth(1.f);
}
void Window::swapBuffer()
@@ -56,8 +62,6 @@ void Window::swapBuffer()
void Window::clearBuffer()
{
- // set the clear depth
- glClearDepth(1.f);
// clears buffer with clear color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
@@ -72,4 +76,14 @@ void Window::show()
glfwShowWindow(window_);
}
+bool Window::shouldClose()
+{
+ return glfwWindowShouldClose(window_);
+}
+
+void Window::pollEvents() const
+{
+ glfwPollEvents();
+}
+
} // namespace yage
diff --git a/yage/base/window.h b/yage/base/window.h
index 3f448132..84ba8303 100644
--- a/yage/base/window.h
+++ b/yage/base/window.h
@@ -47,8 +47,7 @@ public:
Window &operator=(Window &&) = delete;
/// create the window, initialize the handle and update the width and height
- void create(const std::string &window_name, int width, int height,
- unsigned flags = WindowFlags::SHOWN);
+ void create(std::string window_name, int width, int height);
/// swap the buffer
void swapBuffer();
/// clear buffer
@@ -57,6 +56,8 @@ public:
void hide();
/// show window
void show();
+ bool shouldClose();
+ void pollEvents() const;
};
} // namespace yage
diff --git a/yage/math/matrix.h b/yage/math/matrix.h
index 3df1509d..d6d281d3 100644
--- a/yage/math/matrix.h
+++ b/yage/math/matrix.h
@@ -7,7 +7,7 @@
*/
/** @file
- */
+ */
#ifndef YAGE_MATH_MATRIX_H
#define YAGE_MATH_MATRIX_H
@@ -26,23 +26,23 @@ template <int Rows, int Cols, class Type>
class Matrix;
/** @internal Namespace for internal details.
- *
- * Details Namespace
- * ================
- *
- * This is the namespace used for implementation details.
- */
+ *
+ * Details Namespace
+ * ================
+ *
+ * This is the namespace used for implementation details.
+ */
namespace details
{
/** @internal Internal Row class used by the Matrix class to return the
- * internal data structure of the Matrix.
- *
- * Row
- * ===
- *
- * Internal Row class to return a value in the row of the matrix.
- */
+ * internal data structure of the Matrix.
+ *
+ * Row
+ * ===
+ *
+ * Internal Row class to return a value in the row of the matrix.
+ */
template <int Rows, int Cols, class Type>
class Row
{
@@ -71,7 +71,7 @@ public:
} // namespace details
/** Base Matrix class used by other similar classes.
- */
+ */
template <int Rows = 4, int Cols = 4, class Type = double>
class Matrix
{
@@ -94,10 +94,10 @@ public:
int colSize() const { return Cols; }
/** Return the row specified row as a Matrix with only one row.
- *
- * @param row Row number to be returned.
- * @return The row that is specified by the row variables.
- */
+ *
+ * @param row Row number to be returned.
+ * @return The row that is specified by the row variables.
+ */
Matrix<1, Cols, Type> getRow(int row) const
{
Matrix<1, Cols, Type> rowMatrix;
@@ -108,10 +108,10 @@ public:
}
/** Get a specific column in a column vector.
- *
- * @param col Column number to be returned.
- * @return Column Matrix of the selected column.
- */
+ *
+ * @param col Column number to be returned.
+ * @return Column Matrix of the selected column.
+ */
Matrix<Rows, 1, Type> getCol(int col) const
{
Matrix<Rows, 1, Type> colMatrix;
@@ -122,23 +122,23 @@ public:
}
/** Iterator support for the start.
- *
- * @return Iterator pointing to the start of the data.
- */
+ *
+ * @return Iterator pointing to the start of the data.
+ */
typename std::vector<Type>::iterator begin() { return data_.begin(); }
/** Iterator support for the end.
- *
- * @return Iterator pointing to the end of the data.
- */
+ *
+ * @return Iterator pointing to the end of the data.
+ */
typename std::vector<Type>::iterator end() { return data_.end(); }
/** Prints out the matrix, but can also be implemented by other classes to
- * print data differently.
- *
- * @bug When printing certain matrices, it omits a row or column. Still
- * need to determine under which conditions.
- */
+ * print data differently.
+ *
+ * @bug When printing certain matrices, it omits a row or column. Still
+ * need to determine under which conditions.
+ */
virtual std::string toString() const
{
std::stringstream ss;
@@ -166,7 +166,7 @@ public:
details::Row<Rows, Cols, Type> operator[](int row) const
{
return details::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type> *)this,
- row);
+ row);
}
Matrix<Rows, Cols, Type> &operator+=(const Matrix<Rows, Cols, Type> &rhs)
@@ -320,10 +320,10 @@ public:
};
/** 2D Vector class.
- *
- * Two dimensional vector class.
- */
-template <class Type = double>
+ *
+ * Two dimensional vector class.
+ */
+template <typename Type = double>
class Vector2 : public Vector<2, Type>
{
public:
@@ -339,30 +339,90 @@ public:
Vector2<Type>(const Matrix<2, 1, Type> &other) : Vector<2, Type>(other) {}
Type &x() { return this->data_[0]; }
-
const Type &x() const { return this->data_[0]; }
Type &y() { return this->data_[1]; }
-
const Type &y() const { return this->data_[1]; }
};
+/** 3D Vector class.
+ *
+ * Two dimensional vector class.
+ */
+template <typename Type = double>
+class Vector3 : public Vector<3, Type>
+{
+public:
+ Type &x, &y, &z;
+
+ Vector3<Type>() : Vector<4, Type>() {}
+
+ Vector3<Type>(std::vector<Type> data)
+ : Vector<3, Type>(data), x(this->data_[0]), y(this->data_[1]),
+ z(this->data_[2])
+ {
+ }
+
+ Vector3<Type>(Type x_in, Type y_in, Type z_in)
+ : Vector<3, Type>({x_in, y_in, z_in}), x(this->data_[0]),
+ y(this->data_[1]), z(this->data_[2])
+ {
+ }
+};
+
+/** 4D Vector class
+ */
+template <typename Type = double>
+class Vector4 : public Vector<4, Type>
+{
+public:
+ Type &x, &y, &z, &w;
+
+ Vector4<Type>() : Vector<4, Type>() {}
+
+ Vector4<Type>(std::vector<Type> data)
+ : Vector<4, Type>(data), x(this->data_[0]), y(this->data_[1]),
+ z(this->data_[2]), w(this->data_[3])
+ {
+ }
+
+ Vector4<Type>(Type x_in, Type y_in, Type z_in, Type w_in)
+ : Vector<4, Type>({x_in, y_in, z_in, w_in}), x(this->data_[0]),
+ y(this->data_[1]), z(this->data_[2]), w(this->data[3])
+ {
+ }
+};
+
/** Definition of a 2D vector.
- */
+ */
using Vector2d = Vector2<double>;
+using Vector2f = Vector2<float>;
+using Vector2i = Vector2<int>;
+
+/** Definition of a 3D vector.
+ */
+using Vector3d = Vector3<double>;
+using Vector3f = Vector3<float>;
+using Vector3i = Vector3<int>;
+
+/** Definition of a 4D vector
+ */
+using Vector4d = Vector4<double>;
+using Vector4f = Vector4<float>;
+using Vector4i = Vector4<int>;
/** Namespace containing functions that operate on matrices.
- *
- * Implementations defined here are meant to operate on anything that inherits
- * from the base Matrix class.
- */
+ *
+ * Implementations defined here are meant to operate on anything that inherits
+ * from the base Matrix class.
+ */
namespace matrix
{
/** Transposes a matrix and returns the result
- *
- * @param m input matrix.
- */
+ *
+ * @param m input matrix.
+ */
template <int M, int N, class T>
Matrix<N, M, T> transpose(const Matrix<M, N, T> &m)
{
@@ -376,9 +436,9 @@ Matrix<N, M, T> transpose(const Matrix<M, N, T> &m)
}
/** Returns the dot product between two vectors
- *
- * @param m1,m2 Input matrices.
- */
+ *
+ * @param m1,m2 Input matrices.
+ */
template <int R, class T>
T dot(const Matrix<R, 1, T> &m1, const Matrix<R, 1, T> &m2)
{
@@ -390,11 +450,11 @@ T dot(const Matrix<R, 1, T> &m1, const Matrix<R, 1, T> &m2)
}
/** Multiplies two matrices together.
- *
- * @param m1,m2 Matrix inputs
- *
- * Requires the two matrices to be compatible with multiplication.
- */
+ *
+ * @param m1,m2 Matrix inputs
+ *
+ * Requires the two matrices to be compatible with multiplication.
+ */
template <int M, int N, int P, int Q, class T>
Matrix<M, Q, T> multiply(const Matrix<M, N, T> &m1, const Matrix<P, Q, T> &m2)
{