summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2017-02-17 01:14:25 -0500
committerTom Black <[email protected]>2017-02-17 01:14:25 -0500
commit93a5c19c74347246cd3654b445b45cda6658d570 (patch)
treec10d4ffe5fb1695c95ef7e17aa693aa6e1905f60
parent1b4b40cc59bddd7e2f5db8e1617de9a7981d948a (diff)
downloadruby2d-93a5c19c74347246cd3654b445b45cda6658d570.tar.gz
ruby2d-93a5c19c74347246cd3654b445b45cda6658d570.zip
Update web native extension
Extend Ruby 2D classes with native methods, add new API features
-rw-r--r--ext/ruby2d/ruby2d-opal.rb141
1 files changed, 110 insertions, 31 deletions
diff --git a/ext/ruby2d/ruby2d-opal.rb b/ext/ruby2d/ruby2d-opal.rb
index ef33f9a..b86878a 100644
--- a/ext/ruby2d/ruby2d-opal.rb
+++ b/ext/ruby2d/ruby2d-opal.rb
@@ -2,8 +2,7 @@
$SELF = nil
-`
-// ruby2d.js
+`// ruby2d.js
// @type_id values for rendering
const $R2D_TRIANGLE = 1;
@@ -12,12 +11,38 @@ const $R2D_IMAGE = 3;
const $R2D_SPRITE = 4;
const $R2D_TEXT = 5;
+
+function on_key(e, key) {
+ switch (e) {
+ case S2D.KEYDOWN:
+ #{$SELF.key_down_callback(`key`)};
+ break;
+
+ case S2D.KEY:
+ #{$SELF.key_callback(`key`)};
+ break;
+
+ case S2D.KEYUP:
+ #{$SELF.key_up_callback(`key`)};
+ break;
+ }
+}
+
+
+function on_mouse(x, y) {
+ #{$SELF.mouse_callback("any", `x`, `y`)};
+}
+
+
function update() {
#{$SELF.mouse_x = `win.mouse.x`};
#{$SELF.mouse_y = `win.mouse.y`};
+ #{$SELF.frames = `win.frames`};
+ #{$SELF.fps = `win.fps`};
#{$SELF.update_callback};
}
+
function render() {
var objects = #{$SELF.objects};
@@ -47,53 +72,113 @@ function render() {
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;
+ el.data.color.r = el.color.r;
+ el.data.color.g = el.color.g;
+ el.data.color.b = el.color.b;
+ el.data.color.a = el.color.a;
+
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.ClipSprite(
+ el.data,
+ el.clip_x,
+ el.clip_y,
+ el.clip_w,
+ el.clip_h
+ );
+
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);
+ el.data.color.r = el.color.r;
+ el.data.color.g = el.color.g;
+ el.data.color.b = el.color.b;
+ el.data.color.a = el.color.a;
S2D.DrawText(el.data);
- */
break;
}
}
-}
-`
+}`
+
module Ruby2D
+
+ class Image
+ def init(path)
+ `#{self}.data = S2D.CreateImage(path);`
+ end
+ end
+
+ class Sprite
+ def init(path)
+ `#{self}.data = S2D.CreateSprite(path);`
+ end
+ end
+
+ class Text
+ def init
+ `#{self}.data = S2D.CreateText(#{self}.font, #{self}.text, #{self}.size);`
+ end
+
+ def text=(t)
+ @text = t
+ `S2D.SetText(#{self}.data, #{self}.text);`
+ end
+ end
+
+ class Sound
+ def init(path)
+ `#{self}.data = S2D.CreateSound(path);`
+ end
+
+ def play
+ `S2D.PlaySound(#{self}.data);`
+ end
+ end
+
+ class Music
+ def init(path)
+ `#{self}.data = S2D.CreateMusic(path);`
+ end
+
+ def play
+ `S2D.PlayMusic(#{self}.data, #{self}.loop);`
+ end
+
+ def pause
+ `S2D.PauseMusic();`
+ end
+
+ def resume
+ `S2D.ResumeMusic();`
+ end
+
+ def stop
+ `S2D.StopMusic();`
+ end
+
+ def fadeout(ms)
+ `S2D.FadeOutMusic(ms);`
+ end
+ end
+
class Window
def show
$SELF = self
@@ -104,18 +189,12 @@ module Ruby2D
`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;
- # `
+ win.on_key = on_key;
+ win.on_mouse = on_mouse;
- `S2D.Show(win);`
+ S2D.Show(win);`
end
end
end