Problem resizing window (SDL2 and OpenGL)

I’m writing a “template” for SDL OpenGL programs.
This is so I can have a basic piece of code ready (handle keys, resizing, main) and bother only with rendering and updating.

As I’m “moving” to SDL2 and update my code, I want to be compatible with older versions. So, some pieces of code have to be written two times, one for SDL 1.2 and one for SDL2. One of them is my resize function, which handles window resizing. Currently, it looks like this:

?? void resize(GLsizei w, GLsizei h) {
?? ??? if (h == 0) {
?? ??? ??? h = 1;
?? ??? }
?? #if SDL_MAJOR_VERSION == 2
?? ??? SDL_GL_DeleteContext(opengl);
?? ??? SDL_DestroyWindow(window);
??
?? ??? window = SDL_CreateWindow(
?? ??? ??? ??? title,
?? ??? ??? ??? SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
?? ??? ??? ??? w, h,
?? ??? ??? ??? SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
??
?? ??? opengl = SDL_GL_CreateContext(window);
??
?? ??? initGL();
?? #else
??
?? ??? SDL_SetVideoMode(w,h,0,SDL_OPENGL | SDL_RESIZABLE);
??
?? ??? glMatrixMode(GL_PROJECTION);
?? ??? glLoadIdentity();
?? #endif
??
?? ??? glViewport(0,0,w,h);
?? ??? gluPerspective(45.0f, (GLfloat) w / (GLfloat) h, 0.1f, 100.0f);
?? }

Where ‘opengl’ is my SDL_GLContext and ‘window’ is my window.
‘w’ and ‘h’ represent the new width and height for my program.

The code for SDL1.2 works great. However, SDL2 although resizes my window, it doesn’t update the viewport ( The scene size doesn’t change).

I guess the idea ( which I guessed by myself, didn’t see anywhere) of deleting and rebuilding the window and context with the new size is wrong.?

What should I do ?

What does initGL() do, why is it only in the SDL2 code, and why do you only
switch to the projection matrix mode in the SDL 1.2 code?

Jonny DOn Wed, Feb 20, 2013 at 10:06 AM, Aggelos Kolaitis wrote:

I’m writing a “template” for SDL OpenGL programs.
This is so I can have a basic piece of code ready (handle keys, resizing,
main) and bother only with rendering and updating.

As I’m “moving” to SDL2 and update my code, I want to be compatible with
older versions. So, some pieces of code have to be written two times, one
for SDL 1.2 and one for SDL2. One of them is my resize function, which
handles window resizing. Currently, it looks like this:

void resize(GLsizei w, GLsizei h) {
if (h == 0) {
h = 1;
}
#if SDL_MAJOR_VERSION == 2
SDL_GL_DeleteContext(opengl);
SDL_DestroyWindow(window);

  window = SDL_CreateWindow(
          title,
          SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
          w, h,
          SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE |

SDL_WINDOW_SHOWN);

  opengl = SDL_GL_CreateContext(window);

  initGL();

#else

  SDL_SetVideoMode(w,h,0,SDL_OPENGL | SDL_RESIZABLE);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

#endif

  glViewport(0,0,w,h);
  gluPerspective(45.0f, (GLfloat) w / (GLfloat) h, 0.1f, 100.0f);

}

Where ‘opengl’ is my SDL_GLContext and ‘window’ is my window.
‘w’ and ‘h’ represent the new width and height for my program.

The code for SDL1.2 works great. However, SDL2 although resizes my window,
it doesn’t update the viewport ( The scene size doesn’t change).

I guess the idea ( which I guessed by myself, didn’t see anywhere) of
deleting and rebuilding the window and context with the new size is wrong.

What should I do ?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well:
initGL initializes my OpenGL stuff ( lighting, blending, material properties etc). It’s left there from a previous try to fix the problem.

For the others, I’m relatively new to OpenGL and still learning, so most of the time I let stuff untouched when they are working.
So, could you suggest any solution?===============================================
Dancing Gangnam Style burns 500 calories, as well as 3000 years of human progress in music

20 ??? 2013, 15:10, ?/? Jonathan Dearborn ???:

What does initGL() do, why is it only in the SDL2 code, and why do you only switch to the projection matrix mode in the SDL 1.2 code?

Jonny D

On Wed, Feb 20, 2013 at 10:06 AM, Aggelos Kolaitis wrote:

I’m writing a “template” for SDL OpenGL programs.
This is so I can have a basic piece of code ready (handle keys, resizing, main) and bother only with rendering and updating.

As I’m “moving” to SDL2 and update my code, I want to be compatible with older versions. So, some pieces of code have to be written two times, one for SDL 1.2 and one for SDL2. One of them is my resize function, which handles window resizing. Currently, it looks like this:

void resize(GLsizei w, GLsizei h) {
if (h == 0) {
h = 1;
}
#if SDL_MAJOR_VERSION == 2
SDL_GL_DeleteContext(opengl);
SDL_DestroyWindow(window);

  window = SDL_CreateWindow(
          title,
          SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
          w, h,
          SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);

  opengl = SDL_GL_CreateContext(window);

  initGL();

#else

  SDL_SetVideoMode(w,h,0,SDL_OPENGL | SDL_RESIZABLE);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

#endif

  glViewport(0,0,w,h);
  gluPerspective(45.0f, (GLfloat) w / (GLfloat) h, 0.1f, 100.0f);

}

Where ‘opengl’ is my SDL_GLContext and ‘window’ is my window.
‘w’ and ‘h’ represent the new width and height for my program.

The code for SDL1.2 works great. However, SDL2 although resizes my window, it doesn’t update the viewport ( The scene size doesn’t change).

I guess the idea ( which I guessed by myself, didn’t see anywhere) of deleting and rebuilding the window and context with the new size is wrong.

What should I do ?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Try moving the glMatrixMode and glLoadIdentity calls out of the conditional
code. Your viewport is getting set (glViewport tells GL how big your
viewport/window is), but you need to set up the projection matrix so OpenGL
knows how to transform your geometry to screen coordinates (this is what
gluPerspective or glOrtho do). You should probably switch back to
GL_MODELVIEW before you leave the function so that any matrix calls
afterward do what you would expect to drawn objects.

Also, you might want to use SDL_SetWindowSize instead of destroying and
recreating the SDL2 window and GL context.

Jonny DOn Wed, Feb 20, 2013 at 12:18 PM, wrote:

Well:
initGL initializes my OpenGL stuff ( lighting, blending, material
properties etc). It’s left there from a previous try to fix the problem.

For the others, I’m relatively new to OpenGL and still learning, so most
of the time I let stuff untouched when they are working.
So, could you suggest any solution?

Dancing Gangnam Style burns 500 calories, as well as 3000 years of human
progress in music

20 ??? 2013, 15:10, ?/? Jonathan Dearborn <@Jonathan_Dearborn> ???:

What does initGL() do, why is it only in the SDL2 code, and why do you
only switch to the projection matrix mode in the SDL 1.2 code?

Jonny D

On Wed, Feb 20, 2013 at 10:06 AM, Aggelos Kolaitis wrote:

I’m writing a “template” for SDL OpenGL programs.
This is so I can have a basic piece of code ready (handle keys, resizing,
main) and bother only with rendering and updating.

As I’m “moving” to SDL2 and update my code, I want to be compatible with
older versions. So, some pieces of code have to be written two times, one
for SDL 1.2 and one for SDL2. One of them is my resize function, which
handles window resizing. Currently, it looks like this:

void resize(GLsizei w, GLsizei h) {
if (h == 0) {
h = 1;
}
#if SDL_MAJOR_VERSION == 2
SDL_GL_DeleteContext(opengl);
SDL_DestroyWindow(window);

  window = SDL_CreateWindow(
          title,
          SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
          w, h,
          SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE |

SDL_WINDOW_SHOWN);

  opengl = SDL_GL_CreateContext(window);

  initGL();

#else

  SDL_SetVideoMode(w,h,0,SDL_OPENGL | SDL_RESIZABLE);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

#endif

  glViewport(0,0,w,h);
  gluPerspective(45.0f, (GLfloat) w / (GLfloat) h, 0.1f, 100.0f);

}

Where ‘opengl’ is my SDL_GLContext and ‘window’ is my window.
‘w’ and ‘h’ represent the new width and height for my program.

The code for SDL1.2 works great. However, SDL2 although resizes my
window, it doesn’t update the viewport ( The scene size doesn’t change).

I guess the idea ( which I guessed by myself, didn’t see anywhere) of
deleting and rebuilding the window and context with the new size is wrong.

What should I do ?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Ok, I will try it out and back with the results. Thank very much!!!===============================================
Dancing Gangnam Style burns 500 calories, as well as 3000 years of human progress in music

20 ??? 2013, 19:34, ?/? Jonathan Dearborn ???:

Try moving the glMatrixMode and glLoadIdentity calls out of the conditional code. Your viewport is getting set (glViewport tells GL how big your viewport/window is), but you need to set up the projection matrix so OpenGL knows how to transform your geometry to screen coordinates (this is what gluPerspective or glOrtho do). You should probably switch back to GL_MODELVIEW before you leave the function so that any matrix calls afterward do what you would expect to drawn objects.

Also, you might want to use SDL_SetWindowSize instead of destroying and recreating the SDL2 window and GL context.

Jonny D

On Wed, Feb 20, 2013 at 12:18 PM, wrote:

Well:
initGL initializes my OpenGL stuff ( lighting, blending, material properties etc). It’s left there from a previous try to fix the problem.

For the others, I’m relatively new to OpenGL and still learning, so most of the time I let stuff untouched when they are working.
So, could you suggest any solution?

Dancing Gangnam Style burns 500 calories, as well as 3000 years of human progress in music

20 ??? 2013, 15:10, ?/? Jonathan Dearborn ???:

What does initGL() do, why is it only in the SDL2 code, and why do you only switch to the projection matrix mode in the SDL 1.2 code?

Jonny D

On Wed, Feb 20, 2013 at 10:06 AM, Aggelos Kolaitis wrote:

I’m writing a “template” for SDL OpenGL programs.
This is so I can have a basic piece of code ready (handle keys, resizing, main) and bother only with rendering and updating.

As I’m “moving” to SDL2 and update my code, I want to be compatible with older versions. So, some pieces of code have to be written two times, one for SDL 1.2 and one for SDL2. One of them is my resize function, which handles window resizing. Currently, it looks like this:

void resize(GLsizei w, GLsizei h) {
if (h == 0) {
h = 1;
}
#if SDL_MAJOR_VERSION == 2
SDL_GL_DeleteContext(opengl);
SDL_DestroyWindow(window);

  window = SDL_CreateWindow(
          title,
          SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
          w, h,
          SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);

  opengl = SDL_GL_CreateContext(window);

  initGL();

#else

  SDL_SetVideoMode(w,h,0,SDL_OPENGL | SDL_RESIZABLE);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

#endif

  glViewport(0,0,w,h);
  gluPerspective(45.0f, (GLfloat) w / (GLfloat) h, 0.1f, 100.0f);

}

Where ‘opengl’ is my SDL_GLContext and ‘window’ is my window.
‘w’ and ‘h’ represent the new width and height for my program.

The code for SDL1.2 works great. However, SDL2 although resizes my window, it doesn’t update the viewport ( The scene size doesn’t change).

I guess the idea ( which I guessed by myself, didn’t see anywhere) of deleting and rebuilding the window and context with the new size is wrong.

What should I do ?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org