cocos2d-x mruby学习笔记--扩展2(mruby code)
2015-11-11 18:36
435 查看
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 endlogcat 输出如下:
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]"
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories