Bitmap.snap_to_bitmap && Bitmap.each_frame

This commit is contained in:
Roza 2021-05-03 11:51:39 -04:00
parent 2a158f564a
commit c81c09b82e
3 changed files with 456 additions and 411 deletions

View file

@ -1,23 +1,23 @@
/*
** bitmap-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** 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/>.
*/
** bitmap-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** 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 "binding-types.h"
#include "binding-util.h"
@ -667,6 +667,48 @@ RB_METHOD(bitmapGetLooping){
return rb_bool_new(ret);
}
// Captures the Bitmap's current frame data to a new Bitmap
RB_METHOD(bitmapSnapToBitmap) {
RB_UNUSED_PARAM;
rb_check_argc(argc, 0);
Bitmap *b = getPrivateData<Bitmap>(self);
Bitmap *newbitmap = 0;
GUARD_EXC(newbitmap = new Bitmap(*b, false););
VALUE ret = rb_obj_alloc(rb_class_of(self));
bitmapInitProps(newbitmap, ret);
setPrivateData(ret, newbitmap);
return ret;
}
RB_METHOD(bitmapEachFrame) {
RB_UNUSED_PARAM;
rb_check_argc(argc, 0);
Bitmap *b = getPrivateData<Bitmap>(self);
if (!rb_block_given_p()) return RUBY_Qnil;
GUARD_EXC
(
b->ensureNotPlaying();
int cur_frame = b->currentFrameI();
for (int i = 0; i < b->numFrames(); i++) {
b->gotoAndStop(i);
rb_yield_values(2, self, INT2NUM(i));
}
b->gotoAndStop(cur_frame);
);
return RUBY_Qnil;
}
RB_METHOD(bitmapGetMaxSize){
RB_UNUSED_PARAM;
@ -745,10 +787,12 @@ void bitmapBindingInit() {
_rb_define_method(klass, "remove_frame", bitmapRemoveFrame);
_rb_define_method(klass, "next_frame", bitmapNextFrame);
_rb_define_method(klass, "previous_frame", bitmapPreviousFrame);
_rb_define_method(klass, "each_frame", bitmapEachFrame);
_rb_define_method(klass, "frame_rate", bitmapGetFPS);
_rb_define_method(klass, "frame_rate=", bitmapSetFPS);
_rb_define_method(klass, "looping", bitmapGetLooping);
_rb_define_method(klass, "looping=", bitmapSetLooping);
_rb_define_method(klass, "snap_to_bitmap", bitmapSnapToBitmap);
INIT_PROP_BIND(Bitmap, Font, "font");
}

View file

@ -645,13 +645,14 @@ Bitmap::Bitmap(void *pixeldata, int width, int height)
p->addTaintedArea(rect());
}
Bitmap::Bitmap(const Bitmap &other)
Bitmap::Bitmap(const Bitmap &other, bool copyAllFrames)
{
other.ensureNonMega();
p = new BitmapPrivate(this);
if (!other.isAnimated()) {
if (!other.isAnimated() || !copyAllFrames) {
other.ensureNotPlaying();
p->gl = shState->texPool().request(other.width(), other.height());
blt(0, 0, other, rect());
}

View file

@ -42,7 +42,7 @@ public:
Bitmap(int width, int height);
Bitmap(void *pixeldata, int width, int height);
/* Clone constructor */
Bitmap(const Bitmap &other);
Bitmap(const Bitmap &other, bool copyAllFrames = true);
~Bitmap();
int width() const;