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.

[Resolved] Bitmap Filtering(Simple 2x)

Status
Not open for further replies.
Hello all. I have come to a crossroad. I am working on a script right now that will take a bitmap and enlarge it to be twice as big. I'm just doing a simple 2x method. This is the basics of it...

for every x and y coordinate(so one pixel) the new bitmap will represent that as four pixels(2x2). The four coordinates are (2x,2y), (2x+1,2y), (2x,2y+1), (2x+1,2y+1).
My problem is that it takes too long to compute a large bitmap and I'm trying to figure out a way that is efficient to filter the bitmap like this while not lagging the game to death. The main problem is that I am doing this constantly(every Graphics update). If anyone has any ideas please let me know. I'll post anything I come up with in case anyone else tries to do this.
 
Why not...
Code:
# Original Bitmap
original = RPG::Cache.battler("001-Battler01", 0)

# Resized bitmap (needs to create before stretching duh xD)
resized = Bitmap.new(original.width*2, original.height*2)

# Some preparation...
src_rect = Rect.new(0, 0, original.width, original.height)
dest_rect = Rect.new(0, 0, resized.width, resized.height)
opacity = 255 # You can even define the opacity

# Zooming!
resized.stretch_blt(src_rect, original, dest_rect, opacity)

Or, you can use the Sprite#zoom_x and Sprite#zoom_y with the same value (in this case 2), and it´s a better option if you can take it. Any operation with Bitmaps' bits are laggy, and even more with loops.
 
Good thinking. I came up with a bunch of ideas to help speed up things, but the method stretch_blt is much faster than all of my ideas combined. Thanks a bunch for the idea of using stretch_blt. I'm not sure why, but I never considered that to be an option.
 
Just so you know, I have also created a method that scales an image into a strecth regions and centers it. This prevents distortion.

Code:
class Bitmap
  def scale_blt(dest_rect, src_bitmap, 
      src_rect = Rect.new(0, 0, src_bitmap.width, src_bitmap.height), opacity = 255)
    w, h = src_rect.width, src_rect.height
    scale = [w / dest_rect.width.to_f, h / dest_rect.height.to_f].max
    ow, oh = (w / scale).to_i, (h / scale).to_i
    ox, oy = (dest_rect.width - ow) / 2, (dest_rect.height - oh) / 2
    stretch_blt(Rect.new(ox + dest_rect.x, oy + dest_rect.y, ow, oh), 
      src_bitmap, src_rect )
  end
end
You use it the same way you would the stretch_blt, just use scale_blt instead.

This topic has been resolved. If Baskinein or any other users have any questions or further problems regarding this topic, please create a new thread about them.

Thank you!
 
Status
Not open for further replies.

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