Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sprite Sheet/Strip generator
#1
Hello friends.  This is a thing I put together to create sprite strips and sheets from other images.  It's pretty limited, but it does everything I need it to.  There are a lot of features I could probably add, but I feel like my functions are already convoluted enough without redesigning them all.  A lot of the instructions below are repeated in the source.  Let me know if you find any bugs or issues.  I'll try to fix them.  Otherwise, everything is provided as-is with a 100% money back guarantee with no apologies for egregious code commenting.   Smile
  
Basic Usage:
It can take a list of images entered at the command line and build a sprite sheet/strip from them.  You can also list your images in a .txt file and pass the text file to it.  The two can be combined as well and the sheet/strip will be built in the order given.  Images will be added to sheet/strips left to right then top to bottom.
If it is ran without any arguments, it will parse the folder it's in for images and build a sheet/strip that way.  Please note, it will combine them in the order the file system gives and that may not match what your file browser shows.  
If using a .txt file to list the images, each image name must have it's own line, include the file name extension, and not use quotes.  Blank lines are ignored.  It uses the first image loaded to determine the size of the sheet/strip as well as the rest of the image cells.  Images that don't match the size of the first image will be resized to fit the cell.

The program will prompt you for names to save your sheet/strip to.  Saving images uses qb64pe _SaveImage function.  All the limitations or quirks of that function apply.  When entering a name to save the image as .png will be the default if an image extension isn't given.  If no filename is given, it generates a filename that includes the size of the sheet/strip generated.

There are three command line options available, and these can be put inside the .txt file as well.  They shouldn't be used on the same line as an image name, but can be grouped together on their own line.  All of them can be used together, and a sheet and strip can be built at the same time.
-sh -- Creates a sprite sheet (default option)
-st  -- Creates a sprite strip
-8   -- Creates an 8bit image that uses the qb64pe std 256 palette.  Mode 257 couldn't be used because if the sprites had different palettes, they would overwrite one another.

EDIT: fixed some bugs.

ssheet.bas
Code: (Select All)

''------------------------------------------------------------------------------
''Sprite Sheet Builder v1.1 - CMR
''last revision December 2, 2025
''Takes image files and formats them into a single sheet or strip image.

''Usage: Can be given a list of image files, or a .txt file containing a
''list of images and build a sprite sheet or strip from them.  Text file lists
''and image names at the command prompt can be combined.  No quotes ("") should
''be used in the .txt files, and each image name should be on its own line.
''Images are added to the sprite sheet/strip in the order listed at the prompt
''and in .txt file.

''Entering no file at the prompt causes the program to build a sheet from
''all images contained in the current directory.  In this case the order will
''be how they are given by the directory. WARNING: This might not match what is
''shown inside your file manager.
''The size of the sheet/strip is determined by the first image entered.  All
''other images will be resized to fit if necessary.

''Options: -st -sh -8
''  -st  -- creates a sprite strip
''  -sh  -- creates a sprite sheet
''  -8  -- creates an 8 bit sprite sheet/strip

''  note: Both options can be used, and program will generate a sheet and strip.
''  8 bit sheet generation relies on mode 256.  257 caused bugs with the colors
''  when images used different palettes.  Options can also be included in the
''  text file with the image lists.  Options can all be on the same line, but
''  image file names must be on their own line.
''------------------------------------------------------------------------------


''Program Start
Option _Explicit
Const SPRITE_LIMIT = 300 ''number of images that can be loaded

Dim bpp& ''bits per pixel
Dim f_names$(1 To SPRITE_LIMIT) ''file name list
Dim n_count& ''current number of names on the list
Dim opt$ ''command line options


''check files passed at the command line. If it's a text file, parse it for
''file names to images.  If it's an image file, parse the command string
''for other image files to combine.  If nothing is passed, scan the directory
''for image files.
If _CommandCount > 0 Then
    CmdLineGet opt$, f_names$(), n_count&
End If

''check if a file name list was made
If n_count& = 0 Then
    ''scan for images in the current directory
    LoadFromDir f_names$(), n_count&
End If


''command line switches
''Set the bits per pixel to use
bpp& = 32
If InStr(LCase$(opt$), "-8") Then bpp& = 256

''Create a new image that will hold all the other images.  Both can be created.
Print "Bulding Sprite Sheet/Strip......"
Dim sheet&, strip&

