Don't assume that rshift=16, gshift=8, bshift=0 in blending code
[lsnes.git] / src / lua / gui-line.cpp
blobd01517cc560866181af139a20f63e9adef7f658c
1 #include "core/lua-int.hpp"
2 #include "core/render.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 void operator()(struct screen& scr) throw()
14 color.set_palette(scr);
15 int32_t xdiff = x2 - x1;
16 int32_t ydiff = y2 - y1;
17 if(xdiff < 0)
18 xdiff = -xdiff;
19 if(ydiff < 0)
20 ydiff = -ydiff;
21 if(xdiff >= ydiff) {
22 //X-major line.
23 if(x2 < x1) {
24 //Swap points so that x1 < x2.
25 std::swap(x1, x2);
26 std::swap(y1, y2);
28 //The slope of the line is (y2 - y1) / (x2 - x1) = +-ydiff / xdiff
29 int32_t y = y1;
30 int32_t ysub = 0;
31 for(int32_t x = x1; x <= x2; x++) {
32 if(x < 0 || static_cast<uint32_t>(x) >= scr.width)
33 goto nodraw1;
34 if(y < 0 || static_cast<uint32_t>(y) >= scr.height)
35 goto nodraw1;
36 color.apply(scr.rowptr(y)[x]);
37 nodraw1:
38 ysub += ydiff;
39 if(ysub >= xdiff) {
40 ysub -= xdiff;
41 if(y2 > y1)
42 y++;
43 else
44 y--;
47 } else {
48 //Y-major line.
49 if(x2 < x1) {
50 //Swap points so that y1 < y2.
51 std::swap(x1, x2);
52 std::swap(y1, y2);
54 //The slope of the line is (x2 - x1) / (y2 - y1) = +-xdiff / ydiff
55 int32_t x = x1;
56 int32_t xsub = 0;
57 for(int32_t y = y1; y <= y2; y++) {
58 if(x < 0 || static_cast<uint32_t>(x) >= scr.width)
59 goto nodraw2;
60 if(y < 0 || static_cast<uint32_t>(y) >= scr.height)
61 goto nodraw2;
62 color.apply(scr.rowptr(y)[x]);
63 nodraw2:
64 xsub += xdiff;
65 if(xsub >= ydiff) {
66 xsub -= ydiff;
67 if(x2 > x1)
68 x++;
69 else
70 x--;
75 private:
76 int32_t x1;
77 int32_t y1;
78 int32_t x2;
79 int32_t y2;
80 premultiplied_color color;
83 function_ptr_luafun gui_pixel("gui.line", [](lua_State* LS, const std::string& fname) -> int {
84 if(!lua_render_ctx)
85 return 0;
86 int64_t color = 0xFFFFFFU;
87 int32_t x1 = get_numeric_argument<int32_t>(LS, 1, fname.c_str());
88 int32_t y1 = get_numeric_argument<int32_t>(LS, 2, fname.c_str());
89 int32_t x2 = get_numeric_argument<int32_t>(LS, 3, fname.c_str());
90 int32_t y2 = get_numeric_argument<int32_t>(LS, 4, fname.c_str());
91 get_numeric_argument<int64_t>(LS, 5, color, fname.c_str());
92 premultiplied_color pcolor(color);
93 lua_render_ctx->queue->add(*new render_object_line(x1, x2, y1, y2, pcolor));
94 return 0;
95 });