From 3b714579a85658d6fb0d5eb5449ac0ab04cbdedc Mon Sep 17 00:00:00 2001 From: Inori Date: Fri, 6 Sep 2019 15:09:03 -0400 Subject: [PATCH] Add Bitmap.raw_data property --- README.md | 9 +++++++++ binding/audio-binding.cpp | 3 --- binding/bitmap-binding.cpp | 36 +++++++++++++++++++++++++++++++++--- src/bitmap.cpp | 15 +++++++++++++-- src/bitmap.h | 3 ++- src/discordstate.cpp | 4 ++-- 6 files changed, 59 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9b45a368..0bb5d7f6 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,15 @@ To smooth over cross-platform compatibility, functionality that you won't find i * RGSS2 Graphics functions and properties are now bound in RGSS1 mode. * The `Graphics` module has three additional properties: `fullscreen` represents the current fullscreen mode (`true` = fullscreen, `false` = windowed), `show_cursor` hides the system cursor inside the game window when `false`. `scale` represents the current scale factor of the screen, and can be set from `0.5` to `2`. * The `Graphics` module has two additional functions: `#screenshot(path)` will save a screenshot to `path` in BMP format. `#center` will move the window to the center of the screen. + +### Bitmap + +* The `Bitmap` class has one additional property: `raw_data` gets and sets the raw pixel data of a Bitmap in the form of a string. The string must be the size of the bitmap's `width*height*4`. If it is not, no error is raised, but the Bitmap will not be updated. + +### Audio + +* RGSS2 Audio functions and properties are now bound in RGSS1 mode. + ### Discord * This module is only included if the `discord_sdk_path` option is set to the location of the Discord GameSDK when building. diff --git a/binding/audio-binding.cpp b/binding/audio-binding.cpp index 00217423..e99796c4 100644 --- a/binding/audio-binding.cpp +++ b/binding/audio-binding.cpp @@ -137,13 +137,10 @@ audioBindingInit() BIND_PLAY_STOP_FADE( bgs ); BIND_PLAY_STOP_FADE( me ); - if (rgssVer >= 3) - { BIND_POS( bgm ); BIND_POS( bgs ); _rb_define_module_function(module, "setup_midi", audioSetupMidi); - } BIND_PLAY_STOP( se ) diff --git a/binding/bitmap-binding.cpp b/binding/bitmap-binding.cpp index 117bfd97..ece8f5a5 100644 --- a/binding/bitmap-binding.cpp +++ b/binding/bitmap-binding.cpp @@ -417,6 +417,36 @@ RB_METHOD(bitmapRadialBlur) return Qnil; } +RB_METHOD(bitmapGetRawData) +{ + RB_UNUSED_PARAM; + + Bitmap *b = getPrivateData(self); + + char *pixels{}; + int size{}; + + GUARD_EXC( pixels = (char*)b->getRaw(); ); + GUARD_EXC( size = b->width()*b->height()*4; ); + + return rb_str_new(pixels, size); +} + +RB_METHOD(bitmapSetRawData) +{ + RB_UNUSED_PARAM; + + VALUE str; + rb_get_args(argc, argv, "1", &str); + SafeStringValue(str); + + Bitmap *b = getPrivateData(self); + + GUARD_EXC( b->replaceRaw(RSTRING_PTR(str), RSTRING_LEN(str)); ); + + return self; +} + RB_METHOD(bitmapInitializeCopy) { rb_check_argc(argc, 1); @@ -465,14 +495,14 @@ bitmapBindingInit() _rb_define_method(klass, "hue_change", bitmapHueChange); _rb_define_method(klass, "draw_text", bitmapDrawText); _rb_define_method(klass, "text_size", bitmapTextSize); + + _rb_define_method(klass, "raw_data", bitmapGetRawData); + _rb_define_method(klass, "raw_data=", bitmapSetRawData); - if (rgssVer >= 2) - { _rb_define_method(klass, "gradient_fill_rect", bitmapGradientFillRect); _rb_define_method(klass, "clear_rect", bitmapClearRect); _rb_define_method(klass, "blur", bitmapBlur); _rb_define_method(klass, "radial_blur", bitmapRadialBlur); - } INIT_PROP_BIND(Bitmap, Font, "font"); } diff --git a/src/bitmap.cpp b/src/bitmap.cpp index e5ea3b82..b0518572 100644 --- a/src/bitmap.cpp +++ b/src/bitmap.cpp @@ -883,10 +883,21 @@ void Bitmap::setPixel(int x, int y, const Color &color) p->onModified(false); } -void Bitmap::replaceRaw(void *pixel_data, int w, int h) +void *Bitmap::getRaw() { guardDisposed(); - if (w != width() || h != height()) return; + + GUARD_MEGA; + + return p->surface->pixels; +} + +void Bitmap::replaceRaw(void *pixel_data, int size) +{ + guardDisposed(); + int w = width(); + int h = height(); + if (size != w*h*4) return; GUARD_MEGA; diff --git a/src/bitmap.h b/src/bitmap.h index a044b05d..97e8a8dd 100644 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -82,7 +82,8 @@ public: Color getPixel(int x, int y) const; void setPixel(int x, int y, const Color &color); - void replaceRaw(void *pixel_data, int width, int height); + void *getRaw(); + void replaceRaw(void *pixel_data, int size); void hueChange(int hue); diff --git a/src/discordstate.cpp b/src/discordstate.cpp index 0bfd0376..dd86b438 100644 --- a/src/discordstate.cpp +++ b/src/discordstate.cpp @@ -84,7 +84,7 @@ void onActivityInviteRequestCb(void *event_data, enum EDiscordActivityActionType void discordLogHook(void *hook_data, enum EDiscordLogLevel level, const char *message) { - Debug() << "DISCORD:" << message; + Debug() << "Discord:" << message; } int discordTryConnect(DiscordStatePrivate *p) @@ -238,7 +238,7 @@ Bitmap *DiscordState::getAvatar(DiscordUserId userId, int size) int sz = data->bmp->width()*data->bmp->height()*4; uint8_t *buf = new uint8_t[sz]; data->pri->app.images->get_data(data->pri->app.images, handle_result, buf, sz); - data->bmp->replaceRaw(buf, data->bmp->width(), data->bmp->height()); + data->bmp->replaceRaw(buf, sz); delete[] buf; delete data; }