lsnes rr2-β24
[lsnes.git] / src / lua / sysrc.lua
blob5b46ed6091f5730e64f5db95b24152075eb95810
1 loopwrapper = function(fn, ...)
2 local routine = coroutine.create(fn);
3 local resume = function(...)
4 if coroutine.status(routine) ~= "dead" then
5 local x, y;
6 x, y = coroutine.resume(routine, ...);
7 if not x then
8 error(y);
9 end
10 end
11 end
12 local yield = function()
13 return coroutine.yield(routine);
14 end
15 resume(yield, ...);
16 return resume;
17 end;
18 print=print2;
19 loadfile=loadfile2;
20 dofile=dofile2;
21 render_queue_function=function(rq)
22 local _rq = rq;
23 return function()
24 _rq:run();
25 end;
26 end;
27 string.byteU=_lsnes_string_byteU;
28 string.charU=_lsnes_string_charU;
29 string.regex=_lsnes_string_regex;
30 string.hex=_lsnes_string_hex;
31 string.lpad=_lsnes_string_lpad;
32 string.rpad=_lsnes_string_rpad;
34 local _lookup_class = lookup_class;
35 local _all_classes = all_classes;
36 local classes_meta = {
37 ["__newindex"] = function() error("Classes table is not writable"); end,
38 ["__index"] = function(a, b) return _lookup_class(b); end
40 classes = {};
41 setmetatable(classes, classes_meta);
43 local register_in = function(table, class)
44 local methods = {classes[class]._static_methods()};
45 local utable;
46 if table then
47 _G[table] = _G[table] or {};
48 utable = _G[table];
49 else
50 utable = _G;
51 end
52 local i;
53 for i=1,#methods do
54 local m = methods[i];
55 utable[m] = classes[class][m];
56 end
57 end
59 -- Classes
60 memory.address = classes.ADDRESS;
61 memory.mmap = classes.MMAP_STRUCT;
62 zip.writer = classes.ZIPWRITER;
63 gui.tiled_bitmap = classes.TILEMAP;
64 gui.renderctx = classes.RENDERCTX;
65 gui.palette = classes.PALETTE;
66 gui.bitmap = classes.BITMAP;
67 gui.dbitmap = classes.DBITMAP;
68 gui.image = classes.IMAGELOADER;
69 gui.font = classes.CUSTOMFONT;
70 iconv = classes.ICONV;
71 filereader = classes.FILEREADER;
73 -- Some ctors
74 memory2=classes.VMALIST.new();
75 callback=classes.CALLBACKS_LIST.new();
76 memory.mkaddr = classes.ADDRESS.new;
77 memory.map_structure=classes.MMAP_STRUCT.new;
78 memory.compare_new=classes.COMPARE_OBJ.new;
79 zip.create=classes.ZIPWRITER.new;
80 gui.tilemap=classes.TILEMAP.new;
81 gui.renderq_new=classes.RENDERCTX.new;
82 gui.palette_new=classes.PALETTE.new;
83 gui.font_new = classes.CUSTOMFONT.new;
84 gui.loadfont = classes.CUSTOMFONT.load;
85 iconv_new = classes.ICONV.new;
86 create_ibind = classes.INVERSEBIND.new;
87 create_command = classes.COMMANDBIND.new;
88 open_file = classes.FILEREADER.open;
90 local do_arg_err = function(what, n, name)
91 error("Expected "..what.." as argument #"..n.." of "..name);
92 end
94 local normal_method = {}
96 local normal_method = function(class, method, name, parent)
97 if type(class) == "string" then
98 normal_method[name] = normal_method[name] or {};
99 normal_method[name][class] = method;
100 else
101 normal_method[name] = normal_method[name] or {};
102 local k, v;
103 for k, v in pairs(class) do
104 normal_method[name][v] = method;
107 parent[name] = function(o, ...)
108 local m = normal_method[name];
109 local c = identify_class(o);
110 if m[c] then
111 return o[m[c]](o, ...);
112 else
113 local what = "";
114 local k, v;
115 for k, v in pairs(m) do
116 if what ~= "" then
117 what = what .. " or " .. k;
118 else
119 what = k;
122 do_arg_err(what, 1, name);
127 gui.renderq_set=function(o, ...)
128 if type(o) == "nil" then
129 return classes.RENDERCTX.setnull();
130 elseif identify_class(o) == "RENDERCTX" then
131 return o:set(...);
132 else
133 do_arg_err("RENDERCTX or nil", 1, "gui.renderq_set");
137 normal_method("RENDERCTX", "run", "renderq_run", gui);
138 normal_method("RENDERCTX", "synchronous_repaint", "synchronous_repaint", gui);
139 normal_method("RENDERCTX", "clear", "renderq_clear", gui);
141 gui.bitmap_new=function(w, h, type, ...)
142 if type==true then
143 return classes.DBITMAP.new(w,h,...);
144 elseif type==false then
145 return classes.BITMAP.new(w,h,...);
146 else
147 do_arg_err("boolean", 3, "gui.bitmap_new");
151 gui.bitmap_draw=function(x, y, o, ...)
152 if identify_class(o) == "BITMAP" then
153 return o:draw(x, y, ...);
154 elseif identify_class(o) == "DBITMAP" then
155 return o:draw(x, y, ...);
156 else
157 do_arg_err("BITMAP or DBITMAP", 3, "gui.bitmap_draw");
161 gui.bitmap_save_png=function(...)
162 local x = {...};
163 local i = 1;
164 local j;
165 local obj;
166 for j=1,#x do
167 if type(x[j]) ~= "string" then
168 obj = table.remove(x, j);
169 i = j;
170 break;
173 if identify_class(obj) == "BITMAP" then
174 return obj:save_png(unpack(x));
175 elseif identify_class(obj) == "DBITMAP" then
176 return obj:save_png(unpack(x));
177 else
178 do_arg_err("BITMAP or DBITMAP", i, "gui.bitmap_save_png");
182 gui.bitmap_load=classes.IMAGELOADER.load;
183 gui.bitmap_load_str=classes.IMAGELOADER.load_str;
184 gui.bitmap_load_png=classes.IMAGELOADER.load_png;
185 gui.bitmap_load_png_str=classes.IMAGELOADER.load_png_str;
187 normal_method("PALETTE", "set", "palette_set", gui);
188 normal_method("PALETTE", "hash", "palette_hash", gui);
189 normal_method("PALETTE", "debug", "palette_debug", gui);
190 normal_method({"BITMAP", "DBITMAP"}, "pset", "bitmap_pset", gui);
191 normal_method({"BITMAP", "DBITMAP"}, "pget", "bitmap_pget", gui);
192 normal_method({"BITMAP", "DBITMAP"}, "size", "bitmap_size", gui);
193 normal_method({"BITMAP", "DBITMAP"}, "hash", "bitmap_hash", gui);
194 normal_method({"BITMAP", "DBITMAP"}, "blit", "bitmap_blit", gui);
195 normal_method({"BITMAP", "DBITMAP"}, "blit_scaled", "bitmap_blit_scaled", gui);
196 normal_method({"BITMAP", "DBITMAP"}, "blit_porterduff", "bitmap_blit_porterduff", gui);
197 normal_method({"BITMAP", "DBITMAP"}, "blit_scaled_porterduff", "bitmap_blit_scaled_porterduff", gui);
198 normal_method("BITMAP", "blit_priority", "bitmap_blit_priority", gui);
199 normal_method("BITMAP", "blit_scaled_priority", "bitmap_blit_scaled_priority", gui);
200 normal_method({"DBITMAP", "PALETTE"}, "adjust_transparency", "adjust_transparency", gui);