mkxp-z/binding-sandbox/sprite-binding.cpp
刘皓 b8d785b7e1
Implement growing VM memory in libretro builds
The binding coroutines in libretro builds are constructed on the VM
stack, so reallocating the VM memory would corrupt the memory of any
currently existing coroutines.

I've changed it so that the coroutines are no longer constructed on the
VM stack so that they're unaffected by VM memory reallocations, and
added a "slot" mechanism for storing variables on the VM stack. (Any
Ruby `VALUE`s used by a coroutine have to be stored on the VM stack so
that the Ruby garbage collector doesn't free them while they're being
used, which is why the slot mechanism is necessary.)
2025-05-09 22:49:13 -04:00

140 lines
6 KiB
C++

/*
** sprite-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 - 2021 Amaryllis Kulla <ancurio@mapleshrine.eu>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sprite-binding.h"
#include "disposable-binding.h"
#include "etc-binding.h"
#include "flashable-binding.h"
#include "viewportelement-binding.h"
#include "sprite.h"
using namespace mkxp_sandbox;
VALUE mkxp_sandbox::sprite_class;
static struct bindings::rb_data_type sprite_type;
SANDBOX_DEF_ALLOC(sprite_type);
static VALUE initialize(int32_t argc, wasm_ptr_t argv, VALUE self) {
struct coro : boost::asio::coroutine {
VALUE operator()(int32_t argc, wasm_ptr_t argv, VALUE self) {
BOOST_ASIO_CORO_REENTER (this) {
GFX_LOCK;
SANDBOX_AWAIT(viewportelement_initialize<Sprite>, argc, argv, self);
SANDBOX_AWAIT(wrap_property, self, &get_private_data<Sprite>(self)->getSrcRect(), "src_rect", rect_class);
SANDBOX_AWAIT(wrap_property, self, &get_private_data<Sprite>(self)->getColor(), "color", color_class);
SANDBOX_AWAIT(wrap_property, self, &get_private_data<Sprite>(self)->getTone(), "tone", tone_class);
GFX_UNLOCK;
}
return SANDBOX_NIL;
}
};
return sb()->bind<struct coro>()()(argc, argv, self);
}
SANDBOX_DEF_GFX_PROP_OBJ_REF(Sprite, Bitmap, Bitmap, bitmap);
SANDBOX_DEF_GFX_PROP_OBJ_VAL(Sprite, Rect, SrcRect, src_rect);
SANDBOX_DEF_GFX_PROP_I(Sprite, X, x);
SANDBOX_DEF_GFX_PROP_I(Sprite, Y, y);
SANDBOX_DEF_GFX_PROP_I(Sprite, OX, ox);
SANDBOX_DEF_GFX_PROP_I(Sprite, OY, oy);
SANDBOX_DEF_GFX_PROP_F(Sprite, ZoomX, zoom_x);
SANDBOX_DEF_GFX_PROP_F(Sprite, ZoomY, zoom_y);
SANDBOX_DEF_GFX_PROP_F(Sprite, Angle, angle);
SANDBOX_DEF_GFX_PROP_B(Sprite, Mirror, mirror);
SANDBOX_DEF_GFX_PROP_I(Sprite, BushDepth, bush_depth);
SANDBOX_DEF_GFX_PROP_I(Sprite, Opacity, opacity);
SANDBOX_DEF_GFX_PROP_I(Sprite, BlendType, blend_type);
SANDBOX_DEF_GFX_PROP_OBJ_VAL(Sprite, Color, Color, color);
SANDBOX_DEF_GFX_PROP_OBJ_VAL(Sprite, Tone, Tone, tone);
static VALUE width(VALUE self) {
return sb()->bind<struct rb_ll2inum>()()(get_private_data<Sprite>(self)->getWidth());
}
static VALUE height(VALUE self) {
return sb()->bind<struct rb_ll2inum>()()(get_private_data<Sprite>(self)->getHeight());
}
SANDBOX_DEF_GFX_PROP_I(Sprite, BushOpacity, bush_opacity);
SANDBOX_DEF_GFX_PROP_OBJ_REF(Sprite, Bitmap, Pattern, pattern);
SANDBOX_DEF_GFX_PROP_I(Sprite, PatternBlendType, pattern_blend_type);
SANDBOX_DEF_GFX_PROP_B(Sprite, PatternTile, pattern_tile);
SANDBOX_DEF_GFX_PROP_I(Sprite, PatternOpacity, pattern_opacity);
SANDBOX_DEF_GFX_PROP_I(Sprite, PatternScrollX, pattern_scroll_x);
SANDBOX_DEF_GFX_PROP_I(Sprite, PatternScrollY, pattern_scroll_y);
SANDBOX_DEF_GFX_PROP_F(Sprite, PatternZoomX, pattern_zoom_x);
SANDBOX_DEF_GFX_PROP_F(Sprite, PatternZoomY, pattern_zoom_y);
SANDBOX_DEF_GFX_PROP_B(Sprite, Invert, invert);
SANDBOX_DEF_GFX_PROP_I(Sprite, WaveAmp, wave_amp);
SANDBOX_DEF_GFX_PROP_I(Sprite, WaveLength, wave_length);
SANDBOX_DEF_GFX_PROP_I(Sprite, WaveSpeed, wave_speed);
void sprite_binding_init::operator()() {
BOOST_ASIO_CORO_REENTER (this) {
sprite_type = sb()->rb_data_type("Sprite", nullptr, dfree<Sprite>, nullptr, nullptr, 0, 0, 0);
SANDBOX_AWAIT_R(sprite_class, rb_define_class, "Sprite", sb()->rb_cObject());
SANDBOX_AWAIT(rb_define_alloc_func, sprite_class, alloc);
SANDBOX_AWAIT(rb_define_method, sprite_class, "initialize", (VALUE (*)(ANYARGS))initialize, -1);
SANDBOX_AWAIT(disposable_binding_init<Sprite>, sprite_class);
SANDBOX_AWAIT(flashable_binding_init<Sprite>, sprite_class);
SANDBOX_AWAIT(viewportelement_binding_init<Sprite>, sprite_class);
SANDBOX_INIT_PROP_BIND(sprite_class, bitmap);
SANDBOX_INIT_PROP_BIND(sprite_class, src_rect);
SANDBOX_INIT_PROP_BIND(sprite_class, x);
SANDBOX_INIT_PROP_BIND(sprite_class, y);
SANDBOX_INIT_PROP_BIND(sprite_class, ox);
SANDBOX_INIT_PROP_BIND(sprite_class, oy);
SANDBOX_INIT_PROP_BIND(sprite_class, zoom_x);
SANDBOX_INIT_PROP_BIND(sprite_class, zoom_y);
SANDBOX_INIT_PROP_BIND(sprite_class, angle);
SANDBOX_INIT_PROP_BIND(sprite_class, mirror);
SANDBOX_INIT_PROP_BIND(sprite_class, bush_depth);
SANDBOX_INIT_PROP_BIND(sprite_class, opacity);
SANDBOX_INIT_PROP_BIND(sprite_class, blend_type);
SANDBOX_INIT_PROP_BIND(sprite_class, color);
SANDBOX_INIT_PROP_BIND(sprite_class, tone);
SANDBOX_AWAIT(rb_define_method, sprite_class, "width", (VALUE (*)(ANYARGS))width, 0);
SANDBOX_AWAIT(rb_define_method, sprite_class, "height", (VALUE (*)(ANYARGS))height, 0);
SANDBOX_INIT_PROP_BIND(sprite_class, bush_opacity);
SANDBOX_INIT_PROP_BIND(sprite_class, pattern);
SANDBOX_INIT_PROP_BIND(sprite_class, pattern_blend_type);
SANDBOX_INIT_PROP_BIND(sprite_class, pattern_tile);
SANDBOX_INIT_PROP_BIND(sprite_class, pattern_opacity);
SANDBOX_INIT_PROP_BIND(sprite_class, pattern_scroll_x);
SANDBOX_INIT_PROP_BIND(sprite_class, pattern_scroll_y);
SANDBOX_INIT_PROP_BIND(sprite_class, pattern_zoom_x);
SANDBOX_INIT_PROP_BIND(sprite_class, pattern_zoom_y);
SANDBOX_INIT_PROP_BIND(sprite_class, invert);
SANDBOX_INIT_PROP_BIND(sprite_class, wave_amp);
SANDBOX_INIT_PROP_BIND(sprite_class, wave_length);
SANDBOX_INIT_PROP_BIND(sprite_class, wave_speed);
}
}