Resizing an SDL window and drawing in GL, view position moves

I am a little unsure as to what exactly is going on with this, but basically I have the following code:

#include "SDL/SDL.h"
#include "Button.h"
#include "SDL/SDL_opengl.h"
#include <iostream>

using namespace std;

const int SCREENWIDTH  = 800;
const int SCREENHEIGHT = 600;

void buttonAction()
{
    cout<<"Button Pressed"<<endl;
}

void init_GL(int width, int height)
{
    glClearColor( 0, 0, 0, 0 );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, width, height, 0, -1, 1 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

int main( int argc, char* args[] )
{
    //The images
    SDL_Surface* hello  = NULL;
    SDL_Surface* screen = NULL;
    bool         quit   = false;
    SDL_Event    event;

    int screenWidth  = SCREENWIDTH;
    int screenHeight = SCREENHEIGHT;

    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    //Set up screen
    screen = SDL_SetVideoMode( screenWidth, screenHeight, 32, SDL_OPENGL | SDL_RESIZABLE );

    //Start OpenGL
    init_GL(screenWidth, screenHeight);


    SDL_WM_SetCaption( "Button Test", NULL );
    Button button(30, 30, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", *&buttonAction);

    //While the user hasn't quit
    while( quit == false )
    {


            //If there's events to handle
            if( SDL_PollEvent( &event ) )
            {
                if(event.type == SDL_VIDEORESIZE)
                {
                    screenWidth  = event.resize.w;
                    screenHeight = event.resize.h;

                    glViewport(0, 0, screenWidth, screenHeight);
                    glMatrixMode( GL_PROJECTION );
                    glLoadIdentity();
                    glOrtho( 0, screenWidth, screenHeight, 0, -1, 1 );
                    glMatrixMode( GL_MODELVIEW );
                    glLoadIdentity();
                }

                //Handle button events
                button.handleEvents(event);

                //If the user has Xed out the window
                if( event.type == SDL_QUIT )
                {
                    //Quit the program
                    quit = true;
                }
            }

        glPushMatrix();

            //Fill the screen black
            glBegin( GL_QUADS );
                glColor4f( 0.0, 0.0, 0.0, 1.0 );
                glVertex3f( 0, 0, 0 );
                glVertex3f( screenWidth, 0, 0 );
                glVertex3f( screenWidth, screenHeight, 0 );
                glVertex3f( 0, screenHeight, 0 );
            glEnd();

            //Show the button
            button.draw();

        glPopMatrix();

        //Update the screen
        SDL_GL_SwapBuffers();
    }

    //Quit SDL
    SDL_Quit();

    return 0;
}

and when i first draw everything seems to be working fine, and looks like this:
[ATTACH=CONFIG]219[/ATTACH]

but when i resize i see the following:
[ATTACH=CONFIG]220[/ATTACH]

any idea as to what is causing my problem?

Thank you for your time!
Brandon Murphy

You define a clear color but never actually clear the screen. Add a “glClear(GL_COLOR_BUFFER_BIT)” somewhere.

edit:


//Fill the screen black
            glBegin( GL_QUADS );
                glColor4f( 0.0, 0.0, 0.0, 1.0 );
                glVertex3f( 0, 0, 0 );
                glVertex3f( screenWidth, 0, 0 );
                glVertex3f( screenWidth, screenHeight, 0 );
                glVertex3f( 0, screenHeight, 0 );
            glEnd();

Use the mentioned glClear(GL_COLOR_BUFFER_BIT) here instead.

That fixed the issue of it drawing over itself, but the image still moves around the screen as I resize the window. I have the origin set to be the upper left corner (or so I believe, if i did it correctly).

Thanks!
Brandon

You need to call glViewport when the window is resized… Also, you really should call glClear() at the start of every frame.

I do call glViewPort when the screen is resized. I have boiled my problem down a little more and have the following code now:


#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#include <iostream>

using namespace std;

const int SCREENWIDTH  = 800;
const int SCREENHEIGHT = 600;

void init_GL(int width, int height)
{
    glClearColor( 0, 0, 0, 0 );
    glViewport(0, 0, width, height);
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, width, height, 0, -1, 1 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

int main( int argc, char* args[] )
{
    SDL_Surface* screen = NULL;
    bool         quit   = false;
    SDL_Event    event;

    int screenWidth  = SCREENWIDTH;
    int screenHeight = SCREENHEIGHT;

    SDL_Init( SDL_INIT_EVERYTHING );

    screen = SDL_SetVideoMode( screenWidth, screenHeight, 32, SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF);

    init_GL(screenWidth, screenHeight);

    SDL_WM_SetCaption( "Button Test", NULL );

    while( quit == false )
    {
            if( SDL_PollEvent( &event ) )
            {
                if(event.type == SDL_VIDEORESIZE) // The window is resized
                {
                    screenWidth  = event.resize.w;
                    screenHeight = event.resize.h;

                    glViewport(0, 0, screenWidth, screenHeight);
                    glMatrixMode( GL_PROJECTION );
                    glLoadIdentity();
                    glOrtho( 0, screenWidth, screenHeight, 0, -1, 1 );
                    glMatrixMode( GL_MODELVIEW );
                    glLoadIdentity();
                }

                if( event.type == SDL_QUIT )
                {
                    quit = true;
                }
            }

        glPushMatrix();

            glClear(GL_COLOR_BUFFER_BIT);

            glTranslatef( 50, 50, 0 );

            glBegin(GL_LINE_STRIP);
                glColor4f( 1.0, 1.0, 1.0, 1.0 );

                glVertex3f( 0,  0,  0 );
                glVertex3f( 50, 0,  0 );
                glVertex3f( 50, 50, 0 );
                glVertex3f( 0,  50, 0 );
                glVertex3f( 0,  0,  0 );

            glEnd();

        glPopMatrix();

        SDL_GL_SwapBuffers();
    }

    SDL_Quit();

    return 0;
}

if you run that you will notice that as i make the window shorter the square that I drew moves downward, yet I am not calling a translate method on it. when i make the window taller the square moves upward. Resizing the width of the window has no effect on the position of the square.

Thank you for your time,
Brandon Murphy