~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to ext/native/gfx_es2/draw_buffer.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#pragma once
 
2
 
 
3
// "Immediate mode"-lookalike buffered drawing. Very fast way to draw 2D.
 
4
 
 
5
#include "base/basictypes.h"
 
6
#include "base/colorutil.h"
 
7
#include "gfx/gl_lost_manager.h"
 
8
#include "gfx/texture_atlas.h"
 
9
#include "math/geom2d.h"
 
10
#include "math/lin/matrix4x4.h"
 
11
#include "thin3d/thin3d.h"
 
12
 
 
13
#undef DrawText
 
14
 
 
15
struct Atlas;
 
16
 
 
17
enum {
 
18
        ALIGN_LEFT = 0,
 
19
        ALIGN_RIGHT = 16,
 
20
        ALIGN_TOP = 0,
 
21
        ALIGN_BOTTOM = 1,
 
22
        ALIGN_HCENTER = 4,
 
23
        ALIGN_VCENTER = 8,
 
24
        ALIGN_VBASELINE = 32,   // text only, possibly not yet working
 
25
 
 
26
        ALIGN_CENTER = ALIGN_HCENTER | ALIGN_VCENTER,
 
27
        ALIGN_TOPLEFT = ALIGN_TOP | ALIGN_LEFT,
 
28
        ALIGN_TOPRIGHT = ALIGN_TOP | ALIGN_RIGHT,
 
29
        ALIGN_BOTTOMLEFT = ALIGN_BOTTOM | ALIGN_LEFT,
 
30
        ALIGN_BOTTOMRIGHT = ALIGN_BOTTOM | ALIGN_RIGHT,
 
31
 
 
32
        // Only for text drawing
 
33
        ROTATE_90DEG_LEFT = 256,
 
34
        ROTATE_90DEG_RIGHT = 512,
 
35
        ROTATE_180DEG = 1024,
 
36
 
 
37
        // For "uncachable" text like debug log.
 
38
        // Avoids using system font drawing as it's too slow.
 
39
        // Not actually used here but is reserved for whatever system wraps DrawBuffer.
 
40
        FLAG_DYNAMIC_ASCII = 2048,
 
41
        FLAG_NO_PREFIX = 4096,  // means to not process ampersands
 
42
        FLAG_WRAP_TEXT = 8192,
 
43
};
 
44
 
 
45
class Thin3DShaderSet;
 
46
 
 
47
enum DrawBufferPrimitiveMode {
 
48
        DBMODE_NORMAL = 0,
 
49
        DBMODE_LINES = 1
 
50
};
 
51
 
 
52
struct GradientStop {
 
53
        float t;
 
54
        uint32_t color;
 
55
};
 
56
 
 
57
class TextDrawer;
 
