Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Spritebatch.h //////////////////////////////////////////
- #pragma once
- #include<SFML\Graphics.hpp>
- using namespace sf;
- class SpriteBatch
- {
- public:
- SpriteBatch(void);
- ~SpriteBatch(void);
- void setRenderTarget(RenderTarget &rt)
- {
- this->rt = &rt;
- }
- void display(bool reset = true);
- void draw(Sprite &sprite);
- void draw(const Texture *texture, Vector2f position, IntRect rec, Color color, Vector2f scale, Vector2f origin, float rotation = 0);
- void draw(const Texture *texture, FloatRect dest, IntRect rec, Color color);
- private:
- RenderTarget *rt;
- RenderStates state;
- Vertex *vertices;
- int count;
- int capacity;
- };
- //Spritebatch.cpp //////////////////////////////////////////////
- #include "SpriteBatch.h"
- #include<math.h>
- const float Pi = 3.14159265;
- const int MaxCapacity = 400000;
- const int LookupSize = 512;
- float getSin[LookupSize];
- float getCos[LookupSize];
- bool initialized = false;
- void create_lookup()
- {
- for(int i=0; i < LookupSize; i++)
- {
- getSin[i] = sin(i * Pi / LookupSize * 2);
- getCos[i] = cos(i * Pi / LookupSize * 2);
- }
- initialized = true;
- }
- SpriteBatch::SpriteBatch(void)
- {
- count = 0;
- capacity = 20000;
- vertices = new Vertex[capacity];
- if(!initialized)
- create_lookup();
- }
- SpriteBatch::~SpriteBatch(void)
- {
- delete[] vertices;
- vertices = NULL;
- }
- void SpriteBatch::draw(Sprite &sprite)
- {
- draw(sprite.getTexture(), sprite.getPosition(), sprite.getTextureRect(), sprite.getColor(), sprite.getScale(), sprite.getOrigin(), sprite.getRotation());
- }
- void SpriteBatch::display(bool reset)
- {
- rt->draw(vertices, count * 4, PrimitiveType::Quads, state);
- count = 0;
- if(reset) state = RenderStates();
- }
- void SpriteBatch::draw(const Texture *texture, Vector2f position, IntRect rec, Color color, Vector2f scale, Vector2f origin, float rotation)
- {
- if (texture != state.texture)
- {
- display(false);
- state.texture = texture;
- }
- if (count*4 >= capacity)
- {
- display(false);
- if(capacity < MaxCapacity)
- {
- delete[] vertices;
- capacity *= 2;
- if(capacity > MaxCapacity) capacity = MaxCapacity;
- vertices = new Vertex[capacity];
- }
- }
- int rot = (int)(rotation / 360 * LookupSize) & (LookupSize -1);
- float _sin = getSin[rot];
- float _cos = getCos[rot];
- /*
- float _sin = 0, _cos = 1;
- if (rotation != 0)
- {
- rotation = rotation * Pi / 180;
- _sin = sin(rotation);
- _cos = cos(rotation);
- }*/
- origin.x *= scale.x;
- origin.y *= scale.y;
- scale.x *= rec.width;
- scale.y *= rec.height;
- Vertex v;
- v.color = color;
- float pX, pY;
- pX = -origin.x;
- pY = -origin.y;
- v.position.x = pX * _cos - pY * _sin + position.x;
- v.position.y = pX * _sin + pY * _cos + position.y;
- v.texCoords.x = rec.left;
- v.texCoords.y = rec.top;
- vertices[count * 4 + 0] = v;
- pX += scale.x;
- v.position.x = pX * _cos - pY * _sin + position.x;
- v.position.y = pX * _sin + pY * _cos + position.y;
- v.texCoords.x += rec.width;
- vertices[count * 4 + 1] = v;
- pY += scale.y;
- v.position.x = pX * _cos - pY * _sin + position.x;
- v.position.y = pX * _sin + pY * _cos + position.y;
- v.texCoords.y += rec.height;
- vertices[count * 4 + 2] = v;
- pX -= scale.x;
- v.position.x = pX * _cos - pY * _sin + position.x;
- v.position.y = pX * _sin + pY * _cos + position.y;
- v.texCoords.x -= rec.width;
- vertices[count * 4 + 3] = v;
- count++;
- }
- void SpriteBatch::draw(const Texture *texture, FloatRect dest, IntRect rec, Color color)
- {
- draw(texture, Vector2f(dest.left, dest.top), rec, color, Vector2f(1,1), Vector2f(0,0), 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement