[RESOLVED]Bitmap Text

Started by Satoh, November 15, 2009, 03:41:23 pm

Previous topic - Next topic

Satoh

November 15, 2009, 03:41:23 pm Last Edit: November 15, 2009, 06:06:17 pm by Satoh
Spoiler: ShowHide
class Bitmap
 
 $bitfont = Bitmap.new("graphics/pictures/fontmap.png")
 
#def draw_fontmap_text(start_x, y, dummyx, dummyy, my_text, my_bitmap = self.contents)
def draw_text(start_x, y, dummyx = 0, dummyy = 0, my_text = '', a = 0)
 if start_x.is_a?(Rect)
     start_x, y, dummyx, dummyy, my_text, a = start_x.x, start_x.y, start_x.width, start_x.height, y, dummyx
     if dummyx.is_a?(Fixnum)
       a = 255
     end
     
   else
     start_x, y, dummyx, dummyy, my_text, a = start_x, y, dummyx, dummyy, my_text, a
     if a.is_a?(Fixnum)
       a = 255
     end
   end
 x2 = start_x
 #my_bitmap = Bitmap.new(640,480)
 while  ((@letter = my_text.slice!(/./m)) != nil)
   case @letter
   when "A"
     letteroffset = 14 * 0
     caseoffset = 26 * 0
   when "B"
     letteroffset = 14 * 1
     caseoffset = 26 * 0
   when "C"
     letteroffset = 14 * 2
     caseoffset = 26 * 0
   when "D"
     letteroffset = 14 * 3
     caseoffset = 26 * 0
   when "E"
     letteroffset = 14 * 4
     caseoffset = 26 * 0
   when "F"
     letteroffset = 14 * 5
     caseoffset = 26 * 0
   when "G"
     letteroffset = 14 * 6
     caseoffset = 26 * 0
   when "H"
     letteroffset = 14 * 7
     caseoffset = 26 * 0
   when "I"
     letteroffset = 14 * 8
     caseoffset = 26 * 0
   when "J"
     letteroffset = 14 * 9
     caseoffset = 26 * 0
   when "K"
     letteroffset = 14 * 10
     caseoffset = 26 * 0
   when "L"
     letteroffset = 14 * 11
     caseoffset = 26 * 0
   when "M"
     letteroffset = 14 * 12
     caseoffset = 26 * 0
   when "N"
     letteroffset = 14 * 13
     caseoffset = 26 * 0
   when "O"
     letteroffset = 14 * 14
     caseoffset = 26 * 0
   when "P"
     letteroffset = 14 * 15
     caseoffset = 26 * 0
   when "Q"
     letteroffset = 14 * 16
     caseoffset = 26 * 0
   when "R"
     letteroffset = 14 * 17
     caseoffset = 26 * 0
   when "S"
     letteroffset = 14 * 18
     caseoffset = 26 * 0
   when "T"
     letteroffset = 14 * 19
     caseoffset = 26 * 0
   when "U"
     letteroffset = 14 * 20
     caseoffset = 26 * 0
   when "V"
     letteroffset = 14 * 21
     caseoffset = 26 * 0
   when "W"
     letteroffset = 14 * 22
     caseoffset = 26 * 0
   when "X"
     letteroffset = 14 * 23
     caseoffset = 26 * 0
   when "Y"
     letteroffset = 14 * 24
     caseoffset = 26 * 0
   when "Z"
     letteroffset = 14 * 25
     caseoffset = 26 * 0
   when "a"
     letteroffset = 14 * 0
     caseoffset = 26 * 1
   when "b"
     letteroffset = 14 * 1
     caseoffset = 26 * 1
   when "c"
     letteroffset = 14 * 2
     caseoffset = 26 * 1
   when "d"
     letteroffset = 14 * 3
     caseoffset = 26 * 1
   when "e"
     letteroffset = 14 * 4
     caseoffset = 26 * 1
   when "f"
     letteroffset = 14 * 5
     caseoffset = 26 * 1
   when "g"
     letteroffset = 14 * 6
     caseoffset = 26 * 1
   when "h"
     letteroffset = 14 * 7
     caseoffset = 26 * 1
   when "i"
     letteroffset = 14 * 8
     caseoffset = 26 * 1
   when "j"
     letteroffset = 14 * 9
     caseoffset = 26 * 1
   when "k"
     letteroffset = 14 * 10
     caseoffset = 26 * 1
   when "l"
     letteroffset = 14 * 11
     caseoffset = 26 * 1
   when "m"
     letteroffset = 14 * 12
     caseoffset = 26 * 1
   when "n"
     letteroffset = 14 * 13
     caseoffset = 26 * 1
   when "o"
     letteroffset = 14 * 14
     caseoffset = 26 * 1
   when "p"
     letteroffset = 14 * 15
     caseoffset = 26 * 1
   when "q"
     letteroffset = 14 * 16
     caseoffset = 26 * 1
   when "r"
     letteroffset = 14 * 17
     caseoffset = 26 * 1
   when "s"
     letteroffset = 14 * 18
     caseoffset = 26 * 1
   when "t"
     letteroffset = 14 * 19
     caseoffset = 26 * 1
   when "u"
     letteroffset = 14 * 20
     caseoffset = 26 * 1
   when "v"
     letteroffset = 14 * 21
     caseoffset = 26 * 1
   when "w"
     letteroffset = 14 * 22
     caseoffset = 26 * 1
   when "x"
     letteroffset = 14 * 23
     caseoffset = 26 * 1
   when "y"
     letteroffset = 14 * 24
     caseoffset = 26 * 1
   when "z"
     letteroffset = 14 * 25
     caseoffset = 26 * 1
   when "1"
     letteroffset = 14 * 0
     caseoffset = 26 * 2
   when "2"
     letteroffset = 14 * 1
     caseoffset = 26 * 2
   when "3"
     letteroffset = 14 * 2
     caseoffset = 26 * 2
   when "4"
     letteroffset = 14 * 3
     caseoffset = 26 * 2
   when "5"
     letteroffset = 14 * 4
     caseoffset = 26 * 2
   when "6"
     letteroffset = 14 * 5
     caseoffset = 26 * 2
   when "7"
     letteroffset = 14 * 6
     caseoffset = 26 * 2
   when "8"
     letteroffset = 14 * 7
     caseoffset = 26 * 2
   when "9"
     letteroffset = 14 * 8
     caseoffset = 26 * 2
   when "0"
     letteroffset = 14 * 9
     caseoffset = 26 * 2
   when "."
     letteroffset = 14 * 10
     caseoffset = 26 * 2
   when "'"
     letteroffset = 14 * 11
     caseoffset = 26 * 2
   when "?"
     letteroffset = 14 * 12
     caseoffset = 26 * 2
   when "!"
     letteroffset = 14 * 13
     caseoffset = 26 * 2
   when ","
     letteroffset = 14 * 14
     caseoffset = 26 * 2
   when " "
     letteroffset = 14 * 26
     caseoffset = 26 * 0
   else
     letteroffset = 14 * 26
     caseoffset = 26 * 0
   end
       
   
   #my_bitmap.blt(x2, y, $bitfont, Rect.new(letteroffset,caseoffset, 14, 26))
   self.blt(x2, y, $bitfont, Rect.new(letteroffset,caseoffset, 14, 26))
   
   x2 += 14
   
 end
