LWJGL
May 21, 2013, 18:10:20 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL is now using GitHub
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: Changing Resolution while game runs leads to strange output  (Read 1247 times)
sirK
Newbie
*
Posts: 6


« on: January 19, 2012, 03:59:41 »

Hi all,
I hope this is not covered somewhere else. I couldn't find anything by searching google, so I hope you guys can help me.
I am currently trying to get into the LWJGL-librarys. Therefore (not surprising) I am developing a little game.
Now I am working at changing the games resoution while the game already runs. Setting up the display + setting it to fullscreen is no problem at the beginning. I used code provided by NinjaCave for that. So I first look for a supported display mode and then setup the display and OpenGL
Code:
int scrWidth = 1024;
int scrHeight = 768;
DisplayMode targetMode = null;
DisplayMode[] modes = null;
modes = Display.getAvailableDisplayModes();

//Search for display mode
for (int i=0;i<modes.length;i++) {
  if((modes[i].getWidth() == scrWidth) && (modes[i].getHeight() == scrHeight) && (modes[i].getFrequency()==60) && (modes[i].getBitsPerPixel()==32)){
    targetMode = modes[i];
    break;
  }
}

//Setting up display
Display.setDisplayMode(targetMode);
Display.setFullscreen(true);
Display.create();

//Setting up OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glOrtho(0, scrWidth, scrHeight, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

Now I do the exact same thing when changing the resolution while the game is running. 'game' is a class where I keep the stuff that makes up the actual game. game.getSetNewDisplay() returns whether the display is supposed to be set to a new resolution, game.getNewScrWidth() and .getNewScrHeight() return the new resolution and game.fullscreen() returns true if fullscreen is wanted. So I check the following within the game-loop:
Code:
//Check if we are supposed to change Display Resolution;
if(game.getSetNewDisplay()){
  targetMode = null;
  for (int i=0;i<modes.length;i++) {
    if((modes[i].getWidth() == game.getNewScrWidth()) && (modes[i].getHeight() == game.getNewScrHeight()) && (modes[i].getFrequency()==60) && (modes[i].getBitsPerPixel()==32)){
      targetMode = modes[i];
      break;
    }
  }
//targetMode equals null if mode is not supported
if(targetMode == null){
  System.out.print("ERROR: New Display Mode not supported! \n");
}
else{
//If mode issupported, setup display and OpenGLto new Mode
  game.deactivateSetNewDisplay();
  game.setScrParameters(game.getNewScrWidth(), game.getNewScrHeight());
  Display.setDisplayMode(targetMode);
  Display.setFullscreen(game.getFullscreen());

  GL11.glMatrixMode(GL11.GL_PROJECTION);
  GL11.glOrtho(0, game.getNewScrWidth(), game.getNewScrHeight(), 0, 1, -1);
  GL11.glMatrixMode(GL11.GL_MODELVIEW);
}

However, doing it this way leads to awkward outputs. Changing to a higher resolution than the one I am starting with seems to adjust the actual screen size properly but the area to draw on still seems to be at the old resolution and is display at the bottom left of the screen. I therefore suspect it has something to do with the glOrtho routine.
Has anyone a tip on how to do this properly?
Logged
Fool Running
Nerdus Imperius
*****
Posts: 742


« Reply #1 on: January 19, 2012, 05:51:29 »

Do you call glOrtho anywhere else (like in the rendering code)?
Logged

Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option Grin
sirK
Newbie
*
Posts: 6


« Reply #2 on: January 19, 2012, 07:52:55 »

No, I just have this OpenGL-Init at the beginning and then the display-change phase. It's like this:

Code:
GameInitPhase //These two lines represent the first code-quotation of my first entry
OpenGLInit

//After initialization start GameLoop:
while(!game.GetQuit()){
  game.update()
  Display.update();
  Display.sync(50);
  //here starts the second code-quotation
  if(game.getSetNewDisplay(){
    //as mentioned before
  }
}
Logged
Fool Running
Nerdus Imperius
*****
Posts: 742


« Reply #3 on: January 19, 2012, 10:14:48 »

Sorry, the glOrtho question really wasn't relevant... what you need to do is call:
Code:
        glScissor(0, 0, width, height);
        glViewport(0, 0, width, height);
when the size changes. This will change the area that OpenGL uses for rendering.
Logged

Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option Grin
sirK
Newbie
*
Posts: 6


« Reply #4 on: January 19, 2012, 21:58:40 »

Oh, ok I'll try that out Cheesy.
Logged
sirK
Newbie
*
Posts: 6


« Reply #5 on: January 22, 2012, 03:24:36 »

Sry, forgot to answer if it worked. ^^ Well, it works like a charm. Strange I couldn't find a tutorial that handeled that specific problem.
Anyway thank you very much.
Thread can be closed now. Cheesy
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!