Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 731131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:59:28+00:00 2026-05-14T06:59:28+00:00

I’m having a slight problem with my SDL/Opengl code, specifically, when i try to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:59:29+00:00Added an answer on May 14, 2026 at 6:59 am

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should take a look at Tika (http://lucene.apache.org/tika/) which is… May 14, 2026 at 9:48 pm
  • Editorial Team
    Editorial Team added an answer Ok, I've removed after_save callback from A to nested model… May 14, 2026 at 9:48 pm
  • Editorial Team
    Editorial Team added an answer Just use the Ant jar task? May 14, 2026 at 9:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.