[bb] Load Image, Sound, Music and AnimImage with basic error checking by Dan [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Load Image, Sound, Music and AnimImage with basic error checking
Author : Dan
Posted : 1+ years ago

Description : Here are 3 functions CheckLoad,CheckAnimLoad  and CheckLoadError

Replace your LoadImage, LoadSound and LoadMovie with CheckLoad function and
LoadAnimImage with CheckAnimLoad.

This function gives you a basic Error checking:
does the file exists, is the filename a directory, is the filesize 0
and do the above commands load the image properly.

optionaly, you can set the function to check the filesize, to ensure that the files arent modified or replaced.

Usage:

if you dont use the functions as Include file, then paste following lines to the beginning of your program:

Global Lerror=0 ;Set in main program
Dim LerrorDat$(100,1) ;Set in main program and make sure the dimension is high enough to hold the number of loading files


Make sure that the x of LerrorDat$(x,y) has the number of your files, which need to be checked at one time. (the x=0 is not used at all)

As the Graphic files do cause Memmory access violation if the images arent there, you can use this functions as following:

setup your program to preload the images,sprites,graphics as first.
then use the CheckLoadError() function to check if any error has occured.
(checkLoadError displays every filename and the art of the error detected !)

Then you can Preload sounds and use the CheckLoadError function again.
The lerror variable is reseted to 0 after each CheckLoadError call.

example: (will produce errors as the files arent there !)

Global Lerror=0 ;Set in main program
Dim LerrorDat$(100,1) ;Set in main program and make sure the dimension is high enough to

mg_doors=CheckAnimload("gfxDoors24x24.png",24,24,0,12)
img_elevadoor=CheckAnimload("gfxelevatordoor40x32.png",40,32,0,6)
img_hoteldeco=CheckLoad("gfxHotel-deco.png")
img_smallplant=CheckLoad("gfxSmalllPlant.png")
img_elevator=CheckLoad("gfxelevator.png")

img_roomitems=CheckAnimload("gfxRoomItems24x45.png",24,45,0,7)
;Multi Game sprites or images definitions

Spr_Bullet02=CheckLoad("gfxLaser01.png") ;Game 01
Spr_jones=CheckAnimload("gfxChucky9x14.png",9,14,0,10)
spr_girl=CheckLoad("gfxGirl.png")
spr_heart=CheckAnimload("gfxhearth9x8.png",9,8,0,3)
spr_npcs=CheckAnimload("gfx
pcs.png",24,21,0,48)

;Check if any loading errors have occured, and display the information
If CheckLoadError()
 WaitKey ()
 RuntimeError("Please undo the changes from the gfx folder!")   ;Graphic loading errors are fatal !
EndIf

snd_shot=CheckLoad("sfxshot.ogg","s")
snd_laser=CheckLoad("sfxLaserShot.ogg","s")
snd_laser1=CheckLoad("sfxLaserShot01.ogg","s")
snd_Pickup=CheckLoad("sfxPickupEgg.ogg","s")
snd_coin=CheckLoad("sfxcoin.ogg","s")
snd_explode=CheckLoad("sfxExplode.ogg","s")
snd_explode1=CheckLoad("sfxExplode01.ogg","s")

If CheckLoadError()=True     ; Sound file errors usualy don't cause maw's but arent played
   WaitKey()
EndIf


CheckLoadError function returns true if loading errors have occured, or false if not.

Optionaly all 3 functions can use the my PrintF() function to write the data to a textfile.
(youll have to uncomment them in the functions to use them)

The use of PrintF() function in CheckLoad and CheckAnimLoad is to output the filesize into the textfile. Which you then can copy and paste back into your program.
(usage of this is when the game is finished and ready to be released)

the output of this function,if used, looks like:

=CheckAnimload("gfxDoors24x24.png",24,24,0,12,2401)
=CheckLoad("gfxLaser01.png","i",5550)
=CheckLoad("sfxshot.ogg","s",22221)

so that only a bit of copy paste work remains.
The use of PrintF() function in the CheckLoadError() function is to save the Error messages, which this function prints to the screen, to a textfile.
In this case CheckLoadError(save,filename$) has 2 parameters, save=0 (no) or 1 (yes) and optionaly filename$

Here is a copy of the PrintF() function:
(this can be used as include if the needed type and the global are copied to the top)

see <a href="codearcs24f1.html?code=3238" target="_blank">here</a> for a small example
;====================================================================
; Project: PrintF function
; Version: 1.0
; Author: Dan
; Email: -.-
; Copyright:  PD
; Description:      
;     Prints Text To a file ! (easy way)
;     every time a PrintF is called it writes the text to the end of that file.
;     Basic error checking is implemented,but you will need to ensure that the filename is valid
;     every PrintF("text") call adds CrLf ($0d and $0a bytes to the end of the end of the txt$)
;     so that the file can be opened with notepad
; Usage:
;       PrintF(Filename,1) to set the filename
;       PrintF("text")     to write to the file above
;       PrintF("")         to close the opened file !!!
;                          so that a new filename can be set
;===============================================================================================


Function PrintF(Txt$="",setf=0)
;Copy next 8 lines to the beginning of your program, and uncomment them (remove ; )
; Type writeout
; Field Filename$
; Field OldFilename$
; Field filestreamID
; Field open
; Field filenameset
; End Type
; Global pfile.writeout = New writeout
; Const loaddebug=0 ;Used for CheckLoad + CheckAnimLoad functions
; If loaddebug=1 Then PrintF("R:filesize.txt",1) ;to make a list with filenames and filesize. useful for releasing the games, to check if the file has been modified (is it only a simple size check)
;usage:
;PrintF("r: est.txt",1) ;to set the filename
;PrintF("test text") ;to write a line of text to the file
;PrintF("") ;To close the file ! Important before using another file to write!
; ;else it writes to the same file again
Select True
    Case setf=0 And pfilefilenameset=1
If Len(Txt$)>0 ;Is Length of the Text$ greater than 0
If pfileopen=0 ;has the file allready been opened ?
If pfileOldFilename$="" Then pfileOldFilename$=pfileFilename$ ;if no, set the oldfilename as filename$

If FileType(pfileFilename$)=0 ;Doesnt Exists, create new one
pfilefilestreamID=WriteFile(pfileFilename$)
ElseIf FileType(pfileFilename$)=2 ;It is a directory, Stop the program
RuntimeError "PrintF: The Filename is a directory, please check your script"
ElseIf FileType(pfileFilename$)=1 ;File Exists, open it to make additions !
pfilefilestreamID=OpenFile(pfileFilename$)
EndIf
If pfilefilestreamID=0 ;Check if the file could be opened
RuntimeError "PrintF: error cannot open "+pfileFilename$
Else ;The file exists, set the writing position to the end of the file !
SeekFile (pfilefilestreamID,FileSize(pfileFilename$))
EndIf
pfileopen=1 ;Global flag to indicate that the file is open !
ElseIf pfileopen=1 ;File has allready been opened, check if the filename is same (to prevent writing data to a wrong file !)
If pfileFilename$<>pfileOldFilename$
If pfilefilestreamID>0 Then CloseFile pfilefilestreamID
RuntimeError ("PrintF: Filename Missmatch "+pfileFilename$+" is not "+pfileOldFilename$)
EndIf
EndIf
;Write text string into the FilestreamId
For x=1 To Len(Txt$)
WriteByte pfilefilestreamID,Asc(Mid$(Txt$,x,1))
Next
;And add cr+lf, so it can be readed in text editor as new line
WriteByte pfilefilestreamID,$0d
WriteByte pfilefilestreamID,$0a
Else ;if length of the text$ is 0 then the file should be closed !
If pfilefilestreamID>0 Then CloseFile pfilefilestreamID
pfileopen=0
pfileOldFilename=""
EndIf
Case setf=1 And pfilefilenameset=0
  If Txt$="" Then RuntimeError "PrintF (txt$,1) is used To set a filename, And txt$ cannot be empty!"
pfileFilename$=Txt$
pfilefilenameset=1
Case setf=0 And pfilefilenameset=0
   RuntimeError "The Filename was not been set, use PrintF(''c:Filename'',1) before calling PrintF(''text'') writing function"
Case setf=1 And pfilefilenameset=1
   If pfileOldFilename="" And Txt$<>""
  pfileFilename=Txt$
Else
  RuntimeError " Close the filehandle with PrintF('''') before setting a new filename !"
EndIf
End Select
End Function


p.s. CheckLoad function defaults to image format, to load sound or movie set the b$ parameter to "s" or "m"
the default parameter "i" needs to be set incase you want to check the filesize (fsize parameter).

p.p.s (i havent tested the movie parameter as yet, but i hope it works ...) [/i]

Code :
Code (blitzbasic) Select
Global Lerror=0 ;Set in main program
Dim LerrorDat$(100,1) ;Set in main program and make sure the dimension is high enough to
;Const loaddebug=1 ;used for the PrintF() function

;lerrorappdir$=SystemProperty("appdir")+"loadingerrors.txt" ;use this variable as CheckLoadError(1,lerrorappdir$) if the printF() function is loaded
; to save the textfile into app's directory

Function CheckLoad(A$,b$="i",fsize=-1)
; LoadImage and LoadSound Checking.  (replace them whith this command)
; Returns the handle of the loaded image,
; or 0 if none, and increases Lerror for each failed loading.
;
; set the b$ parameter to i for images, s for sound or m for movies/anim gif's
;
; Checks also if file exists, and can be optionally set to check if the filesize is right. (to prevent changes ?!)
;
;copy the 2 lines below to the top of your main Program
;Global Lerror=0 ;CheckLoad + CheckAnimLoad function ;<----Copy this to the start of your main program
;Dim LerrorDat$(100,1) ;CheckLoad + CheckAnimLoad function ;
;
;Make sure the dimension of LerrorDat$(x,1) is high enough to hold the numbers of the loading filenames
;LerrorDat$ - (x,0) contains the reason, (x,1) contains the filename !
;
; if you have the PrintF function installed, you can Copy&Paste and uncomment following 2 lines to the top of your main prog
;const loaddebug=1 in your mainfile, to enable the writing the filesize to a file
;If loaddebug=1 Then PrintF("R:filesize.txt",1) ;Sets the filename for the CheckLoad+CheckAnimLoad function
;
    b$=Lower$(b$)
    If b$="i" Then Err$="Img "
If b$="s" Then Err$="Snd "
If b$="m" Then Err$="Mov "

If FileType(A$)=0 ;Check if file exists !
Lerror=Lerror+1
LerrorDat$(Lerror,0)=Err$+"Filename Missing: "
LerrorDat$(Lerror,1)=A$
stopnext=1
ElseIf FileType(A$)=2
   Lerror=Lerror+1
LerrorDat$(Lerror,0)=Err$+"Filename is a Directory: "
LerrorDat$(Lerror,1)=A$
stopnext=1
EndIf

If FileSize(A$)=0
Lerror=Lerror+1
LerrorDat$(Lerror,0)=Err$+"Filename exists but the filesize is 0: "
LerrorDat$(Lerror,1)=A$
stopnext=1
    EndIf

If fsize>-1 And stopnext=0
If FileSize(A$)<>fsize
    Lerror=Lerror+1
LerrorDat$(Lerror,0)="Reason: "+Err$+"filesize doesnt match:"
LerrorDat$(Lerror,1)=A$
stopnext=2
EndIf
EndIf

; ;uncomment the following 3 lines if you want to use the PrintF function
; If loaddebug=1
; PrintF ("=CheckLoad("+Chr$(34)+A$+Chr$(34)+","+Chr$(34)+b$+Chr$(34)+","+FileSize(A$)+")")  ; Debugging, to print filesize for the fsize parameter
; EndIf

If stopnext=0
If b$="i"
Image=LoadImage(A$)
ElseIf b$="s"
Image=LoadSound(A$)
ElseIf b$="m"
   Image=OpenMovie(A$)
EndIf

If Image=0 And stopnext=0 ;Dont show following if the file was not found !
Lerror=Lerror+1
LerrorDat$(Lerror,0)="Reason: "+Err$+"file corruption:"
LerrorDat$(Lerror,1)=A$
EndIf
EndIf
Return Image

End Function

Function CheckAnimload (A$,Width,Height,FirstI,Count,fsize=-1)
;Replace LoadAnimImage with CheckAnimLoad, the parameters are same, part from fsize parameter, which can be used to check if the filesize was changed
;
; if you have the PrintF function installed, you can set the
;const loaddebug=1 in your mainfile, to enable the writing the filesize to a file (uncomment the If loaddebug=1 below in this function)
;
If FileType(A$)=0 ;Check if file exists !
Lerror=Lerror+1
LerrorDat$(Lerror,0)=Err$+"Ani Filename Missing: "
LerrorDat$(Lerror,1)=A$
stopnext=1
ElseIf FileType(A$)=2
   Lerror=Lerror+1
LerrorDat$(Lerror,0)=Err$+"Filename is a Directory: "
LerrorDat$(Lerror,1)=A$
stopnext=1
EndIf

If FileSize(A$)=0
Lerror=Lerror+1
LerrorDat$(Lerror,0)=Err$+"Filename exists but the filesize is 0: "
LerrorDat$(Lerror,1)=A$
stopnext=1
    EndIf

If fsize>-1 And stopnext=0
If FileSize(A$)<>fsize
Lerror=Lerror+1
LerrorDat$(Lerror,0)="Reason: "+Err$+"filesize doesnt match:"
LerrorDat$(Lerror,1)=A$
stopnext=2
EndIf
EndIf

;uncomment the following 3 lines if you want to use the PrintF function
; If loaddebug=1
; PrintF ("=CheckAnimLoad("+Chr$(34)+A$+Chr$(34)+","+Width+","+Height+","+FirstI+","+Count+","+FileSize(A$)+")")  ; Debugging, to Print filesizes for the fsize parameter
; EndIf

If stopnext=0
Image=LoadAnimImage(A$,Width,Height,FirstI,Count)

If Image=0 And stopnext=0 ;Dont show following if the file was not found !
Lerror=Lerror+1
LerrorDat$(Lerror,0)="Reason: "+Err$+"file corruption:"
LerrorDat$(Lerror,1)=A$
EndIf
EndIf
Return Image
End Function

Function CheckLoadError(save=0,file$="r:loadingerror.txt")
    ;lerrorappdir$=SystemProperty("appdir")+"loadingerrors.txt" ;Copy and paste this to the beginning of your
; ;program and call this function as CheckLoadError(1,lerrorappdir$) to save the error log into app's directory
;usage:
;after the last CheckLoad and CheckAnimLoad function call this function will display
;if following errors have occured:
;
;file cannot be loaded by the LoadAnimImage or LoadImage or LoadSound
;Filename does not exists
;filename is a directory
;filsize check is optional and can be expanded to whatever you like it to be if you write the functions for the checking (hash, md5 etc etc)
;
;
;!!!!! if you have the PrintF() function installed, then
; set save to 1 if you want to save the file as the file$ filename
;
;!!!!!!!!!!!!!!!!!!!!!!!!
;after checking errors with this function, the lerror variable is reseted to 0 !
;so it can be reused after next calls
;the LerrorDat$() is not cleared ! it still contains the names

Color $ff,$ff,$ff
If Lerror>0
Cls
Locate 0,0
Print "OOps,there were errors while loading !"
Print "Please check following files:"
Print ""
y=3

;Uncomment the every if save=1 to enable the PrintF() function saving the text to a file
        ;there are 3 in this function !

; If save=1 ;#1
;    Print "Saving Debuglog to :"
; Print file$
; Print ""
; PrintF() ;Close previous opened files
; PrintF(file$,1)
; y=6
; EndIf

For X=1 To Lerror ;Lists every error message to the screen

; If save=1 ;#2
; PrintF (LerrorDat$(x,0)+LerrorDat$(x,1))
; EndIf

Print LerrorDat$(x,0)+LerrorDat$(x,1) ;Displaying the error message
y=y+1
If y=14
Print ("Press any key to continue !")
Flip
WaitKey()
y=0
Cls
Locate 0,0
FlushKeys()
EndIf
Next
Flip
Delay 10
Lerror=0

; If save=1 Then PrintF("") ;#3

Return True
EndIf
Return False
End Function


Comments : none...