1 #include "core/instance.hpp"
2 #include "lua/internal.hpp"
3 #include "library/framebuffer.hpp"
4 #include "library/lua-framebuffer.hpp"
8 struct render_object_line
: public framebuffer::object
10 render_object_line(int32_t _x1
, int32_t _x2
, int32_t _y1
, int32_t _y2
, framebuffer::color _color
)
12 : x1(_x1
), y1(_y1
), x2(_x2
), y2(_y2
), color(_color
) {}
13 ~render_object_line() throw() {}
14 template<bool X
> void op(struct framebuffer::fb
<X
>& scr
) throw()
16 size_t swidth
= scr
.get_width();
17 size_t sheight
= scr
.get_height();
18 int32_t _x1
= x1
+ scr
.get_origin_x();
19 int32_t _x2
= x2
+ scr
.get_origin_x();
20 int32_t _y1
= y1
+ scr
.get_origin_y();
21 int32_t _y2
= y2
+ scr
.get_origin_y();
22 int32_t xdiff
= _x2
- _x1
;
23 int32_t ydiff
= _y2
- _y1
;
31 //Swap points so that x1 < x2.
35 //The slope of the line is (y2 - y1) / (x2 - x1) = +-ydiff / xdiff
38 for(int32_t x
= _x1
; x
<= _x2
; x
++) {
39 if(x
< 0 || static_cast<uint32_t>(x
) >= swidth
)
41 if(y
< 0 || static_cast<uint32_t>(y
) >= sheight
)
43 color
.apply(scr
.rowptr(y
)[x
]);
57 //Swap points so that y1 < y2.
61 //The slope of the line is (x2 - x1) / (y2 - y1) = +-xdiff / ydiff
64 for(int32_t y
= _y1
; y
<= _y2
; y
++) {
65 if(x
< 0 || static_cast<uint32_t>(x
) >= swidth
)
67 if(y
< 0 || static_cast<uint32_t>(y
) >= sheight
)
69 color
.apply(scr
.rowptr(y
)[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 { q
.clone_helper(this); }
90 framebuffer::color color
;
93 int line(lua::state
& L
, lua::parameters
& P
)
96 int32_t x1
, y1
, x2
, y2
;
97 framebuffer::color pcolor
;
99 if(!core
.lua2
->render_ctx
) return 0;
101 P(x1
, y1
, x2
, y2
, P
.optional(pcolor
, 0xFFFFFFU
));
103 core
.lua2
->render_ctx
->queue
->create_add
<render_object_line
>(x1
, x2
, y1
, y2
, pcolor
);
107 lua::functions
LUA_line_fns(lua_func_misc
, "gui", {