views:

47

answers:

2

I'm having a slight problem with my SDL/Opengl code, specifically, when i try to do something on a mousebuttondown event, the program sends an sdl_quit event to the stack, closing my application.

I know this because I can make the program work (sans the ability to quit out of it :| ) by checking for SDL_QUIT during my event loop, and making it do nothing, rather than quitting the application.

If anyone could help make my program work, while retaining the ability to, well, close it, it'd be much appreciated. Code attached below:

#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

void draw_polygon();
void init();

int main(int argc, char *argv[])
{

    SDL_Event Event;
    int quit = 0;
    GLfloat color[] = { 0.0f, 0.0f, 0.0f };

    init();

    glColor3fv (color);
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    draw_polygon();

    while(!quit)
    {
        while(SDL_PollEvent( &Event )) 
        {
            switch(Event.type)
            {
                case SDL_MOUSEBUTTONDOWN:
                    for (int i = 0; i <= sizeof(color); i++)
                    {
                        color[i] += 0.1f;
                    }
                    glColor3fv ( color );
                    draw_polygon();
                                    break;

                case SDL_KEYDOWN:
                    switch(Event.key.keysym.sym)
                    {
                        case SDLK_ESCAPE:
                            quit = 1;
                                                    break;
                        default:
                            break;
                    }

                default:
                    break;
            }
        }
    }

    SDL_Quit();
    return 0;
}

void draw_polygon() 
{
    glBegin(GL_POLYGON);
        glVertex3f (0.25, 0.25, 0.0);
        glVertex3f (0.75, 0.25, 0.0);
        glVertex3f (0.75, 0.75, 0.0);
        glVertex3f (0.25, 0.75, 0.0);
    glEnd();
    SDL_GL_SwapBuffers();
}

void init()
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
    glClearColor (0.0, 0.0, 0.0, 0.0);  
    glMatrixMode( GL_PROJECTION | GL_MODELVIEW );
    glLoadIdentity(); 
    glClear (GL_COLOR_BUFFER_BIT);
    SDL_WM_SetCaption( "OpenGL Test", NULL );
}

If it matters in this case, I'm compiling via the included compiler with Visual C++ 2008 express.

+1  A: 

You forgot the breaks at the end of your cases in switches. The switch will continue after a mousedown case and execute the sdl_keydown code, which will behave unpredictable.

KillianDS
+3  A: 

You're missing a break statement in the end of your SDL_MOUSEBUTTONDOWN event handler, resulting in unintentional fall-through to the SDL_KEYDOWN handler. Just add a break after the call to draw_polygon() and you're good to go; you should also add a break to the end of your SDL_KEYDOWN handler to avoid falling through into the default case, though that's not a problem now since the default case doesn't do anything, but if it does in the future, you'll have another bug.

EDIT

You also have a buffer overflow. sizeof(color) is the total size in bytes of the array, which in this case is 12 (3 values times 4 bytes/value). So, you're looping 13 times (12, plus 1 for using <= instead of <) instead of 3 when changing the color. It just happens that the compiler has laid out the local variable quit immediately after color, so you're accidentally writing out over quit, plus other unknown data on the stack.

The fix for this is to divide by the size of the array member when calculating the number of members. This is often done using a macro:

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
...
for (int i = 0; i < ARRAY_SIZE(color); i++)

You could also just hardcode the number of color components (3), which isn't likely to change -- you're also hardcoding this implicitly in the call to glColor3fv().

Adam Rosenfield
Hm, I've added the breaks as you suggested, and I'm still having the same problem.
Anthony Clever
And that fixed it :DI was stuck in PHP mode apparently. Completely forgot that sizeof() returned the size in bytes in c++, rather than the array's length.
Anthony Clever