Fix all warnings -Wall spews
[lsnes.git] / render.cpp
blob440aca31e575b625c0bfa50f81646229e3e02efc
1 #include "lsnes.hpp"
2 #include <snes/snes.hpp>
4 #include "render.hpp"
5 #include "png.hpp"
6 #include <sstream>
7 #include <list>
8 #include <iomanip>
9 #include <cstdint>
10 #include <string>
11 typedef uint8_t uint8;
12 typedef uint16_t uint16;
13 typedef uint32_t uint32;
14 typedef int8_t int8;
15 typedef int16_t int16;
16 typedef int32_t int32;
17 #include <nall/platform.hpp>
18 #include <nall/endian.hpp>
19 #include <nall/varint.hpp>
20 #include <nall/bit.hpp>
21 #include <nall/serializer.hpp>
22 #include <nall/property.hpp>
23 using namespace nall;
24 #include <ui-libsnes/libsnes.hpp>
25 using namespace SNES;
28 extern uint32_t fontdata[];
30 namespace
32 inline uint32_t blend(uint32_t orig, uint16_t ialpha, uint32_t pl, uint32_t ph) throw()
34 const uint32_t X = 0xFF00FFU;
35 const uint32_t Y = 0xFF00FF00U;
36 return ((ialpha * ((orig >> 8) & X) + ph) & Y) | (((ialpha * (orig & X) + pl)
37 >> 8) & X);
40 //This is Jenkin's MIX function.
41 uint32_t keyhash(uint32_t key, uint32_t item, uint32_t mod) throw()
43 uint32_t a = key;
44 uint32_t b = 0;
45 uint32_t c = item;
46 a=a-b; a=a-c; a=a^(c >> 13);
47 b=b-c; b=b-a; b=b^(a << 8);
48 c=c-a; c=c-b; c=c^(b >> 13);
49 a=a-b; a=a-c; a=a^(c >> 12);
50 b=b-c; b=b-a; b=b^(a << 16);
51 c=c-a; c=c-b; c=c^(b >> 5);
52 a=a-b; a=a-c; a=a^(c >> 3);
53 b=b-c; b=b-a; b=b^(a << 10);
54 c=c-a; c=c-b; c=c^(b >> 15);
55 return c % mod;
59 //Locate glyph in font. Returns <width, offset> pair. Zero offset should be interpretted as an empty
60 //glyph.
61 std::pair<uint32_t, size_t> find_glyph(uint32_t codepoint, int32_t x, int32_t y, int32_t orig_x,
62 int32_t& next_x, int32_t& next_y) throw()
64 uint32_t cwidth = 0;
65 if(codepoint == 9) {
66 cwidth = 64 - (x - orig_x) % 64;
67 next_x = x + cwidth;
68 next_y = y;
69 return std::make_pair(cwidth, 0);
70 } else if(codepoint == 10) {
71 next_x = orig_x;
72 next_y = y + 16;
73 return std::make_pair(0, 0);
74 } else if(codepoint == 32) {
75 next_x = x + 8;
76 next_y = y;
77 return std::make_pair(8, 0);
78 } else {
79 uint32_t mdir = fontdata[0];
80 uint32_t mseed = fontdata[mdir];
81 uint32_t msize = fontdata[mdir + 1];
82 uint32_t midx = keyhash(mseed, codepoint, msize);
83 uint32_t sdir = fontdata[mdir + 2 + midx];
84 if(!fontdata[sdir + 1]) {
85 //Character not found.
86 next_x = x + 8;
87 next_y = y;
88 return std::make_pair(8, 0);
90 uint32_t sseed = fontdata[sdir];
91 uint32_t ssize = fontdata[sdir + 1];
92 uint32_t sidx = keyhash(sseed, codepoint, ssize);
93 if(fontdata[sdir + 2 + 2 * sidx] != codepoint) {
94 //Character not found.
95 next_x = x + 8;
96 next_y = y;
97 return std::make_pair(8, 0);
99 bool wide = (fontdata[fontdata[sdir + 2 + 2 * sidx + 1]] != 0);
100 next_x = x + (wide ? 16 : 8);
101 next_y = y;
102 return std::make_pair(wide ? 16 : 8, fontdata[sdir + 2 + 2 * sidx + 1] + 1);
106 render_object::~render_object() throw()
110 void render_text(struct screen& scr, int32_t _x, int32_t _y, const std::string& _text, uint32_t _fg,
111 uint16_t _fgalpha, uint32_t _bg, uint16_t _bgalpha) throw(std::bad_alloc)
113 render_object_text tmp(_x, _y, _text, _fg, _fgalpha, _bg, _bgalpha);
114 tmp(scr);
117 render_object_text::render_object_text(int32_t _x, int32_t _y, const std::string& _text, uint32_t _fg,
118 uint16_t _fgalpha, uint32_t _bg, uint16_t _bgalpha) throw(std::bad_alloc)
119 : x(_x), y(_y), fg(_fg), fgalpha(_fgalpha), bg(_bg), bgalpha(_bgalpha), text(_text)
123 void render_queue::add(struct render_object& obj) throw(std::bad_alloc)
125 q.push_back(&obj);
128 void render_queue::run(struct screen& scr) throw()
130 for(auto i = q.begin(); i != q.end(); i++) {
131 try {
132 (**i)(scr);
133 } catch(...) {
135 delete *i;
137 q.clear();
140 void render_queue::clear() throw()
142 for(auto i = q.begin(); i != q.end(); i++)
143 delete *i;
144 q.clear();
147 render_queue::~render_queue() throw()
149 clear();
152 uint32_t screen::make_color(uint8_t r, uint8_t g, uint8_t b) throw()
154 return (static_cast<uint32_t>(r) << active_rshift) |
155 (static_cast<uint32_t>(g) << active_gshift) |
156 (static_cast<uint32_t>(b) << active_bshift);
159 lcscreen::lcscreen(const uint16_t* mem, bool hires, bool interlace, bool overscan, bool region) throw()
161 uint32_t dataoffset = 0;
162 width = hires ? 512 : 256;
163 height = 0;
164 if(region) {
165 //PAL.
166 height = 239;
167 dataoffset = overscan ? 9 : 1;
168 } else {
169 //presumably NTSC.
170 height = 224;
171 dataoffset = overscan ? 16 : 9;
173 if(interlace)
174 height <<= 1;
175 memory = mem + dataoffset * 1024;
176 pitch = interlace ? 512 : 1024;
177 user_memory = false;
180 lcscreen::lcscreen(const uint16_t* mem, uint32_t _width, uint32_t _height) throw()
182 width = _width;
183 height = _height;
184 memory = mem;
185 pitch = width;
186 user_memory = false;
189 lcscreen::lcscreen() throw()
191 width = 0;
192 height = 0;
193 memory = NULL;
194 user_memory = true;
195 pitch = 0;
196 allocated = 0;
199 lcscreen::lcscreen(const lcscreen& ls) throw(std::bad_alloc)
201 width = ls.width;
202 height = ls.height;
203 pitch = width;
204 user_memory = true;
205 allocated = static_cast<size_t>(width) * height;
206 memory = new uint16_t[allocated];
207 for(size_t l = 0; l < height; l++)
208 memcpy(const_cast<uint16_t*>(memory + l * width), ls.memory + l * ls.pitch, 2 * width);
211 lcscreen& lcscreen::operator=(const lcscreen& ls) throw(std::bad_alloc, std::runtime_error)
213 if(!user_memory)
214 throw std::runtime_error("Can't copy to non-user memory");
215 if(this == &ls)
216 return *this;
217 if(allocated < static_cast<size_t>(ls.width) * ls.height) {
218 size_t p_allocated = static_cast<size_t>(ls.width) * ls.height;
219 memory = new uint16_t[p_allocated];
220 allocated = p_allocated;
222 width = ls.width;
223 height = ls.height;
224 pitch = width;
225 for(size_t l = 0; l < height; l++)
226 memcpy(const_cast<uint16_t*>(memory + l * width), ls.memory + l * ls.pitch, 2 * width);
227 return *this;
230 lcscreen::~lcscreen()
232 if(user_memory)
233 delete[] const_cast<uint16_t*>(memory);
236 void lcscreen::load(const std::vector<char>& data) throw(std::bad_alloc, std::runtime_error)
238 if(!user_memory)
239 throw std::runtime_error("Can't load to non-user memory");
240 const uint8_t* data2 = reinterpret_cast<const uint8_t*>(&data[0]);
241 if(data.size() < 2)
242 throw std::runtime_error("Corrupt saved screenshot data");
243 uint32_t _width = static_cast<uint32_t>(data2[0]) * 256 + static_cast<uint32_t>(data2[1]);
244 if(_width > 1 && data.size() % (2 * _width) != 2)
245 throw std::runtime_error("Corrupt saved screenshot data");
246 uint32_t _height = (data.size() - 2) / (2 * _width);
247 if(allocated < static_cast<size_t>(_width) * _height) {
248 size_t p_allocated = static_cast<size_t>(_width) * _height;
249 memory = new uint16_t[p_allocated];
250 allocated = p_allocated;
252 uint16_t* mem = const_cast<uint16_t*>(memory);
253 width = _width;
254 height = _height;
255 pitch = width;
256 for(size_t i = 0; i < (data.size() - 2) / 2; i++)
257 mem[i] = static_cast<uint16_t>(data2[2 + 2 * i]) * 256 +
258 static_cast<uint16_t>(data2[2 + 2 * i + 1]);
261 void lcscreen::save(std::vector<char>& data) throw(std::bad_alloc)
263 data.resize(2 + 2 * static_cast<size_t>(width) * height);
264 uint8_t* data2 = reinterpret_cast<uint8_t*>(&data[0]);
265 data2[0] = (width >> 8);
266 data2[1] = width;
267 for(size_t i = 0; i < (data.size() - 2) / 2; i++) {
268 data[2 + 2 * i] = memory[(i / width) * pitch + (i % width)] >> 8;
269 data[2 + 2 * i + 1] = memory[(i / width) * pitch + (i % width)];
273 void lcscreen::save_png(const std::string& file) throw(std::bad_alloc, std::runtime_error)
275 unsigned char clevels[32];
276 for(unsigned i = 0; i < 32; i++)
277 clevels[i] = 255 * i / 31;
278 uint8_t* buffer = new uint8_t[3 * static_cast<size_t>(width) * height];
279 for(uint32_t j = 0; j < height; j++)
280 for(uint32_t i = 0; i < width; i++) {
281 uint16_t word = memory[pitch * j + i];
282 buffer[3 * static_cast<size_t>(width) * j + 3 * i + 0] = clevels[(word >> 10) & 0x1F];
283 buffer[3 * static_cast<size_t>(width) * j + 3 * i + 1] = clevels[(word >> 5) & 0x1F];
284 buffer[3 * static_cast<size_t>(width) * j + 3 * i + 2] = clevels[(word) & 0x1F];
286 try {
287 save_png_data(file, buffer, width, height);
288 delete[] buffer;
289 } catch(...) {
290 delete[] buffer;
291 throw;
295 void render_object_text::operator()(struct screen& scr) throw()
297 uint32_t pfgl = (fg & 0xFF00FF) * fgalpha;
298 uint32_t pfgh = ((fg >> 8) & 0xFF00FF) * fgalpha;
299 uint32_t pbgl = (bg & 0xFF00FF) * bgalpha;
300 uint32_t pbgh = ((bg >> 8) & 0xFF00FF) * bgalpha;
301 uint16_t ifga = 256 - fgalpha;
302 uint16_t ibga = 256 - bgalpha;
303 int32_t orig_x = x;
304 uint32_t unicode_code = 0;
305 uint8_t unicode_left = 0;
306 for(size_t i = 0; i < text.length(); i++) {
307 uint8_t ch = text[i];
308 if(ch < 128)
309 unicode_code = text[i];
310 else if(ch < 192) {
311 if(!unicode_left)
312 continue;
313 unicode_code = 64 * unicode_code + ch - 128;
314 if(--unicode_left)
315 continue;
316 } else if(ch < 224) {
317 unicode_code = ch - 192;
318 unicode_left = 1;
319 continue;
320 } else if(ch < 240) {
321 unicode_code = ch - 224;
322 unicode_left = 2;
323 continue;
324 } else if(ch < 248) {
325 unicode_code = ch - 240;
326 unicode_left = 3;
327 continue;
328 } else
329 continue;
330 int32_t next_x, next_y;
331 auto p = find_glyph(unicode_code, x, y, orig_x, next_x, next_y);
332 uint32_t dx = 0;
333 uint32_t dw = p.first;
334 uint32_t dy = 0;
335 uint32_t dh = 16;
336 uint32_t cx = static_cast<uint32_t>(static_cast<int32_t>(scr.originx) + x);
337 uint32_t cy = static_cast<uint32_t>(static_cast<int32_t>(scr.originy) + y);
338 while(cx > scr.width && dw > 0) {
339 dx++;
340 dw--;
341 cx++;
343 while(cy > scr.height && dh > 0) {
344 dy++;
345 dh--;
346 cy++;
348 while(cx + dw > scr.width && dw > 0)
349 dw--;
350 while(cy + dh > scr.height && dh > 0)
351 dh--;
352 if(!dw || !dh)
353 continue; //Outside screen.
355 if(p.second == 0) {
356 //Blank glyph.
357 for(uint32_t j = 0; j < dh; j++) {
358 uint32_t* base = scr.rowptr(cy + j) + cx;
359 for(uint32_t i = 0; i < dw; i++)
360 base[i] = blend(base[i], ibga, pbgl, pbgh);
362 } else {
363 //narrow/wide glyph.
364 for(uint32_t j = 0; j < dh; j++) {
365 uint32_t dataword = fontdata[p.second + (dy + j) / (32 / p.first)];
366 uint32_t* base = scr.rowptr(cy + j) + cx;
367 for(uint32_t i = 0; i < dw; i++)
368 if(((dataword >> (31 - ((dy + j) % (32 / p.first)) * p.first - (dx + i))) & 1))
369 base[i] = blend(base[i], ifga, pfgl, pfgh);
370 else
371 base[i] = blend(base[i], ibga, pbgl, pbgh);
374 x = next_x;
375 y = next_y;
379 void screen::copy_from(lcscreen& scr, uint32_t hscale, uint32_t vscale) throw()
381 uint32_t copyable_width = (width - originx) / hscale;
382 uint32_t copyable_height = (height - originy) / vscale;
383 copyable_width = (copyable_width > scr.width) ? scr.width : copyable_width;
384 copyable_height = (copyable_height > scr.height) ? scr.height : copyable_height;
385 for(uint32_t y = 0; y < height; y++) {
386 memset(rowptr(y), 0, 4 * width);
388 for(uint32_t y = 0; y < copyable_height; y++) {
389 uint32_t line = y * vscale + originy;
390 uint32_t* ptr = rowptr(line) + originx;
391 const uint16_t* sbase = scr.memory + y * scr.pitch;
392 for(uint32_t x = 0; x < copyable_width; x++) {
393 uint32_t c = palette[sbase[x] % 32768];
394 for(uint32_t i = 0; i < hscale; i++)
395 *(ptr++) = c;
397 for(uint32_t j = 1; j < vscale; j++)
398 memcpy(rowptr(line + j), rowptr(line), 4 * hscale * copyable_width);
402 void screen::reallocate(uint32_t _width, uint32_t _height, uint32_t _originx, uint32_t _originy, bool upside_down)
403 throw(std::bad_alloc)
405 if(_width == width && _height == height) {
406 originx = _originx;
407 originy = _originy;
408 return;
410 if(!_width || !_height) {
411 width = height = originx = originy = pitch = 0;
412 if(memory && !user_memory)
413 delete[] memory;
414 memory = NULL;
415 user_memory = false;
416 flipped = upside_down;
417 return;
419 uint32_t* newmem = new uint32_t[_width * _height];
420 width = _width;
421 height = _height;
422 originx = _originx;
423 originy = _originy;
424 pitch = 4 * _width;
425 if(memory && !user_memory)
426 delete[] memory;
427 memory = newmem;
428 user_memory = false;
429 flipped = upside_down;
432 void screen::set(uint32_t* _memory, uint32_t _width, uint32_t _height, uint32_t _originx, uint32_t _originy,
433 uint32_t _pitch) throw()
435 if(memory && !user_memory)
436 delete[] memory;
437 width = _width;
438 height = _height;
439 originx = _originx;
440 originy = _originy;
441 pitch = _pitch;
442 user_memory = true;
443 memory = _memory;
444 flipped = false;
447 uint32_t* screen::rowptr(uint32_t row) throw()
449 if(flipped)
450 row = height - row - 1;
451 return reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(memory) + row * pitch);
454 screen::screen() throw()
456 uint32_t _magic = 403703808;
457 uint8_t* magic = reinterpret_cast<uint8_t*>(&_magic);
458 memory = NULL;
459 width = height = originx = originy = pitch = 0;
460 user_memory = false;
461 flipped = false;
462 active_rshift = active_gshift = active_bshift = 255;
463 set_palette(magic[0], magic[1], magic[2]);
466 screen::~screen() throw()
468 if(memory && !user_memory)
469 delete[] memory;
472 void screen::set_palette(uint32_t rshift, uint32_t gshift, uint32_t bshift) throw()
474 if(rshift == active_rshift && gshift == active_gshift && bshift == active_bshift)
475 return;
476 uint32_t old_rshift = active_rshift;
477 uint32_t old_gshift = active_gshift;
478 uint32_t old_bshift = active_bshift;
479 uint32_t xpalette[32];
480 for(unsigned i = 0; i < 32; i++)
481 xpalette[i] = (i * 255 / 31);
482 for(unsigned i = 0; i < 32768; i++) {
483 palette[i] = (xpalette[(i >> 10) & 31] << rshift) +
484 (xpalette[(i >> 5) & 31] << gshift) +
485 (xpalette[i & 31] << bshift);
487 active_rshift = rshift;
488 active_gshift = gshift;
489 active_bshift = bshift;
490 //Convert the data.
491 for(uint32_t j = 0; j < height; j++) {
492 uint32_t* rp = rowptr(j);
493 for(uint32_t i = 0; i < width; i++) {
494 uint32_t x = rp[i];
495 uint32_t r = (x >> old_rshift) & 0xFF;
496 uint32_t g = (x >> old_gshift) & 0xFF;
497 uint32_t b = (x >> old_bshift) & 0xFF;
498 x = (r << active_rshift) | (g << active_gshift) | (b << active_bshift);
499 rp[i] = x;
504 render_object_text::~render_object_text() throw()