Changing Resolution while game runs leads to strange output

Started by sirK, January 19, 2012, 11:59:41

Previous topic - Next topic

sirK

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
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:
//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?

Fool Running

Do you call glOrtho anywhere else (like in the rendering code)?
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

sirK

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

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
  }
}

Fool Running

Sorry, the glOrtho question really wasn't relevant... what you need to do is call:
        glScissor(0, 0, width, height);
        glViewport(0, 0, width, height);

when the size changes. This will change the area that OpenGL uses for rendering.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D


sirK

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. :D