Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [unsolved] Re-sizing and scaling  (Read 558 times)
0 Members and 1 Guest are viewing this topic.
Offline Spacebeans
« Posted 2014-03-28 17:51:22 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   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());
   }
Online Drenius
« Reply #1 - Posted 2014-03-28 17:58:45 »

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?
Offline Spacebeans
« Reply #2 - Posted 2014-03-28 18:05:32 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Drenius
« Reply #3 - Posted 2014-03-28 18:30:22 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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.
Offline Spacebeans
« Reply #4 - Posted 2014-03-28 18:47:33 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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*
***********
***********
***********
***********
Online Drenius
« Reply #5 - Posted 2014-03-28 18:53:37 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
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);
Offline Spacebeans
« Reply #6 - Posted 2014-03-28 19:00:41 »

Alright, this is the re-size method now:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
   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:
Online Drenius
« Reply #7 - Posted 2014-03-28 19:02:45 »

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:
Offline Spacebeans
« Reply #8 - Posted 2014-03-28 19:04:23 »

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.
Online Drenius
« Reply #9 - Posted 2014-03-28 19:06:12 »

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...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Spacebeans
« Reply #10 - Posted 2014-03-28 19:09:08 »

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
1  
glScalef(howMuchX, howMuchY, 0);
to resize the objects with the display.
Online Drenius
« Reply #11 - Posted 2014-03-28 19:13:58 »

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.
Offline LiquidNitrogen
« Reply #12 - Posted 2014-03-28 22:12:32 »

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.

http://stackoverflow.com/questions/18495975/libgdx-window-resizing-keeping-aspect-ratio

Online Drenius
« Reply #13 - Posted 2014-03-28 22:17:29 »

... which is still the same but for libGDX...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!