Forum Replies Created
-
AuthorPosts
-
pixelmap has load method
how can I have missed that!
You could always keep a copy of the image in a pixmap
To me the problem is more how to get the pixmap out of a loaded .png image. Once you have the pixmap you can use Pixmap.Getpixel() or GetARGBpixel().
Nerobot’s solution is of course top performance but needs more work/knowledge.
here’s an Image Extension that will do Image.GetPixel (very slow!). There’s also an Image.GetPixmap (faster to scan the whole image (untested)) but i’m not quite sure how the resource will be managed..
I’m not sure it’s the right/good way to do it though! It’s so complex for such a simple thing…
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536[crayon-5cbaa15bee044558271546 inline="true" ]Class Image ExtensionMethod GetPixel:Color(x:Int,y:Int)Local myCustomImage:=New Image (Self.Width,Self.Height)Local myCanvas:=New Canvas(myCustomImage)myCanvas.Clear(Color.None)myCanvas.DrawImage(Self,0,0)myCanvas.Flush()Local myRecti:=New Recti(0,0,Self.Width-1,Self.Height-1)Local myPixmap:=myCanvas.CopyPixmap(myRecti)Local c:=myPixmap.GetPixel(x,y)myPixmap.Discard()myCustomImage.Discard()Return cEndMethod GetPixmap:Pixmap()Local myCustomImage:=New Image (Self.Width,Self.Height)Local myCanvas:=New Canvas(myCustomImage)myCanvas.Clear(Color.None)myCanvas.DrawImage(Self,0,0)myCanvas.Flush()Local myRecti:=New Recti(0,0,Self.Width-1,Self.Height-1)Local myPixmap:=myCanvas.CopyPixmap(myRecti)myCustomImage.Discard()Return myPixmapEndEndFor exaple, i need to draw 50 monochrome sprites. I load them from file Then i need to change thge color of a sprte to some other color. For example, there are white by default and i change to red. Then draw 50 red sprites. The colro is actually user selectable, if it matters.
If you start with white images you can use the .Color stuff because white multiplied by a color is the color…
I made a small sample (attached zip) that shows that the white dot is always getting the correct color.I don’t see anyt ReadPixels/WritePixels in mojo2 for images and buffers.
There’s something with Image.Texture and Pixmaps.GetPixel but I don’t remember and the link to Texture in the docs is broken.. Can’t find the example I made at the time.
I made a small sample (attached zip) that shows that the white dot is always getting the correct color.
EDIT: Texture is hidden from docs.. At the time I did some modifications to play around with monkey2 itself so I don’t really know how you can “extract” the pixmap of an Image. Maybe create a new Canvas with the Image then myCanvas.CopyPixmap then myPixmap.Getpixel. But this is so complicated. Image.GetPixel would be nicer..
Attachments:
Great to see that custom glsl shaders have some kind of interface now!
The pakz examples are a good starting point. http://monkeycoder.co.nz/forums/topic/monkey-2-examples-github/
Also the language reference docs has been updated till the last release so if you get mx2 from the github dev branch you’ll have more docs about the core language (enums, pointers,…).
The docs about the different available modules is still very gappy though.. and it’s one of the big power of mx2. A lot of essential modules are available already (math, vect/matrix geom, lists/stacks/maps, databuffers, (file)streams, mojo, mojoX gui, mojo3d (still WIP),… ). Once you get them it’s amazing how easy it is to do almost anything you want, from console apps to full games or productive app.
But it was ok for me learning with these, I was already used to mx1 though. But I could not go back to it now!
Primitives are faster than struct fields.. So I would not use vec2 inside such loops.
Here I use vx ans vy but you Can also put their expressions direcly in drawimage arguments.
The image coule be passed direcly too, it might perform better.[/crayon]Monkey123456789101112131415161718192021[crayon-5cbaa15c06aa2154849984 inline="true" ]Method Render(canvas:Canvas, layerId:Int)Local xStart := [calculate x-index for first visible tile accordingly to x-offset]Local yStart := [calculate y-index for first visible tile accordingly to y-offset]Local xEnd := xStart + viewport.width / tileWidthLocal yEnd := yStart + viewport.height / tileHeightLocal vx : FloatLocal vy : FloatLocal image : ImageFor Local y := yStart To yEndFor Local x := xStart To xEndIf layers[layerId].map[x, y] = -1 Continuevx = layers[layerId].x + x * tileWidthvy = layers[layerId].y + y * tileHeightimage = tileArray[layers[layerId].map[x, y]].imagecanvas.DrawImage(image, vx, vy)NextNextEndI think you have to use
[/crayon]Monkey123[crayon-5cbaa15c0b375135366674 inline="true" ]angles.Dataorvarptr angles[0]which are technically the same. i.e. a pointer to ALFloat (pointing the first element of the array)
“angles” alone is actually a pointer so “Varptr angles” is a pointer to a pointer (to be seen in C sys programming but probably not in mx2 sources)
It’s common in C/C++ libs to send an array by sending a pointer to the first element (and a count if the number of elements is variable)This has been discussed on this topic :
An even faster way would be custom glsl shader but I don’t know how shaders are managed now after the modifications induced by mojo3d.
same error here invalid image>memory access violation
W10, latest dev, mingw
Now the rect might be rotated and is more like an polygon. Ok, I know how to check the if a point inside a polygon, BUT
Second problem: How to rotate a polygon?
If you only want rotated rects then nerobot’s solution is probably better (more monkeyish)
Here is a much more intuitive form of Tpoly.. It’s using position, angle and scale fields instead of affineMat3. But is of course internally using an affineMat3 to transform the original poly (centered at the origin, you could add a center/handle field too and make one more translation to center it to the origin before rotating/scaling).
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Struct TransformablePoly 'I'll assume here that center of polys are [0,0] so I'll wrap arround origin while creating themField position:=New Vec2f()Field scale:=1.0Field angle:=0.0PrivateField _nbVertices:intField _vertices:Vec2f[]PublicMethod New(verts:Vec2f[])_vertices=verts_nbVertices=_vertices.GetSize(0) 'getting the number of vec2f in the arrayEndMethod GetTransformedVec2fPoly:Vec2f[]() 'Transforms all the vertices with AffineMat3' creating a transformation matrixLocal transfo:=New AffineMat3f()transfo=transfo.Translate(position) 'third transformtransfo=transfo.Scale(scale,scale) 'around (0,0) , second transformtransfo=transfo.Rotate(angle) 'rotation arrond (0,0)! , first transform'transforming the verticesLocal outputArray:=New Vec2f[_nbVertices]For Local i:=0 Until _nbVerticesoutputArray[i]=transfo.Transform(_vertices[i])NextReturn outputArrayEndMethod GetTransformedXYPoly:Float[]() 'to be used with DrawPoly (it does not accepts vec2f!)Local outputArray:=New Float[_nbVertices*2]Local TranformedVec2:=GetTransformedVec2fPoly() 'getting the tranformed poly in a Vec2f arrayFor Local i:=0 Until _nbVerticesoutputArray[2*i]=TranformedVec2[i].xoutputArray[2*i+1]=TranformedVec2[i].yNextReturn outputArray 'ouputing transformed poly in a x/y Float array (for drawpoly)EndEndClass MyWindow Extends WindowField poly1:TransformablePolyField count:=0Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )poly1=New TransformablePoly(New Vec2f[](New Vec2f(-10.0,-10.0),New Vec2f(10.0,-10.0),New Vec2f(10.0,10.0),New Vec2f(-10.0,10.0))) 'a square in polygon format (center at 0,0)EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()count+=1If count>400 Then count=10poly1.angle=count/100.0poly1.position=New Vec2f(count,count)poly1.scale=1.5-Sin(count/60.0)canvas.DrawPoly(poly1.GetTransformedXYPoly())EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()Endhere’s a little example of a transformable poly that can be enhanced to get what you need, you can add size too..
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Struct TransformablePoly 'I'll assume here that center of polys are [0,0] so I'll wrap arround origin while creating themField transfoMat:AffineMat3fPrivateField _nbVertices:intField _vertices:Vec2f[]PublicMethod New(verts:Vec2f[])_vertices=verts_nbVertices=_vertices.GetSize(0) 'getting the number of vec2f in the arraytransfoMat=New AffineMat3f()EndMethod GetTransformedXYArray:Float[]() 'to be used with DrawPoly (it does not accepts vec2f!)Local outputArray:=New Float[_nbVertices*2]Local TranformedVec2:=GetTransformedVec2fPoly() 'getting the tranformed poly in a Vec2f arrayFor Local i:=0 Until _nbVerticesoutputArray[2*i]=TranformedVec2[i].xoutputArray[2*i+1]=TranformedVec2[i].yNextReturn outputArray 'ouputing transformed poly in a x/y Float array (for drawpoly)EndMethod GetTransformedVec2fPoly:Vec2f[]() 'Transforms all the vertices with the AffineMat3Local outputArray:=New Vec2f[_nbVertices]For Local i:=0 Until _nbVerticesoutputArray[i]=transfoMat.Transform(_vertices[i])NextReturn outputArrayEndEndClass MyWindow Extends WindowField poly1:TransformablePolyMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )' The polys are initialised with (0,0) as their (rotation and scale) center, it the easiest way to "view" with matrixpoly1=New TransformablePoly(New Vec2f[](New Vec2f(-10.0,-10.0),New Vec2f(10.0,-10.0),New Vec2f(10.0,10.0),New Vec2f(-10.0,10.0))) 'a square in polygon format (center at 0,0)' Here is the interresting stuff !!!' The order of the transforms has it's importance!' It looks like we first translate then rotate BUT NOT!!!!!!!!' Think it as a stack: Last in = First out'' Having the rotation as the first transform is more intuitive because' the center of the poly will have the corresponding translate positionpoly1.transfoMat=poly1.transfoMat.Translate(100,100) 'second transformpoly1.transfoMat=poly1.transfoMat.Rotate(Pi/4) 'rotation arrond (0,0)! , first transform' this should be implemented into the struct to have something like "poly1.Translate(100,100)"' and voilà! you have a transformable poly...EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawPoly(poly1.GetTransformedXYArray())EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()Endyou can extract translate,rotate and size from a AffineMat3 too if you need. see http://monkeycoder.co.nz/forums/topic/matrix-scale/
The most common way to do this is to use matrix transform. It’s what mojo uses. There is an affineMat3 class wich is usable for rotate/size/translate transforms. Matrices are your friends, this is the most usefull Math stuff you can have for video games after trigonometry..
Pull request made!
A printable version is attached, you can still see the WIP at: http://turdus.be/monkey2docs/docs/
I’ll be away from Mx2 for a while due to hard work but should be back on December (Half or FULL time!)
Mx2 rocks, I can’t wait for December to start working on my mobile game with this awesome dev. tool!
Attachments:
-
AuthorPosts