From aeeca45ae53c2382354f26fb4dae02fbda62b314 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 5 Apr 2018 15:05:36 +0100 Subject: Adding test abstraction and fixing tests --- yage/core/imageloader.cpp | 8 +++++++- yage/core/spritesheet.cpp | 37 ++++++++++++++++--------------------- yage/core/spritesheet.h | 4 +++- 3 files changed, 26 insertions(+), 23 deletions(-) (limited to 'yage/core') diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp index 23bc9a25..4cd583d1 100644 --- a/yage/core/imageloader.cpp +++ b/yage/core/imageloader.cpp @@ -10,22 +10,28 @@ #include "../data/texture.h" #include "logger.h" #include "stb_image.h" +#include #include #include #include +using std::cout; + namespace yage { Texture ImageLoader::loadPng(const std::string &file_path) { int width, height, num_channels; + yLogDebug << "Loading image from disk: " << file_path; unsigned char *data = stbi_load(file_path.c_str(), &width, &height, &num_channels, 0); - + yLogDebug << "Sucessfully loaded file"; Texture texture(0, static_cast(width), static_cast(height)); + yLogDebug << "Creating texture"; + cout << "Hello"; glGenTextures(1, &texture.id); glBindTexture(GL_TEXTURE_2D, texture.id); diff --git a/yage/core/spritesheet.cpp b/yage/core/spritesheet.cpp index 4ecdcf71..f3f99619 100644 --- a/yage/core/spritesheet.cpp +++ b/yage/core/spritesheet.cpp @@ -18,24 +18,30 @@ #include #include +#include + using rapidjson::Document; using yage::details::Coordinate; using yage::details::SpriteMap; +using std::cout; + namespace yage { SpriteSheet::SpriteSheet(std::string pngFileName, std::string jsonFileName) { int jsonWidth, jsonHeight; + fileLocations_ = parseJson(jsonWidth, jsonHeight, fileContent(jsonFileName)); texture_ = ImageLoader::loadPng(pngFileName); - - if (texture_.width != jsonWidth) + if (texture_.width != jsonWidth) { throw std::runtime_error("JSON width does not match texture width"); - if (texture_.height != jsonHeight) + } + if (texture_.height != jsonHeight) { throw std::runtime_error("JSON height does not match texture height"); + } } std::string SpriteSheet::fileContent(std::string jsonFileName) const @@ -54,27 +60,16 @@ SpriteMap SpriteSheet::parseJson(int &width, int &height, SpriteMap spriteMap; Document jsonAtlas; jsonAtlas.Parse(jsonContent.c_str()); - width = jsonAtlas["width"].GetInt(); height = jsonAtlas["height"].GetInt(); - for (auto &texture : jsonAtlas["sprites"].GetObject()) { - Coordinate coord; - for (auto &value : texture.value.GetObject()) { - std::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 std::runtime_error("JSON key incorrect: " + keyName); - } - } + Coordinate coord; + for(auto &texture : jsonAtlas["sprites"].GetObject()) { + auto texVal = texture.value.GetObject(); + coord.x = texVal["x"].GetInt(); + coord.y = texVal["y"].GetInt(); + coord.width = texVal["width"].GetInt(); + coord.height = texVal["height"].GetInt(); spriteMap[texture.name.GetString()] = coord; } diff --git a/yage/core/spritesheet.h b/yage/core/spritesheet.h index 7b089d25..ea1db44b 100644 --- a/yage/core/spritesheet.h +++ b/yage/core/spritesheet.h @@ -16,6 +16,7 @@ * spritesheet. */ #include "../data/texture.h" +#include "../util/noncopyable.h" #include @@ -46,9 +47,10 @@ typedef std::map SpriteMap; } // namespace details -class SpriteSheet +class SpriteSheet : public NonCopyable { public: + SpriteSheet() = default; SpriteSheet(std::string pngFileName, std::string jsonFileName); void sprite(std::string spriteName) const; -- cgit