forked from SFTtech/openage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.cpp
More file actions
171 lines (148 loc) · 4.99 KB
/
draw.cpp
File metadata and controls
171 lines (148 loc) · 4.99 KB
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
// Copyright 2014-2018 the openage authors. See copying.md for legal info.
#include "draw.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <epoxy/gl.h>
#include "../util/fds.h"
#include "../util/timing.h"
#include "buf.h"
#include "console.h"
#include "../engine.h"
#include "../renderer/text.h"
namespace openage {
namespace console {
namespace draw {
void to_opengl(Engine *engine, Console *console) {
coord::camhud topleft {
console->bottomleft.x,
// TODO This should probably just be console->topright.y
console->bottomleft.y + console->charsize.y * console->buf.dims.y
};
coord::pixel_t ascender = static_cast<coord::pixel_t>(console->font.get_ascender());
renderer::TextRenderer *text_renderer = engine->get_text_renderer();
text_renderer->set_font(&console->font);
int64_t monotime = timing::get_monotonic_time();
bool fastblinking_visible = (monotime % 600000000 < 300000000);
bool slowblinking_visible = (monotime % 300000000 < 150000000);
for (coord::term_t x = 0; x < console->buf.dims.x; x++) {
coord::camhud chartopleft{topleft.x + console->charsize.x * x, 0};
for (coord::term_t y = 0; y < console->buf.dims.y; y++) {
chartopleft.y = topleft.y - console->charsize.y * y;
buf_char p = *(console->buf.chrdataptr({x, y - console->buf.scrollback_pos}));
int fgcolid, bgcolid;
bool cursor_visible_at_current_pos = (
console->buf.cursorpos == coord::term{x, y - console->buf.scrollback_pos}
);
cursor_visible_at_current_pos &= console->buf.cursor_visible;
if ((p.flags & CHR_NEGATIVE) xor cursor_visible_at_current_pos) {
bgcolid = p.fgcol;
fgcolid = p.bgcol;
} else {
bgcolid = p.bgcol;
fgcolid = p.fgcol;
}
if ((p.flags & CHR_INVISIBLE)
or (p.flags & CHR_BLINKING and not slowblinking_visible)
or (p.flags & CHR_BLINKINGFAST and not fastblinking_visible)) {
fgcolid = bgcolid;
}
console->termcolors[bgcolid].use(0.8);
glBegin(GL_QUADS);
{
glVertex3f(chartopleft.x, chartopleft.y, 0);
glVertex3f(chartopleft.x, chartopleft.y - console->charsize.y, 0);
glVertex3f(chartopleft.x + console->charsize.x, chartopleft.y - console->charsize.y, 0);
glVertex3f(chartopleft.x + console->charsize.x, chartopleft.y, 0);
}
glEnd();
console->termcolors[fgcolid].use(1);
char utf8buf[5];
if (util::utf8_encode(p.cp, utf8buf) == 0) {
//unrepresentable character (question mark in black rhombus)
text_renderer->draw(chartopleft.x, chartopleft.y - ascender, "\uFFFD");
} else {
text_renderer->draw(chartopleft.x, chartopleft.y - ascender, utf8buf);
}
}
}
}
void to_terminal(Buf *buf, util::FD *fd, bool clear) {
//move cursor, draw top left corner
fd->puts("\x1b[H\u250c");
if (clear) {
fd->puts("\x1b[J");
}
//draw top line, including title
for (coord::term_t x = 0; x < buf->dims.x; x++) {
if (x >= 3 && (x - 3) < (int) buf->title.size()) {
fd->putcp(buf->title[x - 3]);
} else if (x == 2) {
fd->putbyte('[');
} else if (x - 3 == (int) buf->title.size()) {
fd->putbyte(']');
} else {
fd->puts("\u2500");
}
}
//draw top right corner
fd->puts("\u2510\n");
//calculate pos/size of scrollbar
//scrollbar_top is the first line that has the scrollbar displayed
//scrollbar_bottom is the first line that doesn't have the scrollbar displayed
coord::term_t lines_total = buf->scrollback_possible + buf->dims.y;
coord::term_t pos_total = buf->scrollback_possible - buf->scrollback_pos;
coord::term_t scrollbar_top = (buf->dims.y * pos_total) / lines_total;
coord::term_t scrollbar_bottom = (buf->dims.y * (pos_total + buf->dims.y)) / lines_total;
if (scrollbar_bottom == scrollbar_top) {
if (scrollbar_bottom < buf->dims.y) {
scrollbar_bottom++;
} else {
scrollbar_top--;
}
}
//print lines -scrollback_pos to dims.y - scrollback_pos - 1
for (coord::term_t y = 0; y < buf->dims.y; y++) {
//draw left line
fd->puts("\u2502");
//draw chars of this line
for (coord::term_t x = 0; x < buf->dims.x; x++) {
buf_char p = *(buf->chrdataptr({x, y - buf->scrollback_pos}));
if (p.cp < 32) {
p.cp = '?';
}
fd->printf("\x1b[38;5;%dm\x1b[48;5;%dm", p.fgcol, p.bgcol);
if (p.flags & CHR_BOLD) {
fd->puts("\x1b[1m");
}
if (p.flags & CHR_BLINKING) {
fd->puts("\x1b[5m");
}
bool cursor_visible_at_current_pos = buf->cursorpos == coord::term{x, y - buf->scrollback_pos};
cursor_visible_at_current_pos &= buf->cursor_visible;
if ((p.flags & CHR_NEGATIVE) xor cursor_visible_at_current_pos) {
//print char negative
fd->puts("\x1b[7m");
}
fd->putcp(p.cp);
fd->puts("\x1b[m");
}
//draw right line
if (y >= scrollbar_top and y < scrollbar_bottom) {
//draw scrollbar on this part of right line
fd->puts("\u2503");
} else {
fd->puts("\u2502");
}
fd->putbyte('\n');
}
//draw bottom left corner
fd->puts("\u2514");
//draw bottom line
for (coord::term_t x = 0; x < buf->dims.x; x++) {
fd->puts("\u2500");
}
//draw bottom right corner
fd->puts("\u2518\n");
}
}}} // openage::console::draw