Refactor dynamic state to its own subobject
[lsnes.git] / src / lua / gui-text.cpp
blobe217b4b093c3e94aafe0c0f6023b7231da38cb71
1 #include "core/instance.hpp"
2 #include "lua/internal.hpp"
3 #include "fonts/wrapper.hpp"
4 #include "library/framebuffer.hpp"
5 #include "library/lua-framebuffer.hpp"
7 namespace
9 struct render_object_text : public framebuffer::object
11 render_object_text(int32_t _x, int32_t _y, const std::string& _text, framebuffer::color _fg,
12 framebuffer::color _bg, bool _hdbl = false, bool _vdbl = false) throw()
13 : x(_x), y(_y), text(_text), fg(_fg), bg(_bg), hdbl(_hdbl), vdbl(_vdbl) {}
14 ~render_object_text() throw() {}
15 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
17 main_font.render(scr, x, y, text, fg, bg, hdbl, vdbl);
19 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
20 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
21 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
22 private:
23 int32_t x;
24 int32_t y;
25 std::string text;
26 framebuffer::color fg;
27 framebuffer::color bg;
28 bool hdbl;
29 bool vdbl;
32 template<bool hdbl, bool vdbl>
33 int internal_gui_text(lua::state& L, lua::parameters& P)
35 auto& core = CORE();
36 int32_t x, y;
37 std::string text;
38 framebuffer::color fg, bg;
40 if(!core.lua2->render_ctx) return 0;
42 P(x, y, text, P.optional(fg, 0xFFFFFFU), P.optional(bg, -1));
44 core.lua2->render_ctx->queue->create_add<render_object_text>(x, y, text, fg, bg, hdbl, vdbl);
45 return 0;
48 lua::functions LUA_text_fns(lua_func_misc, "gui", {
49 {"text", internal_gui_text<false, false>},
50 {"textH", internal_gui_text<true, false>},
51 {"textV", internal_gui_text<false, true>},
52 {"textHV", internal_gui_text<true, true>},
53 });