// extern C++ void cocos_android_app_init(JNIEnv* env) { // Create the app instance here. VALUE app_klass = rb_const_get(rb_cObject, "Application"); assert(app_klass != Qnil); mc_Application *app = new mc_Application(); VALUE obj = rb_class_wrap_new((void *)app, app_klass); app->obj = rb_retain(obj); mc_application_instance = rb_retain(app->obj); }
static VALUE scene_schedule(VALUE rcv, SEL sel, int argc, VALUE *argv) { VALUE block = rb_current_block(); if (block == Qnil) { rb_raise(rb_eArgError, "block not given"); } block = rb_retain(block); // FIXME need release... VALUE interval = Qnil, repeat = Qnil, delay = Qnil; rb_scan_args(argc, argv, "12", &delay, &repeat, &interval); float interval_c = RTEST(interval) ? NUM2DBL(interval) : 0; unsigned int repeat_c = RTEST(repeat) ? NUM2LONG(repeat) : 0; float delay_c = NUM2DBL(delay); char key[100]; snprintf(key, sizeof key, "schedule_lambda_%p", (void *)block); SCENE(rcv)->schedule( [block](float delta) { VALUE delta_obj = DBL2NUM(delta); rb_block_call(block, 1, &delta_obj); }, interval_c, repeat_c, delay_c, key); return RSTRING_NEW(key); }
static VALUE application_instance(VALUE rcv, SEL sel) { if (mc_application_instance == Qnil) { VALUE obj = rb_class_wrap_new( (void *)cocos2d::Application::getInstance(), rb_cApplication); mc_application_instance = rb_retain(obj); } return mc_application_instance; }
static VALUE scene_alloc(VALUE rcv, SEL sel) { auto layer = mc_Scene::create(); auto scene = cocos2d::Scene::createWithPhysics(); scene->addChild(layer); layer->scene = scene; VALUE obj = rb_class_wrap_new((void *)layer, rcv); layer->obj = rb_retain(obj); return obj; }
static VALUE node_alloc(VALUE rcv, SEL sel) { struct ruby_css_node *node = (struct ruby_css_node *)malloc(sizeof(struct ruby_css_node)); assert(node != NULL); node->parent = Qnil; node->node = new_css_node(); assert(node->node != NULL); node->node->context = node; node->node->get_child = node_get_child; node->node->is_dirty = node_is_dirty; node->node->measure = NULL; node->children = rb_retain(rb_ary_new()); node->dirty = true; node->obj = rb_weak(rb_class_wrap_new(node, rcv)); return node->obj; }
static VALUE scene_on_contact_begin(VALUE rcv, SEL sel) { VALUE block = rb_current_block(); if (block == Qnil) { rb_raise(rb_eArgError, "block not given"); } block = rb_retain(block); // FIXME need release... auto listener = cocos2d::EventListenerPhysicsContact::create(); listener->onContactBegin = [block](cocos2d::PhysicsContact &contact) -> bool { // VALUE touch_obj = rb_class_wrap_new((void *)touch, // rb_cTouch); return RTEST(rb_block_call(block, 0, NULL)); }; return scene_add_listener(rcv, listener); }
static VALUE scene_on_touch_event(VALUE rcv, SEL sel, mc_Scene_EventType type) { VALUE block = rb_current_block(); if (block == Qnil) { rb_raise(rb_eArgError, "block not given"); } block = rb_retain(block); // FIXME need release... auto scene = SCENE(rcv); if (scene->touch_listener == NULL) { scene->touch_listener = cocos2d::EventListenerTouchOneByOne::create(); } else { scene->getEventDispatcher()->removeEventListener(scene->touch_listener); } auto lambda = [block](cocos2d::Touch *touch, cocos2d::Event *event) -> bool { VALUE touch_obj = rb_class_wrap_new((void *)touch, rb_cTouch); return RTEST(rb_block_call(block, 1, &touch_obj)); }; switch (type) { case ON_BEGIN: scene->touch_listener->onTouchBegan = lambda; break; case ON_MOVE: scene->touch_listener->onTouchMoved = lambda; break; case ON_END: scene->touch_listener->onTouchEnded = lambda; break; case ON_CANCEL: scene->touch_listener->onTouchCancelled = lambda; break; } return scene_add_listener(rcv, scene->touch_listener); }
static VALUE scene_on_accelerate(VALUE rcv, SEL sel) { #if CC_TARGET_OS_APPLETV rb_raise(rb_eRuntimeError, "Not supported in tvOS"); #else VALUE block = rb_current_block(); if (block == Qnil) { rb_raise(rb_eArgError, "block not given"); } block = rb_retain(block); // FIXME need release... cocos2d::Device::setAccelerometerEnabled(true); auto listener = cocos2d::EventListenerAcceleration::create( [block](cocos2d::Acceleration *acc, cocos2d::Event *event) { VALUE acc_obj = rb_class_wrap_new((void *)acc, rb_cAcceleration); rb_block_call(block, 1, &acc_obj); }); return scene_add_listener(rcv, listener); #endif }