TheV360/Love2DSmileBASICLibrary

Sprite Definitions file could be much smaller

Opened this issue · 6 comments

The sprite definition file could be much smaller, as you can remove values (while keeping their commas) to set them to their defaults. Any missing values will also be set to their defaults.

Here's a few expressions that will help:

// Replace u if it matches the default.
uMatch = /^(0)(,\d*,\d*,\d*,\d*,\d*,\d*)/gm;
uReplace = "$2";

// Replace v if it matches the default.
vMatch = /^(\d*)(,0)(,\d*,\d*,\d*,\d*,\d*)/gm;
vReplace = "$1,$3";

// Replace width and height if they match
whMatch = /^(\d*,\d*)(,16,16)(,\d*,\d*,\d*)/gm;
whReplace = "$1,,$3";

// Replace Home with commas if they match the defaults.
hMatch = /^(\d*,\d*,\d*,\d*)(,0,0)(,\d*)/gm;
hReplace = "$1,,$3";

// Replace Home and attributes with commas if they match the defaults.
haMatch = /^(\d*,\d*,\d*,\d*)(,0,0,1)/gm;
haReplace = "$1,,,";

// Remove most trailing commas, as they aren't needed.
// Only one comma is needed to get Lua to know if this is a definition.
// Note to self: Maybe check if even that can be removed?
commaMatch = /,{2,}$/gm;
commaReplace = "";

I literally just wrote them, so they may be broken. I'll do some more testing later.

Edit: fixed first one, added ^ to other ones
Edit: fixed last one. Some of these seem to be broken.
Edit: duh

I'm pretty sure definitions 0 to 2047 are just copies of 2048 to 4095 but with the home x and y set to 0.

EDIT: Smileboom made a few mistakes though:
3456 has the wrong attributes
1459-1482 should've been duplicated at 3507-3530, but they forgot to duplicate 1459.
So 1460-1482 are duplicated at 3507-3529, and 3530 is unused.

There's also a copy of the smilebasic logo at 4095 which doesn't exist at 2047, which is probably intentional.

Fixed the empty line issue. Should I try to fix the mistakes SmileBoom made, or should I keep them the same for accuracy? For now, I'm keeping the definitions, as they're direct copies of the SPDEFs from 3.2.1.

Wait, have the SPDEFs changed since 3.2.1?

They've made a few changes since then:
http://smilebasic.com/en/debug/archive/

Anyway, you only really have to store data for 1483 definitions, since the rest are duplicates or default:
(This is untested of course)

--(This assumes you've already read the data for sprites 2048 to 3529)

local offset=2048
--Copy sprites without home:
for i=0,1482 do
	-- Definition that smileboom forgot to copy:
	if i==1459 then
		Sprites.Definitions[i] = {u=464,v=448,w=16,h=64,home={x=0,y=0},attributes=1}
		offset=offset-1 -- All the other definitions are now off by 1 because of that mistake
	-- Normal:
	else
		local source = Sprites.Definitions[i+offset]
		Sprites.Definitions[i] = {
			u = source.u,
			v = source.v,
			w = source.w,
			h = source.h,
			home = {x=0,y=0},
			attributes = source.attributes,
		}
		-- Wrong attribute:
		if i==1408 then
			Sprites.Definitions[i].attributes = 9
		end
	end
end

--1483 to 2047 are the same as 0, and 3530 to 4094* are the same as 2048
for i=1483,2047 do
	Sprites.Definitions[i] = Sprites.Definitions[0]
	Sprites.Definitions[i+2048] = Sprites.Definitions[2048]
end
--*4095 is an extra smileboom logo:
Sprites.Definitions[4095] = {u=192,v=480,w=96,h=32,home={x=48,y=16},attributes=1}

I might just add this:

~0,0,16,16,0,0,1

This will temporarily change the default definition to the data after it, while still adding the data.
I may also add

*2048

Which will repeat a definition until it reaches the specified line.

I sorta want to hard code as little as possible, but I have to keep in mind that nobody is going to pull out a random (non-class) function from this library and expect it to work immediately.

Still, if you have Citra, the latest SmileBASIC version, and some time, I'd Love2D it if you could run this:

PRGEDIT 1
PRGDEL -1

FOR I=0 TO 4095
 SPDEF I OUT U,V,W,H,OX,OY,A
 PRGINS FORMAT$("%D"*7,U,V,W,H,OX,OY,A)
NEXT

Like how the only diff is Line 167

Might not do anything I listed in the previous post, because I'm lazy I think savings of a few kilobytes shouldn't be a top priority feature.

May return to this later, but for now I need to finish animations.