Refactor some generic reading/writing routines out of moviefile.cpp
[lsnes.git] / src / lua / gui-circle.cpp
blobb9dfeea0a0e42c310b4154cfa57a640042e52677
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) -> int {
63 int32_t x, y;
64 uint32_t radius, thickness;
65 framebuffer::color poutline, pfill;
67 if(!lua_render_ctx) return 0;
69 P(x, y, radius, P.optional(thickness, 1), P.optional(poutline, 0xFFFFFFU), P.optional(pfill, -1));
71 lua_render_ctx->queue->create_add<render_object_circle>(x, y, radius, poutline, pfill, thickness);
72 return 0;
73 });