Lua: Multi-argument parsing and unify color parsing
[lsnes.git] / src / lua / gui-circle.cpp
blobc78edfb8d939cbe159614c71e057c054767e0b46
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
3 #include "library/lua-framebuffer.hpp"
5 namespace
7 struct render_object_circle : public framebuffer::object
9 render_object_circle(int32_t _x, int32_t _y, uint32_t _radius,
10 framebuffer::color _outline, framebuffer::color _fill, uint32_t _thickness) throw()
11 : x(_x), y(_y), outline(_outline), fill(_fill)
13 radius = _radius;
14 radius2 = static_cast<uint64_t>(_radius) * _radius;
15 if(_thickness > _radius)
16 iradius2 = 0;
17 else
18 iradius2 = static_cast<uint64_t>(_radius - _thickness) *
19 (_radius - _thickness);
21 ~render_object_circle() throw() {}
22 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
24 outline.set_palette(scr);
25 fill.set_palette(scr);
26 uint32_t originx = scr.get_origin_x();
27 uint32_t originy = scr.get_origin_y();
28 int32_t xmin = -radius;
29 int32_t xmax = radius;
30 int32_t ymin = -radius;
31 int32_t ymax = radius;
32 framebuffer::clip_range(originx, scr.get_width(), x, xmin, xmax);
33 framebuffer::clip_range(originy, scr.get_height(), y, ymin, ymax);
34 for(int32_t r = ymin; r < ymax; r++) {
35 uint64_t pd2 = static_cast<int64_t>(r) * r;
36 typename framebuffer::fb<X>::element_t* rptr = scr.rowptr(y + r + originy);
37 size_t eptr = x + xmin + originx;
38 for(int32_t c = xmin; c < xmax; c++, eptr++) {
39 uint64_t fd2 = pd2 + static_cast<int64_t>(c) * c;
40 if(fd2 > radius2)
41 continue;
42 else if(fd2 >= iradius2)
43 outline.apply(rptr[eptr]);
44 else
45 fill.apply(rptr[eptr]);
49 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
50 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
51 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
52 private:
53 int32_t x;
54 int32_t y;
55 int32_t radius;
56 uint64_t radius2;
57 uint64_t iradius2;
58 framebuffer::color outline;
59 framebuffer::color fill;
62 lua::fnptr2 gui_rectangle(lua_func_misc, "gui.circle", [](lua::state& L, lua::parameters& P)
63 -> int {
64 if(!lua_render_ctx)
65 return 0;
66 auto x = P.arg<int32_t>();
67 auto y = P.arg<int32_t>();
68 auto radius = P.arg<uint32_t>();
69 uint32_t thickness = P.arg_opt<uint32_t>(1);
70 auto poutline = P.arg_opt<framebuffer::color>(0xFFFFFFU);
71 auto pfill = P.arg_opt<framebuffer::color>(-1);
72 lua_render_ctx->queue->create_add<render_object_circle>(x, y, radius, poutline, pfill, thickness);
73 return 0;
74 });