58
 
 
59
class DrawBuffer {
 
60
public:
 
61
        DrawBuffer();
 
62
        ~DrawBuffer();
 
63
 
 
64
        void Begin(Thin3DShaderSet *shaders, DrawBufferPrimitiveMode mode = DBMODE_NORMAL);
 
65
        void End();
 
66
 
 
67
        // TODO: Enforce these. Now Init is autocalled and shutdown not called.
 
68
        void Init(Thin3DContext *t3d);
 
69
        void Shutdown();
 
70
 
 
71
        int Count() const { return count_; }
 
72
 
 
73
        void Flush(bool set_blend_state = true);
 
74
 
 
75
        void Rect(float x, float y, float w, float h, uint32_t color, int align = ALIGN_TOPLEFT);
 
76
        void hLine(float x1, float y, float x2, uint32_t color);
 
77
        void vLine(float x, float y1, float y2, uint32_t color);
 
78
        void vLineAlpha50(float x, float y1, float y2, uint32_t color);
 
79
 
 
80
        void Line(int atlas_image, float x1, float y1, float x2, float y2, float thickness, uint32_t color);
 
81
 
 
82
        void RectOutline(float x, float y, float w, float h, uint32_t color, int align = ALIGN_TOPLEFT);
 
83
 
 
84
        void RectVGradient(float x, float y, float w, float h, uint32_t colorTop, uint32_t colorBottom);
 
85
        void RectVDarkFaded(float x, float y, float w, float h, uint32_t colorTop) {
 
86
                RectVGradient(x, y, w, h, colorTop, darkenColor(colorTop));
 
87
        }
 
88
 
 
89
        void MultiVGradient(float x, float y, float w, float h, GradientStop *stops, int numStops);
 
90
 
 
91
        void RectCenter(float x, float y, float w, float h, uint32_t color) {
 
92
                Rect(x - w/2, y - h/2, w, h, color);
 
93
        }
 
94
        void Rect(float x, float y, float w, float h, float u, float v, float uw, float uh, uint32_t color);
 
95
 
 
96
        void V(float x, float y, float z, uint32_t color, float u, float v);
 
97
        void V(float x, float y, uint32_t color, float u, float v) {
 
98
                V(x, y, 0.0f, color, u, v);
 
99
        }
 
100
 
 
101
        void Circle(float x, float y, float radius, float thickness, int segments, float startAngle, uint32_t color, float u_mul);
 
102
 
 
103
        // New drawing APIs
 
104
 
 
105
        // Must call this before you use any functions with atlas_image etc.
 
106
        void SetAtlas(const Atlas *_atlas) {
 
107
                atlas = _atlas;
 
108
        }
 
109
        const Atlas *GetAtlas() const { return atlas; }
 
110
        void MeasureImage(ImageID atlas_image, float *w, float *h);
 
111
        void DrawImage(ImageID atlas_image, float x, float y, float scale, Color color = COLOR(0xFFFFFF), int align = ALIGN_TOPLEFT);
 
112
        void DrawImageStretch(ImageID atlas_image, float x1, float y1, float x2, float y2, Color color = COLOR(0xFFFFFF));
 
113
        void DrawImageStretch(int atlas_image, const Bounds &bounds, Color color = COLOR(0xFFFFFF)) {
 
114
                DrawImageStretch(atlas_image, bounds.x, bounds.y, bounds.x2(), bounds.y2(), color);
 
115
        }
 
116
        void DrawImageRotated(ImageID atlas_image, float x, float y, float scale, float angle, Color color = COLOR(0xFFFFFF), bool mirror_h = false);   // Always centers
 
117
        void DrawTexRect(float x1, float y1, float x2, float y2, float u1, float v1, float u2, float v2, Color color);
 
118
        void DrawTexRect(const Bounds &bounds, float u1, float v1, float u2, float v2, Color color) {
 
119
                DrawTexRect(bounds.x, bounds.y, bounds.x2(), bounds.y2(), u1, v1, u2, v2, color);
 
120
        }
 
121
        // Results in 18 triangles. Kind of expensive for a button.
 
122
        void DrawImage4Grid(ImageID atlas_image, float x1, float y1, float x2, float y2, Color color = COLOR(0xFFFFFF), float corner_scale = 1.0);
 
123
        // This is only 6 triangles, much cheaper.
 
124
        void DrawImage2GridH(ImageID atlas_image, float x1, float y1, float x2, Color color = COLOR(0xFFFFFF), float scale = 1.0);
 
125
 
 
126
        void MeasureText(int font, const char *text, float *w, float *h);
 
127
 
 
128
        // NOTE: Count is in plain chars not utf-8 chars!
 
129
        void MeasureTextCount(int font, const char *text, int count, float *w, float *h);
 
130
        void MeasureTextRect(int font, const char *text, int count, const Bounds &bounds, float *w, float *h, int align = 0);
 
131
 
 
132
        void DrawTextRect(int font, const char *text, float x, float y, float w, float h, Color color = 0xFFFFFFFF, int align = 0);
 
133
        void DrawText(int font, const char *text, float x, float y, Color color = 0xFFFFFFFF, int align = 0);
 
134
        void DrawTextShadow(int font, const char *text, float x, float y, Color color = 0xFFFFFFFF, int align = 0);
 
135
 
 
136
        void RotateSprite(ImageID atlas_image, float x, float y, float angle, float scale, Color color);
 
137
 
 
138
        void SetFontScale(float xs, float ys) {
 
139
                fontscalex = xs;
 
140
                fontscaley = ys;
 
141
        }
 
142
 
 
143
        static void DoAlign(int flags, float *x, float *y, float *w, float *h);
 
144
 
 
145
        void SetDrawMatrix(const Matrix4x4 &m) {
 
146
                drawMatrix_ = m;
 
147
        }
 
148
 
 
149
private:
 
150
        struct Vertex {
 
151
                float x, y, z;
 
152
                float u, v;
 
153
                uint32_t rgba;
 
154
        };
 
155
 
 
156
        Matrix4x4 drawMatrix_;
 
157
 
 
158
        Thin3DContext *t3d_;
 
159
        Thin3DBuffer *vbuf_;
 
160
        Thin3DVertexFormat *vformat_;
 
161
        Thin3DShaderSet *shaderSet_;
 
162
 
 
163
        Vertex *verts_;
 
164
        int count_;
 
165
        DrawBufferPrimitiveMode mode_;
 
166
        const Atlas *atlas;
 
167
 
 
168
        bool inited_;
 
169
        float fontscalex;
 
170
        float fontscaley;
 
171
};
 
172