3

I'm developing a game for Android using libGDX. I implemented "spotlight effect" for highlighting some scene elements in tutorial levels. I did use Pixmap class for implementing this. So I did something like this:

public class ComplexSpotlight implements MoveObserver{

// somewhere in class

    public void update(){
        black.setColor(0, 0, 0, 0.7f);
        black.fill();

        black.setColor(0, 0, 0, 0);

        for(Map.Entry<MoveObservable, Vector2> entry : currentObservablePositions.entrySet()){
            Vector2 position = entry.getValue();

            int x = (int)(Gdx.graphics.getWidth()/2+position.x);
            int y = (int)(Gdx.graphics.getHeight()*1.5f-position.y);

            int radius = 50;

            black.fillCircle(x, y, radius);
            System.out.println("at "+x+" "+y);
        }

        overlay.dispose();
        overlay = new Texture(black);
    }

    public Texture getOverlayTexture(){
        return overlay;
    }

Fast explanation - I'm creating a Pixmap filling it with color(0,0,0,0.7f). After this I'm drawing transparent circle using Pixmap.fillCircle() method. And then I'm creating new Texture using this Pixmap. I've tested this code using following devices: HTC One V(480*800), Sony XPeria Neo 15i(480*854), HTC Desire S(480*800) and it works good;

enter image description here

But today I'm found that there are problems on HTC One X(1280*720) and Gamsung Nexus(1280*720) - I'm seeing black screen only.

So it would be nice if someone give me some explanation about this issue. Links, thoughts - anything may be helpful. Thanks in advance!

Viacheslav
  • 5,443
  • 1
  • 29
  • 36
  • 1
    The problematic phones are fairly large screens. Are you running out of texture memory or something like that? (Assuming you are drawing at full resolution). You might try rendering to a smaller viewport to see if that fixes the issue. Also make sure its really the overlay that is the problem (does the background display correctly if you disable the overlay? If you fill it with red or white?) – P.T. Jan 23 '13 at 17:55
  • I have also paid attention to screen size but have no have any suggestion how it can . No one exception/error was occurs during creation and rendering of my overlay. After tapping on screen overlay removes (according to my game logic) and I see my background and game objects. Also I'm showing popup with text message above this overlay and it renders correctly. I will try test that you proposed and update my post with results. – Viacheslav Jan 24 '13 at 06:54
  • @P.T. You was completely right about large screen size - at some reasons I've used 2*screen dimensions size which increases texture size up to 4x! After I changed Pixmap size to screen size all works well. If you post your comment as answer I will approve it as topic solution. Thanks! – Viacheslav Jan 25 '13 at 07:58

2 Answers2

1

I'm not sure but i've been having problems with Pixmap filling alpha on my application too. I found that the fillCircle in pixmap just didnt set the alpha correctly. When I looked into it a bit i found that a better approach was to use a FrameBuffer.

I found the details in the following libgdx SpriteBatch render to texture

Some of my code which i've been using is

public class SplashScreen extends GameScreen {

SpriteBatch batch;
BitmapFont font;

FrameBuffer frmBuff;
TextureRegion frm;
OrthographicCamera frmCam;

int frmSizeX = 256;
int frmSizeY = 256;

public SplashScreen(ReactorApp game) {
    super(game);
    setClearColor(new Color(1,1,1,1));
}

@Override
public void show() {
    // TODO Auto-generated method stub
    super.show();
    batch = new SpriteBatch();
    font = new BitmapFont();

    frmBuff = new FrameBuffer(Format.RGBA8888, frmSizeX, frmSizeY, false);
    frm = new TextureRegion(frmBuff.getColorBufferTexture());
    frmCam= new OrthographicCamera(frmSizeX, frmSizeY);
    frmCam.translate(frmSizeX/2, frmSizeY/2);
}

@Override
public void dispose() {
    // TODO Auto-generated method stub
    super.dispose();
}


public void drawToTexture(){
    frmCam.update();
    ShapeRenderer shape = new ShapeRenderer();
    shape.setProjectionMatrix(frmCam.combined);
    frmBuff.begin();

    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    shape.begin(ShapeType.FilledRectangle);
    shape.setColor(Color.BLACK);
    shape.filledRect(0,0,frmSizeX/2, frmSizeY/2);
    shape.end();

    shape.begin(ShapeType.FilledCircle);
    shape.setColor(Color.GREEN);
    shape.filledCircle(MathUtils.random(frmSizeX), MathUtils.random(frmSizeY), 20);
    shape.end();

    frmBuff.end();
}

int pos = 0;

@Override
public void render(float delta) {
    super.render(delta);
    drawToTexture();

    batch.setProjectionMatrix(cam.combined);


    batch.begin();
    batch.draw(frm, pos++, 30,frmSizeX,frmSizeY);
    if(pos>Gdx.graphics.getWidth()-frmSizeX)pos=0;
    font.setColor(Color.RED);
    StringBuilder message = new StringBuilder();
    message.append("Time :"+(int)(delta*1000)+"\n");
    font.drawMultiLine(batch, message.toString(), 10, Gdx.graphics.getHeight());
    batch.end();
}

}

this give the idea what i've been playing with but it seems to work well.

Community
  • 1
  • 1
joey.enfield
  • 1,229
  • 8
  • 14
  • Thanks for example, Joey! I will try to implement this approach as I assume it will have better performance. – Viacheslav Jan 25 '13 at 08:00
1

The problematic phones are all fairly large screens. Are you running out of texture memory or something like that? (Assuming you are drawing at full resolution.) You might try rendering to a smaller viewport to see if that fixes the issue.

Now that I know this guess was right, I'm curious if there was any evidence in the app's (or Android's?) logs or in the OpenGL error state that points to this problem now that you know what the cause was.

P.T.
  • 24,557
  • 7
  • 64
  • 95