Merge branch 'rr1-maint'
[lsnes.git] / src / lua / gui-line.cpp
blobd72b44f31e738251943965a3242daa3ad81e6765
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
4 namespace
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)
9 throw()
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;
23 if(xdiff < 0)
24 xdiff = -xdiff;
25 if(ydiff < 0)
26 ydiff = -ydiff;
27 if(xdiff >= ydiff) {
28 //X-major line.
29 if(x2 < x1) {
30 //Swap points so that x1 < x2.
31 std::swap(_x1, _x2);
32 std::swap(_y1, _y2);
34 //The slope of the line is (y2 - y1) / (x2 - x1) = +-ydiff / xdiff
35 int32_t y = _y1;
36 int32_t ysub = 0;
37 for(int32_t x = _x1; x <= _x2; x++) {
38 if(x < 0 || static_cast<uint32_t>(x) >= swidth)
39 goto nodraw1;
40 if(y < 0 || static_cast<uint32_t>(y) >= sheight)
41 goto nodraw1;
42 color.apply(scr.rowptr(y)[x]);
43 nodraw1:
44 ysub += ydiff;
45 if(ysub >= xdiff) {
46 ysub -= xdiff;
47 if(_y2 > _y1)
48 y++;
49 else
50 y--;
53 } else {
54 //Y-major line.
55 if(_x2 < _x1) {
56 //Swap points so that y1 < y2.
57 std::swap(_x1, _x2);
58 std::swap(_y1, _y2);
60 //The slope of the line is (x2 - x1) / (y2 - y1) = +-xdiff / ydiff
61 int32_t x = _x1;
62 int32_t xsub = 0;
63 for(int32_t y = _y1; y <= _y2; y++) {
64 if(x < 0 || static_cast<uint32_t>(x) >= swidth)
65 goto nodraw2;
66 if(y < 0 || static_cast<uint32_t>(y) >= sheight)
67 goto nodraw2;
68 color.apply(scr.rowptr(y)[x]);
69 nodraw2:
70 xsub += xdiff;
71 if(xsub >= ydiff) {
72 xsub -= ydiff;
73 if(_x2 > _x1)
74 x++;
75 else
76 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); }
84 private:
85 int32_t x1;
86 int32_t y1;
87 int32_t x2;
88 int32_t y2;
89 premultiplied_color color;
92 function_ptr_luafun gui_pixel(LS, "gui.line", [](lua_state& L, const std::string& fname) -> int {
93 if(!lua_render_ctx)
94 return 0;
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);
103 return 0;