Cleanup lua code by introducing lua::functions
[lsnes.git] / src / lua / gui-crosshair.cpp
blobf3488b6f653a4690585b118d26c6ebbf83eb60a3
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
3 #include "library/lua-framebuffer.hpp"
5 namespace
7 struct render_object_crosshair : public framebuffer::object
9 render_object_crosshair(int32_t _x, int32_t _y, framebuffer::color _color, uint32_t _length) throw()
10 : x(_x), y(_y), color(_color), length(_length) {}
11 ~render_object_crosshair() throw() {}
12 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
14 color.set_palette(scr);
15 uint32_t originx = scr.get_origin_x();
16 uint32_t originy = scr.get_origin_y();
17 int32_t xmin = -static_cast<int32_t>(length);
18 int32_t xmax = static_cast<int32_t>(length + 1);
19 int32_t ymin = -static_cast<int32_t>(length);
20 int32_t ymax = static_cast<int32_t>(length + 1);
21 framebuffer::clip_range(originx, scr.get_width(), x, xmin, xmax);
22 framebuffer::clip_range(originy, scr.get_height(), y, ymin, ymax);
23 if(xmin <= 0 && xmax > 0)
24 for(int32_t r = ymin; r < ymax; r++)
25 color.apply(scr.rowptr(y + r + originy)[x + originx]);
26 if(ymin <= 0 && ymax > 0)
27 for(int32_t r = xmin; r < xmax; r++)
28 color.apply(scr.rowptr(y + originy)[x + r + originx]);
30 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
31 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
32 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
33 private:
34 int32_t x;
35 int32_t y;
36 framebuffer::color color;
37 uint32_t length;
40 int crosshair(lua::state& L, lua::parameters& P)
42 int32_t x, y;
43 uint32_t length;
44 framebuffer::color pcolor;
46 if(!lua_render_ctx) return 0;
48 P(x, y, P.optional(length, 10), P.optional(pcolor, 0xFFFFFFU));
50 lua_render_ctx->queue->create_add<render_object_crosshair>(x, y, pcolor, length);
51 return 0;
54 lua::functions crosshair_fns(lua_func_misc, "gui", {
55 {"crosshair", crosshair},
56 });