Quick event reference, by example

void main_loop() {
        int quit = 0;
        SDL_Event event;

        while (!quit) {
                while(SDL_PollEvent(&event)) {  /* Loop until there are no events left on the queue */
                  switch(event.type) { /* Process the appropiate event type */
                    case SDL_ACTIVEEVENT:
                        if (event.active.state & SDL_APPMOUSEFOCUS) {
                                if (event.active.gain) {
                                        printf("Mouse focus gained\n");
                                } else {
                                        printf("Mouse focus lost\n");
                                }
                        }
                        if (event.active.state & SDL_APPINPUTFOCUS) {
                                if (event.active.gain) {
                                        printf("Input focus gained\n");
                                } else {
                                        printf("Input focus lost\n");
                                }
                        }
                        if (event.active.state & SDL_APPACTIVE) {
                                if (event.active.gain) {
                                        printf("Application restored\n");
                                } else {
                                        printf("Application iconified\n");
                                }
                        }
                        break;
                    case SDL_KEYDOWN:  /* Handle a KEYDOWN event */
                        printf ("Key pressed:\n");
                        printf ("       SDL sim: %i\n",event.key.keysym.sym);
                        printf ("       modifiers: %i\n",event.key.keysym.mod);
                        printf ("       unicode: %i (if enabled with SDL_EnableUNICODE)\n",\
                                        event.key.keysym.unicode);
                        break;
                    case SDL_KEYUP:
                        printf ("Key released:\n");
                        printf ("       SDL sim: %i\n",event.key.keysym.sym);
                        printf ("       modifiers: %i\n",event.key.keysym.mod);
                        printf ("       unicode: %i (if enabled with SDL_EnableUNICODE)\n",\
                                        event.key.keysym.unicode);
                        break;
                    case SDL_MOUSEMOTION:
                        printf ("Mouse moved to: (%i,%i) ", event.motion.x, event.motion.y);
                        printf ("change: (%i,%i)\n", event.motion.xrel, event.motion.yrel);
                        printf ("       button state: %i\n",event.motion.state);
                        break;
                    case SDL_MOUSEBUTTONDOWN:
                        printf ("Mouse button %i ",event.button.button);
                        printf ("pressed with mouse at (%i,%i)\n",event.button.x,event.button.y);
                        break;
                    case SDL_MOUSEBUTTONUP:
                        printf ("Mouse button %i ",event.button.button);
                        printf ("released with mouse at (%i,%i)\n",event.button.x,event.button.y);
                        break;
                    case SDL_JOYAXISMOTION:
                        printf ("Joystick axis %i ",event.jaxis.axis);
                        printf ("on joystick %i ", event.jaxis.which);
                        printf ("moved to %i\n", event.jaxis.value);
                        break;
                    case SDL_JOYBALLMOTION:
                        printf ("Trackball axis %i ",event.jball.ball);
                        printf ("on joystick %i ", event.jball.which);
                        printf ("moved to (%i,%i)\n", event.jball.xrel, event.jball.yrel);
                        break;
                    case SDL_JOYHATMOTION:
                        printf ("Hat axis %i ",event.jhat.hat);
                        printf ("on joystick %i ", event.jhat.which);
                        printf ("moved to %i\n", event.jhat.value);
                        break;
                    case SDL_JOYBUTTONDOWN:
                        printf ("Joystick button %i ",event.jbutton.button);
                        printf ("on joystick %i ", event.jbutton.which);
                        printf ("pressed\n");
                        break;
                    case SDL_JOYBUTTONUP:
                        printf ("Joystick button %i ",event.jbutton.button);
                        printf ("on joystick %i ", event.jbutton.which);
                        printf ("released\n");
                        break;
                    case SDL_VIDEORESIZE:
                        printf ("Window resized to: (%i,%i)\n",event.resize.w, event.resize.h);
                        break;
                    case SDL_VIDEOEXPOSE:
                        printf ("Window exposed\n");
                        break;
                    case SDL_QUIT:
                        printf ("Request to quit\n");
                        quit = 1;
                        break;
                    case SDL_USEREVENT:
                        printf ("User event:\n");
                        printf ("       code:  %i\n",event.user.code);
                        printf ("       data1: %p\n",event.user.data1);
                        printf ("       data2: %p\n",event.user.data2);
                        break;
                    case SDL_SYSWMEVENT:
                        printf ("Window manager event\n");
                        break;
                    default: /* Report an unhandled event */
                        printf("I don't know what this event is!\n");
                  }
                }
        }
}

xD


CategoryExamples

Quick_event_reference (last edited 2008-12-12 01:43:36 by KenBull)