Skip to content

Commit 0437d89

Browse files
committed
Optimised renderer and ucp transformer
1 parent 14dc493 commit 0437d89

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
*.a
33
*.e
44
*wall
5+
*.bmp
6+
*.jpg
7+
*.png

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ EXSRCS := $(shell find $(EXDIR) -name '*.c' -o -name '*.cpp')
88
LIBOBJS := $(addsuffix .o, $(basename $(LIBSRCS)))
99
LIBA := libpluto.a
1010
EXBINS := $(addsuffix .e, $(basename $(EXSRCS)))
11-
CFLAGS = -Wall -Wextra -Werror -O3
11+
CFLAGS = -Wall -Wextra -Werror -O4
1212
LIBCFLAGS = $(CFLAGS)
1313

1414
.PHONY: clean

examples/clfps.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ int main()
328328
}
329329

330330
// Sort Pairs from closest to farthest
331-
sort(p.begin(), p.end(), [](const pair<float, float> &left, const pair<float, float> &right) { return left.first < right.first; });
331+
sort(p.begin(), p.end(), [](const pair<float, float> &left, const pair<float, float> &right)
332+
{ return left.first < right.first; });
332333

333334
// First two/three are closest (we will never see all four)
334335
float fBound = 0.001;

examples/clfps_rgb.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ int main()
235235
p.push_back(make_pair(d, dot));
236236
}
237237

238-
sort(p.begin(), p.end(), [](const pair<float, float> &left, const pair<float, float> &right) { return left.first < right.first; });
238+
sort(p.begin(), p.end(), [](const pair<float, float> &left, const pair<float, float> &right)
239+
{ return left.first < right.first; });
239240

240241
float f_bound = 0.001;
241242
if (acos(p.at(0).second) < f_bound)

examples/wall-density.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main()
2626
}
2727
}
2828
pluto_write_out();
29-
fputs((char *)_pluto_canvas.buffer, fp); // Write buffer to file
29+
fputs((char *)_pluto_canvas.buffer, fp); // Write buffer to file
3030
fclose(fp);
3131

3232
pluto_deinit();

src/render.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const uint8_t _pluto_dinis[4][2] = {
4444

4545
void pluto_transform_ucp(uchar *ret, uint16_t unichr)
4646
{
47-
ret[0] = (uchar)(((unichr >> 12) & 0x0F) | 0xE0);
47+
ret[0] = 0xE2;
4848
ret[1] = (uchar)(((unichr >> 6) & 0x3F) | 0x80);
4949
ret[2] = (uchar)(((unichr)&0x3F) | 0x80);
5050
}
@@ -56,6 +56,7 @@ void _pluto_lock_term()
5656
{
5757
if (_pluto_term_locked)
5858
return;
59+
5960
tcgetattr(0, &_pluto_term);
6061
tcgetattr(0, &_pluto_old_term);
6162
_pluto_term.c_lflag &= ~(ICANON | ECHO);
@@ -67,15 +68,18 @@ void _pluto_unlock_term()
6768
{
6869
if (!_pluto_term_locked)
6970
return;
71+
7072
tcsetattr(0, TCSANOW, &_pluto_old_term);
7173
_pluto_term_locked = false;
7274
}
7375

7476
void pluto_write_out()
7577
{
7678
_pluto_canvas.busy = true;
79+
7780
char buf[20];
7881
uchar *cbuf = (uchar *)&_pluto_canvas.buffer[3];
82+
7983
for (int32_t i = 0; i < _pluto_canvas.bmsize; i++)
8084
{
8185
int bx = (i % _pluto_canvas.width) * 2;
@@ -86,15 +90,15 @@ void pluto_write_out()
8690

8791
for (int y = 0; y < 4; y++)
8892
{
89-
int yp = y + by;
93+
int yp = (y + by) * (_pluto_canvas.width << 1);
9094

9195
for (int x = 0; x < 2; x++)
9296
{
9397
int xp = x + bx;
9498

9599
if (_pluto_canvas.bitmap[i] & (1 << _pluto_dinis[y][x]))
96100
{
97-
pci = yp * (_pluto_canvas.width << 1) + xp;
101+
pci = yp + xp;
98102
bl++;
99103
tr += _pluto_canvas.pix_colour[pci].r;
100104
tg += _pluto_canvas.pix_colour[pci].g;
@@ -105,23 +109,24 @@ void pluto_write_out()
105109

106110
if (bl)
107111
{
108-
tr /= bl;
109-
tg /= bl;
110-
tb /= bl;
112+
tr /= bl, tg /= bl, tb /= bl;
111113
}
112114
else
113115
{
114116
tr = 255, tg = 255, tb = 255;
115117
}
116118

117119
sprintf(buf, "\e[38;2;%03u;%03u;%03um", (uint8_t)tr, (uint8_t)tg, (uint8_t)tb);
118-
strcpy((char *)&cbuf[i * 22 + (i / _pluto_canvas.width)], buf);
119-
pluto_transform_ucp(&cbuf[i * 22 + 19 + (i / _pluto_canvas.width)], PLUTO_CHAR_OFF + _pluto_canvas.bitmap[i]);
120+
int iw = i / _pluto_canvas.width;
121+
strcpy((char *)&cbuf[i * 22 + iw], buf);
122+
pluto_transform_ucp(&cbuf[i * 22 + 19 + iw], PLUTO_CHAR_OFF + _pluto_canvas.bitmap[i]);
120123
}
124+
121125
for (int i = 1; i < _pluto_canvas.height; i++)
122126
{
123-
cbuf[_pluto_canvas.width * 22 * i + i - 1] = '\n';
127+
cbuf[(_pluto_canvas.width * 22 + 1) * i - 1] = '\n';
124128
}
129+
125130
_pluto_canvas.buffer[_pluto_canvas.bufsize - 1] = 0;
126131
_pluto_canvas.busy = false;
127132
}
@@ -136,14 +141,15 @@ void pluto_render()
136141
putchar('\n');
137142

138143
_pluto_first_out = false;
144+
139145
if (_pluto_canvas.use_write)
140146
fflush(stdout);
141147
}
142148
if (_pluto_canvas.use_write)
143149
{
144150
#if defined(__unix__) || defined(__linux__) || defined(BSD) || defined(__APPLE__)
145151
int wr = write(STDOUT_FILENO, _pluto_canvas.buffer, _pluto_canvas.bufsize);
146-
(void)wr; /* Debian fix */
152+
(void)wr; /* Debian fix */
147153
#else
148154
fputs((char *)_pluto_canvas.buffer, stdout);
149155
fflush(stdout);
@@ -154,6 +160,7 @@ void pluto_render()
154160
fputs((char *)_pluto_canvas.buffer, stdout);
155161
fflush(stdout);
156162
}
163+
157164
_pluto_unlock_term();
158165
_pluto_canvas.busy = false;
159166
}

0 commit comments

Comments
 (0)