Lua: Multi-argument parsing and unify color parsing
[lsnes.git] / src / lua / gui-rectangle.cpp
blob6c599d23d4e1b326d4cc994856a5c84aec3dcdfc
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
3 #include "library/lua-framebuffer.hpp"
5 namespace
7 struct render_object_rectangle : public framebuffer::object
9 render_object_rectangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height,
10 framebuffer::color _outline, framebuffer::color _fill, int32_t _thickness) throw()
11 : x(_x), y(_y), width(_width), height(_height), outline(_outline), fill(_fill),
12 thickness(_thickness) {}
13 ~render_object_rectangle() throw() {}
14 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
16 outline.set_palette(scr);
17 fill.set_palette(scr);
18 uint32_t originx = scr.get_origin_x();
19 uint32_t originy = scr.get_origin_y();
20 int32_t xmin = 0;
21 int32_t xmax = width;
22 int32_t ymin = 0;
23 int32_t ymax = height;
24 framebuffer::clip_range(originx, scr.get_width(), x, xmin, xmax);
25 framebuffer::clip_range(originy, scr.get_height(), y, ymin, ymax);
26 for(int32_t r = ymin; r < ymax; r++) {
27 typename framebuffer::fb<X>::element_t* rptr = scr.rowptr(y + r + originy);
28 size_t eptr = x + xmin + originx;
29 for(int32_t c = xmin; c < xmax; c++, eptr++)
30 if(r < thickness || c < thickness || r >= height - thickness ||
31 c >= width - thickness)
32 outline.apply(rptr[eptr]);
33 else
34 fill.apply(rptr[eptr]);
37 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
38 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
39 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
40 private:
41 int32_t x;
42 int32_t y;
43 int32_t width;
44 int32_t height;
45 framebuffer::color outline;
46 framebuffer::color fill;
47 int32_t thickness;
50 lua::fnptr2 gui_rectangle(lua_func_misc, "gui.rectangle", [](lua::state& L, lua::parameters& P)
51 -> int {
52 if(!lua_render_ctx)
53 return 0;
54 auto x = P.arg<int32_t>();
55 auto y = P.arg<int32_t>();
56 auto width = P.arg<uint32_t>();
57 auto height = P.arg<uint32_t>();
58 auto thickness = P.arg_opt<uint32_t>(1);
59 auto poutline = P.arg_opt<framebuffer::color>(0xFFFFFFU);
60 auto pfill = P.arg_opt<framebuffer::color>(-1);
61 lua_render_ctx->queue->create_add<render_object_rectangle>(x, y, width, height, poutline, pfill,
62 thickness);
63 return 0;
64 });