Render once to a bitmap, then re-render

Monkey Forums/Monkey Programming/Render once to a bitmap, then re-render

benmc(Posted 2012) [#1]
It's hard for me to put this question into words...

It it possible in Monkey to render tiles to a Bitmap, then just re-render that 1 bitmap over and over again?

On my older Android games I made in Java, I was able to load up my tile map, then create a single Bitmap from those tiles, then I would just render that one bitmap on each render as my background.

The reason for this is that I have 10x10 tiles, and drawing all 1536 of them on every render is going just a little too slow. But if I could render them to a single Bitmap, and just re-render that 1 bitmap over and over again it would go really fast.


Rex Rhino(Posted 2012) [#2]
As far as I understand it, no... There is no way of rendering to an image and redrawing.


benmc(Posted 2012) [#3]
Dang. I feel like I could put together a module to do this for Android from my previous experience, but I'd be at a loss of all other targets.

The only other thing I was wondering is if I could push a render onto a Matrix of some kind, and then re-pop that Matrix each time before I do my other drawing. Or am I not understanding how Matrix's work?


Rex Rhino(Posted 2012) [#4]
It is a significant restriction, no doubt. My guess is that because there is no way to do it in HTML5, it wasn't included for any platform.


AdamRedwoods(Posted 2012) [#5]
My guess is that because there is no way to do it in HTML5, it wasn't included for any platform.

you are correct (although the w3c is looking into this):
http://stackoverflow.com/questions/2688987/imagedata-of-an-externally-loaded-image

But... if you are OK with only Opengl targets, you can use databuffers for your raw data and upload this to a texture and render a quad.


benmc(Posted 2012) [#6]
Yes, I am only concerned with iOS and Android at the moment. I wish I understood what uploading to a texture and rendering to a quad was :) I'll search the forums.... Do you know of anywhere this was previously discussed?


muddy_shoes(Posted 2012) [#7]
You can do this with HTML5 as long as you don't mean rendering to an actual image that you save as a file. HTML5 allows you to create more than one canvas and render one canvas onto another.

If anything, Android is the trickier platform as you can't trust that the OpenGL implementation supports render to texture. You can use the non-OGL graphics API instead, but that's too slow to do much image construction on the fly.


AdamRedwoods(Posted 2012) [#8]
I wish I understood what uploading to a texture and rendering to a quad was :)


see monkey/bananas/mak/opengltest (in latest monkey)


AdamRedwoods(Posted 2012) [#9]
You can do this with HTML5

you're right! getImageData() setImageData(). Even IE supports this. I'm confused, not sure why there's no ReadPixel() and WritePixel() in monkey now...


Rex Rhino(Posted 2012) [#10]
you're right! getImageData() setImageData(). Even IE supports this. I'm confused, not sure why there's no ReadPixel() and WritePixel() in monkey now...


Yep. And I double checked, and it is possible in XNA for Xbox360 as well.

I think it would probably be good for us to respectfully and enthusiastically encourage Mark to work on that feature. This is something that can drastically improve performance... especially on low-end mobile devices. We *NEED* this feature.


muddy_shoes(Posted 2012) [#11]
It has been mentioned/requested multiple times in various forms since Monkey was released.


benmc(Posted 2012) [#12]
This post derailed a little from what I was originally asking, though I would love a get/set pixel option :)

I was just wondering if it was possible to turn a bunch of tiles in one image into another single image created dynamically for re-rendering, or to be able to create an off-screen canvas from those sprites/images that I could re-draw over and over again.

In Java, I would create an empty Bitmap.

Draw my tiles to that Bitmap.

Then onDraw, I would just re-draw that pre-created Bitmap instead of redrawing every tile every time.

Was hoping for something similar being possible in Monkey town.


muddy_shoes(Posted 2012) [#13]
That is what I'm talking about. Render to texture, render to bitmap, off-screen buffer with grab/copy. They're all routes to being able to create composite images at runtime and then re-use them.

Some of the dozen or so previous mentions of wanting some form of this feature:

http://monkeycoder.co.nz/Community/posts.php?topic=612
http://monkeycoder.co.nz/Community/posts.php?topic=2321
http://monkeycoder.co.nz/Community/posts.php?topic=1369


benmc(Posted 2012) [#14]
Those posts are all from 8 months to almost a year ago, so I don't know if they are still relevant.

I was searching for combining images, or pre-rendering, of which none of these posts showed up, so my terminology must be wrong I guess.

Those threads seem to have been left hanging too, I can't tell if it's possible with the new functionality that's been added to Monkey since then or not.

I looked at the examples in Bananas, and I didn't see where textures were combined into a single texture to be re-drawn.

I did just find this thread linked to from another:
http://www.monkeycoder.co.nz/Community/posts.php?topic=263

It too is from a year ago but Mark says it'll never happen. Wish I had seen that 6 months ago when I started using Monkey :( Things change tho, hoping for a miracle.


muddy_shoes(Posted 2012) [#15]
They're relevant in that they show that the feature has been repeatedly requested already. You're right that the response you link to from Mark is probably the only response that has ever occurred.

The fact is that it can be done but implementations end up bound with the existing mojo library and therefore can't be distributed. My own libraries include this functionality and I'm sure several other people have done it in some way too but, for whatever reason, it's not something that Mark wants to put into the standard package.


AdamRedwoods(Posted 2012) [#16]
One of the reasons read/write pixels is not in there is that mojo loads image files and then flushes them from memory. They are retained by the graphics chip. Saves on memory.

With the opengl.monkey and LoadImageData(), you could add a function in mojo that takes a DataBuffer and creates a surface. This would be your best bet for image manipulation/image combination and keeping in line with Mark's original intentions.

It would be similar to each native gtkSurface's LoadSurface, but with data (from the databuffer) instead of the path. You'd also have to create a public function from the monkey side as well.

I'll see if I can't convince him to make DataBuffer available to all targets, and add a function in mojo, but it'll take time to produce *convincing* examples (as I did with monkeyGL).


muddy_shoes(Posted 2012) [#17]
I would have thought that the utility of this is pretty obvious. It's useful for on-the-fly construction of game elements (e.g. different equipment on your RPG avatar). Also, as benmc is talking about here, it's a way of speeding up rendering by constructing a composite image once and then stamping it out repeatedly, e.g. http://www.stickydesigns.com/monkey/offscreen/MonkeyGame.html


benmc(Posted 2012) [#18]
@muddy_shoes is the off-screen buffering of text example a custom hack you made to mojo or is it already possible?


muddy_shoes(Posted 2012) [#19]
It's my own fork of mojo that allows you to define off-screen buffers.

Edit: And I've changed the example to colorise the text and make the potential speed difference clearer.


OvineByDesign(Posted 2012) [#20]
Thats perfect - just need one for IOS and Android :)

/StuC


Aman(Posted 2012) [#21]
InvaderJim created something useful and similar to what you are asking but it had the same problem as muddy_shoes. Although it also sounds really useful, it won't be of any use to us unless Mark implement them in the official version of mojo.

http://monkeycoder.co.nz/Community/posts.php?topic=2687#27108


AdamRedwoods(Posted 2012) [#22]
DataBuffers and LoadImageData. All targets are capable of this and this would allow us to do what we're after.

It's in opengl, but needs to be in the rest.


OvineByDesign(Posted 2012) [#23]
Id be happy with it just being in IOS/Android/Windows Mobile ! actually IOS would just do :)

I understand why people keep their routines close to their chests - just hope one day it can be released to the masses

/StuC
www.ovine.net


Midimaster(Posted 2012) [#24]
@muddy_shoes

your demo...

http://www.stickydesigns.com/monkey/offscreen/MonkeyGame.html

...of rendering text to an image is perfect. I need something like this for speeding up performance in my music education games. Here I often have the problem of displaying changing text elements (f.e. chord of a song) together with low latency music output. This is not possible with the classical way of dispalying the text letter by letter.

Do you see a chance for adding your code as a module? What about android target?