Calculating the size of a sprite sheet.

New to FreeBASIC? Post your questions here.
Post Reply
SMC
Posts: 33
Joined: Feb 22, 2012 4:05

Calculating the size of a sprite sheet.

Post by SMC »

I made a program to generate a sprite sheet from a list of sprites, but my way of calculating the size for the sheet is broken.

Code: Select all

    If lcount < 4 Then 	''lcount = number of sprites
        sh_col = 2 	''columns
        sh_row = 2	''rows
    Else
        sh_col = Int(Sqr(lcount))
        sh_row = sh_col
        If sh_col * sh_row < lcount Then sh_row = sh_row + 1    
    End If
It takes the square root of the total number of sprites for the rows and columns that are used to calculate the sheet size. As a check it multiplies the rows and columns and tests if lcount is greater. It then adds an extra row. This works in most cases, but not if the lcount number is close to the next square number. For example, If lcount is 8 there won't be enough space for all the sprites because it will only be a 2 x 2 + 1 (6 cell) grid. In most cases there will be a little extra space on the sheet, but I'm trying to keep it from becoming egregious. Worse case scenario I'll just add one to both row and column variables. I'm no math wiz, so any help with this would be appreciated.
RetardedAndProud
Posts: 35
Joined: Jun 09, 2024 18:26

Re: Calculating the size of a sprite sheet.

Post by RetardedAndProud »

If you want the sprite sheet to always be a square, how about...

Code: Select all

#define CEIL(x) (Sgn(x)*(Int(Abs(x)) + Sgn(x)))

Dim As Integer lcount = 8

Print CEIL(Sqr(lcount))
fxm
Moderator
Posts: 12576
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Calculating the size of a sprite sheet.

Post by fxm »

For me, CEIL() is more like:

Code: Select all

#define CEIL(x) (-Int(-(x)))
(Try with 8 then 9)
dodicat
Posts: 8267
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Calculating the size of a sprite sheet.

Post by dodicat »

ceil is defined in crt.bi

Code: Select all

#include "crt.bi"

print "sprites","cols","rows"

for lcount as long =1 to 30
dim as long sh_col,sh_row
    If lcount =1 Then 	''lcount = number of sprites
        sh_col = 2 	''columns
        sh_row = 2	''rows
    Else
        sh_col = ceil(Sqr(lcount))
        sh_row = sh_col
        'If sh_col * sh_row < lcount Then sh_row = sh_row + 1    
    End If
    print lcount,sh_col,sh_row
    if sh_col*sh_row < lcount then print "ERROR"
    next lcount
    
    sleep 
 
Last edited by dodicat on Jun 11, 2025 13:21, edited 1 time in total.
srvaldez
Posts: 3648
Joined: Sep 25, 2005 21:54

Re: Calculating the size of a sprite sheet.

Post by srvaldez »

as for ceil, you can use this identity to write a simple macro for the ceil function; ceil = -Int(-x)

Code: Select all

#ifndef ceil
	#define ceil(x) -Int(-(x))
#endif
SMC
Posts: 33
Joined: Feb 22, 2012 4:05

Re: Calculating the size of a sprite sheet.

Post by SMC »

Thanks for the help. I'm just going to add both an extra row and column, that should give me the same result as the CEIL function would. I'll have a lot of extra space when I get a lower number from the Sqr function, but I'm not going to stress over it anymore. I will be holding on to the CEIL macros you guys provided, though. :D
dodicat
Posts: 8267
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Calculating the size of a sprite sheet.

Post by dodicat »

Here is a sprite sheet array:

Code: Select all


Sub getarray(a() As Long,lim As Long)
    Redim a(1 To lim)
    Dim As Long k=3
    For n As Long=1 To lim
        If n>k*k Then k+=1
        If n<=4 Then
            a(n)=2
        Else
            If n<=k*k Then
                a(n)=k
            End If
        End If
    Next
End Sub

Redim As Long a()
getarray(a(),102)


Print "sprites","cols","rows"
For n As Long=Lbound(a) To Ubound(a)
    Print n,a(n),a(n)
Next
Sleep 
Berkeley
Posts: 115
Joined: Jun 08, 2024 15:03

Re: Calculating the size of a sprite sheet.

Post by Berkeley »

Ain't sensible. I'd make 64 or 32 sprites per line and in case e.g. the screenwidth/32 is the sprite width (and height).
paul doe
Posts: 1878
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: Calculating the size of a sprite sheet.

Post by paul doe »

For what is worth: I just wrote some code to handle this:

Code: Select all

sub sprites_save(fileName as string, sprites() as Fb.Image ptr, w as long, sprSize as long)
  dim as long count = ubound(sprites) + 1
  
  dim as long cols = w \ sprSize
  dim as long rows = count \ cols + abs(count mod cols > 0)
  
  dim as Fb.Image ptr image = imageCreate(cols * sprSize, rows * sprSize, rgba(0, 0, 0, 0))
  
  dim as long xx = 0, yy = 0
  
  for i as integer = 0 to count - 1
    put image, (xx, yy), sprites(i), alpha
    
    xx += sprSize
    
    if (xx >= image->width) then
      xx = 0
      yy += sprSize
    end if
  next
  
  savePNG(fileName, image)
  imageDestroy(image)
end sub
You just give the desired width of the image and the sprite size (works only for square sprites, naturally), and then it calculates the number of rows automatically to fit every sprite needed:

Image

Naturally, it also adjusts the width of the output buffer (regardless of input), so you always end up with an image that is a multiple of the sprite size (however tall; the same principle applies to columns if you want to specify the height and have the image grow horizontally). A bit late, perhaps, but you might find it useful nonetheless.
Post Reply