CreateRamStream ?

BlitzMax Forums/BlitzMax Programming/CreateRamStream ?

Hi :)

I have try to understand the real use of RamStream but without example it's not very easy :)

Anybody have an example code to use Ram Stream ?

I have try :

Global MyRam:TRamStream
Global MyBank:TBank

MyRam=CreateRamStream(MyBank,BankSize(MyBank),True,True)
WriteLine MyRam,"CECI EST UN TEST"




SeekStream(MyRam,0)
Print ReadLine(MyRam)

Strict

Global myRam : Byte Ptr = MemAlloc(128) 'allocate 128 bytes of memory to myRam

Global myStream : TRamStream = CreateRamStream(myRam, 128, True, True) 'Create a ram stream and link it to myRam

WriteLine(myStream, "test text") 'write the text
SeekStream(myStream, 0) 'reset the stream to the beginning
Print ReadLine(myStream)'read the text and print it

myStream = Null
MemFree(myRam, 128) 'clear the alloced memory

End


Many thanks Perturbatio :) your alway here to save me :)

But a last question ! it is possible to load media like image directly from ram stream ? And a little example ? :)

Thanks

np :)

I've just updated it with comments to help out

Thanks ! if you have information about my previous post ! i'll take it :)

incidentally, you can also just use TRamStream.Create() instead of CreateRamStream (it takes the same parameters). CreateRamStream simply calls the create method.

*EDIT*
I'll try and knock up something for loading an image now.

I love you :)

Hi,

RamStream is pretty low level, and in there mainly to support 'incbin::'.

For streaming in/out of banks, use a BankStream instead. It will automatically grow the bank for you when writing and will ensure you don't read/write outside the limits of the bank.

Mark, can you specify a RamStream or a BankStream as a URL?

solved (a bit messy).
Strict

Graphics 800,600,0

'Create a Stream object that refers to the file based image.
Global myFileStream : TStream = OpenStream("ball.png", True)
Global size = StreamSize(myFileStream)
'reserve enough ram to take the entire image
Global myRam : Byte Ptr = MemAlloc(size )

'assign ram to a TRamstream
Global myRamStream : TRamStream = CreateRamStream(myRam, size , True, True)
'copy image to ramstream
CopyStream(myFileStream, myRamStream)
SeekStream(myRamStream, 0)

Global myImage:TImage = LoadImage(myRamStream)


While Not KeyDown(KEY_ESCAPE)
DrawImage myImage, MouseX(), MouseY()
Flip
Cls
Wend
CloseStream(myRamStream)
myRamStream = Null
CloseStream(myFileStream)
myFileStream = Null
MemFree(myRam, size ) 'clear the alloced memory
FlushMem

End


*EDIT*
The same thing using a TBankStream:
Strict

Graphics 800,600,0

'Create a Stream object that refers to the file based image.
Global myFileStream : TStream = OpenStream("ball.png", True)
Global myBank:TBank = CreateBank()
Global myBankStream : TBankStream = CreateBankStream(myBank)
'copy image to stream
CopyStream(myFileStream, myBankStream)
SeekStream(myBankStream, 0)

Global myImage:TImage = LoadImage(myBankStream)


While Not KeyDown(KEY_ESCAPE)
DrawImage myImage, MouseX(), MouseY()
Flip
Cls
Wend
CloseStream(myBankStream)
myBankStream = Null
CloseStream(myFileStream)
myFileStream = Null
myBank = Null
FlushMem

End


Many thanks Perturbatio ! i 'll explore your code !