Get rid of DECLARE_LUACLASS
[lsnes.git] / src / lua / gui-rqueue.cpp
blob079dc3a4edf6f4a0830549104b9771008e1aa447
1 #include "core/framebuffer.hpp"
2 #include "lua/internal.hpp"
3 #include "library/framebuffer.hpp"
5 namespace
7 lua_render_context* saved = NULL;
8 lua_render_context* last = NULL;
9 bool redirect = false;
11 struct render_queue_obj
13 render_queue_obj(lua_state& L, uint32_t width, uint32_t height) throw()
15 lctx.left_gap = std::numeric_limits<uint32_t>::max();
16 lctx.right_gap = std::numeric_limits<uint32_t>::max();
17 lctx.bottom_gap = std::numeric_limits<uint32_t>::max();
18 lctx.top_gap = std::numeric_limits<uint32_t>::max();
19 lctx.queue = &rqueue;
20 lctx.width = width;
21 lctx.height = height;
23 ~render_queue_obj() throw() {}
24 lua_render_context* get() { return &lctx; }
25 std::string print()
27 size_t s = rqueue.get_object_count();
28 return (stringfmt() << s << " " << ((s != 1) ? "objects" : "object")).str();
30 private:
31 framebuffer::queue rqueue;
32 lua_render_context lctx;
35 function_ptr_luafun gui_rq_run(lua_func_misc, "gui.renderq_run", [](lua_state& L, const std::string& fname)
36 -> int {
37 if(!lua_render_ctx)
38 return 0;
39 if(lua_class<render_queue_obj>::is(L, 1)) {
40 lua_class<render_queue_obj>::get(L, 1, fname.c_str());
41 auto q = lua_class<render_queue_obj>::pin(L, 1, fname.c_str());
42 lua_render_context* ptr = q->get();
43 if(ptr->top_gap != std::numeric_limits<uint32_t>::max())
44 lua_render_ctx->top_gap = ptr->top_gap;
45 if(ptr->right_gap != std::numeric_limits<uint32_t>::max())
46 lua_render_ctx->right_gap = ptr->right_gap;
47 if(ptr->bottom_gap != std::numeric_limits<uint32_t>::max())
48 lua_render_ctx->bottom_gap = ptr->bottom_gap;
49 if(ptr->left_gap != std::numeric_limits<uint32_t>::max())
50 lua_render_ctx->left_gap = ptr->left_gap;
51 lua_render_ctx->queue->copy_from(*ptr->queue);
52 } else
53 throw std::runtime_error("Expected RENDERCTX as argument 1 for gui.renderq_run.");
54 return 0;
55 });
57 function_ptr_luafun gui_srepaint(lua_func_misc, "gui.synchronous_repaint", [](lua_state& L,
58 const std::string& fname) -> int {
59 if(lua_class<render_queue_obj>::is(L, 1)) {
60 lua_class<render_queue_obj>::get(L, 1, fname.c_str());
61 auto q = lua_class<render_queue_obj>::pin(L, 1, fname.c_str());
62 synchronous_paint_ctx = &*q;
63 redraw_framebuffer();
64 } else
65 throw std::runtime_error("Expected RENDERCTX as argument 1 for gui.renderq_run.");
66 return 0;
67 });
69 function_ptr_luafun gui_rq_clear(lua_func_misc, "gui.renderq_clear", [](lua_state& L,
70 const std::string& fname) -> int {
71 if(lua_class<render_queue_obj>::is(L, 1)) {
72 lua_class<render_queue_obj>::get(L, 1, fname.c_str());
73 auto q = lua_class<render_queue_obj>::pin(L, 1, fname.c_str());
74 lua_render_context* ptr = q->get();
75 ptr->top_gap = std::numeric_limits<uint32_t>::max();
76 ptr->right_gap = std::numeric_limits<uint32_t>::max();
77 ptr->bottom_gap = std::numeric_limits<uint32_t>::max();
78 ptr->left_gap = std::numeric_limits<uint32_t>::max();
79 ptr->queue->clear();
80 } else
81 throw std::runtime_error("Expected RENDERCTX as argument 1 for gui.renderq_clear.");
82 return 0;
83 });
85 function_ptr_luafun gui_rq_new(lua_func_misc, "gui.renderq_new", [](lua_state& L, const std::string& fname)
86 -> int {
87 int32_t x = L.get_numeric_argument<int32_t>(1, fname.c_str());
88 int32_t y = L.get_numeric_argument<int32_t>(2, fname.c_str());
89 lua_class<render_queue_obj>::create(L, x, y);
90 return 1;
91 });
93 function_ptr_luafun gui_rq_set(lua_func_misc, "gui.renderq_set", [](lua_state& L, const std::string& fname)
94 -> int {
95 if(lua_class<render_queue_obj>::is(L, 1)) {
96 lua_class<render_queue_obj>::get(L, 1, fname.c_str());
97 auto q = lua_class<render_queue_obj>::pin(L, 1, fname.c_str());
98 lua_render_context* ptr = q->get();
99 if(!redirect || last != lua_render_ctx)
100 saved = lua_render_ctx;
101 lua_render_ctx = last = ptr;
102 redirect = true;
103 } else if(L.type(1) == LUA_TNIL) {
104 if(redirect && last == lua_render_ctx)
105 //If there is valid redirect, undo it.
106 lua_render_ctx = saved;
107 redirect = false;
108 last = NULL;
109 saved = NULL;
110 } else
111 throw std::runtime_error("Expected RENDERCTX or nil as argument 1 for gui.renderq_set.");
112 return 0;
115 lua_class<render_queue_obj> class_render_queue_obj("RENDERCTX");
118 void lua_renderq_run(lua_render_context* ctx, void* _sctx)
120 render_queue_obj* sctx = (render_queue_obj*)_sctx;
121 lua_render_context* ptr = sctx->get();
122 if(ptr->top_gap != std::numeric_limits<uint32_t>::max())
123 ctx->top_gap = ptr->top_gap;
124 if(ptr->right_gap != std::numeric_limits<uint32_t>::max())
125 ctx->right_gap = ptr->right_gap;
126 if(ptr->bottom_gap != std::numeric_limits<uint32_t>::max())
127 ctx->bottom_gap = ptr->bottom_gap;
128 if(ptr->left_gap != std::numeric_limits<uint32_t>::max())
129 ctx->left_gap = ptr->left_gap;
130 ctx->queue->copy_from(*ptr->queue);