Merge branch 'rr1-maint'
[lsnes.git] / src / lua / gui-rqueue.cpp
blob2e9ef2222aa93ddc81f7a84c9b828bd38d1289c1
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
4 namespace
6 lua_render_context* saved = NULL;
7 lua_render_context* last = NULL;
8 bool redirect = false;
10 struct render_queue_obj
12 render_queue_obj(uint32_t width, uint32_t height) throw()
14 lctx.left_gap = std::numeric_limits<uint32_t>::max();
15 lctx.right_gap = std::numeric_limits<uint32_t>::max();
16 lctx.bottom_gap = std::numeric_limits<uint32_t>::max();
17 lctx.top_gap = std::numeric_limits<uint32_t>::max();
18 lctx.queue = &rqueue;
19 lctx.width = 512;
20 lctx.height = 448;
22 ~render_queue_obj() throw() {}
23 lua_render_context* get() { return &lctx; }
24 private:
25 render_queue rqueue;
26 lua_render_context lctx;
29 function_ptr_luafun gui_rq_run(LS, "gui.renderq_run", [](lua_state& L, const std::string& fname) -> int {
30 if(!lua_render_ctx)
31 return 0;
32 if(lua_class<render_queue_obj>::is(L, 1)) {
33 lua_class<render_queue_obj>::get(L, 1, fname.c_str());
34 auto q = lua_class<render_queue_obj>::pin(L, 1, fname.c_str());
35 lua_render_context* ptr = q->object()->get();
36 if(ptr->top_gap != std::numeric_limits<uint32_t>::max())
37 lua_render_ctx->top_gap = ptr->top_gap;
38 if(ptr->right_gap != std::numeric_limits<uint32_t>::max())
39 lua_render_ctx->right_gap = ptr->right_gap;
40 if(ptr->bottom_gap != std::numeric_limits<uint32_t>::max())
41 lua_render_ctx->bottom_gap = ptr->bottom_gap;
42 if(ptr->left_gap != std::numeric_limits<uint32_t>::max())
43 lua_render_ctx->left_gap = ptr->left_gap;
44 lua_render_ctx->queue->copy_from(*ptr->queue);
45 } else {
46 L.pushstring("Expected RENDERCTX as argument 1 for gui.renderq_run.");
47 L.error();
49 return 0;
50 });
52 function_ptr_luafun gui_rq_clear(LS, "gui.renderq_clear", [](lua_state& L, const std::string& fname) -> int {
53 if(lua_class<render_queue_obj>::is(L, 1)) {
54 lua_class<render_queue_obj>::get(L, 1, fname.c_str());
55 auto q = lua_class<render_queue_obj>::pin(L, 1, fname.c_str());
56 lua_render_context* ptr = q->object()->get();
57 ptr->top_gap = std::numeric_limits<uint32_t>::max();
58 ptr->right_gap = std::numeric_limits<uint32_t>::max();
59 ptr->bottom_gap = std::numeric_limits<uint32_t>::max();
60 ptr->left_gap = std::numeric_limits<uint32_t>::max();
61 ptr->queue->clear();
62 } else {
63 L.pushstring("Expected RENDERCTX as argument 1 for gui.renderq_clear.");
64 L.error();
66 return 0;
67 });
69 function_ptr_luafun gui_rq_new(LS, "gui.renderq_new", [](lua_state& L, const std::string& fname) -> int {
70 int32_t x = L.get_numeric_argument<int32_t>(1, fname.c_str());
71 int32_t y = L.get_numeric_argument<int32_t>(2, fname.c_str());
72 lua_class<render_queue_obj>::create(L, x, y);
73 return 1;
74 });
76 function_ptr_luafun gui_rq_set(LS, "gui.renderq_set", [](lua_state& L, const std::string& fname) -> int {
77 if(lua_class<render_queue_obj>::is(L, 1)) {
78 lua_class<render_queue_obj>::get(L, 1, fname.c_str());
79 auto q = lua_class<render_queue_obj>::pin(L, 1, fname.c_str());
80 lua_render_context* ptr = q->object()->get();
81 if(!redirect || last != lua_render_ctx)
82 saved = lua_render_ctx;
83 lua_render_ctx = last = ptr;
84 redirect = true;
85 } else if(L.type(1) == LUA_TNIL) {
86 if(redirect && last == lua_render_ctx)
87 //If there is valid redirect, undo it.
88 lua_render_ctx = saved;
89 redirect = false;
90 last = NULL;
91 saved = NULL;
92 } else {
93 L.pushstring("Expected RENDERCTX or nil as argument 1 for gui.renderq_set.");
94 L.error();
96 return 0;
97 });
100 DECLARE_LUACLASS(render_queue_obj, "RENDERCTX");