If InStr(LCase$(opt$), "-sh") _AndAlso InStr(LCase$(opt$), "-st") Then
    Print "-sh -st"
    CreateSheet sheet&, f_names$(), n_count&, bpp&
    CreateStrip strip&, f_names$(), n_count&, bpp&
ElseIf InStr(LCase$(opt$), "-st") Then
    Print "-st"
    CreateStrip strip&, f_names$(), n_count&, bpp&
Else
    CreateSheet sheet&, f_names$(), n_count&, bpp&
End If

''save the images to disk
SaveShSt sheet&, strip&
If sheet& <> 0 Then _FreeImage sheet&
If strip& <> 0 Then _FreeImage strip&

End ''shutdown

'$Include: 'ssheet.bm'


ssheet.bm
Code: (Select All)

'''sprite sheet functions for creating sprite sheets v1.1
$IncludeOnce

Sub CmdLineGet (cmd_opt$, names_list$(), list_count&)
    ''load image file names and options from command prompt
    Dim suf$ ''file name extension suffix .*
    Dim cmd_count&

    While cmd_count& < _CommandCount
        ''test what type of file it is by its extension
        cmd_count& = cmd_count& + 1
        suf$ = Right$(Command$(cmd_count&), 4)

        Select Case suf$
            Case ".png",".bmp",".jpg",".tga",".psd",".gif",".hdr", _
            ".pic",".pnm",".pcx",".svg",".ico",".cur",".qoi"
                list_count& = list_count& + 1
                names_list$(list_count&) = Command$(cmd_count&)
            Case ".txt"
                ''load the text file information
                LoadFromTxt Command$(cmd_count&), names_list$(), list_count&, cmd_opt$
            Case Else
                cmd_opt$ = cmd_opt$ + Command$(cmd_count&)
        End Select
    Wend

End Sub ''CmdLineGet


Sub LoadFromTxt (txt_file$, names_list$(), list_count&, option$)
    ''load a list of image file names from a txt file
    Dim fp&: fp& = FreeFile
    Dim tmp$

    Dim f_ext$
    f_ext$ = ".png.bmp.jpg.tga.psd.gif.hdr.pic.pnm.pcx.svg.ico.cur.qoi"

    Open txt_file$ For Input As #fp&
    While Not EOF(fp&)
        Line Input #fp&, tmp$
        tmp$ = _Trim$(tmp$)
        If tmp$ <> "" Then
            ''test for image suffix .*
            If InStr(f_ext$, Right$(tmp$, 4)) Then
                ''detect if the file is available to load
                If _FileExists(tmp$) Then
                    ''add file name to list
                    list_count& = list_count& + 1
                    names_list$(list_count&) = tmp$
                Else
                    Print "WARNING: unable to locate: " + tmp$
                End If
            Else
                ''detect if element is an option or bad file name
                If Left$(tmp$, 1) = "-" Then
                    option$ = option$ + tmp$
                Else
                    Print "Unkown file type, " + tmp$ + " listed in " + txt_file$
                End If
            End If
        End If
    Wend
    Close fp&

End Sub ''LoadFromTxt


Sub LoadFromDir (names_list$(), list_count&)
    ''load a list of image file names from the current directory
    Dim mr_file$

    Dim f_ext$
    f_ext$ = ".png.bmp.jpg.tga.psd.gif.hdr.pic.pnm.pcx.svg.ico.cur.qoi"

    ''get the files
    mr_file$ = _Files$("*.*") ''only get the files with an extension
    While mr_file$ <> ""
        If InStr(f_ext$, Right$(mr_file$, 4)) Then
            list_count& = list_count& + 1
            names_list$(list_count&) = mr_file$
        End If
        ''get next file name
        mr_file$ = _Files$
    Wend

End Sub ''LoadFromDir


Sub CreateSheet (img_sheet&, names_list$(), list_count&, mode&)

    ''Create a sprite sheet for the images on the list
    Dim irow&, icolumn&, iwidth&, iheight&, icount&, itmp&

    ''test if list is valid
    If list_count& = 0 Then Print "Unable to CreateSheet": Exit Sub

    ''get the rows and columns for the sheet
    If list_count& >= 4 Then
        irow& = Int(Sqr(list_count&))
        icolumn& = irow&
        ''pad the rows if needed
        While (irow& * icolumn&) < list_count&
            irow& = irow& + 1
        Wend
    Else
        irow& = 2
        icolumn& = 2
    End If

    ''calculate the size of the sprite sheet, create it based off first image
    If names_list$(1) <> "" Then itmp& = _LoadImage(names_list$(1), mode&)
    If itmp& < -1 Then
        img_sheet& = _NewImage((_Width(itmp&) * icolumn&), _
                        (_Height(itmp&) * irow&), _
                        mode&) ''get the number bits
        iwidth& = _Width(itmp&) ''set these for _putimage because some
        iheight& = _Height(itmp&) ''image may be different sizes
        _FreeImage (itmp&)
    Else
        Print "Unable to load image from list": Exit Sub
    End If

    ''draw the images to the buffer
    Dim ydx&, xdx&
    For ydx& = 0 To irow& - 1
        For xdx& = 0 To icolumn& - 1
            icount& = icount& + 1

            If names_list$(icount&) = "" Then Exit Sub
            itmp& = _LoadImage(names_list$(icount&), mode&)

            If itmp& = -1 Then Print "Unable to load file: " + names_list$(icount&): System

            _PutImage(xdx& * iwidth&, ydx& * iheight&)- _
                ((xdx& * iwidth&) + iwidth&, (ydx& * iheight&) + iheight&), _
                itmp&, img_sheet&
            _FreeImage (itmp&)
        Next
    Next

End Sub ''CreateSheet


Sub CreateStrip (img_strip&, names_list$(), list_count&, mode&)
    ''Create a sprite strip for the images on the list
    Dim iwidth&, iheight&, icount&, itmp&

    ''test if list is valid
    If list_count& = 0 Then Print "Unable to CreateStrip": Exit Sub

    ''calculate the size of the strip and create it based off the first image
    If names_list$(1) <> "" Then itmp& = _LoadImage(names_list$(1), mode&)
    If itmp& < -1 Then
        img_strip& = _NewImage(_Width(itmp&) * list_count&, _
                    _Height(itmp&), _
                    mode&)
        iwidth& = _Width(itmp&) ''set these for put image because some images
        iheight& = _Height(itmp&) ''may be different sizes
        _FreeImage (itmp&)
    Else
        Print "Unable to load image from list": Exit Sub
    End If

    ''draw the images to the buffer
    Dim xdx&
    For xdx& = 0 To list_count& - 1
        icount& = icount& + 1

        If names_list$(icount&) = "" Then Exit Sub
        itmp& = _LoadImage(names_list$(icount&), mode&)

        If itmp& = -1 Then Print "Unable to load file: " + names_list$(icount&): System

        _PutImage(xdx& * iwidth&, 0)-((xdx& * iwidth&) + iwidth&, _
                  iheight&), itmp&, img_strip&
        _FreeImage (itmp&)
    Next

End Sub ''CreateStrip


Sub SaveShSt (sh&, st&)
    ''Save the sprite image sheet/strip
    Dim tmp$
    Dim f_ext$
    f_ext$ = ".png.bmp.jpg.tga.psd.gif.hdr.pic.pnm.pcx.svg.ico.cur.qoi"

    ''save sprite strip
    tmp$ = ""
    If st& <> 0 Then
        Print ""
        Input ; "Enter a name for the sprite strip image: ", tmp$
        If tmp$ = "" Then ''set default name
            tmp$ = "strip" + Str$(_Width(st&)) + "x" + Str$(_Height(st&))
        End If
        _SaveImage tmp$, st&
    End If

    ''save sprite sheet
    tmp$ = ""
    If sh& <> 0 Then
        Print ""
        Input ; "Enter a name for the sprite sheet image: ", tmp$
        If tmp$ = "" Then ''set default name
            tmp$ = "sheet" + Str$(_Width(sh&)) + "x" + Str$(_Height(sh&))
        End If
        _SaveImage tmp$, sh&
    End If

End Sub ''SaveImage


Here is an example of how a .txt file should be created.  Images will be added to the sheet/strip in the order given.  Command line switches can be put on any line and can even be seperate.  I just put them on the bottom in this example.  Don't put them on the same line as an image file name.

Img1.png
img2.png
xyz.bmp
img3.png
img4.bmp
abc.png

-sh -st 
-8
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)