I'm trying to make a 2D Tile RPG game with LWJGL but I'm having a problem with the display resizing. I want the user to be able to re-size the window to whatever size they want just by expanding it (as opposed to letting them pick a resolution option from a list).

See the images below for the problem.

At default resolution: The tile is drawn perfectly in the screen at the right size

At a re-sized resolution: The tile gets cut off at a certain position if it moves to far up or right

As you can see the tile is drawn correctly in the first image. However, after moving the camera and resizing the display the tile gets cut off as it moves away from the bottom-left corner of the screen.

The default screen-resolution is 800x600. I have a camera object that moves based on the player using the WASD keys. I decide what's to be drawn by using glTransaltef(camera.x,camera.y,0). Should I use if(Display.wasResized()) in the game loop then get what the new resolution is and use it in the glTranslatef function somehow? Any help or suggestions would be great!

EDIT: I added the code in Fletcher D's answer however it produced this: Fletcher D's suggestion result

The tile doesn't get cut off anymore but in the image above the tile is being drawn at a size of 1x1 pixels. Also when the screen resolution changes the resolution of everything on screen changes, this is not what I want. I want the screen to simply display more of the world. As I said I'm using a panning camera that moves with the player's WASD keys. So if the world is currently being displayed at 0,0 to 800,600 and the player re-sizes the window to 1080x720 I want the display to show the world from -140,-60 to 940,660. (Maintaining the center of the screen at the moment of being re-sized.


1 Answer 1


You need to change OpenGL's projection matrix so that it will change the area of the world that is drawn within your window. The projection matrix is what OpenGL uses to determine how the world will be projected onto the screen. Most likely, you're setting this in the beginning of your program to say "draw the area of the world from (0,0) to (800,600)." Then, when the window is resized, it just keeps doing that, stretching as necessary to fit in the window.

Since your game is 2D, I'm guessing you set up your projection matrix with glOrtho() in the beginning of your program. Sound familiar? Try this: When you notice that the window has been resized, call something like glOrtho(0, WIDTH, 0, HEIGHT, -1, 1). You may need to tweak the actual values, but that should get you rendering a different area.

Notice how this means you can do glOrtho(-WIDTH/2, WIDTH/2, -HEIGHT/2, HEIGHT/2, -1, 1) and your camera will center to the point you want, instead of treating it as the lower left corner.

Edit: I just remembered you probably also need to change the viewport, to set the area of the screen which is being rendered to. Try this code whenever the window is resized:

glViewport(0, 0, width, height); //NEW
glOrtho(0, width, 0, height, -1, 1);
  • \$\begingroup\$ I added the glOrtho(0, WIDTH, HEIGHT, 0, -1, 1) like you said (I like the origin in the top-left) inside an if(Display.wasResized()) block that's checked in the game-loop, but the tile still get's clipped as it moves away from the bottom-left corner. Do you know why else this might be happening? \$\endgroup\$ Apr 17, 2014 at 21:19
  • \$\begingroup\$ Stop using deprecated fixed function pipeline. \$\endgroup\$ Apr 18, 2014 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .