summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/tilebuf.cc
blob: 85db8c19161a7c9c06e1bb95b8d6aeb6d8cb6c03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
 * @file
 * @brief Vertex buffer implementaions
**/

#include "AppHdr.h"

#ifdef USE_TILE_LOCAL

#include "tilebuf.h"
#include "tilefont.h"

/////////////////////////////////////////////////////////////////////////////
// VertBuffer

VertBuffer::VertBuffer(bool texture, bool colour, const GenericTexture *tex,
                       drawing_modes prim) :
    m_tex(tex),
    m_prim(prim),
    m_colour_verts(colour),
    m_texture_verts(texture)
{
    m_vert_buf = GLShapeBuffer::create(texture, m_colour_verts, m_prim);
    ASSERT(m_vert_buf);

    // Default state for most buffers.
    m_state.array_vertex = true;
    m_state.blend = true;
    if (m_texture_verts)
    {
        m_state.array_texcoord = true;
        m_state.texture = true;
    }
    if (m_colour_verts)
        m_state.array_colour = true;
}

void VertBuffer::draw() const
{
    // Note:    This check is here because MenuRegion::render will try and
    //          render m_tile_buf[2] which, before the item description table
    //          is created, will be bound to a texture that has not been loaded.
    //          It never crashes, though, because m_tile_buf[2] will always be
    //          empty (no verts) before you start a game. -- ixtli
    if (size() == 0)
        return;

    if (m_texture_verts)
    {
        ASSERT(m_tex);
        m_tex->bind();
    }

    m_vert_buf->draw(m_state);
}

void VertBuffer::clear()
{
    m_vert_buf->clear();
}

void VertBuffer::add_primitive(const GLWPrim &rect)
{
    m_vert_buf->add(rect);
}

unsigned int VertBuffer::size() const
{
    return m_vert_buf->size();
}

void VertBuffer::set_tex(const GenericTexture *new_tex)
{
    ASSERT(m_texture_verts);
    ASSERT(new_tex);
    m_tex = new_tex;
}

VertBuffer::~VertBuffer()
{
    delete m_vert_buf;
}

/////////////////////////////////////////////////////////////////////////////
// FontBuffer

FontBuffer::FontBuffer(FontWrapper *font) :
    VertBuffer(true, true, font->font_tex()), m_font(font)
{
    m_state.array_colour = true;

    ASSERT(m_font);
    ASSERT(m_tex);
}

void FontBuffer::add(const formatted_string &fs, float x, float y)
{
    m_font->store(*this, x, y, fs);
}

void FontBuffer::add(const string &s, const VColour &col, float x, float y)
{
    m_font->store(*this, x, y, s, col);
}

/////////////////////////////////////////////////////////////////////////////
// TileBuffer

TileBuffer::TileBuffer(const TilesTexture *t) : VertBuffer(true, false, t)
{
}

void TileBuffer::add_unscaled(tileidx_t idx, float x, float y, int ymax,
                              float scale)
{
    float pos_sx = x;
    float pos_sy = y;
    float pos_ex, pos_ey, tex_sx, tex_sy, tex_ex, tex_ey;
    TilesTexture *tex = (TilesTexture *)m_tex;
    bool drawn = tex->get_coords(idx, 0, 0,
                                 pos_sx, pos_sy, pos_ex, pos_ey,
                                 tex_sx, tex_sy, tex_ex, tex_ey,
                                 true, -1, ymax, 1.0f / scale, 1.0f / scale);

    if (!drawn)
        return;

    GLWPrim rect(pos_sx, pos_sy, pos_ex, pos_ey);
    rect.set_tex(tex_sx, tex_sy, tex_ex, tex_ey);
    add_primitive(rect);
}

void TileBuffer::add(tileidx_t idx, int x, int y, int ox, int oy,
                     bool centre, int ymax, float tile_x, float tile_y)
{
    float pos_sx = x;
    float pos_sy = y;
    float pos_ex, pos_ey, tex_sx, tex_sy, tex_ex, tex_ey;
    TilesTexture *tex = (TilesTexture *)m_tex;
    bool drawn = tex->get_coords(idx, ox, oy,
                                 pos_sx, pos_sy, pos_ex, pos_ey,
                                 tex_sx, tex_sy, tex_ex, tex_ey,
                                 centre, -1, ymax, tile_x, tile_y);

    if (!drawn)
        return;

    GLWPrim rect(pos_sx, pos_sy, pos_ex, pos_ey);
    rect.set_tex(tex_sx, tex_sy, tex_ex, tex_ey);
    add_primitive(rect);
}

/////////////////////////////////////////////////////////////////////////////
// ColouredTileBuffer

ColouredTileBuffer::ColouredTileBuffer(const TilesTexture *t) :
    VertBuffer(true, true, t)
{
    m_state.array_colour   = true;
    m_state.depthtest      = true;
    m_state.alphatest      = true;
    m_state.alpharef       = 0;
}

static unsigned char _get_alpha(float lerp, int alpha_top, int alpha_bottom)
{
    if (lerp < 0.0f)
        lerp = 0.0f;
    if (lerp > 1.0f)
        lerp = 1.0f;

    int ret = static_cast<int>(alpha_top * (1.0f - lerp) + alpha_bottom * lerp);

    ret = min(max(0, ret), 255);
    return static_cast<unsigned char>(ret);
}

