Some Monkey stuff.
Monkey Forums/Monkey Code/Some Monkey stuff.
| ||
This is some stuff I got laying around which are Pixel Collision and Sockets. Don't get excited, It doesn't work so good, at least the pixel collision doesn't, and sockets( as client ) only work for flash and android. Here you can see an example of the pixel collision: http://shin.cl/pixelcollision/ For the pixel collision I used diddy's GetPixel(), and it only works on flash and html5, but html5 gets 0 fps when testing collision. I tried 2 methods, one was drawing one image frist then getting all pixels of the intersecting rectangle and the draw the second image, finally compare them and check if they are colliding. The second method was drawing both images with and offset and then compare pixels. This one runs a bit better, but still lags a lot. And here you can see a chat made with the socket module, runs on flash and android. http://shin.cl/crosschat/ I was actually building a crossplatform MMO but I got stucked on the server part. The demo is running on an Adobe Air ServerSocket App. Here you can get the code: https://github.com/ilovepixel/MonkeyStuff All this isn't perfectly coded, as I am no programmer but It can probably help someone else, or even improve it. :) |
| ||
I update this post as I've added to my monkeystuff repo a small bitmap module, only working on flash and html5. With this bitmap module you can grab an image an load it to a bitmap or create a bitmap o copy from the canvas. You can also render other bitmaps into your bitmap. It also has a simple get and set pixel. The flash version has pixel-perfect collision. Here you can see a demo: http://shin.cl/bitmap And you can get the module from: https://github.com/ilovepixel/MonkeyStuff/tree/master/bitmap |
| ||
Great. Will definitely find a use for the bitmap render stuff. |
| ||
y u got santa claus penis on the demo :0 The flash version doesnt work here, I am using a computer with an old version of flash on it (cant upgrade) so maybe thats why |
| ||
collisions work really well here.. I would love to see a more intense collisions example tho, something that we can use as a water mark for how well it will perform in a game. |
| ||
checked it with a different pc and it works on flash too, good one, This is actually useful, cos someone might want to copy a tiled background as one image, i wonder what the best way of doing scrolling would be using that method do you have the source code to the demo? basically i want to grab an image from the canvas and draw it back to the canvas |
| ||
I've updated the demo for the bitmap module, It's a simple shoot the enemys with pixel collision checking on every bullet and enemy and the rock at the background. I'll upload the code for the demo and also update the module in a couple of hours as I need some sleep. here is the demo: http://shin.cl/bitmap |
| ||
I've uploaded the code for the demo here: https://github.com/ilovepixel/MonkeyStuff/blob/master/bitmap/demo/main.monkey I've also updated the bitmap module and added a method called ClearPixel() which can be seen in action on the demo, when for example the bullet collides with the big rock thing, or the red space ships. To the demo I've added a small SetPixel effect to the ships which can be disabled by pressing E key. http://shin.cl/bitmap/ I was looking into making an android version of the module but I know nothing of java or opengl. |
| ||
great job and thank you for sharing, I have some ideas I'd like to try out and I will need this. Any plans to make an IOS version too? |
| ||
I tried making a little demo to test grabbing an image from the screen:Strict Import ilovepixel.bitmap Import mojo Const wall_tile_height:Int=8 Const wall_tile_width:Int=8 Const walk_up_limit:Int=88 Global the_camera:camera Function Main:Int() New mygame Return 0 End Function Class mygame Extends App Method OnCreate:Int() SetUpdateRate 40 the_camera=New camera Return 0 End Method Field b:Bitmap Method OnUpdate:Int() If KeyDown(KEY_RIGHT) the_camera.x+=1 Endif If KeyHit(KEY_SPACE) b=New Bitmap() b.MakeBitmapData(30,30,True,255,255, 255) b.x=0 b.y=0 b.CopyFromCanvas(0,0,30,30) b.x=30 b.y=30 Endif Return 0 End Method Method OnRender:Int() Cls PushMatrix() Local switch:Bool Scale 1,1 For Local x:Int=0 To 64 For Local y:Int=0 To 48 If switch=True SetColor(0,255,255) switch=False Else SetColor(200,255,255) switch=True Endif DrawRect(x*10,y*10,10,10) Next Next If b<>Null Print "rendering" b.RenderToApp() Endif Return 0 End Method End Class Class camera Field x:Int Field y:Int End Class but it crashes, only html5 produces an error message |
| ||
Hello, sorry about that. I've already uploaded the fixed CopyFromCanvas(), I have also added a function to get and set pixels from the canvas and not the bitmap, that way you don't have to create a new bitmap to do it, for example: [monkeycode] Bitmap.SetPixelApp(100,100,255,0,0) Local colors:Int[] = Bitmap.GetPixelApp(100,100) Print(colors[0]+","+colors[1]+","+colors[2]+","+colors[3]) [/monkeycode] Here is a demo of how to use CopyFromCanvas(): The CopyFromCanvas should be run after the image or part of the canvas is rendered. [monkeycode] Import mojo Import bitmap Function Main:Int() New Grab() Return 0 End Class Grab Extends App Field bit:Bitmap Field img:Image Field copied:Bool Method OnCreate:Int() img = LoadImage("img.png") bit = New Bitmap() bit.MakeBitmapData(50,50,False,255,0,0) copied = False SetUpdateRate(60) Return 0 End Method OnUpdate:Int() Return 0 End Method OnRender:Int() Cls(0, 0, 0) DrawImage(img, 100, 100) If( Not copied) bit.CopyFromCanvas(100, 100, img.Width(), img.Height()) copied = True EndIf bit.RenderToApp() Return 0 End End [/monkeycode] About an IOS version, I don't know really, as don't know any C++. |
| ||
it works on html5 now, but not on flashStrict Import ilovepixel.bitmap Import mojo Function Main:Int() New mygame Return 0 End Function Class mygame Extends App Method OnCreate:Int() SetUpdateRate 40 Return 0 End Method Field b:Bitmap Method OnUpdate:Int() If KeyHit(KEY_SPACE) b=New Bitmap() b.MakeBitmapData(30,30,True,255,255, 255) b.x=0 b.y=0 b.CopyFromCanvas(0,0,29,29) Endif Return 0 End Method Method OnRender:Int() Cls PushMatrix() Local switch:Bool Scale 1,1 For Local x:Int=0 To 64 For Local y:Int=0 To 48 If switch=True SetColor(0,255,255) switch=False Else SetColor(200,255,255) switch=True Endif DrawRect(x*10,y*10,10,10) Next Next If b<>Null Print "rendering" b.RenderToApp() If b.x<590 b.x+=1 Endif Endif Return 0 End Method End Class the flash version just freezes |
| ||
The flash debugger said that the bitmapDataSource was null, probably is because you are calling CopyFromCanvas before any drawing is done, or other theory would be that monkey disposes the canvas bitmapdata after the render loop, I really couldn't find what was giving problems. Here I made an edit and called the method after everything was already drawn and it worked on flash and html5. Strict Import bitmap Import mojo Function Main:Int() New mygame Return 0 End Function Class mygame Extends App Field copy:Int Method OnCreate:Int() SetUpdateRate 40 copy = 0 Return 0 End Method Field b:Bitmap Method OnUpdate:Int() If KeyHit(KEY_SPACE) copy = 1 Endif Return 0 End Method Method OnRender:Int() Cls PushMatrix() Local switch:Bool Scale 1,1 For Local x:Int=0 To 64 For Local y:Int=0 To 48 If switch=True SetColor(0,255,255) switch = False Else SetColor(200,255,255) switch = True Endif DrawRect(x*10,y*10,10,10) Next Next If(copy = 1) b = New Bitmap() b.MakeBitmapData(30,30,True,255,255, 255) b.x=0 b.y=0 b.CopyFromCanvas(0, 0, 29, 29) copy = 0 EndIf If b<>Null Print "rendering" b.RenderToApp() If b.x<590 b.x += 1 Endif Endif Return 0 End Method End Class |
| ||
A new example of fractal using FillRect(), apparently on html5 this is much faster than SetPixel() http://shin.cl/bitmap/mandelbrotset/ The original code is from: http://www.actionscript.org/resources/articles/614/1/Creating-a-Mandelbrot-set/Page1.html The monkeycode: Import mojo Import bitmap Function Main:Int() New Fract() Return 0 End Class Fract Extends App Field bt:Bitmap Field cycle:Float Method OnCreate:Int() bt = New Bitmap() bt.MakeBitmapData(DeviceWidth(), DeviceHeight(), False, 0, 0, 0) cycle = 128 DrawSet( -2, 1, - 1, 1) SetUpdateRate(60) Return 0 End Method DrawSet:Void(rmin:Float, rmax:Float, imin:Float, imax:Float) Local rStep:Float = (rmax - rmin) / bt.width Local iStep:Float = (imax - imin) / bt.height Local r:Float Local i:Float For Local px:Int = 0 To bt.width r = rmin + px * rStep For Local py:Int = 0 To bt.height i = imin + py * iStep; Local color:Int[] Local cy:Int = GetCycle(r, i) If(cy = cycle) color =[0, 0, 0] Else color =[Int(cy + 90), Int(cy), Int(cy)] EndIf 'bt.SetPixel(px, py, color[0], color[1], color[2]) bt.FillRect(px, py, 1, 1, color[0], color[1], color[2]) Next Next End Method GetCycle:Int(r:Float, i:Float) Local zr:Float = 0 Local zi:Float = 0 Local cr:Float = r Local ci:Float = i Local ozr:Float = 0 Local ozi:Float = 0 Local zrsq:Float = 0 Local zisq:Float = 0 Local a:Int For a = 0 To cycle zi = ozr * ozi * 2 + ci zr = zrsq - zisq + cr zrsq = zr * zr zisq = zi * zi ozr = zr ozi = zi If( (zrsq + zisq) > 4) Exit EndIf Next Return a End Method OnUpdate:Int() Return 0 End Method OnRender:Int() Cls() bt.RenderToApp() Return 0 End End |
| ||
oh i see it is rendering code so it has to go in the Onrender method thanks! |
| ||
If/when you do render-to-surface for GLES targets you'd be better off using VBOs (GLES 2) and render directly to them rather than copying from the canvas. |
| ||
flash acts a bit differently than html5 html only has one way of drawing images but flash has 2 bitmapData.draw(scratch,mmatrix,colorTform,blend,clipRect,image_filtering_enabled); and bitmapData.copyPixels( surface.bitmap.bitmapData,srcrect,new Point( x,y ) ); so here is the new rendertoApp function for actionscript public function RenderToApp():void { if( app.graphics.matrix ){ if( x!=0 || y!=0 ){ //have to translate matrix! TODO! return; } app.graphics.bitmapData.draw(bitmap.bitmapData,app.graphics.matrix,app.graphics.colorTform,app.graphics.blend,app.graphics.clipRect,app.graphics.image_filtering_enabled ); }else if( app.graphics.clipRect || app.graphics.colorTform || app.graphics.blend ){ var mat:Matrix=new Matrix( 1,0,0,1,x,y ); app.graphics.bitmapData.draw( bitmap.bitmapData,mat,app.graphics.colorTform,app.graphics.blend,app.graphics.clipRect,app.graphics.image_filtering_enabled ); }else{ app.graphics.bitmapData.copyPixels( bitmap.bitmapData,rect,new Point( x,y ) ); } } |