From 1bb0ef8960c71ef505a351702bec54c01ba15e22 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 12 Oct 2017 14:57:26 +0100 Subject: Fixing spritesheet and fixed #12 --- yage/CMakeLists.txt | 4 - yage/base/resourcemanager.h | 2 + yage/base/spritebatch.cpp | 4 +- yage/base/spritebatch.h | 10 ++- yage/base/spritesheet.cpp | 33 ++++++++- yage/base/spritesheet.h | 6 +- yage/base/texturecache.cpp | 2 - yage/base/texturecache.h | 3 +- yage/base/window.cpp | 26 +++++-- yage/base/window.h | 5 +- yage/math/matrix.h | 174 +++++++++++++++++++++++++++++--------------- 11 files changed, 186 insertions(+), 83 deletions(-) (limited to 'yage') 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 #include +#include #include @@ -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 @@ -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 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 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 class Row { @@ -71,7 +71,7 @@ public: } // namespace details /** Base Matrix class used by other similar classes. - */ + */ template 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 getCol(int col) const { Matrix 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::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::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 operator[](int row) const { return details::Row((Matrix *)this, - row); + row); } Matrix &operator+=(const Matrix &rhs) @@ -320,10 +320,10 @@ public: }; /** 2D Vector class. - * - * Two dimensional vector class. - */ -template + * + * Two dimensional vector class. + */ +template class Vector2 : public Vector<2, Type> { public: @@ -339,30 +339,90 @@ public: Vector2(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 +class Vector3 : public Vector<3, Type> +{ +public: + Type &x, &y, &z; + + Vector3() : Vector<4, Type>() {} + + Vector3(std::vector data) + : Vector<3, Type>(data), x(this->data_[0]), y(this->data_[1]), + z(this->data_[2]) + { + } + + Vector3(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 +class Vector4 : public Vector<4, Type> +{ +public: + Type &x, &y, &z, &w; + + Vector4() : Vector<4, Type>() {} + + Vector4(std::vector data) + : Vector<4, Type>(data), x(this->data_[0]), y(this->data_[1]), + z(this->data_[2]), w(this->data_[3]) + { + } + + Vector4(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; +using Vector2f = Vector2; +using Vector2i = Vector2; + +/** Definition of a 3D vector. + */ +using Vector3d = Vector3; +using Vector3f = Vector3; +using Vector3i = Vector3; + +/** Definition of a 4D vector + */ +using Vector4d = Vector4; +using Vector4f = Vector4; +using Vector4i = Vector4; /** 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 Matrix transpose(const Matrix &m) { @@ -376,9 +436,9 @@ Matrix transpose(const Matrix &m) } /** Returns the dot product between two vectors - * - * @param m1,m2 Input matrices. - */ + * + * @param m1,m2 Input matrices. + */ template T dot(const Matrix &m1, const Matrix &m2) { @@ -390,11 +450,11 @@ T dot(const Matrix &m1, const Matrix &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 Matrix multiply(const Matrix &m1, const Matrix &m2) { -- cgit