• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Our friends from the Johto Times are hosting a favorite Pokémon poll - and we'd love for you to participate! Click here for information on how to vote for your favorites!
  • Cyndy, May, Hero (Conquest), or Wes - which Pokémon protagonist is your favorite? Let us know by voting in our poll!
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Showing Partial Icon Sprite

  • 7
    Posts
    7
    Years
    • Seen Mar 3, 2023
    I can't figure this out.

    Suppose I had this sprite:
    [PokeCommunity.com] Showing Partial Icon Sprite


    and this sprite:
    [PokeCommunity.com] Showing Partial Icon Sprite


    How would I code it to display this
    [PokeCommunity.com] Showing Partial Icon Sprite
    using only the sprites above?
    I want to show only a partial image and using another sprite as a "window."
     
    There is no way for you to do this by default in neither RMXP itself, or Essentials. You would have to code an entirely new function that would compare the alpha values of two different bitmaps, and use those to "cut" out one from the other. Luckily for you, I required this for my own stuff, and wrote a function to integrate into the Sprite class.
    Code:
    #===============================================================================
    # ** Sprite Mask
    #        by Luka S.J.
    #-------------------------------------------------------------------------------
    # Allows you to use a bitmap, sprite or image path to a bitmap to use as a mask
    # for a sprite's original bitmap.
    #===============================================================================
    # handled as an extension of the Sprite class
    class Sprite
      # function used to to mask the Sprite's current bitmap with another
      def mask(mask = nil,xpush = 0,ypush = 0)
        # exits out of the function if the sprite currently has no bitmap to mask
        return false if !self.bitmap
        # backs up the Sprites current bitmap
        bitmap = self.bitmap.clone
        # check for mask types
        if mask.is_a?(Bitmap) # accepts Bitmap.new
          mbmp = mask
        elsif mask.is_a?(Sprite) # accepts other Sprite.new
          mbmp = mask.bitmap
        elsif mask.is_a?(String) # accepts Strings
          mbmp = BitmapCache.load_bitmap(mask)
        else # exits if non-matching type
          return false
        end
        # creates a new bitmap
        self.bitmap = Bitmap.new(mbmp.width, mbmp.height)
        # creates the main mask
        mask = mbmp.clone
        # calculates the dimension metrics for pixel transfer
        ox = (bitmap.width - mbmp.width) / 2
        oy = (bitmap.height - mbmp.height) / 2
        width = mbmp.width + ox
        height = mbmp.height + oy
        # draws pixels to mask bitmap
        for y in oy...height
          for x in ox...width
            # gets pixel of mask for analysis
            pixel = mask.get_pixel(x - ox, y - oy)
            # gets pixel of current bitmap for analysis
            color = bitmap.get_pixel(x - xpush, y - ypush)
            # sets the new alpha to use the value of the mask alpha
            alpha = pixel.alpha
            alpha = color.alpha if color.alpha < pixel.alpha
            # draws new pixels onto the Sprite's bitmap
            self.bitmap.set_pixel(x - ox, y - oy, Color.new(color.red, color.green,color.blue, alpha))
          end
        end
        # returns finalized bitmap to be used elsewhere
        return self.bitmap
      end
    end
    This piece of code is very simple to use (and I've gone ahead and written plenty of comments, so that you can see what part does exactly what), and an example would look something like this:
    Code:
    sprite = Sprite.new(viewport)
    sprite.bitmap = Bitmap.new("main_sprite.png")
    sprite.mask("mask_sprite.png")
    And just for the sake of illustrating this even further, here's a visual representation of what a Sprite being masked with this function would yield:
    [PokeCommunity.com] Showing Partial Icon Sprite


    EDIT: This is a resource intensive task/function. So I wouldn't recommend using this in conjuncture with large sprites/masks (and you especially shouldn't be using this in some main scene loops), as that will probably cause your performance to drop.
     
    Last edited:
    Back
    Top