Int Array to Short?

BlitzMax Forums/BlitzMax Programming/Int Array to Short?

BLaBZ(Posted 2012) [#1]
How do you convert an int array to short?

Local indices1:Int[] = [0, 1, 2, 0, 2, 3,  ..
						0, 3, 4, 0, 4, 5,  ..
						0, 5, 6, 0, 6, 1,  ..
						7, 6, 1, 7, 1, 2,  ..
						7, 4, 5, 7, 5, 6,  ..
						7, 2, 3, 7, 3, 4]
						
Local indices:Short Ptr[] = Short Ptr(Int Ptr(indices1))[0]



col(Posted 2012) [#2]
Hiya,

Why not use a short array to start with?
One way that springs to mind without testing

Strict

Local Indices:Short[] = [0:Short,1:Short,2:Short,3:Short] 'etc
Local pIndices:Short Ptr = Indices

Print pIndices[3]


Last edited 2012


Yasha(Posted 2012) [#3]
Assuming you can't just use a Short array... you don't. The values contained in the array are not Shorts. You need to convert them on an individual basis.

If you wanted to make certain assumptions about packing (which is never a good idea...), you might be able to do some tricks with copying selected areas of memory... but since the parts to filter out are completely interleaved with the parts to keep, it'd probably be no faster than just looping over the array and doing it in a way that isn't dangerously hackish.


GfK(Posted 2012) [#4]
All of that said, why bother?


col(Posted 2012) [#5]
All of that said, why bother?


I think you need to use shorts in some parts of OpenGL.


GfK(Posted 2012) [#6]
Really? K...


col(Posted 2012) [#7]
*I think*

from an old rusty memory. Most APIs will actually let you choose a type, but I remember specifically using shorts in an index array. I always stand to be corrected though! I haven't used OpenGL for donkeys years, DirectX for me nowadays.


Floyd(Posted 2012) [#8]
[0:Short,1:Short,2:Short,3:Short] 'etc
This has always bothered me. Why do we have to add :Short to every number? I think this should work:
[code][0:Short,1,2,3] 'etc
The elements must all be of the same type. The first one is Short so they are all Short.


Anyway, for the original question I would just create an array of Shorts and copy the Int array element by element.

Last edited 2012


col(Posted 2012) [#9]
I personally think in the scenario of using Local Array:Short[] = [ *values* ], that you shouldn't even need the first [0:Short.......] as you've made the type declaration already. There must be a reason why it's that way though.


BLaBZ(Posted 2012) [#10]
yah I am using it for declaring opengl indices, thanks for the input


*(Posted 2012) [#11]
from personal experience opengl uses int or byte not short


BLaBZ(Posted 2012) [#12]
you can use either, int byte or short when referencing indices