Ooops
January 24, 2021, 01:18:01 PM

Author Topic: [bb] Load Image, Sound, Music and AnimImage with basic error checking by Dan [ 1+ years ago ]  (Read 414 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
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:

Code: [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 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 !)
Code: [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

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:

Code: [Select]
=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[/url] for a small example
Code: [Select]
;====================================================================
; 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
  1. Global Lerror=0                 ;Set in main program
  2. Dim LerrorDat$(100,1)           ;Set in main program and make sure the dimension is high enough to
  3. ;Const loaddebug=1              ;used for the PrintF() function
  4.  
  5. ;lerrorappdir$=SystemProperty("appdir")+"loadingerrors.txt" ;use this variable as CheckLoadError(1,lerrorappdir$) if the printF() function is loaded
  6. ; to save the textfile into app's directory
  7.  
  8. Function CheckLoad(A$,b$="i",fsize=-1)
  9. ; LoadImage and LoadSound Checking.  (replace them whith this command)
  10. ; Returns the handle of the loaded image,
  11. ; or 0 if none, and increases Lerror for each failed loading.
  12. ;
  13. ; set the b$ parameter to i for images, s for sound or m for movies/anim gif's
  14. ;
  15. ; Checks also if file exists, and can be optionally set to check if the filesize is right. (to prevent changes ?!)
  16. ;
  17. ;copy the 2 lines below to the top of your main Program
  18. ;Global Lerror=0                        ;CheckLoad + CheckAnimLoad function ;<----Copy this to the start of your main program
  19. ;Dim LerrorDat$(100,1)          ;CheckLoad + CheckAnimLoad function ;
  20. ;
  21. ;Make sure the dimension of LerrorDat$(x,1) is high enough to hold the numbers of the loading filenames
  22. ;LerrorDat$ - (x,0) contains the reason, (x,1) contains the filename !
  23. ;
  24. ; if you have the PrintF function installed, you can Copy&Paste and uncomment following 2 lines to the top of your main prog
  25. ;const loaddebug=1 in your mainfile, to enable the writing the filesize to a file
  26. ;If loaddebug=1 Then PrintF("R:filesize.txt",1)         ;Sets the filename for the CheckLoad+CheckAnimLoad function
  27. ;      
  28.     b$=Lower$(b$)
  29.     If b$="i" Then Err$="Img "
  30.         If b$="s" Then Err$="Snd "
  31.         If b$="m" Then Err$="Mov "
  32.  
  33.         If FileType(A$)=0                                                       ;Check if file exists !
  34.                 Lerror=Lerror+1
  35.                 LerrorDat$(Lerror,0)=Err$+"Filename Missing: "
  36.                 LerrorDat$(Lerror,1)=A$
  37.                 stopnext=1
  38.         ElseIf FileType(A$)=2
  39.             Lerror=Lerror+1
  40.                 LerrorDat$(Lerror,0)=Err$+"Filename is a Directory: "
  41.                 LerrorDat$(Lerror,1)=A$
  42.                 stopnext=1
  43.         EndIf
  44.        
  45.         If FileSize(A$)=0
  46.                 Lerror=Lerror+1
  47.                 LerrorDat$(Lerror,0)=Err$+"Filename exists but the filesize is 0: "
  48.                 LerrorDat$(Lerror,1)=A$
  49.                 stopnext=1
  50.     EndIf
  51.        
  52.         If fsize>-1 And stopnext=0
  53.                 If FileSize(A$)<>fsize
  54.                 Lerror=Lerror+1
  55.                         LerrorDat$(Lerror,0)="Reason: "+Err$+"filesize doesnt match:"
  56.                         LerrorDat$(Lerror,1)=A$
  57.                         stopnext=2
  58.                 EndIf
  59.         EndIf
  60.        
  61.         ;               ;uncomment the following 3 lines if you want to use the PrintF function
  62. ;       If loaddebug=1
  63. ;               PrintF ("=CheckLoad("+Chr$(34)+A$+Chr$(34)+","+Chr$(34)+b$+Chr$(34)+","+FileSize(A$)+")")  ; Debugging, to print filesize for the fsize parameter
  64. ;       EndIf
  65.        
  66.         If stopnext=0
  67.                 If b$="i"
  68.                         Image=LoadImage(A$)
  69.                 ElseIf b$="s"
  70.                         Image=LoadSound(A$)
  71.                 ElseIf b$="m"
  72.                     Image=OpenMovie(A$)
  73.                 EndIf
  74.                
  75.                 If Image=0 And stopnext=0                               ;Dont show following if the file was not found !
  76.                         Lerror=Lerror+1
  77.                         LerrorDat$(Lerror,0)="Reason: "+Err$+"file corruption:"
  78.                         LerrorDat$(Lerror,1)=A$
  79.                 EndIf
  80.         EndIf
  81.         Return Image
  82.        
  83. End Function
  84.  
  85. Function CheckAnimload (A$,Width,Height,FirstI,Count,fsize=-1)
  86. ;Replace LoadAnimImage with CheckAnimLoad, the parameters are same, part from fsize parameter, which can be used to check if the filesize was changed
  87. ;
  88. ; if you have the PrintF function installed, you can set the
  89. ;const loaddebug=1 in your mainfile, to enable the writing the filesize to a file (uncomment the If loaddebug=1 below in this function)
  90. ;
  91.         If FileType(A$)=0                                                       ;Check if file exists !
  92.                 Lerror=Lerror+1
  93.                 LerrorDat$(Lerror,0)=Err$+"Ani Filename Missing: "
  94.                 LerrorDat$(Lerror,1)=A$
  95.                 stopnext=1
  96.         ElseIf FileType(A$)=2
  97.             Lerror=Lerror+1
  98.                 LerrorDat$(Lerror,0)=Err$+"Filename is a Directory: "
  99.                 LerrorDat$(Lerror,1)=A$
  100.                 stopnext=1
  101.         EndIf
  102.        
  103.         If FileSize(A$)=0
  104.                 Lerror=Lerror+1
  105.                 LerrorDat$(Lerror,0)=Err$+"Filename exists but the filesize is 0: "
  106.                 LerrorDat$(Lerror,1)=A$
  107.                 stopnext=1
  108.     EndIf
  109.        
  110.         If fsize>-1 And stopnext=0
  111.                 If FileSize(A$)<>fsize
  112.                         Lerror=Lerror+1
  113.                         LerrorDat$(Lerror,0)="Reason: "+Err$+"filesize doesnt match:"
  114.                         LerrorDat$(Lerror,1)=A$
  115.                         stopnext=2
  116.                 EndIf
  117.         EndIf
  118.        
  119.         ;uncomment the following 3 lines if you want to use the PrintF function
  120. ;       If loaddebug=1
  121. ;               PrintF ("=CheckAnimLoad("+Chr$(34)+A$+Chr$(34)+","+Width+","+Height+","+FirstI+","+Count+","+FileSize(A$)+")")  ; Debugging, to Print filesizes for the fsize parameter
  122. ;       EndIf
  123.        
  124.         If stopnext=0
  125.                 Image=LoadAnimImage(A$,Width,Height,FirstI,Count)
  126.                
  127.                 If Image=0 And stopnext=0                               ;Dont show following if the file was not found !
  128.                         Lerror=Lerror+1
  129.                         LerrorDat$(Lerror,0)="Reason: "+Err$+"file corruption:"
  130.                         LerrorDat$(Lerror,1)=A$
  131.                 EndIf
  132.         EndIf
  133.         Return Image
  134. End Function
  135.  
  136. Function CheckLoadError(save=0,file$="r:loadingerror.txt")
  137.     ;lerrorappdir$=SystemProperty("appdir")+"loadingerrors.txt"         ;Copy and paste this to the beginning of your
  138.         ;                                                                                                                       ;program and call this function as CheckLoadError(1,lerrorappdir$) to save the error log into app's directory
  139.         ;usage:
  140.         ;after the last CheckLoad and CheckAnimLoad function call this function will display
  141.         ;if following errors have occured:
  142.         ;
  143.         ;file cannot be loaded by the LoadAnimImage or LoadImage or LoadSound
  144.         ;Filename does not exists
  145.         ;filename is a directory
  146.         ;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)
  147.         ;
  148.         ;
  149.         ;!!!!! if you have the PrintF() function installed, then
  150.         ; set save to 1 if you want to save the file as the file$ filename
  151.         ;
  152.         ;!!!!!!!!!!!!!!!!!!!!!!!!
  153.         ;after checking errors with this function, the lerror variable is reseted to 0 !
  154.         ;so it can be reused after next calls
  155.         ;the LerrorDat$() is not cleared ! it still contains the names
  156.        
  157.         Color $ff,$ff,$ff
  158.         If Lerror>0
  159.                 Cls
  160.                 Locate 0,0
  161.                 Print "OOps,there were errors while loading !"
  162.                 Print "Please check following files:"
  163.                 Print ""
  164.                 y=3
  165.                
  166.                 ;Uncomment the every if save=1 to enable the PrintF() function saving the text to a file
  167.         ;there are 3 in this function !
  168.                
  169. ;               If save=1                                       ;#1
  170. ;                   Print "Saving Debuglog to :"
  171. ;                       Print file$
  172. ;                       Print ""
  173. ;                       PrintF()                        ;Close previous opened files
  174. ;                       PrintF(file$,1)
  175. ;               y=6
  176. ;               EndIf
  177.                
  178.                 For X=1 To Lerror                               ;Lists every error message to the screen
  179.                        
  180. ;                       If save=1                               ;#2
  181. ;                               PrintF (LerrorDat$(x,0)+LerrorDat$(x,1))
  182. ;                       EndIf
  183.                        
  184.                         Print LerrorDat$(x,0)+LerrorDat$(x,1)                   ;Displaying the error message
  185.                         y=y+1
  186.                         If y=14
  187.                                 Print ("Press any key to continue !")
  188.                                 Flip
  189.                                 WaitKey()
  190.                                 y=0
  191.                                 Cls
  192.                                 Locate 0,0
  193.                                 FlushKeys()
  194.                         EndIf
  195.                 Next
  196.                 Flip
  197.                 Delay 10
  198.                 Lerror=0
  199.                
  200. ;               If save=1 Then PrintF("")               ;#3
  201.                
  202.                 Return True
  203.         EndIf
  204.         Return False
  205. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal