Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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 387 times)
0 Members and 1 Guest are viewing this topic.
Offline Spacebeans

JGO Knight


Medals: 18
Projects: 2
Exp: 4 years


Bye everyone!


« Posted 2014-03-28 18: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());
   }

This is a dead account. Goodbye everyone!
Offline Drenius
« Reply #1 - Posted 2014-03-28 18: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

JGO Knight


Medals: 18
Projects: 2
Exp: 4 years


Bye everyone!


« Reply #2 - Posted 2014-03-28 19: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.

This is a dead account. Goodbye everyone!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Drenius
« Reply #3 - Posted 2014-03-28 19: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

JGO Knight


Medals: 18
Projects: 2
Exp: 4 years


Bye everyone!


« Reply #4 - Posted 2014-03-28 19: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*
***********
***********
***********
***********

This is a dead account. Goodbye everyone!
Offline Drenius
« Reply #5 - Posted 2014-03-28 19: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

JGO Knight


Medals: 18
Projects: 2
Exp: 4 years


Bye everyone!


« Reply #6 - Posted 2014-03-28 20: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:

This is a dead account. Goodbye everyone!
Offline Drenius
« Reply #7 - Posted 2014-03-28 20: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

JGO Knight


Medals: 18
Projects: 2
Exp: 4 years


Bye everyone!


« Reply #8 - Posted 2014-03-28 20: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.

This is a dead account. Goodbye everyone!
Offline Drenius
« Reply #9 - Posted 2014-03-28 20: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

JGO Knight


Medals: 18
Projects: 2
Exp: 4 years


Bye everyone!


« Reply #10 - Posted 2014-03-28 20: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.

This is a dead account. Goodbye everyone!
Offline Drenius
« Reply #11 - Posted 2014-03-28 20: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 23: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
Offline Drenius
« Reply #13 - Posted 2014-03-28 23:17:29 »

... which is still the same but for libGDX...
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

pw (17 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!