Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set background color in libgdx?

I am trying to make the background a sky blue color with RGB of 135,206,235. When I run it, the background is not the color I expected.

public void render () {
    Gdx.gl.glClearColor(.135f, .206f, .235f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(img, 0, 0);
    batch.end();
}
like image 989
user3554709 Avatar asked Jul 27 '14 21:07

user3554709


1 Answers

glClearColor uses range from 0 to 1, so you need to map from range 0 - 255 by simply dividing by constant 255f:

Gdx.gl.glClearColor(135/255f, 206/255f, 235/255f, 1);

Also be cautious when dividing 2 integers, if you don't convert any of then to float (or double), integer division will be used and result will be 0 (except for 255/255 == 1)

like image 65
kajacx Avatar answered Oct 14 '22 18:10

kajacx