MV Cropping images from different sides

I have a Window_Base, inside of which I've created a Sprite_Base with a bitmap.

This displays an image (woot).

I have figured out how to use setFrame() on the sprite to crop the image and make parts of it get cut off.

However, it always draws from the upper left of the frame, and therefore cuts off to the right and bottom.

How can I cut off the top or left of the image? The bitmap doesn't appear to have an anchor that specifies where it's attached to the sprite, and changing the sprite's anchor makes it go haywire out of the window containing it.

Am I approaching this entirely the wrong way?

Edit: I feel the answer is in Bitmap.blt, but I can't figure out how to make it produce a visual result. It seems to draw nothing. For example,
Code:
    let bar = ImageManager.loadPicture("test");
    this.contents.blt(bar, 0, bar.height * percentage, bar.width, bar.height - bar.height * percentage, 0, 0);
If I recall correctly, loadPicture is async so you need to let it finish. Or you do blt in every update until it is loaded.
Hi there!
I don't know if I understand you correctly. But I did some tests here. I created this window with a sprite inside of it:
JavaScript:
class myWin extends Window_Base{
    initialize(rect){
        super.initialize(rect)
        const bitmap = ImageManager.loadPicture("ilia")
        bitmap.addLoadListener(() => {
            this.mySpr = new Sprite(bitmap)
            this.mySpr.x = (this.width-bitmap.width)/2
            this.mySpr.y = (this.height-bitmap.height)/2
            this.addChild(this.mySpr)
        })

    }
}

Then I made some tests on the console, making the window the size of my game screen/resolution:
1720590498182.png

On the first setFrame, I put X and Y to 10. And when I call the function, I realize that it seems that the Sprite moved -10x and -10y. To fix this, I adjusted the sprite position to +10x and +10y.

Now, for me to give the impression that I'm cutting off the right and bottom of the sprite, I did the second call of the setFrame, but this time changing the width and height of the frame.

My conclusion with this is, whathever you set the frame of the sprite and want to give the impression that he is on the same place, just adjust the sprite position X and Y according to the number you put on the setFrame.

Now, if I did misunderstood what you mean, this is my second attempt!! haha!

I would do the .blt only after the bitmap is loaded, like @utunnels said.
If perhaps what you want is that the sprite only shows the "cutted" version of your bitmap, instead of putting the whole bitmap inside the sprite and use the .blt later, do it before, and pass into the sprite a new bitmap with the result of the .blt.
Example:


JavaScript:
class myWin extends Window_Base{
    initialize(rect){
        super.initialize(rect)
        const faceSetImage = ImageManager.loadFace("Monster")
        faceSetImage.addLoadListener(() => {
            const face = new Bitmap(144,144) // Size of a single face inside the faceSet Image
            const sx = 144*2
            const sy = 0
            const sw = 144
            const sh = 144
            const dx = 0
            const dy = 0
            face.blt(faceSetImage, sx, sy, sw, sh, dx, dy)
            this.mySpr = new Sprite(face)
            this.mySpr.x = (this.width-face.width)/2
            this.mySpr.y = (this.height-face.height)/2
            this.addChild(this.mySpr)
          
        })
    }
}

Hope it helps!
Thank you for responses.

If I recall correctly, loadPicture is async so you need to let it finish. Or you do blt in every update until it is loaded.
That's where the code I pasted was, in the window's update() method (I realize I was non-specific).

If perhaps what you want is that the sprite only shows the "cutted" version of your bitmap, instead of putting the whole bitmap inside the sprite and use the .blt later, do it before, and pass into the sprite a new bitmap with the result of the .blt.
I'll mess around with this. The problem is that I don't only want to show the cropped version, I want to be dynamically resizing it. And I feel like trying to create a new Bitmap every update() is never going to actually load.

The ultimate point of this is to display a gauge - I have a background sprite, then the bar to display on top of it. I want the bar to shrink and grow not by transforming/stretching the image, but by cropping it.

I can get this to work, but only by using setFrame which only crops from the bottom or right.
The way I'd do it would indeed be with blt.
That's where the code I pasted was, in the window's update() method (I realize I was non-specific).
How exactly did you do that? The two lines you've shared would never work together, since bar would continuously get redefined and never actually load, though I'm assuming the actual setup is a bit different.
How exactly did you do that?
I mean, apparently incorrectly? 🤣

Here's a test setup with image files (to be placed in img\pictures)

