Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Resizing actor battler graphic in a window [XP]

Jason

Awesome Bro

Hey, so I don't know how I've possibly forgotten this... but how would I draw an actor battler graphic inside a window? I'm just jazzing up the main menu a little and decided to change the graphic from the sprite to the battler... the line for the sprite is;

Code:
 draw_actor_graphic(actor, x - 40, y + 80)

But yeah, not sure how I'd go about changing this to the battler, I've just completely forgotten lol.


Okay new request now if anyone would like to attempt this...

So for two seperate windows I'm having the battler show, however, I'd like to know if it's possible to resize the battler image, for example, in one window it could be shrunk down to 75% the normal size, whereas on another it could be blown up to 200% the normal size. I know there's a script that exists to do this with the character sprite, which allows you to shrink/grow your character on the map, but how would I go about doing it with a battler image?
 
Well, below is the code to do that, so all you have to do is change that line you provided from

draw_actor_graphic(actor, x - 40, y + 80)

to

draw_actor_battler(actor, x - 40, y + 80)


Good luck!

Code:
#==============================================================================

# Title: Draw Actor Battler | Version 1.0

# Author: TheLaw [aka. TheScripter]

# Requester: jbrist [@ hbgames.org]

#==============================================================================

 

#==============================================================================

# ** Window_Base

#------------------------------------------------------------------------------

#  This class is for all in-game windows.

#==============================================================================

 

class Window_Base < Window

  #--------------------------------------------------------------------------

  # * Draw Graphic

  #     actor : actor

  #     x     : draw spot x-coordinate

  #     y     : draw spot y-coordinate

  #--------------------------------------------------------------------------

  def draw_actor_battler(actor, x, y)

    bitmap = RPG::Cache.battler(actor.battler_name, actor.battler_hue)

    w = bitmap.width

    h = bitmap.height

    src_rect = Rect.new(0, 0, w, h)

    self.contents.blt(x, y, bitmap, src_rect)

  end

end

 

#==============================================================================

# 

# *** END OF SCRIPT ***

#

#==============================================================================
 
I'm not sure how you'd be able to do that with the bitmap drawing method (blt()), but it is very easily done with use of a sprite. If you'd set up the sprite for the battler(s) you want to shrink you can easily apply a scale effect. Like so:
Code:
@battler_sprite = Sprite.new

@battler_sprite.bitmap = RPG::Cache.battler(actor.battler_name, actor.battler_hue)

@battler_sprite.zoom_x = 0.75

@battler_sprite.zoom_y = 0.75
This will work fine. If you're intending to scale battlers for all allies, you could do something similar to this for convenience:
Code:
@battlers = []

for actor in $game_party.actors

  sprite = Sprite.new

  sprite.bitmap = RPG::Cache.battler(actor.battler_name, actor.battler_hue)

  sprite.zoom_x = 0.75

  sprite.zoom_y = 0.75

  @battlers << sprite

end
You'd need to position them yourself. In that case, use an integer for loop for that if necessary eg "for i in 0...$game_party.actors.size".

A quick note for the latter prospect, you can dispose of these sprites by one single line of code while they are inside an array:
Code:
@battlers.each {|sprite| sprite.dispose}
Which also means if you expand this sprite array later, you don't need to expand the disposing code.

This is all I can think of for scaling sprites inside the program rather than scaling them manually in an image editing application.

EDIT: Just to clarify, the "draw_actor_*" methods always draw on a Bitmap instance object, whereas they make use of the blt method, which draws the bitmap from the cache onto an area on the Bitmap instance and leaves it there. Changing it would require a redraw, which is a heavier task than moving or scaling an existing sprite object.

/Sark
 

Atoa

Member

simply use .strecth_blt instead of .blt

bitmap.stretch_blt(rect1, bitmapt, rect2[, opacity])

rect1 is the rect of the redimensioned image,
rect2 is the rect of the the bitmap.
opacity is optional

so:
rect1 = Rect.new(0, 0, 200, 200)
rect2 = Rect.new(0, 0, 50, 50)
self.bitmap.stretch_blt(rect1, bitmapt, rect2)

will make an 50 x 50 rect became an 200 x 200.
 
That's really nice Atoa, I was attempting to use that method, but I think I messed up when implementing it, but your code seems really simple. So using Atoa's code, I added it to the battler method I had in Window Base and now it looks like this:

Code:
#==============================================================================

# Title: Draw Actor Battler | Version 1.0

# Author: TheLaw [aka. TheScripter] and Atoa

# Requester: jbrist [@ hbgames.org]

#==============================================================================

     

#==============================================================================

# ** Window_Base

#------------------------------------------------------------------------------

#  This class is for all in-game windows.

#==============================================================================

 

class Window_Base < Window

  #--------------------------------------------------------------------------

  # * Draw Graphic

  #     actor : actor

  #     x     : draw spot x-coordinate

  #     y     : draw spot y-coordinate

  #--------------------------------------------------------------------------

  def draw_actor_battler(actor, x, y, str_w = 111, str_h = 170)

    bitmap = RPG::Cache.battler(actor.battler_name, actor.battler_hue)

    w = bitmap.width

    h = bitmap.height

    src_rect = Rect.new(0, 0, w, h)

    self.contents.stretch_blt(Rect.new(x, y, str_w, str_h), bitmap, src_rect)

  end

end     

    

#==============================================================================

#

