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.
You’re missing a
breakstatement in the end of yourSDL_MOUSEBUTTONDOWNevent handler, resulting in unintentional fall-through to theSDL_KEYDOWNhandler. Just add abreakafter the call todraw_polygon()and you’re good to go; you should also add abreakto the end of yourSDL_KEYDOWNhandler to avoid falling through into thedefaultcase, though that’s not a problem now since thedefaultcase 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 variablequitimmediately aftercolor, so you’re accidentally writing out overquit, 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:
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().