0
\$\begingroup\$

I am currently working on a small game using LibGDX and Java.

However, I have found one oddly specific problem. I want the game to resize so that it never has to be cropped and so it never has to cut anything off - basically just setting the viewport so everything still fits onto the screen on resize. Sounds simple.

Now, I have the code for that in place. All of my screens extend one BaseScreen in which I defined the following code:

@Override
public void resize (int width, int height) {
    Vector2 size = Scaling.fit.apply(Constants.GAME_WIDTH, Constants.GAME_HEIGHT, width, height);
    int viewportX = (int)(width - size.x) / 2;
    int viewportY = (int)(height - size.y) / 2;
    int viewportWidth = (int)size.x;
    int viewportHeight = (int)size.y;
    Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
}

(Note, I have not written this code by myself, it is one of the few pieces of code I searched online for as I had no previous experience with LibGDX)

Now, this code works absolutely fine in almost every screen - just not in the RoomScreen, which is the screen where the main gameplay is happening. Which is quite a bummer, as it basically makes the game look really shitty except for multiples of my game size (the constants are set as 480 and 272).

Now, I have absolutely no idea why this is the case. I read that this might be because transforms are pushed, but not pulled afterwards, but I did not find any instance of this in my code.

I also tested wether the code is executed, and it indeed is. All variables are correctly set, it simply doesn't work.

If any of you is able to help me, I would be unbelievably thankful!

The full code is currently only available in my Dropbox, as my own Git server recently crashed. Here is the link: https://www.dropbox.com/sh/evvw5kiqcfos7fu/AADwogpkwl1RWj2QcI6eePyda?dl=0 (I am terribly sorry that I am only able to share it this way, but I am afraid that sharing every relevant class in this post itself would in the end result in pasting all my code here, making it almost unreadable. If any of you has a better idea than this, please tell me, I'll gladly share it otherwise!)

If you are wondering, the screen classes are in the folder "screen".

\$\endgroup\$
0
\$\begingroup\$

Okay, after doing a metric butt-ton of debugging I seem to finally have figured out how to solve this - quite peculiar - problem, even though I don't really like the solution.

I am not sure why, but the LibGDX people chose to make a FrameBuffer set the viewport on every begin() call. This is problematic in so far that on an end() call, it seems like the original viewport is not properly restored, even though it should be as far as I can gather from the source code.

This means that the fix is actually rather simple, just reset the viewport after the faulty FrameBuffer end(). Of course this is by far not the ideal solution, as it does make the game rather stuttery, but I'll have to resort to this if I cannot figure out shaders properly.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.