Euphoria
spritebatch.cc
Go to the documentation of this file.
1 #include "render/spritebatch.h"
2 #include "render/gl.h"
3 
4 #include "assert/assert.h"
5 #include "base/cint.h"
6 
7 namespace eu::render
8 {
9  // vertex + uv + color
10  constexpr int quad_cont = 10;
11  constexpr int stride = 2 * 4 + 2 * 4 + 4 * 4;
12 
13 
14  SpriteBatch::SpriteBatch() : is_inside(false), current_quad_count(0), number_of_render_calls(0)
15  {
16  vertex_data.reserve(c_int_to_sizet(stride * quad_cont));
17  quad_indices.reserve(c_int_to_sizet(6 * quad_cont));
18  }
19 
20 
21  SpriteBatch::~SpriteBatch() = default;
22 
23 
24  void
26  {
27  ASSERT(!is_inside && "Already open, missing call to end.");
28  number_of_render_calls = 0;
29  }
30 
31 
32  void
34  (
35  const vec2f& pos,
36  const size2f& quad,
37  const Rectf& uv,
38  const Scale2f& center,
39  const Angle& rotation,
40  const Rgba& color
41  )
42  {
43  ASSERT(is_inside && "batch need to be open");
44 
45  if((current_quad_count + 1) >= quad_cont)
46  {
47  flush();
48  }
49 
50  // add vertices
51  const float w = quad.width;
52  const float h = quad.height;
53  const float sy = -sin(rotation);
54  const float left = pos.x + (-center.x * w) * sy;
55  const float right = pos.x + (-center.x * w + w) * sy;
56  const float top = pos.y + (-center.y * h) * sy;
57  const float bottom = pos.y + (-center.y * h + h) * sy;
58  const auto upper_left = vec2f(left, top);
59  const auto upper_right = vec2f(right, top);
60  const auto lower_left = vec2f(left, bottom);
61  const auto lower_right = vec2f(right, bottom);
62 
63  vertex_data.push_back(upper_left.x);
64  vertex_data.push_back(upper_left.y);
65 
66  vertex_data.push_back(upper_right.x);
67  vertex_data.push_back(upper_right.y);
68 
69  vertex_data.push_back(lower_right.x);
70  vertex_data.push_back(lower_right.y);
71 
72  vertex_data.push_back(lower_left.x);
73  vertex_data.push_back(lower_left.y);
74 
75  // add uv coordinate
76  vertex_data.push_back(uv.left);
77  vertex_data.push_back(uv.top);
78  vertex_data.push_back(uv.right);
79  vertex_data.push_back(uv.top);
80  vertex_data.push_back(uv.right);
81  vertex_data.push_back(uv.bottom);
82  vertex_data.push_back(uv.left);
83  vertex_data.push_back(uv.bottom);
84 
85  // add color
86  for(int color_counter = 0; color_counter < 4; color_counter += 1)
87  {
88  vertex_data.push_back(color.r);
89  vertex_data.push_back(color.g);
90  vertex_data.push_back(color.b);
91  vertex_data.push_back(color.a);
92  }
93 
94  // add index
95  int start = current_quad_count * 4;
96 
97  quad_indices.push_back(start + 0);
98  quad_indices.push_back(start + 1);
99  quad_indices.push_back(start + 2);
100 
101  quad_indices.push_back(start + 0);
102  quad_indices.push_back(start + 2);
103  quad_indices.push_back(start + 3);
104 
105  current_quad_count += 1;
106  }
107 
108 
109  void
111  {
112  ASSERT(is_inside && "not open, missing begin.");
113  flush();
114  is_inside = false;
115  }
116 
117 
118  void
120  {
121  if(current_quad_count == 0)
122  {
123  return;
124  }
125  // todo(Gustav): build vbo & render vbo
126  // remove all items
127  current_quad_count = 0;
128  vertex_data.resize(0);
129  number_of_render_calls += 1;
130  }
131 }
#define ASSERT(x)
Definition: assert.h:29
constexpr int quad_cont
Definition: spritebatch.cc:10
constexpr int stride
Definition: spritebatch.cc:11
size_t c_int_to_sizet(int i)
Definition: cint.cc:35
float sin(const Angle &ang)
Definition: angle.cc:61
Definition: rect.h:27
Definition: rgb.h:143
void render_quad(const vec2f &pos, const size2f &quad, const Rectf &uv, const Scale2f &center, const Angle &rotation, const Rgba &color)
Definition: spritebatch.cc:34
float height
Definition: size2.h:14
float width
Definition: size2.h:13
Definition: vec2.h:33
float x
Definition: vec2.h:34
float y
Definition: vec2.h:35