BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"Freeing allocated objects and resources"
, by thechange
Normally one has to remove what he has left behind, but with Blitz this is ancient history.
Code
; When a program ends, Blitz is able to remove any traces of images, sounds, ; banks and objects, automagically :D ; But if one needs to reuse the allocated space before ending the program, one ; has to do this by hand. The easy way is creating a specific set of functions ; for this task, as it can be cumbersome to do this through trial and error. ; Also when switching graphics resolution, i.e. calling the Graphics function ; or EndGraphics function, all visual related things will be wiped anyway. ; To be able to pause the game, switch graphics resolution, and continue playing ; right after, one will require a technique similar to the one displayed below. ; Suppose we already have 9 unique files for graphics. Const ImageFileCount = 9 .ImageFileNames Data "Player.BMP" Data "PowerUp1.BMP", "PowerUp2.BMP", "PowerUp3.BMP" Data "Enemy1.BMP", "Enemy2.BMP", "Enemy3.BMP" Data "Bullet1.BMP", "Bullet2.BMP" Const Visual_PlayerShip = 1 Const Visual_PowerUp_Small = 2 Const Visual_PowerUp_Medium = 3 Const Visual_PowerUp_Large = 4 Const Visual_EnemyShip_Small = 5 Const Visual_EnemyShip_Medium = 6 Const Visual_EnemyShip_Large = 7 Const Visual_Bullet_Red = 8 Const Visual_Bullet_Blue = 9 ; And 3 unique audio files. Const SoundFileCount = 3 .SoundFileNames Data "Shoot.WAV" Data "Explode.WAV" Data "PickUp.WAV" Const Audio_Shoot = 1 Const Audio_Explode = 2 Const Audio_PickUp = 3 ; To easily access the filenames we'd need to store these in another way though. ; Since there are no more attributes, we can easily use arrays for this. Dim ImageFiles$( ImageFileCount ) Dim SoundFiles$( SoundFileCount ) ; And the functions or methods to read the data into these arrays. Function ReadImageFileNames() Local ImageFile% Restore ImageFileNames For ImageFile = 1 To ImageFileCount Read ImageFiles( ImageFile ) Next End Function Function ReadSoundFileNames() Local SoundFile% Restore SoundFileNames For SoundFile = 1 To SoundFileCount Read SoundFiles( SoundFile ) Next End Function ; But don't forget to call them. ReadImageFileNames ReadSoundFileNames ; Since we already know the number of sounds and images we want to access, and ; we want to access them directly, it's best to use an array. And if we want to ; store more information than just the sounds and images, such as the volume, ; number of frames, time to display each frame etc, we could create an Array of ; a Type collection, which offers higher versatility. ; The Type collection of the images could look like something like this: Type Visual Field Image Field Frames ; Unused in this example Field FrameDelay ; Unused in this example End Type ; And for the audio: Type Audio Field Sound Field Volume ; Unused in this example Field Panning ; Unused in this example End Type ; Now for the media arrays, also referred to as Container Types, the declaration ; could like this: Dim Visual.Visual( ImageFileCount ) Dim Audio.Audio( SoundFileCount ) ; Finally one can load the actual media, using the previously read filenames and ; declared container types, which you can access from anywhere. Function LoadImageFiles() ; Freeing all images first (only if existing) For Image = 1 To ImageFileCount If Visual( Image ) <> Null FreeImage Visual( Image )\Image Delete Visual( Image ) EndIf Next ; And loading the images, using the filenames For Image = 1 To ImageFileCount Visual( Image ) = New Visual Visual( Image )\Image = LoadImage( ImageFiles( Image ) ) Next End Function Function LoadSoundFiles() For Sound = 1 To SoundFileCount If Audio( Sound ) <> Null FreeSound Audio( Sound )\Sound Delete Audio( Sound ) EndIf Next For Sound = 1 To SoundFileCount Audio( Sound ) = New Audio Audio( Sound )\Sound = LoadSound( SoundFiles( Sound ) ) Next End Function ; Then all you have to do is call the functions everytime you switch resolution. LoadImageFiles LoadSoundFiles ; If you'd like to see an example of referencing the images and sounds in ; combination with handling objects, like enemies, take a look at this: Type Enemy Field Visual Field PositionX Field PositionY Field Direction Field Heading Field Speed Field Action End Type ; The 'Visual' field in the Enemy Type is the reference number to the image in ; the Visual Media Container Types. This means simply the Arrays of the Types ; containing the actual images and sounds. The index of these arrays, easily ; referenced by the Visual and Audio constants displayed at the top, is in fact ; what the reference number is refering to. ; When you'd want to create an enemy at this point, the function would have to ; be responsible for the actual type of enemy to be created. For example having ; a Select Case statement to distinct unique enemies, like this: Function SpawnEnemy( EnemyType% , PositionX% , PositionY% , Action% ) Local Enemy.Enemy = New Enemy Select EnemyType Case 1 Enemy\Visual = 1 Enemy\PositionX = PositionX Enemy\PositionY = PositionY Enemy\Direction = 0 Enemy\Heading = 0 Enemy\Speed = 0 Enemy\Action = Action End Select End Function ; A rendering function, to visualize all enemies, could look like the following: Function RenderEnemies() Local Enemy.Enemy For Enemy = Each Enemy DrawImage Visual( Enemy\Visual )\Image , Enemy\PositionX , Enemy\PositionY Next End Function ; But a far more advanced approach is having the enemy specification predefined ; and being able to access them every time when required. Once again, a ; Container Type seems the most appropriate approach. Take a look at a possible ; structure of an Enemy Specification record: Type EnemySpecification Field GenericVisual Field ExplodingVisual Field ProjectileVisual Field AmbientAudio Field ExplodingAudio Field FiringAudio Field MaxRotation Field MaxVelocity Field Thrust End Type ; In the above Type declaration there are 3 different fields used for both sound ; and display. These fields are once again simply reference numbers to the media ; array. ; We could retrieve the actual enemy specs from file or from data statements. ; Whichever one chooses, usually we will know the number of enemy specs. Dim EnemySpecs( EnemySpecCount ) ; We'd also need a bit altered Enemy Type: Type Enemy Field Spec Field PosX Field PosY Field Speed Field Angle Field Heading Field Status End Type ; To demonstrate the connection between the specs and the actual enemies, here's ; a possible function to spawn an enemy: Function SpawnEnemyFromSpec( Spec% , PosX% , PosY% , Status% = 0 ) Local Enemy.Enemy = New Enemy Enemy\Spec = Spec ; The specifications array is now responsible Enemy\PosX = PosX Enemy\PosY = PosY Enemy\Speed = 0 Enemy\Angle = 0 Enemy\Heading = 0 Enemy\Status = Status End Function ; Have fun :D
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations