DataBanks

Blitz3D Forums/Blitz3D Beginners Area/DataBanks

mintybreath(Posted 2007) [#1]
I recently picked up a new programming book that covers dataBanks. I need a little help though.
I am having trouble wrapping my mind around what they would be used for. I have been thinking about it, and i dont really understand. I kind of get that it is used to save values and bytes and stuff, but if someone could give me an example of how they would be used, i think that would really help.

You'll probably see me asking more questions about these and other topics covered in the book, mainly this because i dont understand it much.

Thanks for your help everyone!

-Kevin


PowerPC603(Posted 2007) [#2]
A databank can be used for everything infact.
You can use a bank to store player info (name, current position in the 3D world, score, ...), you can use it to store vertex-data if you want to create meshes yourself throughout your code, you can even use it to store images (the references to them or the data itself):
; Set graphics mode
Graphics 800, 600, 0, 2
; Create a bank of 4 bytes
testbank = CreateBank(4)

; Load an image and store it's handle in the bank at offset 0
PokeInt testbank, 0, LoadImage("Image.bmp")

; Draw the image onto the screen using the handle stored in the bank
DrawImage (PeekInt(testbank, 0)), 10, 10
; Wait for a keypress before ending the program
WaitKey()
End


You can use it to store level-data, enemy-data, ...

You can also use it to read an entire datafile and store it's contents byte per byte into the bank.

The code above shows you how to use a bank by storing the imagehandle into the bank and later on, reading it from the bank to do something (draw it onto the screen).


BLaBZ(Posted 2007) [#3]
when you use a bank, can it make you game run faster?


boomboom(Posted 2007) [#4]
I have noticed when people use banks they stor things in 4 byte intervals. Do all variables use 4 bytes, or does something like a string with alot of letters in it use more? If so, how do you calculate the bank length needed to store it?


PowerPC603(Posted 2007) [#5]
Banks are for high-speed operations, according to the manual, so I guess they are as fast as you can get.
I've never used them myself much, as I've not programmed that much in Blitz3D yet, but I'm planning to use them massively in my game I'm trying to create.

They can also reduce memory-usage.
In my game, I had an array to hold references to type-instances, which define my entire tile-based map.
But the size of the map needs to be 1000x1000 and each type-instance has 3 fields.
I checked TaskManager and the program used 46Mb of RAM, just creating the array and the type-instances.
But each field has a maximum value of 200, so I didn't need types where each field is an integer (4 bytes), I only needed bytes.
I then tried to create a bank of 3000000 bytes (1000x1000 mapsize x 3 fields per tile = 3Mb) and memory-usage dropped to 12Mb.
So using banks instead of types and arrays for my game, I'm reducing memory-usage by 34Mb.

Each character in a string is a byte, so a string with 20 characters will use 20 bytes.
You can store a string by first storing the length of the string, followed by the seperate characters (their ASCII-code) of the string.
If your strings are maximum 255 characters, then you can store the length in a byte.

This example has a string which consists of 16 characters.
The length of the string is stored first as a byte, so storing this string in a bank uses 17 bytes.
Graphics 800, 600, 0, 2

testbank = CreateBank(500)

; Write the given string to the bank at offset 10
a$ = "This is a string"
WriteStringToBank(testbank, 10, a$)

; Read the string from the bank which is located at offset 10
Print ReadStringFromBank$(testbank, 10)

Print ""

; Read each byte and character seperately
Print "Length = " + PeekByte(testbank, 10)
For i = 1 To PeekByte(testbank, 10)
	Print PeekByte(testbank, 10+i) + " = " + Chr$(34) + Chr$(PeekByte(testbank, 10+i)) + Chr$(34)
Next

WaitKey()
End




Function WriteStringToBank(bank, offset, s$)
	; Check if length if lower than 256
	If Len(s$) < 256 Then
		; Write the length to the bank as a byte
		PokeByte bank, offset, Len(s$)

		; Loop through the entire string
		For i = 1 To Len(s$)
			; Write each character's ASCII-code to the bank
			PokeByte bank, offset + i, Asc(Mid$(s$, i, 1))
		Next
	EndIf
End Function

Function ReadStringFromBank$(bank, offset)
	Local s$, c$
	; Read the length of the string
	Local length = PeekByte(bank, offset)

	; ReCreate the string by reading each character's ASCII-code and merging them into one string
	For i = 1 To length
		; Read the ASCII-code and convert it to a character
		c$ = Chr$(PeekByte(bank, offset + i))
		; Add the character to the string
		s$ = s$ + c$
	Next

	; Return the string to the calling routine
	Return s$
End Function



mintybreath(Posted 2007) [#6]
That does help alot. I just want completely sure about what they are used for, but now i believe i have a better grasp on them. Thanks for your help PowerPC. :)


Jasu(Posted 2007) [#7]
According to my tests, using arrays is 19% faster than using banks. I too was originally in the misconception that it was faster, but this is the case, at least with blitz. I guess it's because when peeking or poking, the data needs to be converted and moved from memory address to another. When using an array, the data is always usable as it is and from where it is.

I would use banks only to
- concerve memory
- encryption
- file handling assistance
- net package handling assistance
- non speed reliant tasks where banks are handy


big10p(Posted 2007) [#8]
Yes, banks are slower than arrays so you need to weigh up the pros and cons when deciding which to use.