[unsolved] Re-sizing and scaling

I’ve been wondering how to make a games window resizable without just stretching the pixels over a larger screen, or using glOrtho and putting in TOO much content and everything is small.

This is the code I am using, it is called whenever Display.wasResized = true.


	public void resize(){
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
		glMatrixMode(GL_MODELVIEW);

		glEnable(GL_STENCIL_TEST);
		glClearColor(0, 0, 0, 0);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	
		glViewport(0, 0, Display.getWidth(), Display.getHeight());
		glLoadIdentity();
		
		System.out.println("Resized to " + Display.getWidth() + " x " + Display.getHeight());
	}

Depends on what you want.
You can stretch the scenery, cut off the borders, expand the scenery…
What you are doing is the first mentioned way.
Whats your question?

Sorry I wasn’t clear. I meant I want the screen to always be 16:9, and when its resized, scale everything instead of expanding.


float x, y, width, height;
if(Display.getWidth()/resolution.width < Display.getHeight()/resolution.height)
{
	x = 		0;
	y = 	((Display.getHeight()-((Display.getWidth()/resolution.width)*resolution.height))/2f);
	width = 	Display.getWidth();
	height = 	(Display.getWidth()/resolution.width*resolution.height);
			
}
else
{
	x = 		(int)((Display.getWidth()-((Display.getHeight()/resolution.height)*resolution.width))/2f);
	y = 		0;
	width = 	(int)(Display.getHeight()/resolution.height*resolution.width);
	height = 	Display.getHeight();
}

glViewport((int) x, (int)y, (int)width, (int)height);

Not the best or cleanest way, but a vague idea and it works.

It works, but what I meant is that I wanted the content to always remain in a 16:9 ratio, and when re-sized, kept that way but with a higher resolution.

EDIT: Say I want the player to be here on a 10x5p screen:


*********O




But when I re size it to 11x6, hes here:


********O





This is exactly what this does.
You would have to use the way you were already using, but with these lines instead of the glViewport line.


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//the code here
glMatrixMode(GL_MODELVIEW);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0f, resolution.width, 0f, resolution.height, -1f, 1f);
glMatrixMode(GL_MODELVIEW);

Alright, this is the re-size method now:


	public void resize(){
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
		glMatrixMode(GL_MODELVIEW);

		glEnable(GL_STENCIL_TEST);
		glClearColor(0, 0, 0, 0);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);

		float x, y;
		if(Display.getWidth() / width < Display.getHeight() / height)
		{
		   x =       0;
		   y =    ((Display.getHeight()-((Display.getWidth() / width) * height))/2f);
		   width =    Display.getWidth();
		   height =    (Display.getWidth()/ width * height);
		         
		}
		else
		{
		   x =       (int)((Display.getWidth()-((Display.getHeight() / height) * width))/2f);
		   y =       0;
		   width =    (int)(Display.getHeight() / height * width);
		   height =    Display.getHeight();
		}

		glViewport((int) x, (int)y, (int)width, (int)height);
		
		System.out.println("Resized to " + Display.getWidth() + " x " + Display.getHeight());
	}

Before I resize it:

After I resize it:

This is what it is supposed to do, yea.
Resize the window and the scene will stay the same.
But you did not use it correctly:

Okay, but what I needed if for it do is to keep the aspect ratio (16:9), and just make the squares, lights and everything bigger and in a higher resolution.

You cannot use Display.getWidth()/Height() for lgOrtho anymore.
This will always resize the scene. Alway use the same values there.

EDIT: You will not necessarily need to use glOrtho more than once at all. Not sure…

I was expecting more along the lines of

glScalef(howMuchX, howMuchY, 0);

to resize the objects with the display.

As mentioned, my method is possibly not the best and for sure not the only one.
But if it is used correctly it does what you expect.

Take a look at the reply to this question. You will obviously have to alter it if you aren’t using LibGDX (and find the code for Scaling.fit.apply()), but I am using this in my game and it works very nicely.

… which is still the same but for libGDX…