void ColouredTileBuffer::add(tileidx_t idx, int x, int y, int z,
                             int ox,  int oy, int ymin, int ymax,
                             int alpha_top, int alpha_bottom)
{
    float pos_sx = x;
    float pos_sy = y;
    float pos_ex, pos_ey, tex_sx, tex_sy, tex_ex, tex_ey;
    TilesTexture *tex = (TilesTexture *)m_tex;
    bool drawn = tex->get_coords(idx, ox, oy,
                                 pos_sx, pos_sy, pos_ex, pos_ey,
                                 tex_sx, tex_sy, tex_ex, tex_ey,
                                 true, -1, ymax);

    if (!drawn)
        return;

    float lerp_s = (pos_sy - y);
    float lerp_e = (pos_ey - y);

    // Need to calculate the alpha at the top and bottom.  As the quad
    // being drawn may not actually start on the grid boundary, we need
    // to figure out what alpha value its vertices should use.
    unsigned char alpha_s = _get_alpha(lerp_s, alpha_top, alpha_bottom);
    unsigned char alpha_e = _get_alpha(lerp_e, alpha_top, alpha_bottom);

    VColour col_sy(255, 255, 255, alpha_s);
    VColour col_ey(255, 255, 255, alpha_e);

    float pos_z = (float)z;

    GLWPrim rect(pos_sx, pos_sy, pos_ex, pos_ey, pos_z);
    rect.set_tex(tex_sx, tex_sy, tex_ex, tex_ey);
    rect.set_col(col_sy, col_ey);
    add_primitive(rect);
}

/////////////////////////////////////////////////////////////////////////////
// SubmergedTileBuffer

SubmergedTileBuffer::SubmergedTileBuffer(const TilesTexture *tex,
                                         int water_level) :
    m_water_level(water_level),
    m_below_water(tex),
    m_above_water(tex)
{
}

void SubmergedTileBuffer::add(tileidx_t idx, int x, int y, int z, bool submerged,
                              bool ghost, int ox, int oy, int ymax)
{
    // Arbitrary alpha values for the top and bottom.
    int alpha_top = ghost ? 100 : 255;
    int alpha_bottom = ghost ? 0 : 40;

    if (submerged)
    {
        m_below_water.add(idx, x, y, z, ox, oy, m_water_level, ymax,
                          alpha_top, alpha_bottom);
        m_above_water.add(idx, x, y, z, ox, oy, -1, m_water_level,
                          alpha_top, alpha_top);
    }
    else
        m_above_water.add(idx, x, y, z, ox, oy, -1, ymax, alpha_top, alpha_top);
}

// Adds a tile masked by the water later
void SubmergedTileBuffer::add_masked(tileidx_t idx, int x, int y, int z,
                                    int ox, int oy, int ymax,
                                    int alpha_above, int alpha_below,
                                    int water_level)
{
    if (!water_level) water_level = m_water_level;
    m_below_water.add(idx, x, y, z, ox, oy, water_level, ymax, alpha_above, alpha_below);
    m_above_water.add(idx, x, y, z, ox, oy, -1, water_level, alpha_above, alpha_above);
}

// Adds a tile with a specified alpha value
void SubmergedTileBuffer::add_alpha(tileidx_t idx, int x, int y, int z,
                                    int ox, int oy, int ymax, int alpha)
{
    m_above_water.add(idx, x, y, z, ox, oy, -1, ymax, alpha, alpha);
}

void SubmergedTileBuffer::draw() const
{
    m_below_water.draw();
    m_above_water.draw();
}

void SubmergedTileBuffer::clear()
{
    m_below_water.clear();
    m_above_water.clear();
}

/////////////////////////////////////////////////////////////////////////////
// ShapeBuffer

// [enne] - GL_POINTS should probably be used here, but there's (apparently)
// a bug in the OpenGL driver that I'm using and it doesn't respect
// glPointSize unless GL_SMOOTH_POINTS is on.  GL_SMOOTH_POINTS is
// *terrible* for performance if it has to fall back on software rendering,
// so instead we'll just make quads.
ShapeBuffer::ShapeBuffer() : VertBuffer(false, true)
{
    m_state.array_colour = true;
}

void ShapeBuffer::add(float pos_sx, float pos_sy, float pos_ex, float pos_ey,
                      const VColour &col)
{
    GLWPrim rect(pos_sx, pos_sy, pos_ex, pos_ey);
    rect.set_col(col);
    add_primitive(rect);
}

/////////////////////////////////////////////////////////////////////////////
// LineBuffer

LineBuffer::LineBuffer() : VertBuffer(false, true, NULL, GLW_LINES)
{
    m_state.array_colour = true;
}

void LineBuffer::add(float pos_sx, float pos_sy, float pos_ex, float pos_ey,
                     const VColour &col)
{
    GLWPrim rect(pos_sx, pos_sy, pos_ex, pos_ey);
    rect.set_col(col);
    add_primitive(rect);
}

void LineBuffer::add_square(float sx, float sy, float ex, float ey,
                            const VColour &col)
{
    add(sx, sy, ex, sy, col);
    add(ex, sy, ex, ey, col);
    add(ex, ey, sx, ey, col);
    add(sx, ey, sx, sy, col);
}

#endif