lsnes rr2-β24
[lsnes.git] / src / lua / gui-rectangle.cpp
blobbefbac3e0b8d4f61785d9bbcc17f18de5fc1fd25
1 #include "core/instance.hpp"
2 #include "lua/internal.hpp"
3 #include "library/framebuffer.hpp"
4 #include "library/range.hpp"
5 #include "library/lua-framebuffer.hpp"
7 namespace
9 struct render_object_rectangle : public framebuffer::object
11 render_object_rectangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height,
12 framebuffer::color _outline, framebuffer::color _fill, int32_t _thickness) throw()
13 : x(_x), y(_y), width(_width), height(_height), outline(_outline), fill(_fill),
14 thickness(_thickness) {}
15 ~render_object_rectangle() throw() {}
16 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
18 uint32_t oX = x + scr.get_origin_x();
19 uint32_t oY = y + scr.get_origin_y();
20 range bX = (range::make_w(scr.get_width()) - oX) & range::make_w(width);
21 range bY = (range::make_w(scr.get_height()) - oY) & range::make_w(height);
22 for(uint32_t r = bY.low(); r != bY.high(); r++) {
23 typename framebuffer::fb<X>::element_t* rptr = scr.rowptr(oY + r);
24 size_t eptr = oX + bX.low();
25 for(uint32_t c = bX.low(); c != bX.high(); c++, eptr++)
26 if(r < thickness || c < thickness || r >= height - thickness ||
27 c >= width - thickness)
28 outline.apply(rptr[eptr]);
29 else
30 fill.apply(rptr[eptr]);
33 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
34 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
35 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
36 private:
37 int32_t x;
38 int32_t y;
39 uint32_t width;
40 uint32_t height;
41 framebuffer::color outline;
42 framebuffer::color fill;
43 uint32_t thickness;
46 int rectangle(lua::state& L, lua::parameters& P)
48 auto& core = CORE();
49 int32_t x, y;
50 uint32_t width, height, thickness;
51 framebuffer::color poutline, pfill;
53 if(!core.lua2->render_ctx) return 0;
55 P(x, y, width, height, P.optional(thickness, 1), P.optional(poutline, 0xFFFFFFU),
56 P.optional(pfill, -1));
58 core.lua2->render_ctx->queue->create_add<render_object_rectangle>(x, y, width, height, poutline,
59 pfill, thickness);
60 return 0;
63 int srectangle(lua::state& L, lua::parameters& P)
65 auto& core = CORE();
66 int32_t x, y;
67 uint32_t width, height;
68 framebuffer::color pcolor;
70 if(!core.lua2->render_ctx) return 0;
72 P(x, y, width, height, P.optional(pcolor, 0xFFFFFFU));
74 core.lua2->render_ctx->queue->create_add<render_object_rectangle>(x, y, width, height, pcolor, pcolor,
75 0);
76 return 0;
79 lua::functions LUA_rectangle_fns(lua_func_misc, "gui", {
80 {"rectangle", rectangle},
81 {"solidrectangle", srectangle},
82 });