Refactor Lua support
[lsnes.git] / src / lua / gui-text.cpp
blob6fe5c049295648561dc6b2e5ad4af5c99dc5f503
1 #include "lua/internal.hpp"
2 #include "fonts/wrapper.hpp"
3 #include "library/framebuffer.hpp"
5 namespace
7 struct render_object_text : public render_object
9 render_object_text(int32_t _x, int32_t _y, const std::string& _text, premultiplied_color _fg,
10 premultiplied_color _bg, bool _hdbl = false, bool _vdbl = false) throw()
11 : x(_x), y(_y), text(_text), fg(_fg), bg(_bg), hdbl(_hdbl), vdbl(_vdbl) {}
12 ~render_object_text() throw() {}
13 template<bool X> void op(struct framebuffer<X>& scr) throw()
15 fg.set_palette(scr);
16 bg.set_palette(scr);
17 main_font.render(scr, x, y, text, fg, bg, hdbl, vdbl);
19 void operator()(struct framebuffer<true>& scr) throw() { op(scr); }
20 void operator()(struct framebuffer<false>& scr) throw() { op(scr); }
21 private:
22 int32_t x;
23 int32_t y;
24 premultiplied_color fg;
25 premultiplied_color bg;
26 std::string text;
27 bool hdbl;
28 bool vdbl;
31 int internal_gui_text(lua_state& L, const std::string& fname, bool hdbl, bool vdbl)
33 if(!lua_render_ctx)
34 return 0;
35 int64_t fgc = 0xFFFFFFU;
36 int64_t bgc = -1;
37 int32_t _x = L.get_numeric_argument<int32_t>(1, fname.c_str());
38 int32_t _y = L.get_numeric_argument<int32_t>(2, fname.c_str());
39 L.get_numeric_argument<int64_t>(4, fgc, fname.c_str());
40 L.get_numeric_argument<int64_t>(5, bgc, fname.c_str());
41 std::string text = L.get_string(3, fname.c_str());
42 premultiplied_color fg(fgc);
43 premultiplied_color bg(bgc);
44 lua_render_ctx->queue->create_add<render_object_text>(_x, _y, text, fg, bg, hdbl, vdbl);
45 return 0;
48 function_ptr_luafun gui_text(LS, "gui.text", [](lua_state& L, const std::string& fname) -> int {
49 internal_gui_text(L, fname, false, false);
50 });
52 function_ptr_luafun gui_textH(LS, "gui.textH", [](lua_state& L, const std::string& fname) -> int {
53 internal_gui_text(L, fname, true, false);
54 });
56 function_ptr_luafun gui_textV(LS, "gui.textV", [](lua_state& L, const std::string& fname) -> int {
57 internal_gui_text(L, fname, false, true);
58 });
60 function_ptr_luafun gui_textHV(LS, "gui.textHV", [](lua_state& L, const std::string& fname) -> int {
61 internal_gui_text(L, fname, true, true);
62 });