Log In  


Cart #print_sprite-4 | 2025-07-10 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

A simple function for inserting sprites in the middle of text on screen.
There's a second version that can also highlight text. I made them different to reduce overhead if you only need the first one.

prtspr() arguments:

txtbl: text table, combo of strings and sprite numbers. Example: {"hello",5,"world"}
x,y: line position
c: text color
spry: adjust sprite y pos (optional argument)

prtspr_2() arguments:

txtbl: text table, combo of strings and sprite numbers. Example: {"hello",5,"world"}
x,y: line position
ctx: text color
chi: highlight color
spry: adjust sprite y pos (optional argument)
cdrp: drop shadow color

the code

function prtspr(txtbl,x,y,c,spry)
	local lx=x --current x on line
	for t in all(txtbl) do
		if tonum(t) == nil then
			--is text
			print(t,lx,y,c)
			local len=#t
			lx += (len)*4
		else
			--is sprite number
			local sy = y-1
			if (spry) sy+=spry
			spr(t,lx,sy)
			lx += 9
		end
	end
end

A more complex version of the function with text highlight if string start with a special char like ^. The highlight character is removed when printing the string.

Valid special characters:

  • ^ : Higlight text with color
  • ~ : Add drop shadow
  • ♪ : Highlight AND drop shadow
function prtspr_2(txtbl,x,y,ctx,chi,spry,cdrp)
	local lx=x --current x on line
	for t in all(txtbl) do
		if tonum(t) == nil then
			--if entry is text
			--check for starting ^ to highlight
			local col = ctx
			local len=#t
			local start=1
			local firstc = sub(t,1,1)
			local fx = 0
			--effects if string starts with special character
			if (firstc == "^") fx += 1 --highlight
			if (firstc == "~") fx += 2 --drop shadow
			if (firstc == "♪") fx += 3 --both hi & drop (♪=shift n)
			local c = ctx
			if fx > 0 then
				len-=1
				start=2
				if fx&1 > 0 then
					--highlight
					c = chi
				end
				if fx&2 > 0 then
					--drop shadow
					print(sub(t,2),lx+1,y+1,cdrp)
				end
			end

			--print the text
			print(sub(t,start),lx,y,c)
			--move the typehead
			lx += (len)*4
		else
			--if entry is sprite number
			local sy = y-1
			--move sprite up if spry is defined
			if (spry) sy+=spry
			spr(t,lx,sy)
			--move the typehead
			lx += 9
		end
	end
end

Example use

rbw=7

function _draw()
	cls(0)
	-- examples
	prtspr({"hello",4,"world"},30,10,12)

	prtspr({3,"did you know you can\nput sprites in text?"},8,25,8,3)

	prtspr({"just ",1," like ",2," this"},17,50,7,-1)

	print("highlights and drop shadows:",10,65,12)
	--alternate highlight function
	prtspr_2({"you got a ","^jelpi ",5,"!"},17,75,7,14,-1)

	prtspr_2({"here's a ","♪drop ","~shadow"},17,85,7,14,-1,5)

	--switching the highlight color, done separate from the function
	rbw = max(7,(rbw+.1)%15)
	prtspr_2({"♪rainbow!"},17,95,7,rbw,-1,2)
end



[Please log in to post a comment]