From 55a1e0ad7c9d2661c266b2e767bfcb2f944e859f Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 4 Apr 2017 12:19:41 +0100 Subject: Adding spritebatch class --- include/YAGE/camera2d.hpp | 36 +++++++++++++++++++++++++++ include/YAGE/gl_texture.hpp | 13 ---------- include/YAGE/glsl_program.hpp | 33 ------------------------- include/YAGE/glslprogram.hpp | 37 ++++++++++++++++++++++++++++ include/YAGE/gltexture.hpp | 18 ++++++++++++++ include/YAGE/image_loader.hpp | 15 ----------- include/YAGE/imageloader.hpp | 19 ++++++++++++++ include/YAGE/io_manager.hpp | 14 ----------- include/YAGE/iomanager.hpp | 18 ++++++++++++++ include/YAGE/logger.hpp | 24 ------------------ include/YAGE/pico_png.hpp | 4 --- include/YAGE/picopng.hpp | 9 +++++++ include/YAGE/resource_manager.hpp | 18 -------------- include/YAGE/resourcemanager.hpp | 22 +++++++++++++++++ include/YAGE/sprite.hpp | 7 +++++- include/YAGE/spritebatch.hpp | 52 +++++++++++++++++++++++++++++++++++++++ include/YAGE/texture_cache.hpp | 20 --------------- include/YAGE/texturecache.hpp | 24 ++++++++++++++++++ include/YAGE/vertex.hpp | 5 ++++ include/YAGE/window.hpp | 22 +++++++++++++---- 20 files changed, 263 insertions(+), 147 deletions(-) create mode 100644 include/YAGE/camera2d.hpp delete mode 100644 include/YAGE/gl_texture.hpp delete mode 100644 include/YAGE/glsl_program.hpp create mode 100644 include/YAGE/glslprogram.hpp create mode 100644 include/YAGE/gltexture.hpp delete mode 100644 include/YAGE/image_loader.hpp create mode 100644 include/YAGE/imageloader.hpp delete mode 100644 include/YAGE/io_manager.hpp create mode 100644 include/YAGE/iomanager.hpp delete mode 100644 include/YAGE/logger.hpp delete mode 100644 include/YAGE/pico_png.hpp create mode 100644 include/YAGE/picopng.hpp delete mode 100644 include/YAGE/resource_manager.hpp create mode 100644 include/YAGE/resourcemanager.hpp create mode 100644 include/YAGE/spritebatch.hpp delete mode 100644 include/YAGE/texture_cache.hpp create mode 100644 include/YAGE/texturecache.hpp (limited to 'include') diff --git a/include/YAGE/camera2d.hpp b/include/YAGE/camera2d.hpp new file mode 100644 index 00000000..030b0a37 --- /dev/null +++ b/include/YAGE/camera2d.hpp @@ -0,0 +1,36 @@ +#ifndef CAMERA_2D_HPP +#define CAMERA_2D_HPP + +#include +#include + +namespace yage +{ + +class Camera2D +{ +private: + bool matrix_needs_update_=true; + float scale_=1.f; + glm::vec2 position_; + glm::mat4 camera_matrix_; + glm::mat4 ortho_matrix_; +public: + Camera2D(int screen_width=1280, int screen_height=720); + virtual ~Camera2D(); + + void update(); + + // setters + void setPosition(const glm::vec2 &new_position) { position_=new_position; matrix_needs_update_=true; } + void setScale(float new_scale) {scale_=new_scale; matrix_needs_update_=true; } + + // getters + float getScale() { return scale_; } + glm::vec2 getPosition() { return position_; } + glm::mat4 getCameraMatrix() { return camera_matrix_; } +}; + +} // yage + +#endif diff --git a/include/YAGE/gl_texture.hpp b/include/YAGE/gl_texture.hpp deleted file mode 100644 index 808d86b1..00000000 --- a/include/YAGE/gl_texture.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef GL_TEXTURE_HPP -#define GL_TEXTURE_HPP - -#include - -struct GlTexture -{ - GLuint id; - int width; - int height; -}; - -#endif diff --git a/include/YAGE/glsl_program.hpp b/include/YAGE/glsl_program.hpp deleted file mode 100644 index cef38a8e..00000000 --- a/include/YAGE/glsl_program.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef GLSL_PROGRAM_HPP -#define GLSL_PROGRAM_HPP - -#include - -#include - -class GlslProgram -{ -private: - // compiled shader program id - GLuint program_id_ = 0; - GLuint vertex_shader_id_ = 0; - GLuint fragment_shader_id_ = 0; - int attribute_index_ = 0; - - // compiles one shader - void compileShader(const GLuint &shader, const std::string &file_path); -public: - GlslProgram(); - ~GlslProgram(); - - // compiles vertex and fragment shader - void compileShaders(const std::string &vertex_shader_path, const std::string &fragment_shader_path); - void linkShaders(); - void addAttribute(const std::string &attribute_name); - GLint getUniformLocation(const std::string &uniform_name); - void use(); - void unuse(); -}; - - -#endif diff --git a/include/YAGE/glslprogram.hpp b/include/YAGE/glslprogram.hpp new file mode 100644 index 00000000..3e03723a --- /dev/null +++ b/include/YAGE/glslprogram.hpp @@ -0,0 +1,37 @@ +#ifndef GLSL_PROGRAM_HPP +#define GLSL_PROGRAM_HPP + +#include + +#include + +namespace yage +{ + +class GlslProgram +{ +private: + // compiled shader program id + GLuint program_id_ = 0; + GLuint vertex_shader_id_ = 0; + GLuint fragment_shader_id_ = 0; + int attribute_index_ = 0; + + // compiles one shader + void compileShader(const GLuint &shader, const std::string &file_path); +public: + GlslProgram(); + ~GlslProgram(); + + // compiles vertex and fragment shader + void compileShaders(const std::string &vertex_shader_path, const std::string &fragment_shader_path); + void linkShaders(); + void addAttribute(const std::string &attribute_name); + GLint getUniformLocation(const std::string &uniform_name); + void use(); + void unuse(); +}; + +} // yage + +#endif diff --git a/include/YAGE/gltexture.hpp b/include/YAGE/gltexture.hpp new file mode 100644 index 00000000..7446d560 --- /dev/null +++ b/include/YAGE/gltexture.hpp @@ -0,0 +1,18 @@ +#ifndef GL_TEXTURE_HPP +#define GL_TEXTURE_HPP + +#include + +namespace yage +{ + +struct GlTexture +{ + GLuint id; + int width; + int height; +}; + +} // yage + +#endif diff --git a/include/YAGE/image_loader.hpp b/include/YAGE/image_loader.hpp deleted file mode 100644 index 5f7b97f0..00000000 --- a/include/YAGE/image_loader.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef IMAGE_LOADER_HPP -#define IMAGE_LOADER_HPP - -#include "gl_texture.hpp" - -#include - -class ImageLoader -{ -public: - static GlTexture loadPng(const std::string &file_path); -}; - - -#endif diff --git a/include/YAGE/imageloader.hpp b/include/YAGE/imageloader.hpp new file mode 100644 index 00000000..632897dc --- /dev/null +++ b/include/YAGE/imageloader.hpp @@ -0,0 +1,19 @@ +#ifndef IMAGE_LOADER_HPP +#define IMAGE_LOADER_HPP + +#include "gltexture.hpp" + +#include + +namespace yage +{ + +class ImageLoader +{ +public: + static GlTexture loadPng(const std::string &file_path); +}; + +} // yage + +#endif diff --git a/include/YAGE/io_manager.hpp b/include/YAGE/io_manager.hpp deleted file mode 100644 index 05d288b5..00000000 --- a/include/YAGE/io_manager.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef IO_MANAGER_HPP -#define IO_MANAGER_HPP - -#include -#include - -class IoManager -{ -public: - static bool readFileToBuffer(const std::string &file_path, std::vector &buffer); -}; - - -#endif diff --git a/include/YAGE/iomanager.hpp b/include/YAGE/iomanager.hpp new file mode 100644 index 00000000..ef80b515 --- /dev/null +++ b/include/YAGE/iomanager.hpp @@ -0,0 +1,18 @@ +#ifndef IO_MANAGER_HPP +#define IO_MANAGER_HPP + +#include +#include + +namespace yage +{ + +class IoManager +{ +public: + static bool readFileToBuffer(const std::string &file_path, std::vector &buffer); +}; + +} // yage + +#endif diff --git a/include/YAGE/logger.hpp b/include/YAGE/logger.hpp deleted file mode 100644 index 36c7b9b3..00000000 --- a/include/YAGE/logger.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef LOGGER_HPP -#define LOGGER_HPP - -#include - -class Logger -{ -public: - template - static std::string log(std::ostream &out, Tail &&tail) - { - out< - static std::string log(std::ostream &out, Head &&head, Tail &&...tail) - { - out<(head); - log(out, std::forward(tail)...); - } -}; - - -#endif diff --git a/include/YAGE/pico_png.hpp b/include/YAGE/pico_png.hpp deleted file mode 100644 index ef123573..00000000 --- a/include/YAGE/pico_png.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#include -#include - -extern int decodePNG(std::vector &out_image, unsigned long &image_width, unsigned long &image_height, const unsigned char *in_png, size_t in_size, bool convert_to_rgba32 = true); diff --git a/include/YAGE/picopng.hpp b/include/YAGE/picopng.hpp new file mode 100644 index 00000000..8fea4f0d --- /dev/null +++ b/include/YAGE/picopng.hpp @@ -0,0 +1,9 @@ +#include +#include + +namespace yage +{ + +extern int decodePNG(std::vector &out_image, unsigned long &image_width, unsigned long &image_height, const unsigned char *in_png, size_t in_size, bool convert_to_rgba32 = true); + +} // yage diff --git a/include/YAGE/resource_manager.hpp b/include/YAGE/resource_manager.hpp deleted file mode 100644 index 155515a3..00000000 --- a/include/YAGE/resource_manager.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef RESOURCE_MANAGER_HPP -#define RESOURCE_MANAGER_HPP - -#include "gl_texture.hpp" -#include "texture_cache.hpp" - -#include - -class ResourceManager -{ -private: - static TextureCache texture_cache_; -public: - static GlTexture getTexture(const std::string &texture_path); -}; - - -#endif diff --git a/include/YAGE/resourcemanager.hpp b/include/YAGE/resourcemanager.hpp new file mode 100644 index 00000000..08ab31e0 --- /dev/null +++ b/include/YAGE/resourcemanager.hpp @@ -0,0 +1,22 @@ +#ifndef RESOURCE_MANAGER_HPP +#define RESOURCE_MANAGER_HPP + +#include "gltexture.hpp" +#include "texturecache.hpp" + +#include + +namespace yage +{ + +class ResourceManager +{ +private: + static TextureCache texture_cache_; +public: + static GlTexture getTexture(const std::string &texture_path); +}; + +} // yage + +#endif diff --git a/include/YAGE/sprite.hpp b/include/YAGE/sprite.hpp index 9f765c7d..8abc339a 100644 --- a/include/YAGE/sprite.hpp +++ b/include/YAGE/sprite.hpp @@ -1,12 +1,15 @@ #ifndef SPRITE_HPP #define SPRITE_HPP -#include "gl_texture.hpp" +#include "gltexture.hpp" #include #include +namespace yage +{ + class Sprite { private: @@ -23,5 +26,7 @@ public: void init(float x, float y, float width, float height, const std::string &texture_path); void draw(); }; + +} // yage #endif diff --git a/include/YAGE/spritebatch.hpp b/include/YAGE/spritebatch.hpp new file mode 100644 index 00000000..fcc6faec --- /dev/null +++ b/include/YAGE/spritebatch.hpp @@ -0,0 +1,52 @@ +#ifndef SPRITE_BATCH_HPP +#define SPRITE_BATCH_HPP + +#include "vertex.hpp" + +#include +#include + +#include + +namespace yage +{ + +struct Glyph +{ + GLuint texture; + float depth; + + Vertex top_left; + Vertex top_right; + Vertex bottom_right; + Vertex bottom_left; +}; + +class SpriteBatch +{ +public: // member variables +private: + GLuint vbo_=0; + GLuint vao_=0; + + std::vector glyphs_; + std::vector glyph_ptrs_; + +public: // member functions + SpriteBatch(); + ~SpriteBatch(); + + void begin(); + void end(); + + void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect, GLuint texture, const Color &color, float depth); + + void renderBatch(); +private: + void createVertexArray(); + void sortGlyphs(); +}; + +} // yage + +#endif diff --git a/include/YAGE/texture_cache.hpp b/include/YAGE/texture_cache.hpp deleted file mode 100644 index 44dba2f8..00000000 --- a/include/YAGE/texture_cache.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef TEXTURE_CACHE_HPP -#define TEXTURE_CACHE_HPP - -#include "gl_texture.hpp" - -#include - -class TextureCache -{ -private: - std::unordered_map texture_map_; -public: - TextureCache(); - ~TextureCache(); - - GlTexture getTexture(const std::string &texture_path); -}; - - -#endif diff --git a/include/YAGE/texturecache.hpp b/include/YAGE/texturecache.hpp new file mode 100644 index 00000000..43266ee9 --- /dev/null +++ b/include/YAGE/texturecache.hpp @@ -0,0 +1,24 @@ +#ifndef TEXTURE_CACHE_HPP +#define TEXTURE_CACHE_HPP + +#include "gltexture.hpp" + +#include + +namespace yage +{ + +class TextureCache +{ +private: + std::unordered_map texture_map_; +public: + TextureCache(); + ~TextureCache(); + + GlTexture getTexture(const std::string &texture_path); +}; + +} // yage + +#endif diff --git a/include/YAGE/vertex.hpp b/include/YAGE/vertex.hpp index d9ab1138..5826aeee 100644 --- a/include/YAGE/vertex.hpp +++ b/include/YAGE/vertex.hpp @@ -3,6 +3,9 @@ #include +namespace yage +{ + struct Position { float x; @@ -49,5 +52,7 @@ struct Vertex uv.v = v; } }; + +} // yage #endif diff --git a/include/YAGE/window.hpp b/include/YAGE/window.hpp index 6f3058e9..5f104912 100644 --- a/include/YAGE/window.hpp +++ b/include/YAGE/window.hpp @@ -5,7 +5,11 @@ #include -enum class WindowFlags +namespace yage +{ + +// window flags that can change it's appearance +enum WindowFlags : unsigned { SHOWN=0x1, HIDDEN=0x2, @@ -13,18 +17,26 @@ enum class WindowFlags BORDERLESS=0x8, }; +// window wrapper around SDL_Window pointer class Window { +public: // member variables private: + // window handle SDL_Window *window_=nullptr; - int width_=1280; - int height_=720; -public: +public: // member functions Window(); + // destroys the window handle ~Window(); - void create(const std::string &window_name, int width, int height, WindowFlags flags=WindowFlags::SHOWN); + // 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); + // swap the buffer + void swapBuffer(); +private: }; + +} // yage #endif -- cgit