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/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 +++-- 9 files changed, 69 insertions(+), 22 deletions(-) (limited to 'yage/base') 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 -- cgit