]> git.localhorst.tv Git - blank.git/blob - src/graphics/render.cpp
747bfa2dbee75a01d88e14bf55255f15007289c5
[blank.git] / src / graphics / render.cpp
1 #include "BlendedSprite.hpp"
2 #include "FixedText.hpp"
3 #include "Font.hpp"
4 #include "Format.hpp"
5 #include "MessageBox.hpp"
6 #include "Text.hpp"
7 #include "Texture.hpp"
8 #include "Viewport.hpp"
9
10 #include <algorithm>
11 #include <cstring>
12 #include <memory>
13 #include <stdexcept>
14
15
16 namespace blank {
17
18 Font::Font(const char *src, int size, long index)
19 : handle(TTF_OpenFontIndex(src, size, index)) {
20         if (!handle) {
21                 throw std::runtime_error(TTF_GetError());
22         }
23 }
24
25 Font::~Font() {
26         if (handle) {
27                 TTF_CloseFont(handle);
28         }
29 }
30
31 Font::Font(Font &&other) noexcept
32 : handle(other.handle) {
33         other.handle = nullptr;
34 }
35
36 Font &Font::operator =(Font &&other) noexcept {
37         std::swap(handle, other.handle);
38         return *this;
39 }
40
41
42 int Font::Style() const noexcept {
43         return TTF_GetFontStyle(handle);
44 }
45
46 void Font::Style(int s) const noexcept {
47         TTF_SetFontStyle(handle, s);
48 }
49
50 int Font::Outline() const noexcept {
51         return TTF_GetFontOutline(handle);
52 }
53
54 void Font::Outline(int px) noexcept {
55         TTF_SetFontOutline(handle, px);
56 }
57
58
59 int Font::Hinting() const noexcept {
60         return TTF_GetFontHinting(handle);
61 }
62
63 void Font::Hinting(int h) const noexcept {
64         TTF_SetFontHinting(handle, h);
65 }
66
67 bool Font::Kerning() const noexcept {
68         return TTF_GetFontKerning(handle);
69 }
70
71 void Font::Kerning(bool b) noexcept {
72         TTF_SetFontKerning(handle, b);
73 }
74
75
76 int Font::Height() const noexcept {
77         return TTF_FontHeight(handle);
78 }
79
80 int Font::Ascent() const noexcept {
81         return TTF_FontAscent(handle);
82 }
83
84 int Font::Descent() const noexcept {
85         return TTF_FontDescent(handle);
86 }
87
88 int Font::LineSkip() const noexcept {
89         return TTF_FontLineSkip(handle);
90 }
91
92
93 const char *Font::FamilyName() const noexcept {
94         return TTF_FontFaceFamilyName(handle);
95 }
96
97 const char *Font::StyleName() const noexcept {
98         return TTF_FontFaceStyleName(handle);
99 }
100
101
102 bool Font::HasGlyph(Uint16 c) const noexcept {
103         return TTF_GlyphIsProvided(handle, c);
104 }
105
106
107 glm::tvec2<int> Font::TextSize(const char *text) const {
108         glm::tvec2<int> size;
109         if (TTF_SizeUTF8(handle, text, &size.x, &size.y) != 0) {
110                 throw std::runtime_error(TTF_GetError());
111         }
112         return size;
113 }
114
115 Texture Font::Render(const char *text) const {
116         Texture tex;
117         Render(text, tex);
118         return tex;
119 }
120
121 void Font::Render(const char *text, Texture &tex) const {
122         SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, { 0xFF, 0xFF, 0xFF, 0xFF });
123         if (!srf) {
124                 throw std::runtime_error(TTF_GetError());
125         }
126         tex.Bind();
127         tex.Data(*srf, false);
128         tex.FilterLinear();
129         SDL_FreeSurface(srf);
130 }
131
132
133 void Format::ReadPixelFormat(const SDL_PixelFormat &fmt) {
134         if (fmt.BytesPerPixel == 4) {
135                 if (fmt.Amask == 0xFF) {
136                         if (fmt.Rmask == 0xFF00) {
137                                 format = GL_BGRA;
138                         } else {
139                                 format = GL_RGBA;
140                         }
141                         type = GL_UNSIGNED_INT_8_8_8_8;
142                 } else {
143                         if (fmt.Rmask == 0xFF) {
144                                 format = GL_RGBA;
145                         } else {
146                                 format = GL_BGRA;
147                         }
148                         type = GL_UNSIGNED_INT_8_8_8_8_REV;
149                 }
150                 internal = GL_RGBA8;
151         } else {
152                 if (fmt.Rmask == 0xFF) {
153                         format = GL_RGB;
154                 } else {
155                         format = GL_BGR;
156                 }
157                 type = GL_UNSIGNED_BYTE;
158                 internal = GL_RGB8;
159         }
160 }
161
162
163 MessageBox::MessageBox(const Font &f)
164 : font(f)
165 , lines()
166 , max_lines(10)
167 , pos(0.0f)
168 , adv(0.0f, font.LineSkip(), 0.0f)
169 , bg(1.0f, 1.0f, 1.0f, 0.0f)
170 , fg(1.0f, 1.0f, 1.0f, 1.0f)
171 , grav(Gravity::NORTH_WEST) {
172
173 }
174
175 void MessageBox::Position(const glm::vec3 &p, Gravity g) noexcept {
176         pos = p;
177         grav = g;
178         if (get_y(g) == Align::END) {
179                 adv.y = -font.LineSkip();
180         } else {
181                 adv.y = font.LineSkip();
182         }
183         for (Text &txt : lines) {
184                 txt.Pivot(g);
185         }
186 }
187
188 void MessageBox::PushLine(const char *text) {
189         lines.emplace_front();
190         Text &txt = lines.front();
191         txt.Set(font, text);
192         txt.Pivot(grav);
193
194         while (lines.size() > max_lines) {
195                 lines.pop_back();
196         }
197 }
198
199 void MessageBox::Render(Viewport &viewport) noexcept {
200         BlendedSprite &prog = viewport.SpriteProgram();
201         prog.SetBG(bg);
202         prog.SetFG(fg);
203         viewport.SetCursor(pos, grav);
204         for (Text &txt : lines) {
205                 prog.SetM(viewport.Cursor());
206                 txt.Render(viewport);
207                 viewport.MoveCursor(adv);
208         }
209 }
210
211
212 Text::Text() noexcept
213 : tex()
214 , sprite()
215 , size(0.0f)
216 , pivot(Gravity::NORTH_WEST)
217 , dirty(false) {
218
219 }
220
221 FixedText::FixedText() noexcept
222 : Text()
223 , bg(1.0f, 1.0f, 1.0f, 0.0f)
224 , fg(1.0f, 1.0f, 1.0f, 1.0f)
225 , pos(0.0f)
226 , grav(Gravity::NORTH_WEST)
227 , visible(false) {
228
229 }
230
231 void Text::Set(const Font &font, const char *text) {
232         font.Render(text, tex);
233         size = font.TextSize(text);
234         dirty = true;
235 }
236
237 void Text::Update() {
238         sprite.LoadRect(size.x, size.y, align(pivot, size));
239         dirty = false;
240 }
241
242 void FixedText::Render(Viewport &viewport) noexcept {
243         BlendedSprite &prog = viewport.SpriteProgram();
244         viewport.SetCursor(pos, grav);
245         prog.SetM(viewport.Cursor());
246         prog.SetBG(bg);
247         prog.SetFG(fg);
248         Text::Render(viewport);
249 }
250
251 void Text::Render(Viewport &viewport) noexcept {
252         if (dirty) {
253                 Update();
254         }
255         BlendedSprite &prog = viewport.SpriteProgram();
256         prog.SetTexture(tex);
257         sprite.Draw();
258 }
259
260
261 Texture::Texture()
262 : handle(0)
263 , width(0)
264 , height(0) {
265         glGenTextures(1, &handle);
266 }
267
268 Texture::~Texture() {
269         if (handle != 0) {
270                 glDeleteTextures(1, &handle);
271         }
272 }
273
274 Texture::Texture(Texture &&other) noexcept
275 : handle(other.handle) {
276         other.handle = 0;
277         width = other.width;
278         height = other.height;
279 }
280
281 Texture &Texture::operator =(Texture &&other) noexcept {
282         std::swap(handle, other.handle);
283         width = other.width;
284         height = other.height;
285         return *this;
286 }
287
288
289 void Texture::Bind() noexcept {
290         glBindTexture(GL_TEXTURE_2D, handle);
291 }
292
293 namespace {
294         bool ispow2(unsigned int i) {
295                 // don't care about i == 0 here
296                 return !(i & (i - 1));
297         }
298 }
299
300 void Texture::Data(const SDL_Surface &srf, bool pad2) noexcept {
301         Format format;
302         format.ReadPixelFormat(*srf.format);
303
304         if (!pad2 || (ispow2(srf.w) && ispow2(srf.h))) {
305                 int align = UnpackAlignmentFromPitch(srf.pitch);
306
307                 int pitch = (srf.w * srf.format->BytesPerPixel + align - 1) / align * align;
308                 if (srf.pitch - pitch >= align) {
309                         UnpackRowLength(srf.pitch / srf.format->BytesPerPixel);
310                 } else {
311                         UnpackRowLength(0);
312                 }
313
314                 Data(srf.w, srf.h, format, srf.pixels);
315
316                 UnpackRowLength(0);
317         } else if (srf.w > (1 << 30) || srf.h > (1 << 30)) {
318 #ifndef NDEBUG
319                 throw std::runtime_error("texture too large");
320 #endif
321         } else {
322                 GLsizei width = 1, height = 1;
323                 while (width < srf.w) {
324                         width <<= 1;
325                 }
326                 while (height < srf.h) {
327                         height <<= 1;
328                 }
329                 size_t pitch = width * srf.format->BytesPerPixel;
330                 size_t size = pitch * height;
331                 size_t row_pad = pitch - srf.pitch;
332                 std::unique_ptr<unsigned char[]> data(new unsigned char[size]);
333                 unsigned char *src = reinterpret_cast<unsigned char *>(srf.pixels);
334                 unsigned char *dst = data.get();
335                 for (int row = 0; row < srf.h; ++row) {
336                         std::memcpy(dst, src, srf.pitch);
337                         src += srf.pitch;
338                         dst += srf.pitch;
339                         std::memset(dst, 0, row_pad);
340                         dst += row_pad;
341                 }
342                 std::memset(dst, 0, (height - srf.h) * pitch);
343                 UnpackAlignmentFromPitch(pitch);
344                 Data(width, height, format, data.get());
345         }
346
347         UnpackAlignment(4);
348 }
349
350 void Texture::Data(GLsizei w, GLsizei h, const Format &format, GLvoid *data) noexcept {
351         glTexImage2D(
352                 GL_TEXTURE_2D,
353                 0, format.internal,
354                 w, h,
355                 0,
356                 format.format, format.type,
357                 data
358         );
359         width = w;
360         height = h;
361 }
362
363
364 void Texture::FilterNearest() noexcept {
365         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
366         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
367 }
368
369 void Texture::FilterLinear() noexcept {
370         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
371         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
372 }
373
374 void Texture::FilterTrilinear() noexcept {
375         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
376         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
377         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
378         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
379         glGenerateMipmap(GL_TEXTURE_2D);
380 }
381
382
383 void Texture::UnpackAlignment(GLint i) noexcept {
384         glPixelStorei(GL_UNPACK_ALIGNMENT, i);
385 }
386
387 int Texture::UnpackAlignmentFromPitch(int pitch) noexcept {
388         int align = 8;
389         while (pitch % align) {
390                 align >>= 1;
391         }
392         UnpackAlignment(align);
393         return align;
394 }
395
396 void Texture::UnpackRowLength(GLint i) noexcept {
397         glPixelStorei(GL_UNPACK_ROW_LENGTH, i);
398 }
399
400 }