type symbols

BlitzMax Forums/BlitzMax Programming/type symbols

col(Posted 2011) [#1]
Hi all,

Should be a simple one but I dont recall anything like this...

I want to make an array of bytes using :-

Local Array:Byte[] = [ 1 , 2 , 3 , 4 , 5 ]

but this creates an error during compilation as the numbers are recognised as integers.
My question is is there a type symbol for bytes?? The same as in a float is #, double is !, hex is $ etc

cheers.

Just trying to create a byte array without first knowing its size. I have done it converting from an int to byte array, but its a long way round.

Last edited 2011


GfK(Posted 2011) [#2]
This seems to work:
Local Array:Byte[] = [ Byte(1) , Byte(2) , Byte(3) , Byte(4) , Byte(5)]



col(Posted 2011) [#3]
Hiya Gfk,

Yes it does, and its a lot of typing when inputting say 40 bytes, and it also looks unsitely...
Is there a byte symbol to make it soo much easier ? I've scanned the docs and can't it?


GfK(Posted 2011) [#4]
I don't believe so, and even if there was you'd use a symbol to define a variable type rather than using it alongside the value itself.


col(Posted 2011) [#5]
Hmm, OK.

I have another idea then.

Thankyou for the fast replies.

Last edited 2011


GfK(Posted 2011) [#6]
I stand corrected - you can use @ to specify a byte, however it doesn't seem to work in this case. The nearest I can get it by defining each number as a byte (i.e. 1:Byte), or casting to a byte as in the previous example.


col(Posted 2011) [#7]
I was using the :Byte method, but its very long winded when more than a couple of bytes to use.

Like you say...it doesnt work in that scenario.

It only seems to work on 'stand-alone' variables. ie
Local b@ = 5

Local b@[10] :- doesnt work too.

Never mind. The idea I have is wrap it in a Type and send in an integer array and convert it. I can make it really easy to use and the external code will look a whole neater.

Thanks again :-)


GfK(Posted 2011) [#8]
You could do this if you were desperate to get your array declaration shorter:
Local Array:Byte[] = [ b(1) , b(2) , b(3) , b(4) , b(5) ]

Function b:Byte(val:Int)
  Return Byte(val)
End Function


...IF you were desperate. :D


Warner(Posted 2011) [#9]
It indeed doesn't seem to work.
Local Array1:Float[] = [1#, 2#, 3#] 'fine
Local Array2:Int[] = [1%, 2%, 3%] 'also fine
Local Array3:String[] = ["1", "2", "3"] 'somewhat different, but works
'Local Array4:Byte[] = [1@, 2@, 3@] 'nope ..


To elaborate a bit on Gfk's example:

Maybe instead, you could better look into incbin byte data instead, using LoadByteArray and IncBin.


col(Posted 2011) [#10]
A little snippet :-

Type SRSVertexDeclaration
	Field Declaration:Byte[]
		
	Method AddElement( In:Int[] )
		Declaration = Declaration[ ..Len( Declaration ) + 8 ]
		Local Offset:Int = Len( Declaration ) - 8
		
		Declaration[ OffSet + 0 ] = In[ 0 ]
		Declaration[ OffSet + 1 ] = ( In[ 0 ] & $FF00 ) Shr 16
		Declaration[ OffSet + 2 ] = In[ 1 ]
		Declaration[ OffSet + 3 ] = ( In[ 1 ] & $FF00 ) Shr 16
		Declaration[ OffSet + 4 ] = In[ 2 ]
		Declaration[ OffSet + 5 ] = In[ 3 ]
		Declaration[ OffSet + 6 ] = In[ 4 ] 
		Declaration[ OffSet + 7 ] = In[ 5 ]
	EndMethod
	
	Method GetDeclaration:Byte[]()
		Return Declaration
	EndMethod
EndType


An example of using it :-

Global VertexDecl:SRSVertexDeclaration = CreateVertexDeclaration()
VertexDecl.AddElement( [ 0 , 0 , D3DDECLTYPE_FLOAT3 , D3DDECLMETHOD_DEFAULT , D3DDECLUSAGE_POSITION , 0 ] )
VertexDecl.AddElement( [ 0 , 12, D3DDECLTYPE_FLOAT3 , D3DDECLMETHOD_DEFAULT , D3DDECLUSAGE_NORMAL   , 0 ] )
VertexDecl.AddElement( [ 0 , 24, D3DDECLTYPE_FLOAT2 , D3DDECLMETHOD_DEFAULT , D3DDECLUSAGE_TEXCOORD , 0 ] )
VertexDecl.AddElement( D3DDECL_END() )


I was using the older FVF method, which still sets up the programmable shader declarations correctly strangely enough, and I also wanted to make it compatible with Dx10 and Dx11 should I want to update the engine to those versions, which to be honest wouldn't be that difficult.

As you can see, by wrapping it in a TYPE, the actual code is nice and neat to use. Imagine all those ':Byte' or Byte(int) in the code?!?! that would be so ugly.

I know it doesnt have to be an array passed in in that example but the array is also used elsewhere. Just keeps things simple and fluid to use, like using D3DDECL_END() for eg which is itself an array too. And it could easily be an array of shorts instead of integers, but I'm sure you get the idea.

Thanks all,

Last edited 2011


Czar Flavius(Posted 2011) [#11]
What happens if you just use an array of Ints? I know it's not solving the problem but if you are using bytes to save memory or something, I really wouldn't bother.

You could also try this (not tested)

Global intarr:Int[] = [1, 2, 3]
Global bytearr:Byte[] = Byte[](intarr)


Last edited 2011


col(Posted 2011) [#12]
Hiya,
I'm using this directly with DirectX.
The correct format to setup and use a D3DVertexDeclaration* in c++ is to use a

Short , Short , Byte , Byte , Byte , Byte.

to describe a singe D3DVertexElement*. The vertex declaration is then made up of any number of these elements.

Anything else and it doesn't work.
I've seen other engines written in BMax that palm out to c/c++ for this, but there's no need when you can do it this way inside of BMax.

The example code that uses it is practically identical to how the D3D* manuals tell you to use it in c++. I'm keeping things as close to the manual as possible for intuitiveness.

I didn't want to mess with fudge code and 32bit integer.

My code above solves the problem and is very intuitive to other programmers used to DirectX.

* = 9/10/11.

Cheers