cocos2d-x mruby学习笔记--扩展2(mruby code)

ruby gems 的风格是除了c扩展,还可以添加ruby扩展,为什么我们不试一试呢,定义module.rb如下
module CC

  class Vec2
      alias_method :native_to_s,:to_s
      def to_s
        "%s[x:%f,y:%f]"%[self.native_to_s,self.x,self.y]
      end
  end

  class Vec3
      alias_method :native_to_s,:to_s
      def to_s
        "%s[x:%f,y:%f,z:%f]"%[self.native_to_s,self.x,self.y,self.z]
      end
  end

  class Vec4
      alias_method :native_to_s,:to_s
      def to_s
        "%s[x:%f,y:%f,z:%f,w:%f]"%[self.native_to_s,self.x,self.y,self.z,self.w]
      end
  end

  class Size
     alias_method :native_to_s,:to_s
     def to_s
       "%s[width:%f,height:%f]"%[self.native_to_s,self.width,self.height]
     end
  end


  class Rect
    alias_method :native_contains_point,:contains_point
    alias_method :native_to_s,:to_s
 
    def x
      self.origin.x
    end

    def y
      self.origin.y
    end

    def width
      self.size.width
    end

    def height
      self.size.height
    end

    def contains_point(x,y=nil)
      
      if x.is_a?(Numeric) && y.is_a?(Numeric)
         native_contains_point(CC::Vec2.new(x,y))
      elsif x.is_a?(CC::Vec2)
         native_contains_point(x)
      else
        raise "unknow args ,expect (Numeric x,Numeric y) or (CC::Vec2 vec)" 
      end
    end

    def to_s
       "%s[x:%f,y:%f,width:%f,height:%f]"%[self.native_to_s,self.x,self.y,self.width,self.height]
    end

  end 


  class Color3B
    alias_method :native_to_s,:to_s
    def to_s
       "%s[r:0x%02X,g:0x%02X,b:0x%02X]"%[self.native_to_s,self.r,self.g,self.b]
    end
  end

  class Color4B
    alias_method :native_to_s,:to_s
    def to_s
       "%s[r:0x%02X,g:0x%02X,b:0x%02X,a:0x%02X]"%[self.native_to_s,self.r,self.g,self.b,self.a]
    end
  end

  class Color4F
    alias_method :native_to_s,:to_s
    def to_s
       "%s[r:%f,g:%f,b:%f,a:%f]"%[self.native_to_s,self.r,self.g,self.b,self.a]
    end
  end

end


然后我们就需要mrbc工具了,这个工具可以将ruby代码转换为java风格的字节码,比如

mrbc -BMODULE_CC_TO_S module.rb 

会生成C风格的字节数组

/* dumped in little endian order.
   use `mrbc -E` option for big endian CPU. */
#include <stdint.h>
const uint8_t
#if defined __GNUC__
__attribute__((aligned(4)))
#elif defined _MSC_VER
__declspec(align(4))
#endif
MODULE_CC_TO_S[] = {
0x45,0x54,0x49,0x52,0x30,0x30,0x30,0x33,0x7d,0xa5,0x00,0x00,0x0b,0x7e,0x4d,0x41,
0x54,0x5a,0x30,0x30,0x30,0x30,0x49,0x52,0x45,0x50,0x00,0x00,0x0b,0x12,0x30,0x30,
......
};


在任何你喜欢的地方。比如bool RubyEngine::init(void)

中添加

 int ai = mrb_gc_arena_save(_mrb);
    mrb_load_irep(_mrb, MODULE_CC_TO_S);
    if (_mrb->exc) {
        mrb_print_error(_mrb);
        exit(EXIT_FAILURE);
    }
    mrb_gc_arena_restore(_mrb, ai);

这样你就成功的扩展了自定义的一些方法,如果你不需要这样麻烦的话,也可以直接require 'module.rb'来扩展,这儿仅仅是一个示例。表示我们使用mruby代码能成功的扩展内置功能


好了。我们来试一试吧

 begin
     size=CC::Size.new(200,200.1)
     p size.to_s
     p CC::Vec3.new(100,100,100).to_s
     p CC::Vec4.new(100,100,100,100).to_s
     vec2=CC::Vec2.new(50,50)
     rect=CC::Rect.new(10,10,100,100)
     p rect.to_s
     p "contains_point(%d,%d) %s"%[vec2.x,vec2.y,rect.contains_point(vec2.x,vec2.y) ]
     p "contains_point(%s) %s"%[vec2.to_s, rect.contains_point(vec2) ]

     p CC::Color3B.new(0x00,0xff,0xcc).to_s
     p CC::Color4B.new(0x00,0xff,0xcc,0xdd).to_s
     p CC::Color4F.new(0x00,0xff,0xcc,0xdd).to_s
rescue RuntimeError=>exp
  p exp.message
rescue StandardError=>exp
  p exp.message
end


logcat 输出如下:

D/cocos2d-x debug info(14126): [RUBY] "#<CC::Size:0x7aa03f20>[width:200.000000,height:200.100006]"
D/cocos2d-x debug info(14126): [RUBY] "#<CC::Vec3:0x7aa03de8>[x:100.000000,y:100.000000,z:100.000000]"
D/cocos2d-x debug info(14126): [RUBY] "#<CC::Vec4:0x7aa03cc8>[x:100.000000,y:100.000000,z:100.000000,w:100.000000]"
D/cocos2d-x debug info(14126): [RUBY] "#<CC::Rect:0x7aa03b78>[x:10.000000,y:10.000000,width:100.000000,height:100.000000]
D/cocos2d-x debug info(14126): [RUBY] "contains_point(50,50) true"
D/cocos2d-x debug info(14126): [RUBY] "contains_point(#<CC::Vec2:0x7aa03b90>[x:50.000000,y:50.000000]) true"
D/cocos2d-x debug info(14126): [RUBY] "#<CC::Color3B:0x78fc9ed0>[r:0x00,g:0xFF,b:0xCC]"
D/cocos2d-x debug info(14126): [RUBY] "#<CC::Color4B:0x78fc9cd8>[r:0x00,g:0xFF,b:0xCC,a:0xDD]"
D/cocos2d-x debug info(14126): [RUBY] "#<CC::Color4F:0x78fc99d8>[r:0.000000,g:255.000000,b:204.000000,a:221.000000]"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值