From 905f72775fa91b0a467f3c0847c60cf0f85a6d80 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 4 Apr 2017 21:47:16 +0100 Subject: Sprite batching workin --- include/YAGE/camera2d.hpp | 10 ++++--- include/YAGE/inputmanager.hpp | 23 +++++++++++++++ include/YAGE/spritebatch.hpp | 67 +++++++++++++++++++++++++++++++++---------- include/YAGE/window.hpp | 2 ++ 4 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 include/YAGE/inputmanager.hpp (limited to 'include') diff --git a/include/YAGE/camera2d.hpp b/include/YAGE/camera2d.hpp index 030b0a37..0c86fc31 100644 --- a/include/YAGE/camera2d.hpp +++ b/include/YAGE/camera2d.hpp @@ -9,21 +9,23 @@ namespace yage class Camera2D { + // member variables private: bool matrix_needs_update_=true; float scale_=1.f; glm::vec2 position_; glm::mat4 camera_matrix_; glm::mat4 ortho_matrix_; + + // member functions public: Camera2D(int screen_width=1280, int screen_height=720); virtual ~Camera2D(); + // update camera location 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; } + // camera movement + void move(const glm::vec2 &direction); // getters float getScale() { return scale_; } diff --git a/include/YAGE/inputmanager.hpp b/include/YAGE/inputmanager.hpp new file mode 100644 index 00000000..7f49c3b3 --- /dev/null +++ b/include/YAGE/inputmanager.hpp @@ -0,0 +1,23 @@ +#ifndef INPUT_MANAGER_HPP +#define INPUT_MANAGER_HPP + +#include + +namespace yage +{ + +class InputManager +{ + // member variables +private: + std::unordered_map key_map_; + + // member functions +public: + void keyPressed(unsigned key); + void keyReleased(unsigned key); + bool isKeyPressed(unsigned key) const; +}; + +} +#endif diff --git a/include/YAGE/spritebatch.hpp b/include/YAGE/spritebatch.hpp index fcc6faec..3f40a009 100644 --- a/include/YAGE/spritebatch.hpp +++ b/include/YAGE/spritebatch.hpp @@ -11,39 +11,76 @@ namespace yage { -struct Glyph +class Glyph { - GLuint texture; - float depth; - - Vertex top_left; - Vertex top_right; - Vertex bottom_right; - Vertex bottom_left; + // member variables +private: + GLuint texture_; + float depth_; + Vertex top_left_; + Vertex top_right_; + Vertex bottom_right_; + Vertex bottom_left_; + + // member functions +public: + Glyph(GLuint texture, float depth, const Vertex &top_left, const Vertex &top_right, const Vertex &bottom_right, const Vertex &bottom_left); + + inline GLuint texture() const { return texture_; } + inline float depth() const { return depth_; } + inline Vertex top_left() const { return top_left_; } + inline Vertex top_right() const { return top_right_; } + inline Vertex bottom_right() const { return bottom_right_; } + inline Vertex bottom_left() const { return bottom_left_; } +}; + +class RenderBatch +{ + // member variables +public: + GLint offset_; +private: + GLsizei num_vertices_; + GLuint texture_; + + // member functions +public: + RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture); + + // getters + inline GLint offset() const { return offset_; } + inline GLsizei num_vertices() const { return num_vertices_; } + inline GLuint texture() const { return texture_; } }; class SpriteBatch { -public: // member variables + // member variables +public: + static const int NUM_VERTICES=6; private: GLuint vbo_=0; GLuint vao_=0; - std::vector glyphs_; std::vector glyph_ptrs_; - -public: // member functions + std::vector render_batches_; + + // member functions +public: SpriteBatch(); ~SpriteBatch(); + // initialize vaos and vbos + void init(); 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 renderBatch(); + // render the batch + void render(); private: void createVertexArray(); + void createRenderBatches(); void sortGlyphs(); }; diff --git a/include/YAGE/window.hpp b/include/YAGE/window.hpp index 5f104912..d86c00ac 100644 --- a/include/YAGE/window.hpp +++ b/include/YAGE/window.hpp @@ -34,6 +34,8 @@ public: // member functions void create(const std::string &window_name, int width, int height, unsigned flags=WindowFlags::SHOWN); // swap the buffer void swapBuffer(); + // clear buffer + void clearBuffer(); private: }; -- cgit