end
end


For some reason, the string values of most menu options get removed after seemingly any command menu is closed...
Requesting Rule #1: BE SPECIFIC!!

Light a man a fire and he'll be warm for a night...
Light a man afire and he'll be warm for the rest of his life... ¬ ¬;

My Resources

Blizzard

Use "my_text.clone.slice!" instead of "my_text.slice!". The latter alters your original string, hence it isn't the same later. Generally methods with an exclamation mark at the end are destructive (meaning that they alter calling the object rather than returning a new object after the operation).
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Satoh

November 15, 2009, 04:46:16 pm #2 Last Edit: November 15, 2009, 04:52:31 pm by Satoh
Quote from: Blizzard on November 15, 2009, 04:23:03 pm
Use "my_text.clone.slice!" instead of "my_text.slice!". The latter alters your original string, hence it isn't the same later. Generally methods with an exclamation mark at the end are destructive (meaning that they alter calling the object rather than returning a new object after the operation).


I see, thanks. I didn't know that about the !. I'll try it and see what happens.

EDIT: Well, that SEEMS to have fixed the issues I was having... I'll keep testing... but I think this script is finished... I won't [RESOLVE] until I'm sure though.

I thank you once again, my friend.
Requesting Rule #1: BE SPECIFIC!!

Light a man a fire and he'll be warm for a night...
Light a man afire and he'll be warm for the rest of his life... ¬ ¬;

My Resources

Blizzard

No problem. I'm actually pretty sure that this is what was causing the problem. ^_^
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

fugibo

Quote from: Blizzard on November 15, 2009, 04:23:03 pm
Use "my_text.clone.slice!" instead of "my_text.slice!". The latter alters your original string, hence it isn't the same later. Generally methods with an exclamation mark at the end are destructive (meaning that they alter calling the object rather than returning a new object after the operation).


Or just "my_text.slice"?

Satoh

November 15, 2009, 06:03:52 pm #5 Last Edit: November 15, 2009, 06:05:53 pm by Satoh
Quote from: Longfellow on November 15, 2009, 05:17:04 pm
Quote from: Blizzard on November 15, 2009, 04:23:03 pm
Use "my_text.clone.slice!" instead of "my_text.slice!". The latter alters your original string, hence it isn't the same later. Generally methods with an exclamation mark at the end are destructive (meaning that they alter calling the object rather than returning a new object after the operation).


Or just "my_text.slice"?


No, it removes a single letter everytime a letter is drawn, that way the next letter is drawn instead of the first letter being drawn over and over.

EDIT: I'm gonna do a few more tests, but for the time being, I'm calling this done.

If I understand the usage of the method used in Window_Message...
Requesting Rule #1: BE SPECIFIC!!

Light a man a fire and he'll be warm for a night...
Light a man afire and he'll be warm for the rest of his life... ¬ ¬;

My Resources

fugibo

Have you considered using ".each_byte"? It's slightly inconvenient, but you might be able to trim down a lot of code and potentially speed up your script.

Satoh

Quote from: Longfellow on November 15, 2009, 06:08:50 pm
Have you considered using ".each_byte"? It's slightly inconvenient, but you might be able to trim down a lot of code and potentially speed up your script.


my code is only long due to the case statement that defines each letter/symbol's offset in the image file...
Requesting Rule #1: BE SPECIFIC!!

Light a man a fire and he'll be warm for a night...
Light a man afire and he'll be warm for the rest of his life... ¬ ¬;

My Resources

Fantasist

Yeah, and I think LF is suggesting the use of each_byte because it returns an integer for each alphabet which is it's ASCII code. You can use the ASCII code to get rid of the case statements and reduce the whole thing to a simple formula. You can then simply arrange your font bitmap according to the ASCII order. Check this out, you'll get an idea.
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Blizzard

each_byte also doesn't handle UTF-8 characters.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.