Make various instance stuff to take references to other instance objs
[lsnes.git] / src / core / framebuffer.cpp
blob817b5b2691d5193a32ee349f338c587534915f4f
1 #include "core/command.hpp"
2 #include "core/dispatch.hpp"
3 #include "core/framebuffer.hpp"
4 #include "core/instance.hpp"
5 #include "core/keymapper.hpp"
6 #include "core/memorywatch.hpp"
7 #include "core/misc.hpp"
8 #include "core/moviedata.hpp"
9 #include "core/moviefile.hpp"
10 #include "core/subtitles.hpp"
11 #include "core/settings.hpp"
12 #include "core/window.hpp"
13 #include "lua/lua.hpp"
14 #include "fonts/wrapper.hpp"
15 #include "library/framebuffer.hpp"
16 #include "library/framebuffer-pixfmt-lrgb.hpp"
17 #include "library/minmax.hpp"
18 #include "library/triplebuffer.hpp"
20 framebuffer::raw screen_corrupt;
21 void update_movie_state();
23 namespace
25 struct render_list_entry
27 uint32_t codepoint;
28 uint32_t x;
29 uint32_t y;
30 uint32_t scale;
33 struct render_list_entry rl_corrupt[] = {
34 {'S', 88, 56, 7},
35 {'Y', 144, 56, 7},
36 {'S', 200, 56, 7},
37 {'T', 256, 56, 7},
38 {'E', 312, 56, 7},
39 {'M', 368, 56, 7},
40 {'S', 116, 168, 7},
41 {'T', 172, 168, 7},
42 {'A', 224, 168, 7},
43 {'T', 280, 168, 7},
44 {'E', 336, 168, 7},
45 {'C', 60, 280, 7},
46 {'O', 116, 280, 7},
47 {'R', 172, 280, 7},
48 {'R', 228, 280, 7},
49 {'U', 284, 280, 7},
50 {'P', 340, 280, 7},
51 {'T', 396, 280, 7},
52 {0, 0, 0, 0}
55 void draw_special_screen(uint32_t* target, struct render_list_entry* rlist)
57 while(rlist->scale) {
58 auto g = main_font.get_glyph(rlist->codepoint);
59 for(uint32_t j = 0; j < 16; j++) {
60 for(uint32_t i = 0; i < (g.wide ? 16 : 8); i++) {
61 uint32_t slice = g.data[j / (g.wide ? 2 : 4)];
62 uint32_t bit = 31 - ((j % (g.wide ? 2 : 4)) * (g.wide ? 16 : 8) + i);
63 uint32_t value = (slice >> bit) & 1;
64 if(value) {
65 uint32_t basex = rlist->x + rlist->scale * i;
66 uint32_t basey = rlist->y + rlist->scale * j;
67 for(uint32_t j2 = 0; j2 < rlist->scale; j2++)
68 for(uint32_t i2 = 0; i2 < rlist->scale; i2++)
69 target[(basey + j2) * 512 + (basex + i2)] = 0x7FFFF;
73 rlist++;
77 void draw_corrupt(uint32_t* target)
79 for(unsigned i = 0; i < 512 * 448; i++)
80 target[i] = 0x7FC00;
81 draw_special_screen(target, rl_corrupt);
84 command::fnptr<command::arg_filename> take_screenshot_cmd(lsnes_cmds, "take-screenshot", "Takes a screenshot",
85 "Syntax: take-screenshot <file>\nSaves screenshot to PNG file <file>\n",
86 [](command::arg_filename file) throw(std::bad_alloc, std::runtime_error) {
87 CORE().fbuf.take_screenshot(file);
88 messages << "Saved PNG screenshot" << std::endl;
89 });
91 settingvar::supervariable<settingvar::model_int<0, 8191>> dtb(lsnes_setgrp, "top-border",
92 "UI‣Top padding", 0);
93 settingvar::supervariable<settingvar::model_int<0, 8191>> dbb(lsnes_setgrp, "bottom-border",
94 "UI‣Bottom padding", 0);
95 settingvar::supervariable<settingvar::model_int<0, 8191>> dlb(lsnes_setgrp, "left-border",
96 "UI‣Left padding", 0);
97 settingvar::supervariable<settingvar::model_int<0, 8191>> drb(lsnes_setgrp, "right-border",
98 "UI‣Right padding", 0);
101 framebuffer::raw emu_framebuffer::screen_corrupt;
103 emu_framebuffer::emu_framebuffer(subtitle_commentary& _subtitles, settingvar::group& _settings, memwatch_set& _mwatch,
104 keyboard::keyboard& _keyboard)
105 : buffering(buffer1, buffer2, buffer3), subtitles(_subtitles), settings(_settings), mwatch(_mwatch),
106 keyboard(_keyboard)
108 last_redraw_no_lua = false;
111 void emu_framebuffer::take_screenshot(const std::string& file) throw(std::bad_alloc, std::runtime_error)
113 render_info& ri = buffering.get_read();
114 ri.fbuf.save_png(file);
115 buffering.put_read();
119 void emu_framebuffer::init_special_screens() throw(std::bad_alloc)
121 std::vector<uint32_t> buf;
122 buf.resize(512*448);
124 framebuffer::info inf;
125 inf.type = &framebuffer::pixfmt_lrgb;
126 inf.mem = reinterpret_cast<char*>(&buf[0]);
127 inf.physwidth = 512;
128 inf.physheight = 448;
129 inf.physstride = 2048;
130 inf.width = 512;
131 inf.height = 448;
132 inf.stride = 2048;
133 inf.offset_x = 0;
134 inf.offset_y = 0;
136 draw_corrupt(&buf[0]);
137 screen_corrupt = framebuffer::raw(inf);
140 void emu_framebuffer::redraw_framebuffer(framebuffer::raw& todraw, bool no_lua, bool spontaneous)
142 uint32_t hscl, vscl;
143 auto g = our_rom.rtype->get_scale_factors(todraw.get_width(), todraw.get_height());
144 hscl = g.first;
145 vscl = g.second;
146 render_info& ri = buffering.get_write();
147 ri.rq.clear();
148 struct lua::render_context lrc;
149 lrc.left_gap = 0;
150 lrc.right_gap = 0;
151 lrc.bottom_gap = 0;
152 lrc.top_gap = 0;
153 lrc.queue = &ri.rq;
154 lrc.width = todraw.get_width() * hscl;
155 lrc.height = todraw.get_height() * vscl;
156 if(!no_lua) {
157 lua_callback_do_paint(&lrc, spontaneous);
158 subtitles.render(lrc);
160 ri.fbuf = todraw;
161 ri.hscl = hscl;
162 ri.vscl = vscl;
163 ri.lgap = max(lrc.left_gap, (unsigned)dlb(settings));
164 ri.rgap = max(lrc.right_gap, (unsigned)drb(settings));
165 ri.tgap = max(lrc.top_gap, (unsigned)dtb(settings));
166 ri.bgap = max(lrc.bottom_gap, (unsigned)dbb(settings));
167 mwatch.watch(ri.rq);
168 buffering.put_write();
169 notify_screen_update();
170 last_redraw_no_lua = no_lua;
171 update_movie_state();
174 void emu_framebuffer::redraw_framebuffer()
176 render_info& ri = buffering.get_read();
177 framebuffer::raw copy = ri.fbuf;
178 buffering.put_read();
179 //Redraws are never spontaneous
180 redraw_framebuffer(copy, last_redraw_no_lua, false);
183 void emu_framebuffer::render_framebuffer()
185 render_info& ri = buffering.get_read();
186 main_screen.reallocate(ri.fbuf.get_width() * ri.hscl + ri.lgap + ri.rgap, ri.fbuf.get_height() * ri.vscl +
187 ri.tgap + ri.bgap);
188 main_screen.set_origin(ri.lgap, ri.tgap);
189 main_screen.copy_from(ri.fbuf, ri.hscl, ri.vscl);
190 ri.rq.run(main_screen);
191 notify_set_screen(main_screen);
192 //We would want divide by 2, but we'll do it ourselves in order to do mouse.
193 keyboard::key* mouse_x = keyboard.try_lookup_key("mouse_x");
194 keyboard::key* mouse_y = keyboard.try_lookup_key("mouse_y");
195 keyboard::mouse_calibration xcal;
196 keyboard::mouse_calibration ycal;
197 xcal.offset = ri.lgap;
198 ycal.offset = ri.tgap;
199 if(mouse_x && mouse_x->get_type() == keyboard::KBD_KEYTYPE_MOUSE)
200 mouse_x->cast_mouse()->set_calibration(xcal);
201 if(mouse_y && mouse_y->get_type() == keyboard::KBD_KEYTYPE_MOUSE)
202 mouse_y->cast_mouse()->set_calibration(ycal);
203 buffering.put_read();
206 std::pair<uint32_t, uint32_t> emu_framebuffer::get_framebuffer_size()
208 uint32_t v, h;
209 render_info& ri = buffering.get_read();
210 v = ri.fbuf.get_width();
211 h = ri.fbuf.get_height();
212 buffering.put_read();
213 return std::make_pair(h, v);
216 framebuffer::raw emu_framebuffer::get_framebuffer() throw(std::bad_alloc)
218 render_info& ri = buffering.get_read();
219 framebuffer::raw copy = ri.fbuf;
220 buffering.put_read();
221 return copy;
224 void emu_framebuffer::render_kill_request(void* obj)
226 buffer1.rq.kill_request(obj);
227 buffer2.rq.kill_request(obj);
228 buffer3.rq.kill_request(obj);
231 framebuffer::raw& emu_framebuffer::render_get_latest_screen()
233 return buffering.get_read().fbuf;
236 void emu_framebuffer::render_get_latest_screen_end()
238 buffering.put_read();