Log In  


Cart #shpr-0 | 2024-09-11 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
3

A remake of the SPR() sprite function that i used as a way to become more familiar with the software. Enjoy!

--shpr (spr, but bad)
function shpr(s,x,y,w,h,fx,fy)

	--default position and size
	--if not declated.
	w=(w or 1)*8
	h=(h or 1)*8
	x=x or 0
	y=y or 0

	--default flip to false
	--if not declared.
	fx=fx or false
	fy=fy or false

	offx=0
	offy=0
	s=flr(s)

	--changing offsets if the
	--sprite is fliped
	if fx==true or fy==1 then
		fx=-1
		offx=w-1
	else
		fx=1
	end
	if fy==true or fy==1 then
		fy=-1
		offy=h-1
	else
		fy=1
	end

	offs=0
	drawns=s

 --checkign where the sprite is
 --based on the sprite index.
	if s>15 then
		repeat
			offs+=8
			drawns-=16
		until s-(offs*2)<16
	end

	--repeat for the entire sprite. size
	for px=0,w-1 do
		for py=0,h-1 do
			--check colour of
			--current pixel.
			local col=sget(drawns*8+px,py+offs)

			--checking if palt
			--has been used.
			--the value is +16 if
			--transparent.
			if (peek(0x5f00+col)<16) then
				--drawing the current
				--pixel to screen.
				pset(x+(px*fx)+offx,y+(py*fy)+offy,col)
			end		
		end
	end
end
3


1

Not bad as an exercise, I'll steal the idea for my students if you don't mind.

You have to deal with parameter truncation ( s=flr(s) ), or not (w and h NOT truncated), default values, 2D array arithmetic (you could have uses % and \ ), nested loops with computed boundaries, memory peeks, understanding of pal(), (pset does the color translation).

An overall very complete exercise. Ready to code tline() ?

Edit :
LUA Booleans are a bit unusual compared to other languages :
NIL is false, Boolean false is also false, and everything else is true.
This means number zero, empty string or empty array are all considered true, but your code will only accept true and number one as true.
You can simply use fx and fy as booleans in your code to get the same behaviour as the spr function.


@RealShadowCaster Thanks so much for checking my project out! Still a relative novice, so thanks a bunch for your feedback, I'll try and fix what i can. Feel free to use the project however you want :)



[Please log in to post a comment]