Should i re-draw SurfaceLayer on every frame?

Go To StackoverFlow.com

0

I've create simple example: background surface layer and 10 small "dots" on it (10 surface layers 10x10 px each filled with color via fillRect()). Paint method simply moves the dots around periodically:

private SurfaceLayer background;
private List<Layer> dots = new ArrayList<Layer>();

@Override
public void init()
{
    background = graphics().createSurfaceLayer(graphics().width(), graphics().height());
    background.surface().setFillColor(Color.rgb(100, 100, 100));
    background.surface().fillRect(0, 0, graphics().width(), graphics().height());
    graphics().rootLayer().add(background);

    for (int i = 0; i < 10; i++)
    {
        SurfaceLayer dot = graphics().createSurfaceLayer(10, 10);
        dot.surface().clear();
        dot.surface().setFillColor(Color.rgb(250, 250, 250));
        dot.surface().fillRect(0, 0, 10, 10);
        dot.setDepth(1);
        dot.setTranslation(random()*graphics().width(), random()*graphics().height());
        dots.add(dot);

        graphics().rootLayer().add(dot);
    }
}

@Override
public void paint(float alpha)
{
    for (Layer dot : dots)
    {
        if (random() > 0.999)
        {
            dot.setTranslation(random()*graphics().width(), random()*graphics().height());
        }
    }
}

Somehow java version draws all dots while html and android version draw only 1.

Manual doesn't clearly say if i should re-draw all these dots in every paint() call. And as far as i understood SurfaceLayer is meant for cases when you do not modify layer on every frame (so same buffer can be reused?), but this doesn't work.

So can you guys help me with correct SurfaceLayer usage? If i just filled a rectangular on SurfaceLayer - would it ramin on this layer forever or should i fill it in each paint call? If yes - is this different from ImmeadiateLayer?

2012-04-04 06:20
by user1312030


1

You don't need to redraw a surface layer on every call to paint. As you have shown, you draw it only when preparing it, and the texture into which you've draw will be rendered every frame without further action on your part.

If the Android and HTML backend are not drawing all of your surface layers, there must be a bug. I'll try to reproduce your test and see if it works for me.

One note: creating a giant surface the size of the screen and drawing a solid color into it is a huge waste of texture memory. Just create an ImmediateLayer that calls fillRect() on every frame, which is far more efficient than creating a massive screen-covering texture.

2012-04-04 21:00
by samskivert
I added a test like yours to the PlayN surface tests and they work fine for me on Java, HTML and Android. So maybe something else is wrong - samskivert 2012-04-04 21:30
Now here is a test game: http://speedy.sh/Q56mx/test-game.tar.gz

I launch it with:

mvn clean install
mvn test -Ptest-html

Then open localhost:8080 in FF or Chrome - it shows black screen with a single white dot.

Perhaps you can take a look at the code and find an error quickly? Or share your test so i can figure the problem out myself - user1312030 2012-04-06 16:12

Ads