1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
6 struct render_object_line
: public render_object
8 render_object_line(int32_t _x1
, int32_t _x2
, int32_t _y1
, int32_t _y2
, premultiplied_color _color
)
10 : x1(_x1
), y1(_y1
), x2(_x2
), y2(_y2
), color(_color
) {}
11 ~render_object_line() throw() {}
12 template<bool X
> void op(struct framebuffer
<X
>& scr
) throw()
14 size_t swidth
= scr
.get_width();
15 size_t sheight
= scr
.get_height();
16 int32_t _x1
= x1
+ scr
.get_origin_x();
17 int32_t _x2
= x2
+ scr
.get_origin_x();
18 int32_t _y1
= y1
+ scr
.get_origin_y();
19 int32_t _y2
= y2
+ scr
.get_origin_y();
20 color
.set_palette(scr
);
21 int32_t xdiff
= _x2
- _x1
;
22 int32_t ydiff
= _y2
- _y1
;
30 //Swap points so that x1 < x2.
34 //The slope of the line is (y2 - y1) / (x2 - x1) = +-ydiff / xdiff
37 for(int32_t x
= _x1
; x
<= _x2
; x
++) {
38 if(x
< 0 || static_cast<uint32_t>(x
) >= swidth
)
40 if(y
< 0 || static_cast<uint32_t>(y
) >= sheight
)
42 color
.apply(scr
.rowptr(y
)[x
]);
56 //Swap points so that y1 < y2.
60 //The slope of the line is (x2 - x1) / (y2 - y1) = +-xdiff / ydiff
63 for(int32_t y
= _y1
; y
<= _y2
; y
++) {
64 if(x
< 0 || static_cast<uint32_t>(x
) >= swidth
)
66 if(y
< 0 || static_cast<uint32_t>(y
) >= sheight
)
68 color
.apply(scr
.rowptr(y
)[x
]);
81 void operator()(struct framebuffer
<true>& scr
) throw() { op(scr
); }
82 void operator()(struct framebuffer
<false>& scr
) throw() { op(scr
); }
83 void clone(render_queue
& q
) const throw(std::bad_alloc
) { q
.clone_helper(this); }
89 premultiplied_color color
;
92 function_ptr_luafun
gui_pixel(LS
, "gui.line", [](lua_state
& L
, const std::string
& fname
) -> int {
95 int64_t color
= 0xFFFFFFU
;
96 int32_t x1
= L
.get_numeric_argument
<int32_t>(1, fname
.c_str());
97 int32_t y1
= L
.get_numeric_argument
<int32_t>(2, fname
.c_str());
98 int32_t x2
= L
.get_numeric_argument
<int32_t>(3, fname
.c_str());
99 int32_t y2
= L
.get_numeric_argument
<int32_t>(4, fname
.c_str());
100 L
.get_numeric_argument
<int64_t>(5, color
, fname
.c_str());
101 premultiplied_color
pcolor(color
);
102 lua_render_ctx
->queue
->create_add
<render_object_line
>(x1
, x2
, y1
, y2
, pcolor
);