LibGDX: How to draw in the area of the black bars of a FitViewport?

烈酒焚心 提交于 2020-01-04 05:35:08

问题


I got a FitViewport with a virtual width and virtual height. When the screen got another aspect ratio than my virtual resolution black bars are added. I want to draw something inside these letterboxes.

I tried to do it that way, but only objects "inside" my virtual resolution are drawn, objects "outside" are just not visible:

viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, cam);
viewport.apply();

batch.setProjectionMatrix(cam.combined);

batch.begin();
batch.draw(texture, viewport.getRightGutterX(), 0);
batch.end();

How to draw inside the letterboxes?


回答1:


You would need a second viewport, probably ExtendViewport with the same virtual dimensions as your FitViewport.

//create:
fitViewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
extendViewport = new ExtendViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

//resize:
fitViewport.update(width, height);
extendViewport.update(width, height);

//render:

fitViewport.apply();
batch.setProjectionMatrix(fitViewport.getCamera().combined);
batch.begin();
//draw game
batch.end();

extendViewport.apply();
batch.setProjectionMatrix(extendViewport.getCamera().combined);
batch.begin();
//draw stuff in border
batch.end();

If you want to be sure the border stuff doesn't overlap your game, you could swap the draw order above.

Or you could just use ExtendViewport to begin with for everything.



来源:https://stackoverflow.com/questions/34120303/libgdx-how-to-draw-in-the-area-of-the-black-bars-of-a-fitviewport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!