grid - LibGdx: most efficient way to draw a checkerboard in background -


i building game libgdx. game screen grid scene2d actors. actors displayed on front. draw background looks checkerboard, coloring every 2 cells 1 color , other cells color.

it easy wondering if there classes in libgdx optimize such background, make light , optimized possible. example, coloring each cell individually should work doesn't seem best approach.

i have started dig tiledmaptilelayer class i'd have fill each cell tile object , seems heavier color.

ideally, define 2 colors, set coordinates of cells, , fill them colors without having use objects or color cells 1 one.

what best approach?

i think simple way be, define 2 tiles black , white or ever , create tiledmap on runtime 2 tiles. it's filling cells in 2 loops(x,y) same tiles. should easy manage. put texture of inside of 1 texture minimize rendertime. dont need handle rendering , stuff yourself. need implement creation of board tiledmap system. there no solution can define 2 colors. need iterate on cells somehow think.

take @ example code of libgdx creating tiledmap on runtime.

you can copy paste , change line

cell.settile(new statictiledmaptile(splittiles[ty][tx])); 

simply add texture region , white there depending on cell are. this

if(y % 2 !=0){     cell.settile(new statictiledmaptile(blacktextureregion)); //or color need }else{     cell.settile(new statictiledmaptile(whitetextureregion)); //or color need } 

this create black , white rows. recomend use atlas textureregions or define them yourself.

play around x , y values , check need. maybe change if statement wrote right need.

if want define color need create pixmap 32x32 filled color on runtime , create texture out of it. can use create tilemap shown above.

here how can create 32x32 tiles need. can create 1 texture 32x64 both tiles. create textureregions of 0,0,32,32 , 32,0,32,32.

pixmap pixmap = new pixmap(64, 32, format.rgba8888); pixmap.setcolor(color.blue); // add 1 color here pixmap.fillrectangle(0, 0, 32, 32);  pixmap.setcolor(color.red); // add 2 color here pixmap.fillrectangle(32, 0, 32, 32); // outcome texture blue left square , red right square texture t = new texture(pixmap); textureregion reg1 = new textureregion(t, 0, 0, 32, 32); textureregion reg2 = new textureregion(t, 32, 0, 32, 32); //now use create statictiledmaptile 

if glue should have system have. here go:

pixmap pixmap = new pixmap(64, 32, format.rgba8888); pixmap.setcolor(color.blue); // add 1 color here pixmap.fillrectangle(0, 0, 32, 32);  pixmap.setcolor(color.red); // add 2 color here pixmap.fillrectangle(32, 0, 32, 32); // outcome texture blue left square , red right // square texture t = new texture(pixmap); textureregion reg1 = new textureregion(t, 0, 0, 32, 32); textureregion reg2 = new textureregion(t, 32, 0, 32, 32);  tiledmap map = new tiledmap(); maplayers layers = map.getlayers(); (int l = 0; l < 20; l++) {     tiledmaptilelayer layer = new tiledmaptilelayer(150, 100, 32, 32);     (int x = 0; x < 150; x++) {         (int y = 0; y < 100; y++) {             cell cell = new cell();             if (y % 2 != 0) {                 if (x % 2 != 0) {                     cell.settile(new statictiledmaptile(reg1));                 } else {                     cell.settile(new statictiledmaptile(reg2));                 }             } else {                 if (x % 2 != 0) {                     cell.settile(new statictiledmaptile(reg2));                 } else {                     cell.settile(new statictiledmaptile(reg1));                 }             }             layer.setcell(x, y, cell);         }     }     layers.add(layer); } 

to render create orthogonaltiledmaprenderer , call render() method.

in minimum example output:

cheststyle

already has parameters width , height , layercount. think wont need more 1 layer code:

public class mainclass implements applicationlistener {     private orthographiccamera camera;     private orthogonaltiledmaprenderer render;     private final static int width = 150, height = 100, layercount = 1;     private tiledmap map;      @override     public void create() {         float w = gdx.graphics.getwidth();         float h = gdx.graphics.getheight();          camera = new orthographiccamera(w, h);          pixmap pixmap = new pixmap(64, 32, format.rgba8888);         pixmap.setcolor(color.blue); // add 1 color here         pixmap.fillrectangle(0, 0, 32, 32);          pixmap.setcolor(color.red); // add 2 color here         pixmap.fillrectangle(32, 0, 32, 32);         // outcome texture blue left square , red right         // square         texture t = new texture(pixmap);         textureregion reg1 = new textureregion(t, 0, 0, 32, 32);         textureregion reg2 = new textureregion(t, 32, 0, 32, 32);          map = new tiledmap();         maplayers layers = map.getlayers();         (int l = 0; l < layercount; l++) {             tiledmaptilelayer layer = new tiledmaptilelayer(width, height, 32,                     32);             (int x = 0; x < width; x++) {                 (int y = 0; y < height; y++) {                     cell cell = new cell();                     if (y % 2 != 0) {                         if (x % 2 != 0) {                             cell.settile(new statictiledmaptile(reg1));                         } else {                             cell.settile(new statictiledmaptile(reg2));                         }                     } else {                         if (x % 2 != 0) {                             cell.settile(new statictiledmaptile(reg2));                         } else {                             cell.settile(new statictiledmaptile(reg1));                         }                     }                     layer.setcell(x, y, cell);                 }             }             layers.add(layer);         }         render = new orthogonaltiledmaprenderer(map);         render.setview(camera);         camera.translate(gdx.graphics.getwidth() / 2,                 gdx.graphics.getheight() / 2);     }      @override     public void dispose() {         render.dispose();         map.dispose();     }      private static final float movmentspeed = 5f;      @override     public void render() {          gdx.gl.glclearcolor(1, 1, 1, 1);         gdx.gl.glclear(gl10.gl_color_buffer_bit);         camera.update();         render.setview(camera);         render.render();          if (gdx.input.iskeypressed(keys.left)) {             camera.translate(-movmentspeed, 0);         } else if (gdx.input.iskeypressed(keys.right)) {             camera.translate(movmentspeed, 0);         } else if (gdx.input.iskeypressed(keys.up)) {             camera.translate(0, movmentspeed);         } else if (gdx.input.iskeypressed(keys.down)) {             camera.translate(0, -movmentspeed);         }     }      @override     public void resize(int width, int height) {     }      @override     public void pause() {     }      @override     public void resume() {     } } 

in end. can't tell if efficient! maybe there more efficient ways create thing on runtime , if dont creation on , on again shouldn't problem since once load game example. think efficient because render efficient , draw tiles visible on screen. use 1 texture background 1 opengl bind whole background.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -