Drawing on a pixmap

BlitzMax Forums/BlitzMax Programming/Drawing on a pixmap

Tylerbot(Posted 2007) [#1]
I want to draw small sprites directly onto a background image/pixmap.

Right now, the only way I can see to do that is to do the following at each rendering step:

1. draw the background image to the buffer
2. if it is necessary to draw such a sprite to the background:
2a. draw the sprite to the buffer
2b. grab a new pixmap and set the return value to the background image (so that next time I draw the background image, the sprite is automatically incorporated)
3. draw the rest of the scene

I have considered the possibility of blitting the pixels from the sprite directly onto the pixels of the background image, but that seems to be really inefficient and I'd have to write my own transforms.

What should I be doing instead?


tonyg(Posted 2007) [#2]
If you don't need alpha then use pixmap.paste method..
I wrote this which still might work to memcopy non-maskcolour pixels only but really for small sprites.
Writepixel is quick so might be an option as is drawimage/grabimage. Finally, Indiepath wrote a Render-To-Texture module to do just this which I can't get to work since Mark changed the DX7 Driver.
If you ask nicely maybe Tim will rewrite it.


Punksmurf(Posted 2007) [#3]
I have considered the possibility of blitting the pixels from the sprite directly onto the pixels of the background image, but that seems to be really inefficient and I'd have to write my own transforms.

What makes you think that is inefficient? It's not like Blitz interally does something else (at least, I cannot not imagine what, not where alpha is involved anyway).

Besides that, grabimage is amazingly slow (I don't remember why, but there's an answer out there). Anyway, what you're doing now is drawing your background picture (big) to the background, then drawing a small image onto the background, and then grabbing it (thus, effectively drawing it again). Just blitting the small image over the background image would thus be way faster.


peltazoid(Posted 2007) [#4]
Alternatively, hold each sprite as an array and plot each pixel to the pixmap when required.

For one of my projects, I need 'old school' sprite splitting effects and having the sprite as an array of colour indices to a pallet array then drawing each pixel to the background for the sprite. Not hard to do and is faster than having to grab an image each time, also it is more flexible.


Tylerbot(Posted 2007) [#5]
Thanks for your prompt replies, but I don't yet see the answer I'm looking for. Let me elaborate a bit.

tonyg: I do need alpha.

Punksmurf: The copying of the pixels of each sprite becomes inefficient when there are many, many sprites. I'd like to stop keeping track of a sprite after I draw it (since it's static from then on). Whereas, I figured updating the background texture once every couple of frames would be OK, and it wouldn't care how many sprites are "pasted" onto it. I hadn't counted on grabimage being so ghastly inefficient, though.

peltazoid: The problem there is, I need to rotate the image and possibly also scale it just before plotting the pixels. I'm not familiar with the smoothing algorithms and I really like the native ones in Blitz. So, I'd like to use the native rotation and scale methods, as well as alpha per-pixel transparency, when I blit the sprite onto the background texture.

Thanks for your help so far though. I'm still experimenting heavily with this and always open to new techniques.


TomToad(Posted 2007) [#6]
One idea I could think of is to break up the background into smaller sections, then you only need to draw and grab the section that the new sprite will cover (or sections if the sprite overlaps more than one).


Punksmurf(Posted 2007) [#7]
Ah, when you want to draw with those transforms, nothing beats your GPU. Perhaps TomToads idea works, otherwise I wouldn't know.


assari(Posted 2007) [#8]
Tylerbot,
There is another technique you can use which is using renderlists, see this link http://www.blitzmax.com/Community/posts.php?topic=56615 or search forum for renderlists.

If you need a more basic intro, you can try here
http://www.2dgamecreators.com/tutorials/gameprogramming/basics/F4%20-%20Revised%20Framework.html


Tylerbot(Posted 2007) [#9]
TomToad: Yes your idea might actually be the most efficient, and I might not actually have to break up the background into discrete tiles. I got to thinking about optimization, here's a variation I came up with:
1. CLS
2. Draw the current background bitmap from memory
3. Draw any new, static sprites that are to be "added"
4. Using some auxiliary data about the static sprites, grab only those rectangular areas which have been modified, and save that pixel data (pixel by pixel) back into the background texture
5. Draw the rest of the scene

It's kind of like dynamic-sized tiles. What do you think?