|
[이전]
목차
[다음]
Simple DirectMedia Layer API 사용하기
이벤트(Events)
SDL_WaitEvent() 함수를 사용해서 이벤트를 기다린다.
|
팁:
SDL 은 국제 키보드를 지원한다. 키 이벤트를 유니코드와 유사한 코드로
바꾸어 event.key.keysym.unicode 에 넣는다. 이러한 작업은 약간의
프로세싱 오버헤드를 감수해야 하기 때문에, SDL_EnableUNICODE() 가
활성화되어야만 사용된다.
|
예제:
{
SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_KEYDOWN:
printf("The %s key was pressed!\n",
SDL_GetKeyName(event.key.keysym.sym));
break;
case SDL_QUIT:
exit(0);
}
}
|
SDL_PollEvent() 함수를 사용해서 이벤트를 폴링한다(검사한다).
|
팁:
SDL_PeepEvents() 함수에 SDL_PEEKEVENT 를 넘겨 이벤트를 삭제하지 않고도
이벤트 큐에서 이벤트를 확인할 수 있다.
|
예제:
{
SDL_Event event;
while ( SDL_PollEvent(&event) ) {
switch (event.type) {
case SDL_MOUSEMOTION:
printf("Mouse moved by %d,%d to (%d,%d)\n",
event.motion.xrel, event.motion.yrel,
event.motion.x, event.motion.y);
break;
case SDL_MOUSEBUTTONDOWN:
printf("Mouse button %d pressed at (%d,%d)\n",
event.button.button, event.button.x, event.button.y);
break;
case SDL_QUIT:
exit(0);
}
}
}
|
이벤트를 직접적으로 처리하는 것 뿐만 아니라,
각 이벤트 타입은 애플리케이션 이벤트 상태를 체크할 수 있는
함수를 제공한다. 만약 이 함수를 독점적으로 사용하면,
SDL_EventState() 함수로 모든 이벤트를 무시하고, SDL_PumpEvents()
함수를 주기적으로 호출하여 애플리케이션 이벤트 상태를 갱신해야만
한다.
|
팁:
SDL_ShowCursor() 를 사용해서 시스템 마우스 커서를 감추거나
보이게 할 수 있다.
|
예제:
{
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
}
void CheckMouseHover(void)
{
int mouse_x, mouse_y;
SDL_PumpEvents();
SDL_GetMouseState(&mouse_x, &mouse_y);
if ( (mouse_x < 32) && (mouse_y < 32) ) {
printf("Mouse in upper left hand corner!\n");
}
}
|
[이전]
목차
[다음]
|