aboutsummaryrefslogtreecommitdiffstats
path: root/yage
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-09-25 17:46:30 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-09-25 17:46:30 +0100
commit5e6067f74e7ac072656f11ead0f22ec7f8e9c525 (patch)
treeca9e3758832d68ea1d9cf531c48d1962532d8dfa /yage
parent619746d51e91f02e7c5327f0c65ebb5952632bc7 (diff)
downloadYAGE-5e6067f74e7ac072656f11ead0f22ec7f8e9c525.tar.gz
YAGE-5e6067f74e7ac072656f11ead0f22ec7f8e9c525.zip
Testing spritesheet
Diffstat (limited to 'yage')
-rw-r--r--yage/base/CMakeLists.txt2
-rw-r--r--yage/base/imageloader.cpp11
-rw-r--r--yage/base/spritesheet.cpp81
-rw-r--r--yage/base/spritesheet.h3
-rw-r--r--yage/base/texture.h2
-rw-r--r--yage/base/window.cpp21
-rw-r--r--yage/base/window.h6
-rw-r--r--yage/yage.h1
8 files changed, 106 insertions, 21 deletions
diff --git a/yage/base/CMakeLists.txt b/yage/base/CMakeLists.txt
index acdb78bf..f36d806d 100644
--- a/yage/base/CMakeLists.txt
+++ b/yage/base/CMakeLists.txt
@@ -3,7 +3,9 @@ set(YAGE_BASE_SOURCES
base/window.cpp
base/texturecache.cpp
base/glslprogram.cpp
+ base/sprite.cpp
base/spritebatch.cpp
+ base/spritesheet.cpp
base/resourcemanager.cpp
base/sprite.cpp
base/inputmanager.cpp
diff --git a/yage/base/imageloader.cpp b/yage/base/imageloader.cpp
index 0d7d5df9..e1d54dc1 100644
--- a/yage/base/imageloader.cpp
+++ b/yage/base/imageloader.cpp
@@ -6,10 +6,16 @@
* ----------------------------------------------------------------------------
*/
-#include <yage/base/imageloader.h>
+#include "imageloader.h"
+
+#ifndef UNIT_TESTS
+#include <glad/glad.h>
+#endif
+
#include <yage/base/iomanager.h>
#include <yage/base/picopng.h>
+#include <iostream>
#include <stdexcept>
namespace yage
@@ -33,7 +39,8 @@ Texture ImageLoader::loadPng(const std::string &file_path)
std::to_string(error_code));
}
- Texture texture{0, (int)width, (int)height};
+ Texture texture(0, static_cast<int>(width), static_cast<int>(height));
+ std::cout << "Geometry: " << texture.width << "x" << texture.height << "\n";
glGenTextures(1, &texture.id);
diff --git a/yage/base/spritesheet.cpp b/yage/base/spritesheet.cpp
index 748cdb5c..311bb955 100644
--- a/yage/base/spritesheet.cpp
+++ b/yage/base/spritesheet.cpp
@@ -8,6 +8,18 @@
#include "spritesheet.h"
+#include <cassert>
+#include <fstream>
+#include <sstream>
+#include <stdexcept>
+
+#include <rapidjson/reader.h>
+#include <yage/base/imageloader.h>
+
+using namespace std;
+using namespace rapidjson;
+using namespace yage::details;
+
namespace yage
{
@@ -49,14 +61,14 @@ bool SpriteSheetHandler::Double(double d)
return handleNumber(static_cast<int>(d));
}
-bool SpriteSheetHandler::String(const char *, rapidjson::SizeType, bool)
+bool SpriteSheetHandler::String(const char *, SizeType, bool)
{
return true;
}
-bool SpriteSheetHandler::Key(const char *str, rapidjson::SizeType length, bool)
+bool SpriteSheetHandler::Key(const char *str, SizeType length, bool)
{
- current_key_ = std::string(str, length);
+ current_key_ = string(str, length);
return true;
}
@@ -64,16 +76,16 @@ bool SpriteSheetHandler::StartObject()
{
depth_++;
- if(depth_ == 3) {
+ if (depth_ == 3) {
current_image_ = current_key_;
}
return true;
}
-bool SpriteSheetHandler::EndObject(rapidjson::SizeType)
+bool SpriteSheetHandler::EndObject(SizeType)
{
- if(depth_ == 3) {
+ if (depth_ == 3) {
map_[current_image_] = coord_;
}
depth_--;
@@ -85,7 +97,7 @@ bool SpriteSheetHandler::StartArray()
return true;
}
-bool SpriteSheetHandler::EndArray(rapidjson::SizeType)
+bool SpriteSheetHandler::EndArray(SizeType)
{
return true;
}
@@ -95,23 +107,33 @@ SpriteMap SpriteSheetHandler::spriteMap() const
return map_;
}
+int SpriteSheetHandler::imageWidth() const
+{
+ return image_width_;
+}
+
+int SpriteSheetHandler::imageHeight() const
+{
+ return image_height_;
+}
+
bool SpriteSheetHandler::handleNumber(int i)
{
- if(current_key_ == "width") {
- if(depth_ == 1) {
+ if (current_key_ == "width") {
+ if (depth_ == 1) {
image_width_ = i;
} else {
coord_.width = i;
}
- } else if(current_key_ == "height") {
- if(depth_ == 1) {
+ } else if (current_key_ == "height") {
+ if (depth_ == 1) {
image_height_ = i;
} else {
coord_.height = i;
}
- } else if(current_key_ == "x") {
+ } else if (current_key_ == "x") {
coord_.x = i;
- } else if(current_key_ == "y") {
+ } else if (current_key_ == "y") {
coord_.y = i;
}
return true;
@@ -119,4 +141,37 @@ bool SpriteSheetHandler::handleNumber(int i)
} // namespace details
+SpriteSheet::SpriteSheet(string pngFileName, string jsonFileName)
+{
+ string fileContents = fileContent(jsonFileName);
+
+ SpriteSheetHandler ssHandler;
+ Reader reader;
+ StringStream ss(fileContents.c_str());
+ reader.Parse(ss, ssHandler);
+
+ fileLocations_ = ssHandler.spriteMap();
+ texture_ = ImageLoader::loadPng(pngFileName);
+
+ if (texture_.width != ssHandler.imageWidth())
+ throw runtime_error("Texture width not equal: " +
+ to_string(texture_.width) +
+ " != " + to_string(ssHandler.imageWidth()));
+ if (texture_.height != ssHandler.imageHeight())
+ throw runtime_error(
+ "Texture height not equal: " + to_string(texture_.height) +
+ " != " + to_string(ssHandler.imageHeight()));
+ assert(texture_.height == ssHandler.imageHeight());
+}
+
+string SpriteSheet::fileContent(string jsonFileName) const
+{
+ ifstream inputFile(jsonFileName);
+
+ stringstream stream;
+ stream << inputFile.rdbuf();
+
+ return stream.str();
+}
+
} // namespace yage
diff --git a/yage/base/spritesheet.h b/yage/base/spritesheet.h
index bc60f8b9..3dca6b57 100644
--- a/yage/base/spritesheet.h
+++ b/yage/base/spritesheet.h
@@ -58,6 +58,8 @@ public:
bool EndArray(rapidjson::SizeType memberCount);
SpriteMap spriteMap() const;
+ int imageWidth() const;
+ int imageHeight() const;
private:
std::string current_key_;
@@ -79,6 +81,7 @@ public:
SpriteSheet(std::string pngFileName, std::string jsonFileName);
void sprite(std::string spriteName) const;
+ std::string fileContent(std::string jsonFileName) const;
private:
Texture texture_;
diff --git a/yage/base/texture.h b/yage/base/texture.h
index cc9dc857..82b7dde3 100644
--- a/yage/base/texture.h
+++ b/yage/base/texture.h
@@ -19,6 +19,8 @@ struct Texture {
int width;
int height;
+ Texture() = default;
+
Texture(GLuint id_i, int width_i, int height_i)
: id(id_i), width(width_i), height(height_i)
{
diff --git a/yage/base/window.cpp b/yage/base/window.cpp
index 67f32bb2..38056b14 100644
--- a/yage/base/window.cpp
+++ b/yage/base/window.cpp
@@ -8,7 +8,6 @@
#include "window.h"
-#include <iostream>
#include <stdexcept>
namespace yage
@@ -22,12 +21,13 @@ Window::~Window()
}
void Window::create(const std::string &window_name, int width, int height,
- unsigned flags)
+ unsigned)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
- window_ = glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr);
+ window_ =
+ glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr);
if (window_ == nullptr) {
throw std::runtime_error("GLFW Window creation failed");
}
@@ -35,9 +35,8 @@ void Window::create(const std::string &window_name, int width, int height,
// initialize the gl context
glfwMakeContextCurrent(window_);
- // print out the current OpenGL version to debug
- std::cout << "*** OpenGL version: " << glGetString(GL_VERSION)
- << " ***\n";
+ // initialize glad
+ gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
// set vsync on
glfwSwapInterval(1);
@@ -63,4 +62,14 @@ void Window::clearBuffer()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
+void Window::hide()
+{
+ glfwHideWindow(window_);
+}
+
+void Window::show()
+{
+ glfwShowWindow(window_);
+}
+
} // namespace yage
diff --git a/yage/base/window.h b/yage/base/window.h
index eb4cf005..3f448132 100644
--- a/yage/base/window.h
+++ b/yage/base/window.h
@@ -12,6 +12,8 @@
#ifndef WINDOW_H
#define WINDOW_H
+#include <glad/glad.h>
+
#include <GLFW/glfw3.h>
#include <string>
@@ -51,6 +53,10 @@ public:
void swapBuffer();
/// clear buffer
void clearBuffer();
+ /// hide windowProc
+ void hide();
+ /// show window
+ void show();
};
} // namespace yage
diff --git a/yage/yage.h b/yage/yage.h
index 4fce87dc..920652c1 100644
--- a/yage/yage.h
+++ b/yage/yage.h
@@ -22,6 +22,7 @@
#include "base/picopng.h"
#include "base/resourcemanager.h"
#include "base/spritebatch.h"
+#include "base/spritesheet.h"
#include "base/texture.h"
#include "base/vertex.h"
#include "base/window.h"