java - NinePatch rendering issue on TextButton background (libgdx) -


i'm testing out using stage main menu simple button using ninepatch button background.

the code set looks this:

ninepatch textbutton setup

ninepatch btnnormal9 = ninepatchhelper.processninepatchfile("data/button_normal.9.png");  ninepatchdrawable btnnormal9drawable = new ninepatchdrawable(btnnormal9);  textbuttonstyle style = new textbuttonstyle(btnnormal9drawable, btnnormal9drawable, btnnormal9drawable); style.font = new bitmapfont(); style.fontcolor = new color(1, 1, 1, 1);  button = new textbutton("start game", style); button.setsize(200, 100); button.setposition(0, 0);  stage.addactor(button); 


in resize function i'm doing now:

resize code

@override public void resize(int width, int height) {     camera.viewportwidth = width;     camera.viewportheight = height;     camera.update(); } 


regardless of size of window (which size of camera viewport), button should @ (0,0) on screen , 200 pixels 100 pixels.

the ninepatch image ("data/button_normal.9.png") 26x26 pixels. cropped 24x24 texture 4px divisions, using code (taken here: loading nine-patch image libgdx scene2d button background looks awful):

create ninepatch

public static ninepatch processninepatchfile(string filename) {     final texture t = new texture(gdx.files.internal(filename));     final int width = t.getwidth() - 2;     final int height = t.getheight() - 2;     return new ninepatch(new textureregion(t, 1, 1, width, height), 4, 4, 4, 4); } 


works fine long window dimensions number, e.g. 640x400 pixels, in screenshot below:

enter image description here

however, if window dimensions odd number, e.g. 639x401 pixels, ninepatch isn't scaled correctly:

enter image description here

if height odd number, rendering error occurs in vertical scaling of ninepatch, below:

enter image description here

i don't understand why happening, given button size , position hardcoded same in every case, ninepatch should drawn same.

if can provide i'd appreciate it.

thanks in advance.

okay think realise what's going on, i'm being stupid.

if dimensions of screen odd, centre of screen no longer integer pixel value , instead .5 value. mean drawn @ integer position blurry.

the simplest way can think fix altering resize method. if height or width odd, set x or y position of camera 0.5 instead of 0, shown below. fixes problem :)

if (width % 2 != 0) guicamera.position.x = 0.5f; else guicamera.position.x = 0f;  if (height % 2 != 0) guicamera.position.y = 0.5f; else guicamera.position.y = 0f; 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -