summaryrefslogtreecommitdiffhomepage
path: root/ext
diff options
context:
space:
mode:
authorTom Black <[email protected]>2016-11-27 23:13:26 -0500
committerTom Black <[email protected]>2016-11-27 23:13:26 -0500
commit3e019cb28b2a1ea1e369b490c54adeba0011db6a (patch)
tree63e25e9dcd370a4d17d27fed7e3cb77a8a98c41a /ext
parent2b9aa6eeeb6d875f40807f7e0a2e35bec3690171 (diff)
downloadruby2d-3e019cb28b2a1ea1e369b490c54adeba0011db6a.tar.gz
ruby2d-3e019cb28b2a1ea1e369b490c54adeba0011db6a.zip
Initial Opal Support
Diffstat (limited to 'ext')
-rw-r--r--ext/ruby2d/ruby2d-opal.rb121
1 files changed, 121 insertions, 0 deletions
diff --git a/ext/ruby2d/ruby2d-opal.rb b/ext/ruby2d/ruby2d-opal.rb
new file mode 100644
index 0000000..ef33f9a
--- /dev/null
+++ b/ext/ruby2d/ruby2d-opal.rb
@@ -0,0 +1,121 @@
+# ruby2d-opal.rb
+
+$SELF = nil
+
+`
+// ruby2d.js
+
+// @type_id values for rendering
+const $R2D_TRIANGLE = 1;
+const $R2D_QUAD = 2;
+const $R2D_IMAGE = 3;
+const $R2D_SPRITE = 4;
+const $R2D_TEXT = 5;
+
+function update() {
+ #{$SELF.mouse_x = `win.mouse.x`};
+ #{$SELF.mouse_y = `win.mouse.y`};
+ #{$SELF.update_callback};
+}
+
+function render() {
+
+ var objects = #{$SELF.objects};
+
+ for (var i = 0; i < objects.length; i++) {
+
+ var el = objects[i];
+
+ switch (el.type_id) {
+
+ case $R2D_TRIANGLE:
+
+ S2D.DrawTriangle(
+ el.x1, el.y1, el.c1.r, el.c1.g, el.c1.b, el.c1.a,
+ el.x2, el.y2, el.c2.r, el.c2.g, el.c2.b, el.c2.a,
+ el.x3, el.y3, el.c3.r, el.c3.g, el.c3.b, el.c3.a
+ );
+ break;
+
+ case $R2D_QUAD:
+ S2D.DrawQuad(
+ el.x1, el.y1, el.c1.r, el.c1.g, el.c1.b, el.c1.a,
+ el.x2, el.y2, el.c2.r, el.c2.g, el.c2.b, el.c2.a,
+ el.x3, el.y3, el.c3.r, el.c3.g, el.c3.b, el.c3.a,
+ el.x4, el.y4, el.c4.r, el.c4.g, el.c4.b, el.c4.a
+ );
+ break;
+
+ case $R2D_IMAGE:
+ if (el.data == Opal.nil) {
+ el.data = S2D.CreateImage(el.path);
+ }
+
+ el.data.x = el.x;
+ el.data.y = el.y;
+
+ if (el.width != Opal.nil) el.data.width = el.width;
+ if (el.height != Opal.nil) el.data.height = el.height;
+
+ S2D.DrawImage(el.data);
+ break;
+
+ case $R2D_SPRITE:
+ /*
+ if (el.data == Opal.nil) {
+ el.data = S2D.CreateSprite(el.path);
+ }
+
+ el.data.x = el.x;
+ el.data.y = el.y;
+
+ S2D.DrawSprite(el.data);
+ */
+ break;
+
+ case $R2D_TEXT:
+ /*
+ if (el.data == Opal.nil) {
+ el.data = S2D.CreateText(el.font, el.text, el.size);
+ }
+
+ el.data.x = el.x;
+ el.data.y = el.y;
+
+ S2D_SetText(el.data, el.text);
+
+ S2D.DrawText(el.data);
+ */
+ break;
+ }
+
+ }
+}
+`
+
+module Ruby2D
+ class Window
+ def show
+ $SELF = self
+
+ if $SELF.diagnostics
+ # `S2D.Diagnostics(true);`
+ end
+
+ `win = S2D.CreateWindow(
+ #{$SELF.title}, #{$SELF.width}, #{$SELF.height}, update, render, "ruby2d-app", {}
+ );`
+
+ # TODO: Read flags, viewport w/h
+ # `
+ # win.viewport.width = #{$SELF.viewport_width};
+ # win.viewport.height = #{$SELF.viewport_height};
+ # win.on_key = on_key;
+ # win.on_mouse = on_mouse;
+ # win.on_controller = on_controller;
+ # `
+
+ `S2D.Show(win);`
+ end
+ end
+end