aboutsummaryrefslogtreecommitdiffstats
path: root/yage
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-04-05 15:05:36 +0100
committerYann Herklotz <ymherklotz@gmail.com>2018-04-05 15:05:36 +0100
commitaeeca45ae53c2382354f26fb4dae02fbda62b314 (patch)
tree04e94114d58b2fa726943c590d3261ed472507ea /yage
parent0d10ba12062144a270a368ef9bffd88517a6c27c (diff)
downloadYAGE-aeeca45ae53c2382354f26fb4dae02fbda62b314.tar.gz
YAGE-aeeca45ae53c2382354f26fb4dae02fbda62b314.zip
Adding test abstraction and fixing tests
Diffstat (limited to 'yage')
-rw-r--r--yage/core/imageloader.cpp8
-rw-r--r--yage/core/spritesheet.cpp37
-rw-r--r--yage/core/spritesheet.h4
3 files changed, 26 insertions, 23 deletions
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 <iostream>
#include <glad/glad.h>
#include <iostream>
#include <stdexcept>
+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<int>(width), static_cast<int>(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 <sstream>
#include <stdexcept>
+#include <iostream>
+
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 <rapidjson/reader.h>
@@ -46,9 +47,10 @@ typedef std::map<std::string, details::Coordinate> 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;