Exemplos de Eventos
Filtrando e Manipulando Eventos
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
/* Esta função pode executar em um processo separado */
int FilterEvents(const SDL_Event *event) {
static int boycott = 1;
/* Este evento SDL_QUIT sinaliza o fechamento de uma janela */
if ( (event->type == SDL_QUIT) && boycott ) {
printf("Evento sair filtrado -- tente novamente.\n");
boycott = 0;
return(0);
}
if ( event->type == SDL_MOUSEMOTION ) {
printf("Mouse movido para (%d,%d)\n",
event->motion.x, event->motion.y);
return(0); /* Nós pegamos o evento */
}
return(1);
}
int main(int argc, char *argv[])
{
SDL_Event event;
/* Inicializamos a biblioteca SDL (inicia o laço de eventos) */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Não pode iniciar SDL: %s\n", SDL_GetError());
exit(1);
}
/* Limpa ao sair, sai quando a janela fecha e interrompe a SDL */
atexit(SDL_Quit);
/* Ignora estado das teclas */
SDL_EventState(SDL_KEYDOWN, SDL_IGNORE);
SDL_EventState(SDL_KEYUP, SDL_IGNORE);
/* Filtro para eventos de saída e de movimento do mouse */
SDL_SetEventFilter(FilterEvents);
/* O mouse não pode ser usado a menos que tenhamos um display para referência */
if ( SDL_SetVideoMode(640, 480, 8, 0) == NULL ) {
fprintf(stderr, "Não pode estabelecer o modo de vídeo 640x480x8: %s\n", SDL_GetError());
exit(1);
}
/* Laço esperando ESC + Botão do mouse */
while ( SDL_WaitEvent(&event) >= 0 ) {
switch (event.type) {
case SDL_ACTIVEEVENT: {
if ( event.active.state & SDL_APPACTIVE ) {
if ( event.active.gain ) {
printf("Aplicação ativada\n");
} else {
printf("Aplicação minimizada\n");
}
}
}
break;
case SDL_MOUSEBUTTONDOWN: {
Uint8 *keys;
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) {
printf("Bye bye...\n");
exit(0);
}
printf("Botão do mouse pressionado\n");
}
break;
case SDL_QUIT: {
printf("Saída solicitada, saindo.\n");
exit(0);
}
break;
}
}
/* Isto nunca deve acontecer */
printf("Erro em SDL_WaitEvent: %s\n", SDL_GetError());
exit(1);
}