Code:
Scene_Map.prototype.createAllWindows = function() {
    this.createMessageWindow();
    this.createScrollTextWindow();
    this.createTestGauge();
};

Scene_Map.prototype.createTestGauge = function() {
    this._testGauge = new Window_Base();
    this.addChild(this._testGauge);
    this._testGauge.move(0, 0, 640, 25);
    this._testGauge.opacity = 0;
    this._testGauge._frame = new Sprite_Base();
    this._testGauge._frame.bitmap = ImageManager.loadPicture("meter-frame");
    this._testGauge.addChild(this._testGauge._frame);

/*    The commented out material is what works

    this._testGauge._bar = new Sprite_Base();
    this._testGauge._bar.bitmap = ImageManager.loadPicture("meter-bar");
    this._testGauge.addChild(this._testGauge._bar);
*/
};

var origMapUpdate = Scene_Map.prototype.update;
Scene_Map.prototype.update = function()
{
    origMapUpdate.call(this);

    this.updateTestGauge();
};

Scene_Map.prototype.updateTestGauge = function()
{
// Using a static value for testing, but this will be recalculated during gameplay
    let fill = .5;
 
/*    The commented out material is what works

    let bar = this._testGauge._bar;
    bar.setFrame(bar.x, bar.y, bar.bitmap.width * fill, bar.bitmap.height);
*/
// This part doesn't display anything
    let bar = ImageManager.loadPicture("meter-bar");
    this._testGauge.contents.blt(bar, bar.width * fill, 0, bar.width - bar.width * fill, bar.height, 0, 0);
};

I've also tried with leaving the original bitmap in the createTestGauge() and redrawing it with blt(), doesn't do anything.

My current goal would be to show just the right half of the bar.

The two lines you've shared would never work together, since bar would continuously get redefined and never actually load
That's how it's written in, say, Window_Base.drawFace(). Is it because update() gets called too frequently? Do I need to use update() to track whether the fill percentage has changed, then call a separate refresh function?
As for the anchor solution:

JavaScript:
var spr1 = new Sprite(ImageManager.loadPicture('Image1'));
var spr2 = new Sprite(ImageManager.loadPicture('Image2'));
var spr3 = new Sprite(ImageManager.loadPicture('Image3'));
var scene = SceneManager._scene;
spr1.addChild(spr2); spr1.addChild(spr3);
var barx=5, bary=5, barwidth = 450, barheight = 22, barpercentage = 0.86;
spr3.anchor.x = 1; spr3.anchor.y = 0;
spr2.x = barx, spr2.y = bary;
spr3.x = barx + barwidth;spr3.y = bary;
spr2.setFrame(0,0,barwidth*barpercentage,barheight);
spr3.setFrame(barwidth*barpercentage,0,barwidth*(1-barpercentage),barheight);
spr1.x = 5; spr1.y = Graphics.height-37; scene.addChild(spr1);

1720622061910.png

1720622118664.png
That's how it's written in, say, Window_Base.drawFace()
Ah, I see how you got to this.

When an event starts, it looks through some specific commands and preloads any images they need. So drawFace doesn't need to account for an image not being loaded, but when making custom stuff you usually do.

The typical approach is similar to the one shared by @Eliaquim : created a listener for the image being loaded, and only do things with it when it's actually loaded.

So when the window is created, you'd do
JavaScript:
let barTemp = ImageManager.loadPicture("meter-bar");
barTemp.addLoadListener(() => {
    this.bar = barTemp;
});

And then only do blt when that this.bar exists.

For some examples, here is a window that simply displays a specific image, and here is a more complex one that displays a bunch of images that move on each frame.
As for the anchor solution:
So I appreciate the help, but I'm having a little bit of a hard time grokking it because of how you changed the setup into three sprites - it appears as if your code is using setFrame to show still the left portion of spr3, just positioning it next to spr2.

That's not what I want - I want specifically to show the right or bottom portion of an image: the bar is an example, presume it should work with asymmetrical images.

But I'll poke at it, see if I'm misunderstanding what you're doing.

