Chronosv2 [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=2634 Draw Number Sprites <p>Hey there! I wrote a small library to make drawing numbers using sprites quick and easy using the output from <code>string.format</code>.<br /> It can <em>technically</em> be used for non-integer values but any symbol that can't be drawn as a number will just be skipped over and a digit-wide space will be left. I wrote it this way so that you can draw a number with either leading zeroes or leading spaces.</p> <img style="margin-bottom:16px" border=0 src="/media/2634/6_Screenshot 2024-03-21 155646.png" alt="" /> <img style="margin-bottom:16px" border=0 src="/media/2634/Screenshot 2024-03-21 164656.png" alt="" /> <p>There's also a version that allows you to have leading zeroes displayed as sprites too.</p> <img style="margin-bottom:16px" border=0 src="/media/2634/Screenshot 2024-03-21 170044.png" alt="" /> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--sprite_num.lua --[[ Description: Implements a function for drawing numbers out of sprites. Useful for non-text numbers, like scores for arcade games. Best used with numbers formatted with 'string.format' Requirements: Number sprites need to be stored in contiguous order from 0-9 (0123456789) Optional: A 'const' table containing the following: const.sprite_0 : Integer value holding the sprite ID of the 0 digit. const.number_width : Integer value holding the width of each number sprite. const.lz_sprite : Integer value holding the &quot;Leading Zero&quot; sprite for sprite_number_lz If the const table isn't used, this function call requires its optional arguments. Usage: sprite_number(input,x,y,[sprite_0,sprite_w]) sprite_number_lz(input,x,y,lz_sprite,[sprite_0,sprite_w]) input : String of numbers. x : Integer providing the horizontal location of the top-left pixel of the first digit y : Integer providing the vertical location of the top-left pixel of the first digit sprite_0 : Integer providing the sprite ID of the 0 digit. (Required if not using const.sprite_0, or if using the sprite_w argument) sprite_w : Integer providing the width of each number sprite. (Required if not using const.number_width) For sprite_number_lz: lz_sprite : Integer providing the &quot;leading zero&quot; sprite. Example: const = { sprite_0=8, number_width=14, } sprite_number(string.format(&quot;%09i&quot;,score),352,7) Draws a 9-digit zero-padded variable 'score' starting at 352,7, with the zero sprite at sprite 8 and the numbers 14 pixels wide. sprite_number(string.format(&quot;%9i&quot;,score),352,7,8,14) Draws a 9-digit space-padded variable 'score' starting at 352,7 but defines the zero sprite and width manually. sprite_number_lz(string.format(&quot;%9i&quot;,score),352,7) Draws a 9-digit zero-padded variable 'score' using leading zero sprites. ]] function sprite_number(input,x,y,sprite_0,sprite_w) --If 'input' can't be converted into a number, we can't operate on it. assert(tonumber(input) != nil,&quot;Input &quot;..input..&quot; can't be converted into a number.&quot;) --Get the location for the 0 sprite in the graphics table if sprite_0 == nil then sprite_0 = const.sprite_0 end --Get the digit width if sprite_w == nil then sprite_w = const.number_width end --If sprite_0 is nil after trying to get its value, it wasn't set in const.sprite_0 or as an argument. assert(sprite_0 != nil,&quot;Missing const.sprite_0 or sprite_0 argument. See documentation for details.&quot;) --If sprite_w is nil after trying to get its value, it wasn't set in const.number_width or as an argument. assert(sprite_w != nil,&quot;Missing const.number_width or sprite_w argument. See documentation for details.&quot;) for i=1,#input do --Parse the number string local number = tonumber(sub(input,i,i)) --Convert a digit in the string to a number if number != nil then --If the value in 'number' is a number (not a space or some other symbol spr(sprite_0+number,x+((i-1)*sprite_w),y) --Draw it to the screen with the number sprite end end end function sprite_number_lz(input,x,y,lz_sprite,sprite_0,sprite_w) --If 'input' can't be converted into a number, we can't operate on it. assert(tonumber(input) != nil,&quot;Input &quot;..input..&quot; can't be converted into a number.&quot;) --Get the leading zero sprite number if lz_sprite == nil then lz_sprite = const.lz_sprite end --Get the location for the 0 sprite in the graphics table if sprite_0 == nil then sprite_0 = const.sprite_0 end --Get the digit width if sprite_w == nil then sprite_w = const.number_width end --If sprite_0 is nil after trying to get its value, it wasn't set in const.sprite_0 or as an argument. assert(sprite_0 != nil,&quot;Missing const.sprite_0 or sprite_0 argument. See documentation for details.&quot;) --If sprite_w is nil after trying to get its value, it wasn't set in const.number_width or as an argument. assert(sprite_w != nil,&quot;Missing const.number_width or sprite_w argument. See documentation for details.&quot;) --If lz_sprite is nil after trying to get its value, it wasn't set in const.lz_sprite or as an argument. assert(lz_sprite != nil,&quot;Missing const.lz_sprite or lz_sprite argument. See documentation for details.&quot;) local nz_reached = false for i=1,#input do --Parse the number string local number = tonumber(sub(input,i,i)) --Convert a digit in the string to a number if nz_reached then if number != nil then --If the value in 'number' is a number (not a space or some other symbol spr(sprite_0+number,x+((i-1)*sprite_w),y) --Draw it to the screen with the number sprite nz_reached = true --Non-zero digit reached end else if number != nil then --If the value in 'number' is a number (not a space or some other symbol if number != 0 or i==#input then --If the number is non-zero or the last digi spr(sprite_0+number,x+((i-1)*sprite_w),y) --Draw it to the screen with the number sprite nz_reached = true --Non-zero digit reached else spr(lz_sprite,x+((i-1)*sprite_w),y) --Draw it to the screen with the number sprite end end end end end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>While I was fixing the &quot;leading zeroes separate sprite&quot; version I decided to make a second font to see how it looked.</p> <img style="margin-bottom:16px" border=0 src="/media/2634/Screenshot 2024-03-21 182752.png" alt="" /> https://www.lexaloffle.com/bbs/?tid=140975 https://www.lexaloffle.com/bbs/?tid=140975 Thu, 21 Mar 2024 20:57:40 UTC