Log In  


Hey there! I wrote a small library to make drawing numbers using sprites quick and easy using the output from string.format.
It can technically 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.

There's also a version that allows you to have leading zeroes displayed as sprites too.

--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 "Leading Zero" 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 "leading zero" sprite.
Example:
	const = {
		sprite_0=8,
		number_width=14,
	}
	sprite_number(string.format("%09i",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("%9i",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("%9i",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,"Input "..input.." can't be converted into a number.")
	--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,"Missing const.sprite_0 or sprite_0 argument. See documentation for details.")
	--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,"Missing const.number_width or sprite_w argument. See documentation for details.")
	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,"Input "..input.." can't be converted into a number.")
	--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,"Missing const.sprite_0 or sprite_0 argument. See documentation for details.")
	--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,"Missing const.number_width or sprite_w argument. See documentation for details.")
	--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,"Missing const.lz_sprite or lz_sprite argument. See documentation for details.")
	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

While I was fixing the "leading zeroes separate sprite" version I decided to make a second font to see how it looked.

4



[Please log in to post a comment]