Advertisement
Guest User

C++ SFML SpriteBatch

a guest
Jan 6th, 2013
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. //Spritebatch.h //////////////////////////////////////////
  2. #pragma once
  3. #include<SFML\Graphics.hpp>
  4.  
  5. using namespace sf;
  6.  
  7. class SpriteBatch
  8. {
  9. public:
  10.     SpriteBatch(void);
  11.     ~SpriteBatch(void);
  12.  
  13.     void setRenderTarget(RenderTarget &rt)
  14.     {
  15.         this->rt = &rt;
  16.     }
  17.     void display(bool reset = true);
  18.     void draw(Sprite &sprite);
  19.     void draw(const Texture *texture, Vector2f position, IntRect rec, Color color, Vector2f scale, Vector2f origin, float rotation = 0);
  20.     void draw(const Texture *texture, FloatRect dest, IntRect rec, Color color);
  21. private:
  22.     RenderTarget *rt;
  23.     RenderStates state;
  24.     Vertex *vertices;
  25.     int count;
  26.     int capacity;
  27.  
  28. };
  29.  
  30. //Spritebatch.cpp //////////////////////////////////////////////
  31.  
  32. #include "SpriteBatch.h"
  33. #include<math.h>
  34.  
  35. const float Pi = 3.14159265;
  36. const int MaxCapacity = 400000;
  37. const int LookupSize = 512;
  38.  
  39. float getSin[LookupSize];
  40. float getCos[LookupSize];
  41. bool initialized = false;
  42.  
  43. void create_lookup()
  44. {
  45.     for(int i=0; i < LookupSize; i++)
  46.     {
  47.         getSin[i] = sin(i * Pi / LookupSize * 2);
  48.         getCos[i] = cos(i * Pi / LookupSize * 2);
  49.     }
  50.     initialized = true;
  51. }
  52.  
  53. SpriteBatch::SpriteBatch(void)
  54. {
  55.     count = 0;
  56.     capacity = 20000;
  57.     vertices = new Vertex[capacity];
  58.  
  59.     if(!initialized)
  60.         create_lookup();
  61. }
  62.  
  63.  
  64. SpriteBatch::~SpriteBatch(void)
  65. {
  66.     delete[] vertices;
  67.     vertices = NULL;
  68. }
  69.  
  70. void SpriteBatch::draw(Sprite &sprite)
  71. {
  72.     draw(sprite.getTexture(), sprite.getPosition(), sprite.getTextureRect(), sprite.getColor(), sprite.getScale(), sprite.getOrigin(), sprite.getRotation());
  73. }
  74.  
  75. void SpriteBatch::display(bool reset)
  76. {
  77.     rt->draw(vertices, count * 4, PrimitiveType::Quads, state);
  78.     count = 0;
  79.     if(reset) state = RenderStates();
  80. }
  81. void SpriteBatch::draw(const Texture *texture, Vector2f position, IntRect rec, Color color, Vector2f scale, Vector2f origin, float rotation)
  82.     {
  83.         if (texture != state.texture)
  84.         {
  85.             display(false);
  86.             state.texture = texture;
  87.         }
  88.  
  89.         if (count*4 >= capacity)
  90.         {
  91.             display(false);
  92.             if(capacity < MaxCapacity)
  93.             {
  94.                 delete[] vertices;
  95.                 capacity *= 2;
  96.                 if(capacity > MaxCapacity) capacity = MaxCapacity;
  97.                 vertices = new Vertex[capacity];
  98.             }
  99.         }                
  100.    
  101.         int rot = (int)(rotation / 360 * LookupSize) & (LookupSize -1);
  102.         float _sin = getSin[rot];
  103.         float _cos = getCos[rot];
  104.         /*
  105.         float _sin = 0, _cos = 1;
  106.         if (rotation != 0)
  107.         {
  108.                
  109.             rotation = rotation * Pi / 180;
  110.             _sin = sin(rotation);
  111.             _cos = cos(rotation);
  112.         }*/
  113.  
  114.         origin.x *= scale.x;
  115.         origin.y *= scale.y;
  116.         scale.x *= rec.width;
  117.         scale.y *= rec.height;
  118.  
  119.         Vertex v;
  120.  
  121.         v.color = color;
  122.  
  123.         float pX, pY;
  124.  
  125.         pX = -origin.x;
  126.         pY = -origin.y;
  127.         v.position.x = pX * _cos - pY * _sin + position.x;
  128.         v.position.y = pX * _sin + pY * _cos + position.y;
  129.         v.texCoords.x = rec.left;
  130.         v.texCoords.y = rec.top;
  131.         vertices[count * 4 + 0] = v;
  132.  
  133.         pX += scale.x;
  134.         v.position.x = pX * _cos - pY * _sin + position.x;
  135.         v.position.y = pX * _sin + pY * _cos + position.y;
  136.         v.texCoords.x += rec.width;
  137.         vertices[count * 4 + 1] = v;
  138.  
  139.         pY += scale.y;
  140.         v.position.x = pX * _cos - pY * _sin + position.x;
  141.         v.position.y = pX * _sin + pY * _cos + position.y;
  142.         v.texCoords.y += rec.height;
  143.         vertices[count * 4 + 2] = v;
  144.  
  145.         pX -= scale.x;
  146.         v.position.x = pX * _cos - pY * _sin + position.x;
  147.         v.position.y = pX * _sin + pY * _cos + position.y;
  148.         v.texCoords.x -= rec.width;
  149.         vertices[count * 4 + 3] = v;
  150.  
  151.         count++;
  152.     }
  153.  
  154. void SpriteBatch::draw(const Texture *texture, FloatRect dest, IntRect rec, Color color)
  155. {
  156.     draw(texture, Vector2f(dest.left, dest.top), rec, color, Vector2f(1,1), Vector2f(0,0), 0);
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement