Advertisement
Guest User

Untitled

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