Numeric range class, obsolete clip_range.
[lsnes.git] / src / lua / gui-circle.cpp
blob3f2e89c550301fde31bf106ac8a88c352e6c3dbc
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
3 #include "library/range.hpp"
4 #include "library/lua-framebuffer.hpp"
6 namespace
8 struct render_object_circle : public framebuffer::object
10 render_object_circle(int32_t _x, int32_t _y, uint32_t _radius,
11 framebuffer::color _outline, framebuffer::color _fill, uint32_t _thickness) throw()
12 : x(_x), y(_y), outline(_outline), fill(_fill)
14 radius = _radius;
15 radius2 = static_cast<uint64_t>(_radius) * _radius;
16 if(_thickness > _radius)
17 iradius2 = 0;
18 else
19 iradius2 = static_cast<uint64_t>(_radius - _thickness) *
20 (_radius - _thickness);
22 ~render_object_circle() throw() {}
23 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
25 uint32_t oX = x + scr.get_origin_x();
26 uint32_t oY = y + scr.get_origin_y();
27 range bX = (range::make_w(scr.get_width()) - oX) & range::make_b(-radius, radius + 1);
28 range bY = (range::make_w(scr.get_height()) - oY) & range::make_b(-radius, radius + 1);
29 for(uint32_t r = bY.low(); r != bY.high(); r++) {
30 uint64_t pd2 = static_cast<int64_t>(r) * r;
31 typename framebuffer::fb<X>::element_t* rptr = scr.rowptr(oY + r);
32 size_t eptr = oX + bX.low();
33 for(uint32_t c = bX.low(); c != bX.high(); c++, eptr++) {
34 uint64_t fd2 = pd2 + static_cast<int64_t>(c) * c;
35 if(fd2 > radius2)
36 continue;
37 else if(fd2 >= iradius2)
38 outline.apply(rptr[eptr]);
39 else
40 fill.apply(rptr[eptr]);
44 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
45 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
46 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
47 private:
48 int32_t x;
49 int32_t y;
50 int32_t radius;
51 uint64_t radius2;
52 uint64_t iradius2;
53 framebuffer::color outline;
54 framebuffer::color fill;
57 int circle(lua::state& L, lua::parameters& P)
59 int32_t x, y;
60 uint32_t radius, thickness;
61 framebuffer::color poutline, pfill;
63 if(!lua_render_ctx) return 0;
65 P(x, y, radius, P.optional(thickness, 1), P.optional(poutline, 0xFFFFFFU), P.optional(pfill, -1));
67 lua_render_ctx->queue->create_add<render_object_circle>(x, y, radius, poutline, pfill, thickness);
68 return 0;
71 lua::functions circle_fns(lua_func_misc, "gui", {
72 {"circle", circle},
73 });