About Monkey 2 › Forums › Monkey 2 Programming Help › equivilent of java's float buffer
This topic contains 5 replies, has 3 voices, and was last updated by
codifies
2 years, 5 months ago.
-
AuthorPosts
-
November 8, 2016 at 12:20 am #4854
I want to be able to build and maintain a chunk of “raw” memory that I could (easily) put float values into and then send a pointer to a c library
I notice the GLES module uses ptr but I’m not too sure about the correct way to use this, for example, are “normal” mx2 float arrays guaranteed to be contiguous in memory…
November 8, 2016 at 2:54 am #4855mx2 arrays are cpp arrays (because all mx-code translated to cpp before compiling), so they must be contiguous in memory, I think.
November 8, 2016 at 5:24 am #4857are “normal” mx2 float arrays guaranteed to be contiguous in memory…
Yes, and arrays also have a ‘Data’ property that returns the raw memory used by the array in the form of a pointer, eg: an Int[] will return an Int Ptr.
You can also use a DataBuffer and poke in values using PokeFloat etc, or even a Stack<Float> which also has a Data property only it returns the underlying array, eg:
Monkey12345Local buf:=New Stack<Float>For Local i:=0 until 1000buf.Push( i )NextLocal rawData:=buf.Data.Data 'A float ptr pointing to 0,1,2,3...!Also note that if the stack is modified, the underlying array may have to be resized (therefore changed) so you need to be a bit careful using stack like this.
November 9, 2016 at 3:26 pm #4880thanks thats a great help I ended up with…
Monkey1234Local texture:GLuint[] = New GLuint[1]texture[0]=123 ' any old value to see some changeglGenTextures(1,texture.Data)Print("texture ="+texture[0])I assume this is only safe as long as texture never leaves scope or is global
looks like its much closer to the “metal” than I thought which is nice…
November 9, 2016 at 8:54 pm #4881Yep, that should work. There’s also Varptr:
Monkey123Local texture:GLuintglGenTextures( 1,Varptr texture )And of course there always good old ‘malloc’:
Monkey12345Local fp:=Cast<Float Ptr>( libc.malloc( 1024 ) )fp[0]=1fp[1]=2libc.free( fp )looks like its much closer to the “metal” than I thought which is nice…
You can indeed use it *very* close to the metal! The general idea is that if you write code without the use of pointers, the code should be 100% ‘safe’. Once you go into pointer land you’re on your own so you need to be careful, but at the same time mx2 doesn’t do anything to stop you doing this if you want/need to.
November 9, 2016 at 10:33 pm #4882I managed to find varptr and Cast<type> by grep-ing …
Its interesting that I like I can get to the “metal”, but equally love the fact that it uses actual human readable words in the code, I’ve been using Java for a while since the days of BlitzMax but its just getting too much ASCII soup and every shiny new fashionable language feature, sometimes you just want easy to read code, but that shouldn’t mean lack of power.
-
AuthorPosts
You must be logged in to reply to this topic.