I have been doing some testing using images created with DirectGraphics-methods and found out the following:
1. The transparency of images created with DirectGraphics is lost when drawing through an ordinary Graphics-object. Drawing through a DirectGraphics-object fixes the problem.
Ok, this i can live with, BUT:
2. Clipping does not work when drawing an image through DirectGraphics into a mutable image. Instead of a clipped image the whole image is drawn. The clipping only seems to work when drawing directly to the screen in the paint-method.
// earlier I've initialized an image background and gotten
// the Graphics-object for it via getGraphics
backgroundGraphics.setClip(0, 0, 60, 60);
DirectGraphics dg = DirectUtils.getDirectGraphics(backgroundGraphics);
dg.drawImage(directGraphicsImage, 0, 0, Graphics.LEFT|Graphics.TOP, 0);
g.drawImage(background, 0, 0, Graphics.LEFT|Graphics.TOP);
So of these two examples a) clips properly into a 60x60 rectangle and b) does not (but draws the entire image).
Clipping has been a very convenient way to draw animated sprites until now, but it seems I cannot do it any more with DirectGraphics-images? The alternative would be to create a separate image for each of the sprite's frames which would cause a lot of unnecessary memoryloss.
Is there any workaround for this problem? Am I doing something wrong? I already tried creating the background-image using DirectGraphics createImage-method - did not help.
>>Clipping has been a very convenient way to draw animated
>>The alternative would be to create a separate image for each
>>of the sprite's frames which would cause a lot of unnecessary >>memoryloss.
There's another and better option to that problem.
First, you should forget about using drawImage at all - its slow, unflexible, and on some devices it simply does not work like expected.
Use drawPixels() instead - its way faster and works across all devices (at least, the currently available ones...
For animated sprites, we've created a class ("ImageStrip") that loads an image containing all animation frames vertically aligned, allocates (direct) pixel data, and renders the image into the pixel data. ImageStrip is given the number of "cells" (= animation frames) either from the file itself or by parameter, and thus knows the height per cell.
When you want to draw a specific cell, the drawing method has the offset into the pixel data (cell number * cell height) and simply calls drawPixels() with that info.
To avoid the multiplications, ImageStrip creates an array of cell pixel data offsets upon initialization.
As an additional benefit, you can even manipulate the pixel data on the fly, e.g. change colors. In our case, we have additional functionality for doing scaled blits for zoom-sprites.