Skip to content

Instantly share code, notes, and snippets.

@DCubix
Created December 2, 2016 20:13
Show Gist options
  • Save DCubix/b37d97273a90c579eea79e3bd1bdea63 to your computer and use it in GitHub Desktop.
Save DCubix/b37d97273a90c579eea79e3bd1bdea63 to your computer and use it in GitHub Desktop.
/*
* The MIT License
*
* Copyright 2016 twisterge.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* File: SpriteBatch.h
* Author: twisterge
*
* Created on November 30, 2016, 10:39 AM
*/
#ifndef SPRITEBATCH_H
#define SPRITEBATCH_H
#include "vmath.h"
#include "Texture.h"
#include "Shader.h"
#include <vector>
typedef struct {
Vector3f position;
Vector2f uv;
Vector4f color;
} Vertex;
typedef struct {
Vertex v1, v2, v3, v4;
Texture* diffuse = nullptr;
Texture* normal = nullptr;
Texture* depth = nullptr;
} Sprite;
typedef struct {
Texture* diffuse = nullptr;
Texture* normal = nullptr;
Texture* depth = nullptr;
uint offset, vertex_count;
} RenderBatch;
#define SB_SPRITE_SIZE sizeof(Vertex) * 6
#define SB_MAX_SPRITES 20000
#define SB_BUFFER_SIZE SB_MAX_SPRITES * SB_SPRITE_SIZE
#define OGL_OFFSET(t, x) ((void*)offsetof(t, x))
class SpriteBatch {
public:
SpriteBatch(int width, int height);
~SpriteBatch();
void Draw(Vector2f position, Texture* diffuse);
void Begin();
void End();
void Render();
private:
GLuint m_vbo, m_vao;
std::vector<Sprite*> m_sprites;
std::vector<RenderBatch*> m_batches;
bool m_drawing;
Shader* m_default;
Matrix4f model, projection;
};
#endif /* SPRITEBATCH_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment