Search code examples
playn

Cannot render SurfaceLayer during paint method


Edit This was a bug that has been fixed in https://github.com/threerings/playn/commit/137ce50dddd2716f84c38ec1568d7ae5a368434b. See Par Eklund's answer for details.

Before I begin: I am a novice at game programming in general so it's very likely that I have a fundamental misunderstanding of the tools I am using. However I'll push on with my question...

Whenever I try to draw something on my Surface Layer during the paint method I get a black screen. If I draw during the init method it shows up OK.

I am trying to replicate the code under the Surface Layer banner here. The code in question is this:

public void paint() {
  // Draw 100 random peas imperatively.
  surf.clear(0);
  for (int i = 0; i < 100; ++i) {
    int x = (int)(random() * WIDTH);
    int y = (int)(random() * HEIGHT);
    surf.drawImage(pea, x, y);
  }
}

I am using PlayN 1.4 so while that code doesn't directly match up to what I am writing I thought I at least had the equivalent (except I am drawing white rectangles):

public void paint(final float alpha) {
  surfaceLayer.surface().clear();
  surfaceLayer.surface().setFillColor(Color.rgb(255, 255, 255));
  for (int i = 0; i < 100; ++i) {
    int x = (int) (Math.random() * width);
    int y = (int) (Math.random() * height);
    surfaceLayer.surface().fillRect(x, y, 5, 5);
  }
}

Nothing is rendered to the screen. If I move that code into the init method it renders as expected.

Now interestingly if I make PlayN 1.3.1 project and use the exact same code it renders fine during the paint method (given that I'm still using the exact same code I'm not sure what version the PlayN website is using in its example code...).

What have I missed here? Has 1.4 changed something to do with the Surface Layer and I must use it differently now? Thanks.


Solution

  • I am basically a novice to game programming myself and I have run in to the same problem as you (same symptoms at least) with PlayN 1.5-SNAPSHOT. I filed an issue for it here: http://code.google.com/p/playn/issues/detail?id=205

    Also, my experience is that it does not seem to be specifically related to whether rendering is done in the init- or the paint method. Rather, it seems as if rendering only succeeds the _first_time_ (first frame rendering occurs). Please see the attached file in the issue for a (somewhat) minimal example.

    Like I stated in the issue, I suspect that this is related to the GL20 implementation and how frame buffers are being managed, but given that I am even more a novice on OpenGL you should take my speculation with a huge grain of salt.

    The PlayN main contributors should be able to answer and solve this pretty quickly though. Let's hope that we receive their attention soon. :)