So when the window is created, you'd do
And then only do blt when that this.bar exists.
This did not appear to work. I modified the code/file I provided above as such:
Code:
Scene_Map.prototype.createTestGauge = function() {
    this._testGauge = new Window_Base();
    this.addChild(this._testGauge);
    this._testGauge.move(0, 0, 640, 25);
    this._testGauge.opacity = 0;
    this._testGauge._frame = new Sprite_Base();
    this._testGauge._frame.bitmap = ImageManager.loadPicture("meter-frame");
    this._testGauge.addChild(this._testGauge._frame);

// 'Cause Mac told me to
    let barTemp = ImageManager.loadPicture("meter-bar");
    barTemp.addLoadListener(() => {
        this.barSprite = barTemp;
    });
};
Scene_Map.prototype.updateTestGauge = function()
{
    let fill = .5;
    if (this._testGauge.barSprite)
    {
        let bar = this._testGauge.barSprite.bitmap;
        this._testGauge.contents.blt(bar, bar.width * fill, 0, bar.width - bar.width * fill, bar.height, 0, 0);
    }
};

but it still draws nothing.

I'll look at the examples you linked to, see if I can figure out what I'm doing differently.
This did not appear to work. I modified the code/file I provided above as such:
You seem to be adding barSprite as a property of Scene_Map, but checking if this property exists on _testGauge.

There's also no need to access its bitmap property, it already is one 😉

Try this:
JavaScript:
Scene_Map.prototype.updateTestGauge = function()
{
    let fill = .5;
    if (this.barSprite)
    {
        let bar = this.barSprite;
        this._testGauge.contents.blt(bar, bar.width * fill, 0, bar.width - bar.width * fill, bar.height, 0, 0);
    }
};
You seem to be adding barSprite as a property of Scene_Map, but checking if this property exists on _testGauge.
I do seem to be doing that, don't I?

Well, while I seem to be doing that, what I'm really doing is seeing if you're paying attention! 😛

...I fixed it and it still doesn't draw anything 🤣

Code:
Scene_Map.prototype.createTestGauge = function() {
// Stuff stuff stuff for the gauge window

    let barTemp = ImageManager.loadPicture("meter-bar");
    barTemp.addLoadListener(() => {
        this._testGauge.bar = barTemp;
    });
};

Scene_Map.prototype.updateTestGauge = function()
{
    let fill = .5;

    if (this._testGauge.bar)
    {
        let bar = this._testGauge.bar;
        this._testGauge.contents.blt(bar, bar.width * fill, 0, bar.width - bar.width * fill, bar.height, 0, 0);
    }
};
@ATT_Turan
Well, anchor is where the "center" point of the sprite. So change x to 1 will make it "anchor" to the right side.
I made two new images so you can see more clearly.
Just call updatebar function and give it a new percentage value.
You don't need to adjust position once it is anchored to the right side.
JavaScript:
window.spr1 = new Sprite(ImageManager.loadPicture('Image1'));
window.spr2 = new Sprite(ImageManager.loadPicture('Image2'));
window.spr3 = new Sprite(ImageManager.loadPicture('Image3'));
var scene = SceneManager._scene;
spr1.addChild(spr2); spr1.addChild(spr3);
Object.assign(window, {barx:5, bary:5, barwidth:450, barheight:22, barpercentage:0.86});
spr3.anchor.x = 1; spr2.x = barx; spr2.y = bary; spr3.x = barx + barwidth; spr3.y = bary;
spr1.x = 5; spr1.y = Graphics.height-37; scene.addChild(spr1);
window.updatebar=function (){
    spr2.setFrame(0,0,barwidth*barpercentage,barheight);//green bar
    spr3.setFrame(barwidth*barpercentage,0,barwidth*(1-barpercentage),barheight);//red bar
};
updatebar();
1720628146683.png

1720628791870.png

1720628129507.png
Alright, first thing on the "Why is my block transfer not working this time?" checklist:
Making sure the contents area is large enough... which re-reading the code, I think it might be not be. I assumed it must have been since the other approach worked, but I guess that one doesn't use contents in the first place. Try doing this._testGauge = new Window_Base(0, 0, 640, 25); instead of leaving the constructor empty.

EDIT: Or actually, maybe increase that 25, since it might be too small for the contents area to fit between padding?
Making sure the contents area is large enough... which re-reading the code, I think it might be not be. I assumed it must have been since the other approach worked, but I guess that one doesn't use contents in the first place. Try doing this._testGauge = new Window_Base(0, 0, 640, 25); instead of leaving the constructor empty.
EDIT: Or actually, maybe increase that 25, since it might be too small for the contents area to fit between padding?
Ahhh, that did something! I would have to screw with the window dimensions and figure out how to get rid of the padding and such, but it achieved things.

1720629537488.png


