'*
'* constants used with SL_LOADSPRITESHEET function
'*
CONST SL_SHEETTRANSPARENCY
= -1 ' use sheet's transparency info (.PNG) CONST SL_SETTRANSPARENCY
= 0 ' manually set transparency CONST SL_NOTRANSPARENCY
= 1 ' don't use transparency with sheet '*
'* type declarations
'*
inuse
AS INTEGER ' sheet is in use (true / false) sheetimage
AS LONG ' image handle of sheet spritewidth
AS INTEGER ' width of each sprite spriteheight
AS INTEGER ' height of each sprite transparency
AS INTEGER ' sheet transparency type columns
AS INTEGER ' number of sprite columns'*
'* defined arrays
'*
REDIM SL_sheet
(1) AS SL_SHEET
' sprite sheet list
'*
'* main code (for testing)
'*
tr3bsheet
= SL_LOADSPRITESHEET
("tr3bsheet266x266.png", 266, 266, SL_SETTRANSPARENCY
, _RGB32(255, 0, 255))
PRINT SL_sheet
(tr3bsheet
).inuse
PRINT SL_sheet
(tr3bsheet
).sheetwidth
PRINT SL_sheet
(tr3bsheet
).sheetheight
PRINT SL_sheet
(tr3bsheet
).spritewidth
PRINT SL_sheet
(tr3bsheet
).spriteheight
PRINT SL_sheet
(tr3bsheet
).transparency
PRINT SL_sheet
(tr3bsheet
).transcolor
PRINT SL_sheet
(tr3bsheet
).columns
PRINT SL_sheet
(tr3bsheet
).rows
'######################################################################################################################################
FUNCTION SL_LOADSPRITESHEET
(filename$
, spritewidth%
, spriteheight%
, transparency%
, transcolor~&
) ' SL_LOADSPRITESHEET '+----------------------------------------------------------------------------+---------------------------------------------------+
'| Loads a sprite sheet into the sprite sheet array and assigns an integer | - NOTES - |
'| handle value pointing to the sheet. | |
'| | |
'| Usage : mysheet% = SL_LOADSPRITESHEET("sprites.png", 64, 96, _RGB(0, 1, 0))| |
'| | - Specifying a file name of a file that does not |
'| Input : filename$ - the name of the sprite sheet image | exist will result in the function reporting an |
'| spritewidth% - the width of each sprite on the sheet | error and halting program execution. |
'| spriteheight% - the height of each sprite on the sheet | |
'| transparency% - type of transparency to apply to sheet | |
'| -1 = use sheet's (constant SL_SHEETTRANSPARENCY) | |
'| 0 = user defined (constant SL_SETTRANSPARENCY) | |
'| 1 = no transparency (constant SL_NOTRANSPARENCY) | - The constants SL_SHEETTRANSPARENCY, |
'| transcolor~& - user defined transpareny color of sheet | SL_SETTRANSPARENCY, and SL_NOTRANSPARENCY have |
'| only used when transparency% set to zero (0) | been created to be used with this function. |
'| | |
'| | |
'| Output: integer value greater than 0 pointing to the newly loaded sheet | |
'| | |
'+----------------------------------------------------------------------------+---------------------------------------------------+
'| Known Issues: None |
'| |
'+--------------------------------------------------------------------------------------------------------------------------------+
SHARED SL_sheet
() AS SL_SHEET
' array defining sprite sheets
DIM handle%
' (INTEGER) handle number of new sprite sheet DIM x%
, y%
' (INTEGER) generic counters DIM osource&
' (LONG) original source image DIM pixel~&
' (_UNSIGNED LONG) pixel color at x%, y% coordinate DIM alpha~&
' (_UNSIGNED LONG) alpha level of current pixel
SL_SPRITEERROR "SL_LOADSPRITESHEET", 106, filename$ ' no, report error to programmer
IF ABS(transparency%
) > 1 THEN ' valid transparency setting? SL_SPRITEERROR "SL_LOADSPRITESHEET", 108, "" ' no, report error to programmer
IF spritewidth%
< 1 OR spriteheight%
< 1 THEN ' valid sprite width/height supplied? SL_SPRITEERROR "SL_LOADSPRITESHEET", 109, "" ' no, report error to programmer
handle% = 0 ' initialize handle value
DO ' look for the next available handle handle% = handle% + 1 ' increment the handle value
LOOP UNTIL (NOT SL_sheet
(handle%
).inuse
) OR handle%
= UBOUND(SL_sheet
) ' stop looking when valid handle value found IF SL_sheet
(handle%
).inuse
THEN ' is the last array element in use? handle% = handle% + 1 ' yes, increment the handle value
REDIM _PRESERVE SL_sheet
(handle%
) AS SL_SHEET
' increase the size of sprite sheet array SL_sheet
(handle%
).sheetimage
= _LOADIMAGE(filename$
, 32) ' assign the image to the array SL_sheet(handle%).inuse = -1 ' (TRUE) mark this element as being used
SL_sheet
(handle%
).sheetwidth
= _WIDTH(SL_sheet
(handle%
).sheetimage
) ' save the width of the sprite sheet SL_sheet
(handle%
).sheetheight
= _HEIGHT(SL_sheet
(handle%
).sheetimage
) ' save the height of the sprite sheet SL_sheet(handle%).spritewidth = spritewidth% ' save the width of each sprite
SL_sheet(handle%).spriteheight = spriteheight% ' save the height of each sprite
SL_sheet(handle%).columns = SL_sheet(handle%).sheetwidth \ spritewidth% ' number of sprite columns on sheet
SL_sheet(handle%).rows = SL_sheet(handle%).sheetheight \ spriteheight% ' number of sprite rows on sheet
SL_sheet(handle%).transparency = transparency% ' sheet transparency type
IF transparency%
= -1 THEN ' (constant SL_SHEETTRANSPARENCY) sheet have alpha channel? x% = 0 ' yes, start at upper left x of sheet
y% = 0 ' start at upper left y of sheet
osource&
= _SOURCE ' remember current source _SOURCE SL_sheet
(handle%
).sheetimage
' set the sprite sheet image as the source image DO ' start looping through the sheet's pixels pixel~&
= POINT(x%
, y%
) ' get the pixel's color attributes alpha~&
= _ALPHA32(pixel~&
) ' get the alpha level (0 - 255) IF alpha~&
= 0 THEN EXIT DO ' if it is transparent then leave the loop x% = x% + 1 ' move right one pixel
IF x%
> SL_sheet
(handle%
).sheetwidth
THEN ' have we gone off the sheet? x% = 0 ' yes, reset back to the left beginning
y% = y% + 1 ' move down one pixel
LOOP UNTIL y%
> SL_sheet
(handle%
).sheetheight
' don't stop until the entire sheet has been checked _SOURCE osource&
' return source to current IF alpha~&
= 0 THEN ' did we find a transparent pixel? SL_sheet(handle%).transcolor = pixel~& ' yes, set the sheet's transparency to this color
ELSE ' no transparency found within sheet SL_sheet(handle%).transparency = 1 ' set to sheet having no alpha channel
ELSEIF transparency%
= 0 THEN ' (constant SL_SETTRANSPARENCY) manually set alpha channel? SL_sheet(handle%).transcolor = transcolor~& ' yes, set alpha channel
_CLEARCOLOR transcolor~&
, SL_sheet
(handle%
).sheetimage
' apply alpha channel to sheet SL_LOADSPRITESHEET = handle% ' return the handle number pointing to this sheet
'######################################################################################################################################
SUB SL_SPRITEERROR
(routine$
, errno%
, info$
) ' SL_SPRITEERROR '+----------------------------------------------------------------------------+---------------------------------------------------+
'| Reports a Sprite Library error to the programmer. Used internally only. | - NOTES - |
'| | This routine will report the error to the |
'| Input : routine$ - the function/subroutine the error occurred in | programmer and halt the program |
'| | |
'| Input : errno% - the error number associated with the error | |
'| 100 - sprite does not exist | |
'| 101 - sprite is not in use | |
'| 102 - sprite can't be hidden | |
'| 103 - invalid zoom value | |
'| 104 - invalid rotation angle | |
'| 105 - invalid flipping behavior | |
'| 106 - sheet does not exist | |
'| 107 - sheet is not in use | |
'| 108 - invalid transparency setting | |
'| 109 - invalid sprite width/height | |
'| | |
'| info$ - any information that need to be conveyed with the error | |
'| such as a file name | |
'| | |
'+----------------------------------------------------------------------------+---------------------------------------------------+
'| Known Issues: None |
'| |
'+--------------------------------------------------------------------------------------------------------------------------------+
'| Credits: This routine was created in response to a request from QB64 member pitt |
'| http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=7281.0 (link no longer works) |
'+--------------------------------------------------------------------------------------------------------------------------------+
SCREEN 0, 0, 0, 0 ' go to a pure text screen _FONT 16 ' set the standard screen 0 font PRINT "**" ' print error header PRINT "** Sprite Library error encountered" PRINT routine$;
" has reported the following error:" ' print sub/function reporting the error SELECT CASE errno%
' which error number is being reported? PRINT "- the specified sprite does not exist" PRINT "- it was either never created or removed with SPRITEFREE" PRINT "- the specified sprite is not in use" PRINT "- it was probably removed with SPRITEFREE" PRINT "- the specified sprite can't be hidden" PRINT "- change the sprite's behavior in SPRITENEW to save background" PRINT "- the zoom value specified must be greater than zero" PRINT "- the angle specified must be 0 to 360" PRINT "- invalid flipping behavior specified - valid settings are" PRINT "- : 0 = no flipping (constant SL_NONE)" PRINT "- : 1 = flip horizontal (constant SL_HORIZONTAL)" PRINT "- : 2 = flip vertical (constant SL_VERTICAL)" PRINT "- : 3 = flip both (constant SL_BOTH)" PRINT "- ";
CHR$(34); info$;
CHR$(34);
" sprite sheet does not exist" PRINT "- check path or spelling" PRINT "- the specified sprite sheet is not in use" PRINT "- invalid transparency setting supplied - valid settings are" PRINT "- : -1 (constant SL_SHEETTRANSPARENCY)" PRINT "- : 0 (constant SL_SETTRANSPARENCY)" PRINT "- : 1 (constant SL_NOTRANSPARENCY)" PRINT "- sprite width and height must be greater than zero"
'##################################################################################################################################