-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathrenderer.cpp
906 lines (817 loc) · 29.2 KB
/
renderer.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/* Copyright (C) 2013-2014 Michal Brzozowski ([email protected])
This file is part of KeeperRL.
KeeperRL is free software; you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
KeeperRL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.gnu.org/licenses/ . */
#include "stdafx.h"
#include "dirent.h"
#include "extern/lodepng.h"
#include "renderer.h"
#include "view_object.h"
#include "tile.h"
#include "fontstash.h"
#include "sdl_event_generator.h"
#include "clock.h"
#include "gzstream.h"
#include "opengl.h"
#include "tileset.h"
#include "steam_input.h"
void Renderer::renderDeferredSprites() {
static vector<SDL::GLfloat> vertices;
static vector<SDL::GLfloat> texCoords;
static vector<SDL::GLfloat> colors;
vertices.clear();
texCoords.clear();
colors.clear();
auto addVertex = [&](Vec2 v, int texX, int texY, Vec2 texSize, Color color) {
vertices.push_back(v.x);
vertices.push_back(v.y);
texCoords.push_back(((float)texX) / texSize.x);
texCoords.push_back(((float)texY) / texSize.y);
colors.push_back(((float) color.r) / 255);
colors.push_back(((float) color.g) / 255);
colors.push_back(((float) color.b) / 255);
colors.push_back(((float) color.a) / 255);
};
for (auto& elem : deferredSprites) {
auto add = [&](Vec2 v, int texX, int texY, const DeferredSprite& draw) {
addVertex(v, texX, texY, draw.realSize, draw.color.value_or(Color::WHITE));
};
add(elem.a, elem.p.x, elem.p.y, elem);
add(elem.b, elem.k.x, elem.p.y, elem);
add(elem.c, elem.k.x, elem.k.y, elem);
add(elem.a, elem.p.x, elem.p.y, elem);
add(elem.c, elem.k.x, elem.k.y, elem);
add(elem.d, elem.p.x, elem.k.y, elem);
}
if (!vertices.empty()) {
CHECK_OPENGL_ERROR();
SDL::glBindTexture(GL_TEXTURE_2D, *currentTexture);
SDL::glEnable(GL_TEXTURE_2D);
SDL::glEnableClientState(GL_VERTEX_ARRAY);
SDL::glEnableClientState(GL_TEXTURE_COORD_ARRAY);
SDL::glEnableClientState(GL_COLOR_ARRAY);
SDL::glColorPointer(4, GL_FLOAT, 0, colors.data());
SDL::glVertexPointer(2, GL_FLOAT, 0, vertices.data());
SDL::glTexCoordPointer(2, GL_FLOAT, 0, texCoords.data());
SDL::glDrawArrays(GL_TRIANGLES, 0, vertices.size() / 2);
vertices.clear();
texCoords.clear();
colors.clear();
SDL::glDisableClientState(GL_VERTEX_ARRAY);
SDL::glDisableClientState(GL_TEXTURE_COORD_ARRAY);
SDL::glDisableClientState(GL_COLOR_ARRAY);
SDL::glDisable(GL_TEXTURE_2D);
CHECK_OPENGL_ERROR();
}
deferredSprites.clear();
}
void Renderer::drawSprite(const Texture& t, Vec2 topLeft, Vec2 bottomRight, Vec2 p, Vec2 k, optional<Color> color) {
drawSprite(t, topLeft, Vec2(bottomRight.x, topLeft.y), bottomRight, Vec2(topLeft.x, bottomRight.y), p, k, color);
}
void Renderer::drawSprite(const Texture& t, Vec2 a, Vec2 b, Vec2 c, Vec2 d, Vec2 p, Vec2 k, optional<Color> color) {
if (currentTexture && currentTexture != t.getTexId())
renderDeferredSprites();
currentTexture = t.getTexId();
CHECK(currentTexture);
deferredSprites.push_back({a, b, c, d, p, k, t.getRealSize(), color});
}
static float sizeConv(int size) {
return 1.15 * (float)size;
}
int Renderer::getTextLength(const string& s, int size, FontId font) {
return getTextSize(s, size, font).x;
}
static optional<Vec2> getMapFontOffset(char32_t c) {
switch (c) {
case U'A': return Vec2(0, 0);
case U'B': return Vec2(14, 0);
case U'C': return Vec2(28, 0);
case U'D': return Vec2(42, 0);
case U'E': return Vec2(56, 0);
case U'F': return Vec2(70, 0);
case U'G': return Vec2(84, 0);
case U'H': return Vec2(98, 0);
case U'I': return Vec2(112, 0);
case U'J': return Vec2(122, 0);
case U'K': return Vec2(136, 0);
case U'L': return Vec2(150, 0);
case U'M': return Vec2(162, 0);
case U'N': return Vec2(180, 0);
case U'O': return Vec2(194, 0);
case U'P': return Vec2(208, 0);
case U'Q': return Vec2(222, 0);
case U'R': return Vec2(236, 0);
case U'S': return Vec2(250, 0);
case U'T': return Vec2(264, 0);
case U'U': return Vec2(278, 0);
case U'V': return Vec2(292, 0);
case U'W': return Vec2(306, 0);
case U'X': return Vec2(324, 0);
case U'Y': return Vec2(338, 0);
case U'Z': return Vec2(352, 0);
case U'a': return Vec2(366, 0);
case U'b': return Vec2(378, 0);
case U'c': return Vec2(390, 0);
case U'd': return Vec2(400, 0);
case U'e': return Vec2(412, 0);
case U'f': return Vec2(424, 0);
case U'g': return Vec2(434, 0);
case U'h': return Vec2(446, 0);
case U'i': return Vec2(458, 0);
case U'j': return Vec2(464, 0);
case U'k': return Vec2(472, 0);
case U'l': return Vec2(484, 0);
case U'm': return Vec2(490, 0);
case U'n': return Vec2(508, 0);
case U'o': return Vec2(520, 0);
case U'p': return Vec2(532, 0);
case U'q': return Vec2(544, 0);
case U'r': return Vec2(556, 0);
case U's': return Vec2(566, 0);
case U't': return Vec2(578, 0);
case U'u': return Vec2(588, 0);
case U'v': return Vec2(600, 0);
case U'w': return Vec2(612, 0);
case U'x': return Vec2(626, 0);
case U'y': return Vec2(638, 0);
case U'z': return Vec2(650, 0);
case U'z' + 1: return Vec2(660, 0);
case U'Ć': return Vec2(0, 28);
case U'Ź': return Vec2(14, 28);
case U'Ż': return Vec2(28, 28);
case U'Ł': return Vec2(42, 28);
case U'Ó': return Vec2(56, 28);
case U'Ś': return Vec2(70, 28);
case U'ą': return Vec2(366, 28);
case U'ć': return Vec2(378, 28);
case U'ę': return Vec2(388, 28);
case U'ł': return Vec2(400, 28);
case U'ń': return Vec2(410, 28);
case U'ó': return Vec2(422, 28);
case U'ś': return Vec2(434, 28);
case U'ż': return Vec2(446, 28);
case U'ź': return Vec2(456, 28);
}
return none;
}
static optional<int> getMapFontWidth(char32_t c) {
if (c == U' ')
return 8;
if (c >= 'A' && c <= 'Y')
return getMapFontOffset(c + 1)->x - getMapFontOffset(c)->x;
if (c == 'Z')
return getMapFontOffset('a')->x - getMapFontOffset(c)->x;
if (c >= 'a' && c <= 'z')
return getMapFontOffset(c + 1)->x - getMapFontOffset(c)->x;
switch (c) {
case U'Ć':
case U'Ź':
case U'Ż':
case U'Ł':
case U'Ó':
case U'Ś': return 14;
case U'ą': return 12;
case U'ć': return 10;
case U'ę': return 12;
case U'ł': return 10;
case U'ń': return 12;
case U'ó': return 12;
case U'ś': return 12;
case U'ż': return 10;
case U'ź': return 10;
}
return none;
}
Vec2 Renderer::getTextSize(const string& s, int size, FontId id) {
if (id == FontId::MAP_FONT) {
auto ret = 0;
for (auto c : s)
if (auto width = getMapFontWidth(c))
ret += *width - 2;
return Vec2(ret, 22);
}
if (s.empty())
return Vec2(0, 0);
float minx, maxx, miny, maxy;
int font = getFont(id);
sth_dim_text(fontStash, font, sizeConv(size), s.c_str(), &minx, &miny, &maxx, &maxy);
float height;
sth_vmetrics(fontStash, font, sizeConv(size), nullptr, nullptr, &height);
return Vec2(maxx - minx, height);
}
int Renderer::getFont(FontId id) {
// vector<FontSet>& fontSet = currentThreadId() == *renderThreadId ? fonts : fontsOtherThread;
FontSet& fontSet = fonts;
switch (id) {
case FontId::TILE_FONT:
case FontId::TEXT_FONT: return fontSet.textFont;
case FontId::SYMBOL_FONT: return fontSet.symbolFont;
case FontId::MAP_FONT: return 0;
}
}
void Renderer::drawText(FontId id, int size, Color color, Vec2 pos, const string& s, CenterType center) {
renderDeferredSprites();
if (id == FontId::MAP_FONT) {
std::mbstate_t state{};
char32_t c32;
for (int i = 0; i < s.size();) {
i += std::mbrtoc32(&c32, &s[i], s.size() - i, &state);
if (auto offset = getMapFontOffset(c32))
drawSprite(pos, *offset, Vec2(*getMapFontWidth(c32), 28), *mapFontTexture, none, color);
if (auto width = getMapFontWidth(c32))
pos.x += *width - 2;
}
return;
}
if (!s.empty()) {
int ox = 0;
int oy = 0;
Vec2 dim = getTextSize(s, size, id);
switch (center) {
case HOR:
ox -= dim.x / 2;
break;
case VER:
oy -= dim.y / 2;
break;
case HOR_VER:
ox -= dim.x / 2;
oy -= dim.y / 2;
break;
default:
break;
}
sth_begin_draw(fontStash);
glColor(color);
sth_draw_text(fontStash, getFont(id), sizeConv(size), ox + pos.x, oy + pos.y + (dim.y * 0.9), s.c_str(), nullptr);
sth_end_draw(fontStash);
}
}
void Renderer::drawText(Color color, Vec2 pos, const char* c, CenterType center, int size) {
drawText(FontId::TEXT_FONT, size, color, pos, c, center);
}
void Renderer::drawText(Color color, Vec2 pos, const string& c, CenterType center, int size) {
drawText(FontId::TEXT_FONT, size, color, pos, c, center);
}
void Renderer::drawTextWithHotkey(Color color, Vec2 pos, const string& text, char key) {
if (key) {
int ind = lowercase(text).find(key);
if (ind != string::npos) {
int posHotkey = pos.x + getTextLength(text.substr(0, ind));
drawFilledRectangle(posHotkey, pos.y + 23, posHotkey + getTextLength(text.substr(ind, 1)), pos.y + 25, Color::GREEN);
}
}
drawText(color, pos, text);
}
void Renderer::drawImage(int px, int py, const Texture& image, double scale, optional<Color> color) {
Vec2 p(px, py);
drawSprite(image, p, p + image.getSize() * scale, Vec2(0, 0), image.getSize(), color);
}
void Renderer::drawImage(Rectangle target, Rectangle source, const Texture& image) {
drawSprite(target.topLeft(), source.topLeft(), source.getSize(), image, target.getSize());
}
void Renderer::drawSprite(Vec2 pos, Vec2 stretchSize, const Texture& t) {
drawSprite(pos, Vec2(0, 0), t.getSize(), t, stretchSize);
}
static Vec2 rotate(Vec2 pos, Vec2 origin, double x, double y) {
Vec2 v = pos - origin;
return origin + Vec2(v.x * x - v.y * y, v.x * y + v.y * x);
}
void Renderer::drawAnimation(AnimationId id, Vec2 pos, double state, Vec2 squareSize, Dir orientation, Color color) {
if (auto& animInfo = animations[id]) {
int zoomLevel = squareSize.y / nominalSize;
int frame = int(animInfo->numFrames * state);
int width = animInfo->tex.getSize().x / animInfo->numFrames;
Vec2 size(width, animInfo->tex.getSize().y);
drawSprite(pos - size * zoomLevel / 2, Vec2(frame * width, 0), size, animInfo->tex, size * zoomLevel, color,
SpriteOrientation(Vec2(rotate(rotate(orientation))), false));
}
}
void Renderer::drawSprite(Vec2 pos, Vec2 source, Vec2 size, const Texture& t,
optional<Vec2> targetSize, optional<Color> color, SpriteOrientation orientation) {
Vec2 a = pos;
Vec2 c = targetSize ? (a + *targetSize) : (a + size);
Vec2 b(c.x, a.y);
Vec2 d(a.x, c.y);
if (orientation.horizontalFlip) {
swap(a, b);
swap(c, d);
}
if (orientation.x != 1 || orientation.y != 0) {
Vec2 mid = a + c;
a = rotate(a * 2, mid, orientation.x, orientation.y) / 2;
b = rotate(b * 2, mid, orientation.x, orientation.y) / 2;
c = rotate(c * 2, mid, orientation.x, orientation.y) / 2;
d = rotate(d * 2, mid, orientation.x, orientation.y) / 2;
}
drawSprite(t, a, b, c, d, source, source + size, color);
}
void Renderer::drawFilledRectangle(const Rectangle& t, Color color, optional<Color> outline) {
renderDeferredSprites();
Vec2 a = t.topLeft();
Vec2 b = t.bottomRight();
if (outline) {
SDL::glLineWidth(2);
SDL::glBegin(GL_LINE_LOOP);
glColor(*outline);
SDL::glVertex2f(a.x + 1.5f, a.y + 1.0f);
SDL::glVertex2f(b.x - 0.5f, a.y + 1.0f);
SDL::glVertex2f(b.x - 0.5f, b.y - 0.5f);
SDL::glVertex2f(a.x + 1.5f, b.y - 0.5f);
SDL::glEnd();
a += Vec2(2, 2);
b -= Vec2(1, 1);
}
SDL::glBegin(GL_QUADS);
glColor(color);
SDL::glVertex2f(a.x, a.y);
SDL::glVertex2f(b.x, a.y);
SDL::glVertex2f(b.x, b.y);
SDL::glVertex2f(a.x, b.y);
SDL::glEnd();
}
void Renderer::drawLine(Vec2 from, Vec2 to, Color color, double width) {
renderDeferredSprites();
SDL::glBegin(GL_QUADS);
double dx = to.x - from.x;
double dy = to.y - from.y;
double length = sqrt(dx * dx + dy * dy);
dx /= length;
dy /= length;
glColor(color);
width /= 2;
SDL::glVertex2d(from.x + dy * width, from.y - dx * width);
SDL::glVertex2d(to.x + dy * width, to.y - dx * width);
SDL::glVertex2d(to.x - dy * width, to.y + dx * width);
SDL::glVertex2d(from.x - dy * width, from.y + dx * width);
SDL::glEnd();
}
void Renderer::drawFilledRectangle(int px, int py, int kx, int ky, Color color, optional<Color> outline) {
drawFilledRectangle(Rectangle(px, py, kx, ky), color, outline);
}
void Renderer::drawPoint(Vec2 pos, Color color, int size) {
SDL::glPointSize(size);
SDL::glBegin(GL_POINTS);
glColor(color);
SDL::glVertex2f(pos.x, pos.y);
SDL::glEnd();
}
void Renderer::addQuad(const Rectangle& r, Color color) {
drawFilledRectangle(r.left(), r.top(), r.right(), r.bottom(), color);
}
void Renderer::setScissor(optional<Rectangle> s, bool reset) {
renderDeferredSprites();
auto applyScissor = [&] (Rectangle rect) {
double zoom = getZoom();
SDL::glScissor(rect.left() * zoom, (getSize().y - rect.bottom()) * zoom,
rect.width() * zoom, rect.height() * zoom);
SDL::glEnable(GL_SCISSOR_TEST);
};
if (s) {
Rectangle rect = *s;
if (!scissorStack.empty() && !reset)
rect = rect.intersection(scissorStack.back());
applyScissor(rect);
scissorStack.push_back(rect);
}
else {
if (!scissorStack.empty())
scissorStack.pop_back();
if (!scissorStack.empty())
applyScissor(scissorStack.back());
else
SDL::glDisable(GL_SCISSOR_TEST);
}
}
void Renderer::setTopLayer() {
renderDeferredSprites();
SDL::glPushMatrix();
SDL::glTranslated(0, 0, 1);
SDL::glDisable(GL_SCISSOR_TEST);
CHECK_OPENGL_ERROR();
}
void Renderer::popLayer() {
renderDeferredSprites();
SDL::glPopMatrix();
if (!scissorStack.empty())
SDL::glEnable(GL_SCISSOR_TEST);
CHECK_OPENGL_ERROR();
}
Vec2 Renderer::getSize() {
return Vec2(width / getZoom(), height / getZoom());
}
Vec2 Renderer::getWindowSize() {
return Vec2(width, height);
}
bool Renderer::isFullscreen() {
return fullscreen;
}
void Renderer::putPixel(SDL::SDL_Surface* surface, Vec2 pos, Color color) {
SDL::Uint32 *pixels = (SDL::Uint32 *)surface->pixels;
pixels[ ( pos.y * surface->w ) + pos.x ] = SDL::SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a);;
}
void Renderer::setFullscreen(bool v) {
fullscreen = v;
CHECK(SDL::SDL_SetWindowFullscreen(window, v ? SDL::SDL_WINDOW_FULLSCREEN_DESKTOP : 0) == 0) << SDL::SDL_GetError();
SDL_GL_GetDrawableSize(window, &width, &height);
initOpenGL();
}
void Renderer::setFullscreenMode(int v) {
fullscreenMode = v;
}
static const Vec2 minResolution = Vec2(800, 600);
Vec2 Renderer::getMinResolution() {
return minResolution;
}
double Renderer::getZoom() {
return min(zoom, min(double(width) / minResolution.x, double(height) / minResolution.y));
}
void Renderer::setZoom(double v) {
zoom = v;
initOpenGL();
}
void Renderer::enableCustomCursor(bool state) {
cursorEnabled = state;
reloadCursors();
}
void Renderer::initOpenGL() {
setupOpenglView(width, height, getZoom());
SDL::glEnable(GL_BLEND);
SDL::glEnable(GL_TEXTURE_2D);
SDL::glEnable(GL_DEPTH_TEST);
SDL::glDepthFunc(GL_LEQUAL);
SDL::glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
CHECK(SDL::glGetError() == GL_NO_ERROR);
reloadCursors();
}
SDL::SDL_Surface* Renderer::loadScaledSurface(const FilePath& path, double scale) {
if (auto surface = SDL::IMG_Load(path.getPath())) {
if (scale == 1)
return surface;
if (auto scaled = Texture::createSurface(surface->w * scale, surface->h * scale)) {
SDL::SDL_SetSurfaceBlendMode(surface, SDL::SDL_BLENDMODE_NONE);
CHECK(!SDL_BlitScaled(surface, nullptr, scaled, nullptr)) << SDL::IMG_GetError();
SDL_FreeSurface(surface);
return scaled;
}
SDL_FreeSurface(surface);
}
return nullptr;
}
void Renderer::reloadCursors() {
if (!cursorEnabled) {
SDL_SetCursor(originalCursor);
cursor = cursorClicked = nullptr;
} else {
if (auto surface = loadScaledSurface(cursorPath, getZoom()))
cursor = SDL_CreateColorCursor(surface, 0, 0);
if (auto surface = loadScaledSurface(clickedCursorPath, getZoom()))
cursorClicked = SDL_CreateColorCursor(surface, 0, 0);
if (cursor)
SDL_SetCursor(cursor);
}
}
void Renderer::initialize() {
if (!renderThreadId)
renderThreadId = currentThreadId();
else
CHECK(currentThreadId() == *renderThreadId);
initOpenGL();
}
void Renderer::loadFonts(const DirectoryPath& fontPath, FontSet& fonts) {
CHECK(fontStash = sth_create(512, 512)) << "Error initializing fonts";
auto textFont = fontPath.file("Lato-Bol.ttf");
auto symbolFont = fontPath.file("Symbola.ttf");
fonts.textFont = sth_add_font(fontStash, textFont.getPath());
fonts.symbolFont = sth_add_font(fontStash, symbolFont.getPath());
CHECK(fonts.textFont >= 0) << "Error loading " << textFont;
CHECK(fonts.symbolFont >= 0) << "Error loading " << symbolFont;
}
void Renderer::showError(const string& s) {
SDL_ShowSimpleMessageBox(SDL::SDL_MESSAGEBOX_ERROR, "Error", s.c_str(), window);
}
TileSet& Renderer::getTileSet() {
return *tileSet;
}
void Renderer::setTileSet(TileSet* s) {
tileSet = s;
}
void Renderer::setVsync(bool on) {
SDL::SDL_GL_SetSwapInterval(on ? 1 : 0);
}
void Renderer::setFpsLimit(int fps) {
fpsLimit = fps;
}
Renderer::Renderer(Clock* clock, MySteamInput* i, const string& title, const DirectoryPath& fontPath,
const FilePath& cursorP, const FilePath& clickedCursorP, const FilePath& iconPath, const FilePath& mapFontPath)
: cursorPath(cursorP), clickedCursorPath(clickedCursorP),
clock(clock), steamInput(i) {
CHECK(SDL::SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS) >= 0) << SDL::SDL_GetError();
SDL::SDL_GL_SetAttribute(SDL::SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
SDL::SDL_GL_SetAttribute(SDL::SDL_GL_CONTEXT_MINOR_VERSION, 1 );
CHECK(window = SDL::SDL_CreateWindow("KeeperRL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1200, 720,
SDL::SDL_WINDOW_RESIZABLE | SDL::SDL_WINDOW_SHOWN | SDL::SDL_WINDOW_MAXIMIZED | SDL::SDL_WINDOW_OPENGL)) << SDL::SDL_GetError();
CHECK(SDL::SDL_GL_CreateContext(window)) << SDL::SDL_GetError();
SDL_SetWindowMinimumSize(window, minResolution.x, minResolution.y);
SDL::SDL_Event ev;
while(SDL_PollEvent(&ev)){}
SDL::SDL_GL_GetDrawableSize(window, &width, &height);
setVsync(true);
originalCursor = SDL::SDL_GetCursor();
initOpenGL();
loadFonts(fontPath, fonts);
auto icon = SDL::IMG_Load(iconPath.getPath());
SDL_SetWindowIcon(window, icon);
mapFontTexture.emplace(std::move(*Texture::loadMaybe(mapFontPath))); // work around compiler bug?
}
Vec2 getOffset(Vec2 sizeDiff, double scale) {
return Vec2(round(sizeDiff.x * scale * 0.5), round(sizeDiff.y * scale * 0.5));
}
void Renderer::drawTile(Vec2 pos, const vector<TileCoord>& coords, Vec2 size, Color color, SpriteOrientation orientation,
optional<Color> secondColor, optional<double> scale) {
if (coords.empty())
return;
auto drawCoord = [&size, scale, this, pos, orientation] (const TileCoord& coord, Color color) {
auto sizeDiff = coord.size.x == 8 ? Vec2(0, 0) : Vec2(nominalSize, nominalSize) - coord.size;
Vec2 off = scale ? getOffset(sizeDiff, *scale) : sizeDiff.mult(size) / (nominalSize * 2);
Vec2 tileSize = scale ? coord.size * *scale : coord.size.mult(size) / nominalSize;
if (coord.size.y > nominalSize)
off.y -= 3 * size.y / nominalSize;
drawSprite(pos + off, coord.pos.mult(coord.size), coord.size, *coord.texture, tileSize, color, orientation);
};
if (secondColor && coords.size() > 1) {
drawCoord(coords[0], color);
if (secondColor != Color::WHITE)
drawCoord(coords[1], color * *secondColor);
} else {
auto frame = clock->getRealMillis().count() / 200;
drawCoord(coords[frame % coords.size()], color);
}
}
void Renderer::drawViewObject(Vec2 pos, ViewId id, Color color) {
drawViewObject(pos, id, true, 1, color);
}
void Renderer::drawViewObject(Vec2 pos, ViewIdList id, bool useSprite, double scale, Color color) {
for (int i : All(id)) {
const Tile& tile = tileSet->getTile(id[i], useSprite);
if (tile.hasSpriteCoord()) {
optional<Color> colorVariant;
if (!tile.animated)
colorVariant = id[i].getColor();
auto thisPos = pos;
auto coord = tile.getSpriteCoord(DirSet::fullSet());
if (tile.weaponOrigin && i < id.size() - 1) {
auto creatureTile = tileSet->getTile(id[i + 1], useSprite);
if (creatureTile.weaponOrigin) {
auto weaponSize = coord[0].size;
auto creatureSize = creatureTile.getSpriteCoord(DirSet::fullSet())[0].size;
thisPos -= (*tile.weaponOrigin - *creatureTile.weaponOrigin + (creatureSize - weaponSize) / 2) * scale;
}
}
drawTile(thisPos, coord, Vec2(), color * tile.color, {}, colorVariant, scale);
} else
drawText(tile.symFont ? FontId::SYMBOL_FONT : FontId::TEXT_FONT, 20 * scale, color * tile.color,
pos + Vec2(scale * nominalSize / 2, 0), tile.text, HOR);
}
}
void Renderer::drawViewObject(Vec2 pos, ViewId id, bool useSprite, double scale, Color color) {
const Tile& tile = tileSet->getTile(id, useSprite);
if (tile.hasSpriteCoord()) {
optional<Color> colorVariant;
if (!tile.animated)
colorVariant = id.getColor();
drawTile(pos, tile.getSpriteCoord(DirSet::fullSet()), Vec2(), color * tile.color, {}, colorVariant, scale);
} else
drawText(tile.symFont ? FontId::SYMBOL_FONT : FontId::TEXT_FONT, 20 * scale, color * tile.color,
pos + Vec2(scale * nominalSize / 2, 0), tile.text, HOR);
}
void Renderer::drawViewObject(Vec2 pos, ViewId id, bool useSprite, Vec2 size, Color color, SpriteOrientation orient) {
const Tile& tile = tileSet->getTile(id, useSprite);
if (tile.hasSpriteCoord()) {
optional<Color> colorVariant;
if (!tile.animated)
colorVariant = id.getColor();
drawTile(pos, tile.getSpriteCoord(DirSet::fullSet()), size, color * tile.color, orient, colorVariant);
} else
drawText(tile.symFont ? FontId::SYMBOL_FONT : FontId::TEXT_FONT, size.y, color * tile.color, pos, tile.text);
}
void Renderer::drawViewObject(Vec2 pos, const ViewObject& object, bool useSprite, Vec2 size) {
drawViewObject(pos, object.id(), useSprite, size, Color::WHITE);
}
void Renderer::drawViewObject(Vec2 pos, const ViewObject& object, bool useSprite, double scale, Color color) {
const Tile& tile = tileSet->getTile(object.id(), useSprite);
if (object.weaponViewId && tile.weaponOrigin) {
const Tile& weaponTile = tileSet->getTile(*object.weaponViewId, useSprite);
if (weaponTile.weaponOrigin) {
auto size = tile.getSpriteCoord(DirSet::fullSet())[0].size;
auto weaponSize = weaponTile.getSpriteCoord(DirSet::fullSet())[0].size;
drawViewObject(pos - (*weaponTile.weaponOrigin - *tile.weaponOrigin + (size - weaponSize) / 2) * scale, *object.weaponViewId,
useSprite, scale, Color::WHITE);
}
}
drawViewObject(pos, object.id(), useSprite, scale, color);
}
void Renderer::drawViewObject(Vec2 pos, const ViewObject& object) {
drawViewObject(pos, object.id(), Color::WHITE);
}
void Renderer::drawAsciiBackground(ViewId id, Rectangle bounds) {
if (!tileSet->getTile(id, true).hasSpriteCoord())
drawFilledRectangle(bounds, Color::BLACK);
}
void Renderer::loadAnimations() {
if (animationDirectory) {
for (auto id : ENUM_ALL(AnimationId))
animations[id] = AnimationInfo { Texture(animationDirectory->file(getFileName(id))), getNumFrames(id)};
}
}
void Renderer::setAnimationsDirectory(const DirectoryPath& path) {
animationDirectory = path;
}
void Renderer::drawAndClearBuffer() {
if (steamInput)
steamInput->runFrame();
renderDeferredSprites();
CHECK_OPENGL_ERROR();
if (fpsLimit) {
uint64_t end = SDL::SDL_GetPerformanceCounter();
float elapsedMs = (end - frameStart) / (float)SDL::SDL_GetPerformanceFrequency() * 1000.0f;
float sleepMs = 1000.0f / fpsLimit - elapsedMs;
if (sleepMs > 0.0)
SDL::SDL_Delay(sleepMs);
}
SDL::SDL_GL_SwapWindow(window);
CHECK_OPENGL_ERROR();
frameStart = SDL::SDL_GetPerformanceCounter();
SDL::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL::glClearColor(0.0, 0.0, 0.0, 0.0);
CHECK_OPENGL_ERROR();
}
void Renderer::resize(int w, int h) {
SDL_GL_GetDrawableSize(window, &width, &height);
initOpenGL();
}
bool Renderer::isKeypressed(SDL::SDL_Scancode key) {
return keypressed[key];
}
void Renderer::updateKeypressed(const Event& ev) {
if (ev.type == SDL::SDL_KEYDOWN)
keypressed[ev.key.keysym.scancode] = true;
else if (ev.type == SDL::SDL_KEYUP)
keypressed[ev.key.keysym.scancode] = false;
}
bool Renderer::pollEventOrFromQueue(Event& ev) {
if (!eventQueue.empty()) {
ev = eventQueue.front();
eventQueue.pop_front();
updateKeypressed(ev);
return true;
} else if (SDL_PollEvent(&ev)) {
zoomMousePos(ev);
considerMouseMoveEvent(ev);
considerMouseCursorAnim(ev);
updateKeypressed(ev);
return true;
} else
return false;
}
void Renderer::considerMouseCursorAnim(Event& ev) {
if (ev.type == SDL::SDL_MOUSEBUTTONDOWN) {
if (cursorClicked)
SDL_SetCursor(cursorClicked);
} else
if (ev.type == SDL::SDL_MOUSEBUTTONUP) {
if (cursor)
SDL_SetCursor(cursor);
}
}
void Renderer::considerMouseMoveEvent(Event& ev) {
if (ev.type == SDL::SDL_MOUSEMOTION)
mousePos = Vec2(ev.motion.x, ev.motion.y);
}
bool Renderer::pollEvent(Event& ev) {
CHECK(currentThreadId() == *renderThreadId);
if (steamInput)
if (auto e = steamInput->getEvent()) {
ev.type = SDL::SDL_KEYDOWN;
ev.key = SDL::SDL_KeyboardEvent {
ev.type,
0, 0,
SDL_PRESSED,
0, 0, 0,
SDL::SDL_Keysym { SDL::SDL_Scancode{} , (SDL::SDL_Keycode) *e }
};
return true;
}
if (monkey) {
if (Random.roll(2))
return pollEventOrFromQueue(ev);
ev = SdlEventGenerator::getRandom(Random, getSize());
return true;
} else
return pollEventOrFromQueue(ev);
}
Vec2 Renderer::getDiscreteJoyPos(ControllerJoy joy) {
Vec2 ret;
if (steamInput) {
auto pos = steamInput->getJoyPos(joy);
if (pos.first <= -0.5)
ret.x = -1;
else if (pos.first >= 0.5)
ret.x = 1;
if (pos.second <= -0.5)
ret.y = 1;
else if (pos.second >= 0.5)
ret.y = -1;
}
return ret;
}
MySteamInput* Renderer::getSteamInput() {
return steamInput;
}
void Renderer::flushEvents(EventType type) {
Event ev;
while (SDL_PollEvent(&ev)) {
zoomMousePos(ev);
considerMouseMoveEvent(ev);
if (ev.type != type)
eventQueue.push_back(ev);
}
}
void Renderer::zoomMousePos(Event& ev) {
auto zoom = getZoom();
switch (ev.type) {
case SDL::SDL_MOUSEBUTTONUP:
case SDL::SDL_MOUSEBUTTONDOWN:
ev.button.x /= zoom;
ev.button.y /= zoom;
break;
case SDL::SDL_MOUSEMOTION:
ev.motion.x /= zoom;
ev.motion.y /= zoom;
break;
default:
break;
}
}
void Renderer::waitEvent(Event& ev) {
if (monkey) {
ev = SdlEventGenerator::getRandom(Random, getSize());
return;
} else {
if (!eventQueue.empty()) {
ev = eventQueue.front();
eventQueue.pop_front();
} else {
SDL_WaitEvent(&ev);
zoomMousePos(ev);
considerMouseMoveEvent(ev);
}
}
}
Vec2 Renderer::getMousePos() {
if (monkey)
return Vec2(Random.get(getSize().x), Random.get(getSize().y));
else
return mousePos;
}
void Renderer::startMonkey() {
monkey = true;
}
bool Renderer::isMonkey() {
return monkey;
}
SDL::SDL_Surface* flipVert(SDL::SDL_Surface* sfc) {
auto result = SDL::SDL_CreateRGBSurface(sfc->flags, sfc->w, sfc->h,
sfc->format->BytesPerPixel * 8, sfc->format->Rmask, sfc->format->Gmask,
sfc->format->Bmask, sfc->format->Amask);
CHECK(result);
std::uint8_t* pixels = (std::uint8_t*) sfc->pixels;
std::uint8_t* rpixels = (std::uint8_t*) result->pixels;
std::uint32_t pitch = sfc->pitch;
std::uint32_t pxlength = pitch*sfc->h;
for(auto line = 0; line < sfc->h; ++line) {
std::uint32_t pos = line * pitch;
memcpy(rpixels + pos, pixels + pxlength - pos - pitch, pitch);
}
return result;
}
void Renderer::makeScreenshot(const FilePath& path, Rectangle bounds) {
auto image = SDL::SDL_CreateRGBSurface(SDL_SWSURFACE, bounds.width(), bounds.height(), 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
SDL::glReadBuffer(GL_FRONT);
SDL::glReadPixels(bounds.left(), height - bounds.bottom(), bounds.width(), bounds.height(), GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
auto inverted = flipVert(image);
unsigned error = lodepng::encode(path.getPath(), (unsigned char*)inverted->pixels, bounds.width(), bounds.height(), LCT_RGB);
USER_CHECK(!error) << "encoder error " << error << ": "<< lodepng_error_text(error);
SDL_FreeSurface(image);
SDL_FreeSurface(inverted);
}
void playfile(const char *fname, SDL::SDL_Window* screen, Renderer&, float volume);
void Renderer::playVideo(const string& path, int volume) {
playfile(path.data(), window, *this, ((float)volume) / 100);
}