Well, anchor is where the "center" point of the sprite. So change x to 1 will make it "anchor" to the right side.
You don't need to adjust position once it is anchored to the right side.
Okay - I understand what you're saying, but I'm having a hard time adapting your example because your syntax for the whole thing is completely different from how I started. So I'm trying to figure out which bits are a part of your solution and which bits I'd ignore because it's attaching the sprites directly to the scene instead of a window, etc.

I tried this, and I don't see anything:
Code:
Scene_Map.prototype.createTestGauge = function()
{
    this._testGauge = new Window_Base(0, 0, 640, 50);
    this.addChild(this._testGauge);
    this._testGauge.opacity = 0;
    this._testGauge._frame = new Sprite_Base();
    this._testGauge._frame.bitmap = ImageManager.loadPicture("meter-frame");
    this._testGauge.addChild(this._testGauge._frame);

// Based on your example
    this._testGauge._bar = new Sprite_Base();
    this._testGauge._bar.bitmap = ImageManager.loadPicture("meter-bar");
    this._testGauge._bar.anchor.x = 1;
    this._testGauge._bar.x = 640;
    this._testGauge.addChild(this._testGauge._bar);
};
Strange. What's the different between Sprite and Sprite_Base? I never use the Base type. Are you sure it is visible by default?
Nice, I'm glad that worked. For an explanation as to why: Windows create their contents bitmap inside the constructor, and if you don't give them any arguments, it will be a 0x0 bitmap, unable to display anything. That bitmap is *not* resized when resizing the window (since it can't really be resized at all).
figure out how to get rid of the padding
This is mildly annoying, since you can't change padding before your window exists, but after you create it the bitmap already has a size that's based on default padding. I'd generally make them this way (assuming you want no padding in a window with size 640x25):
JavaScript:
let padding = Window_Base.prototype.standardPadding();
this._testGauge = new Window_Base(0, 0, 640 + padding * 2, 25 + padding * 2);
this._testGauge.standardPadding = () => 0;
this._testGauge.padding = 0;
this._testGauge.move(0, 0, 640, 25);
Strange. What's the different between Sprite and Sprite_Base?
Sprite_Base is...what RPG Maker uses?

Sprite_Character, Sprite_Battler, Sprite_Actor, Sprite_StateOverlay, Sprite_Weapon, Sprite_Balloon all inherit from Sprite_Base.

I haven't delved into the difference between them because I've never needed the knowledge, so I can't immediately answer the question.

I never use the Base type. Are you sure it is visible by default?
Yes - as I mentioned originally, all of the code I had worked and I could see all the sprites (and the frame you see in post #14 is the same Sprite_Base), I just couldn't control which way it cropped.

But changing the code in post 14 to declare a new Sprite as opposed to Sprite_Base has no effect, I still don't see anything of the bar.


Nice, I'm glad that worked. For an explanation as to why: Windows create their contents bitmap inside the constructor, and if you don't give them any arguments, it will be a 0x0 bitmap, unable to display anything. That bitmap is *not* resized when resizing the window (since it can't really be resized at all).
So couldn't I not use the contents, instead creating the sprite directly in the window the way I was, and blocking onto that? e.g. Scene_Map._testGauge.bar.bitmap.blt() ?

1720632227086.png

(this is not with what I was suggesting above, this is with your window padding tweak)
😂
@ATT_Turan Well, that is even more strange, because I copied your code and save it as a plugin, it just worked. The only difference is the image files.
I called the function using an event.

1720632796696.png
@ATT_Turan Well, that is even more strange, because I copied your code and save it as a plugin, it just worked.
It's because of my update(). I didn't think to disable/change it initially, because I figured I'd see something. But I had to reexamine my math inside it.

That code does now work to display the bar. I just had to change my arguments for setFrame() - looking back at your earlier example, I see that even though the anchor is changed, the coordinates for setFrame() are still placing the upper left corner of the frame.

I adjusted that and it works 🙂

Thanks to you and @Mac15001900 for two functioning solutions.
So couldn't I not use the contents, instead creating the sprite directly in the window the way I was, and blocking onto that? e.g. Scene_Map._testGauge.bar.bitmap.blt() ?
I'm not sure, but I guess this might work, since they seem to use the same Bitmap class as the contents of a window. I mostly use windows since there are already so many ways (both from the engine and ones I've made myself) for manipulating them, but there shouldn't be any problems with using a sprite as a container.

I guess you could go a step further and instead of creating anything that has a bitmap as a component just make a bitmap directly.

Original thread: https://forums.rpgmakerweb.com/threads/cropping-images-from-different-sides.170163/