# *** END OF SCRIPT ***

#

#==============================================================================


And then for line of code in the menustatus, it should look like this:

draw_actor_battler(actor, x - 40, y + 80, STRETCHED_WIDTH, STRETCHED_HEIGHT)

Obviously, the last two values are the width and height of the stretched out battler. But, if you do not want to stretch your battler out, just leave it like so:

draw_actor_battler(actor, x - 40, y + 80)

because I left in default values for the last two arguments. Good luck!
 
You should probably have it more dynamically, like this:
Code:
  def draw_actor_battler(actor, x, y, str_w = 1.0, str_h = 1.0)

    bitmap = RPG::Cache.battler(actor.battler_name, actor.battler_hue)

    w = bitmap.width

    h = bitmap.height

    src_rect = Rect.new(0, 0, w, h)

    dest_rect = Rect.new(0, 0, (w.to_f * str_w).to_i, (h.to_f * str_h).to_i)

    self.contents.stretch_blt(dest_rect, bitmap, src_rect)

  end
Allows scaling of percentages based on the given battler image, as their size varies.
Alternatively you can use both. It depends on needs. If you want to scale by portion, then this would be the best choice. If by actual pixels, TheScripter's definition is more accurate.

Just an idea.

/Sark
 
Yea, yours would probably be the better option because he did say that he wanted percentages, so that like 0.75 would be like 75% (like how he requested) or 2.0 would be 200%. So yea, nice job Sarkilas :D
 

Jason

Awesome Bro

Hmm, well it resizes them fine based on percentages, however, I can't reposition them, no matter which co-ordinates I set them in, all of the battler images are locked into the top right corner of the window, which isn't very good for me... do you have any ideas why this may be happening?
 
Here you go, this should work, just assign the x and y values that you want on that line in MenuStatus:

Code:
#==============================================================================

# Title: Draw Actor Battler | Version 1.0

# Requester: jbrist [@ hbgames.org]

#==============================================================================

     

#==============================================================================

# ** Window_Base

#------------------------------------------------------------------------------

#  This class is for all in-game windows.

#==============================================================================

 

class Window_Base < Window

  #--------------------------------------------------------------------------

  # * Draw Graphic

  #     actor : actor

  #     x     : draw spot x-coordinate

  #     y     : draw spot y-coordinate

  #--------------------------------------------------------------------------

  def draw_actor_battler(actor, x, y, str_w = 111, str_h = 170)

    bitmap = RPG::Cache.battler(actor.battler_name, actor.battler_hue)

    w = bitmap.width

    h = bitmap.height

    src_rect = Rect.new(0, 0, w, h)

    dest_rect = Rect.new(x, y, (w.to_f * str_w).to_i, (h.to_f * str_h).to_i)

    self.contents.stretch_blt(dest_rect, bitmap, src_rect)

  end

end     

    

#==============================================================================

#

# *** END OF SCRIPT ***

#

#==============================================================================
 

Jason

Awesome Bro

Ah thanks for this, it's working perfectly now, just out of curiosity, would I be able to use this when creating windows, for example, a heads up display to use while walking around the maps?
 
Yes, as long as it's a window, all you have to do is call the same method (which is draw_actor_battler), because all the in-game windows and user created windows should be a child class of Window_Base, which means that they would be able to use the methods (and variables) in the parent class of Window_Base. Since we defined that method in Window_Base, everything that is a child of it should work. So, basically, to answer to your question, yes :D
 
You can scale any image you have with the same type of method, yes. But not with that exact method if it's not battlers for actors from the database. It'd need a rewrite. Like so:

Code:
class Window_Base < Window

  def draw_scaled_graphic(bitmap, x, y, width_scale = 1.0, height_scale = 1.0)

    w = bitmap.width

    h = bitmap.height

    src_rect = Rect.new(0, 0, w, h)

    dest_rect = Rect.new(x, y, (w.to_f * width_scale).to_i, (h.to_f * height_scale).to_i)

    self.contents.stretch_blt(dest_rect, bitmap, src_rect)

  end

end
So if you want to draw an image from your Pictures folder, an example would be (inside your HUD window class's refresh method):
Code:
bitmap = RPG::Cache.picture("Picture Name")

draw_scaled_graphic(bitmap, 0, 0, 0.7, 0.7) # draws the picture with 70% of original scale
Naturally change the coordinates and width/height scale to whatever you feel like

I'm assuming this is what you wanted.

/Sark
 

Jason

Awesome Bro

Ah I didn't think to do it with pictures, would this also be the case with icons too? Because that too would also be a big help for what I have in mind, lol.

You two have been quite the scripting duo in this thread haven't you, lol...
 
Yep, do exactly what Sarkilas did except change this line in your HUD class' refresh from:

Code:
bitmap = RPG::Cache.picture("Picture Name")

draw_scaled_graphic(bitmap, 0, 0, 0.7, 0.7) # draws the picture with 70% of original scale

to

Code:
bitmap = RPG::Cache.icon("Icon Name")

draw_scaled_graphic(bitmap, 0, 0, 0.7, 0.7) # draws the picture with 70% of original scale


I just changed RPG::Cache.picture to RPG::Cache.icon to know what file folder to look in. That should work with the method Sarkilas gave you.

In fact, look in the Help File and search for RPG::Cache, that will show you all the file types you can use.


You two have been quite the scripting duo in this thread haven't you, lol...

:box: :box:
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top