1#ifndef DRAW_CACHE
2#define DRAW_CACHE
3
4#include "atlas.h"
5#include "coordsbuffer.h"
6#include "graphics.h"
7#include "painter.h"
8
9class DrawCache {
10public:
11 static const int MAX_SIZE = 65536;
12 static const int HALF_MAX_SIZE = MAX_SIZE / 2;
13
14 void draw();
15 void bind();
16 void release();
17 bool hasSpace(int size) {
18 return size + m_size < MAX_SIZE;
19 }
20 inline int getSize() { return m_size; }
21 void addRect(const Rect& dest, const Color& color);
22 void addTexturedRect(const Rect& dest, const Rect& src, const Color& color);
23 void addCoords(CoordsBuffer& coords, const Color& color);
24 void addTexturedCoords(CoordsBuffer& coords, const Point& offset, const Color& color);
25
26private:
27 inline void addRectRaw(float* dest, const Rect& rect)
28 {
29 dest[0] = dest[4] = dest[6] = rect.left();
30 dest[1] = dest[3] = dest[9] = rect.top();
31 dest[2] = dest[8] = dest[10] = rect.right() + 1;
32 dest[5] = dest[7] = dest[11] = rect.bottom() + 1;
33 }
34 inline void addColorRaw(const Color& color, int count)
35 {
36 static float c[4];
37 c[0] = color.rF();
38 c[1] = color.gF();
39 c[2] = color.bF();
40 c[3] = color.aF();
41 for (int start = m_size * 4, end = (m_size + count) * 4; start < end; start += 4) {
42 memcpy(m_color.data() + start, c, 4 * sizeof(float));
43 }
44 }
45
46 std::vector<float> m_destCoord = std::vector<float>(MAX_SIZE * 2);
47 std::vector<float> m_srcCoord = std::vector<float>(MAX_SIZE * 2);
48 std::vector<float> m_color = std::vector<float>(MAX_SIZE * 4);
49 bool m_bound = false;
50 int m_size = 0;
51};
52
53extern DrawCache g_drawCache;
54
55#endif
56