Bitmap Number
#1
Bitmap Number
v 1.4b

This is a very basic script I wrote a while back for VX, then recently dug up and rescued for use in my XP projects. ... and then later on I used it again for VXAce. It's a pretty flexible script. A thing that has always bothered me is coming across a nice font, only to find that the kerning for numbers is inconsistent - seeing things like the play-time or similar jump about when a 1 appears, or worse, other numbers too.

This script allows you to use an image for numbers wherever you please. This isn't plug-and-play. If you want to see your image, you'll have to add the code where necessary.

Sample Bitmap
Huh? Oh fine here you go:
[Image: O81KI.png]

Use
  • Place contents.draw_bitmap_number(value, x, y, align, skin, offset) where you want your number to be, with the stuff between ( ) what you want it to be. Only value, x and y are required. You'll need bit of script understanding to use this.
    • In VXAce, contents.draw_bitmap_number is used in windows, and in non-window elements something like bitmap.draw_bitmap_number may be used.
    • A simple example would be contents.draw_bitmap_number(actor.level, 200, 0), although I wouldn't have absolute numbers for actor.level but I'm trying not to sound confusing...
  • value is what the number is supposed to be. i.e. actor.hp. Do take note for things like certain actor stats, you'll need another draw_bitmap_number for the maximum.
  • x, y, align, and skin all ought to be obvious. The skin location can be changed by altering RPG::Cache.windowskin.
  • offset is the number of pixels to subtract between digits. You'll probably be using this for any numbers that have drop shadows- that slight overlap makes things look much more natural. (By the way, the offset for the sample image is 4.)

The defaults can be changed by editing the definition in the code. It's really not that big a script, so it shouldn't be hard.

Script
The code below is formatted for XP. To use in VX/Ace, replace RPG::Cache.windowskin with Cache.system, or whichever line you need to use for wherever you have your number image.

Code:
#==============================================================================
# ■ Bitmap
#==============================================================================

class Bitmap
  #--------------------------------------------------------------------------
  # ● Draw Bitmap Number
  #--------------------------------------------------------------------------
  def draw_bitmap_number(value, x, y, align = 0, skin = "numbers/nwh", offset = 2)
    # Get image
    numerals = RPG::Cache.windowskin(skin) # for VX/Ace: numerals = Cache.system(skin)
    digit = (numerals.width / 10)
    text = value.to_s
    value = value.to_i if value.is_a?(String)
    char = 0
    # Get number size
    tx = ((digit-offset) * text.length) + offset
    # Offset x
    case align
    when 1
      x -= (tx / 2)
    when 2
      x -= tx
    end
    while ((c = text.slice!(/./)) != nil)
      n = digit * c.to_i
      src = Rect.new(n, 0, digit, numerals.height)
      self.blt(x + char, y, numerals, src)
      char += digit - offset
    end
  end
end

Credits
Credits should go to myself "Taylor".
Reply }
#2
Bump!
Wheee.

Updated the script with a major addition that makes working out the complete sprite's length much more sensible. Or to put it another way, I just learnt about String.length. |V In addition, numbers with leading zeros like playtime hour/min/sec should be able to be used with this script.
Code:
hour = sprintf("%02d", @total_sec / 60 / 60)
min = sprintf("%02d", @total_sec / 60 % 60)
sec = sprintf("%02d", @total_sec % 60)
draw_bitmap_number(sec, self.width - 44, y + LINE_HEIGHT, 0, 2)
draw_bitmap_number(min, self.width - 84, y + LINE_HEIGHT, 0, 2)
draw_bitmap_number(hour, self.width - 124, y + LINE_HEIGHT, 0, 2)
Reply }
#3
updaaaaate

Dear past me, what made you think tx = (digit * text.length) - digit or tx = ((digit-offset) * text.length) - digit worked for finding the width of the created bitmap? On that, since when did x -= (tx / 2) + ((digit-offset) / 2) make sense for the centre offset, when the tx alone should be giving the width needed?

Except well, it didn't. tx was ballsingly wrong and that wasn't obvious to me until I wanted a specific right-align value, only to discover... it wasn't drawing to the right place. I then spent a night realigning every instance of this in my own project after fixing this.

So this got an update, I fixed the bad calculations and switched around the arguments a little so that some can be omitted in a more sensible fashion. That is to say, wanting to specify a different alignment would be more common than a different offset, if you've actually got consistent number sizes.
Reply }
#4
nice stuff so far! Color me intrigued, especially if this works well with the bitmap fonts scripts out there.
[Image: AVvXsEga1vIr5Rx3FkoFbGo1OoMJAqgq6TeqcbpF...L3AWg=s752]

BLOG: JayVinci.blogspot.com
FORUM COMING SOON
Currently working on:
Ambitions: RPG Developer Bakin
Heart of Vox: RPG Developer Bakin
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   MapEffects for Whiteflute's Bitmap EX Narzew 3 9,094 05-05-2012, 09:31 PM
Last Post: Narzew
   Additional Bitmap Methods untra 4 10,348 06-04-2010, 03:55 AM
Last Post: untra



Users browsing this thread: 1 Guest(s)