Cleanup lua code by introducing lua::functions
[lsnes.git] / src / lua / gui-line.cpp
blobeae731767bac1a4c0d489281387cb844b2d74e29
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
3 #include "library/lua-framebuffer.hpp"
5 namespace
7 struct render_object_line : public framebuffer::object
9 render_object_line(int32_t _x1, int32_t _x2, int32_t _y1, int32_t _y2, framebuffer::color _color)
10 throw()
11 : x1(_x1), y1(_y1), x2(_x2), y2(_y2), color(_color) {}
12 ~render_object_line() throw() {}
13 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
15 size_t swidth = scr.get_width();
16 size_t sheight = scr.get_height();
17 int32_t _x1 = x1 + scr.get_origin_x();
18 int32_t _x2 = x2 + scr.get_origin_x();
19 int32_t _y1 = y1 + scr.get_origin_y();
20 int32_t _y2 = y2 + scr.get_origin_y();
21 color.set_palette(scr);
22 int32_t xdiff = _x2 - _x1;
23 int32_t ydiff = _y2 - _y1;
24 if(xdiff < 0)
25 xdiff = -xdiff;
26 if(ydiff < 0)
27 ydiff = -ydiff;
28 if(xdiff >= ydiff) {
29 //X-major line.
30 if(x2 < x1) {
31 //Swap points so that x1 < x2.
32 std::swap(_x1, _x2);
33 std::swap(_y1, _y2);
35 //The slope of the line is (y2 - y1) / (x2 - x1) = +-ydiff / xdiff
36 int32_t y = _y1;
37 int32_t ysub = 0;
38 for(int32_t x = _x1; x <= _x2; x++) {
39 if(x < 0 || static_cast<uint32_t>(x) >= swidth)
40 goto nodraw1;
41 if(y < 0 || static_cast<uint32_t>(y) >= sheight)
42 goto nodraw1;
43 color.apply(scr.rowptr(y)[x]);
44 nodraw1:
45 ysub += ydiff;
46 if(ysub >= xdiff) {
47 ysub -= xdiff;
48 if(_y2 > _y1)
49 y++;
50 else
51 y--;
54 } else {
55 //Y-major line.
56 if(_y2 < _y1) {
57 //Swap points so that y1 < y2.
58 std::swap(_x1, _x2);
59 std::swap(_y1, _y2);
61 //The slope of the line is (x2 - x1) / (y2 - y1) = +-xdiff / ydiff
62 int32_t x = _x1;
63 int32_t xsub = 0;
64 for(int32_t y = _y1; y <= _y2; y++) {
65 if(x < 0 || static_cast<uint32_t>(x) >= swidth)
66 goto nodraw2;
67 if(y < 0 || static_cast<uint32_t>(y) >= sheight)
68 goto nodraw2;
69 color.apply(scr.rowptr(y)[x]);
70 nodraw2:
71 xsub += xdiff;
72 if(xsub >= ydiff) {
73 xsub -= ydiff;
74 if(_x2 > _x1)
75 x++;
76 else
77 x--;
82 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
83 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
84 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
85 private:
86 int32_t x1;
87 int32_t y1;
88 int32_t x2;
89 int32_t y2;
90 framebuffer::color color;
93 int line(lua::state& L, lua::parameters& P)
95 int32_t x1, y1, x2, y2;
96 framebuffer::color pcolor;
98 if(!lua_render_ctx) return 0;
100 P(x1, y1, x2, y2, P.optional(pcolor, 0xFFFFFFU));
102 lua_render_ctx->queue->create_add<render_object_line>(x1, x2, y1, y2, pcolor);
103 return 0;
106 lua::functions line_fns(lua_func_misc, "gui", {
107 {"line", line},