SDL Guide 中文譯版(三下) (轉)

worldblog發表於2007-12-15
SDL Guide 中文譯版(三下) (轉)[@more@] 

第三章下 鍵盤輸入

鍵盤相關資料結構

  • SDLKey 列舉型別,每一個符號代表一個鍵,如SDLK_a、SDLK_SPACE,在SDL_keysym.h中定義。
  • SDLMod 列舉型別,類似SDLKey,但用於修飾鍵,如Control、Alt、Shift,可以組合。
  • SDL_keysym

    typedef struct{ Uint8 scancode; SDLKey sym; SDLMod mod; Uint16 unicode; } SDL_keysym;

    用於按鍵和放開訊息。scancode是相關鍵碼。除非你有特殊用途,否則忽略。sym指示鍵,mod為修飾鍵集合,例如KMOD_NUM | KMOD_CAPS | KMOD_LSHIFT 為Numlock、Capslock和左Shift組合。最後unicode為對應字元。注意,僅當是按鍵訊息時unicode才有效,放開訊息時無效。Unicode變換必須用SDL_EnableUNICODE()開啟。
    
  • SDL_KeyboardEvent

    typedef struct{ Uint8 type; Uint8 state; SDL_keysym keysym; } SDL_KeyboardEvent;

    描述一個鍵盤訊息。type指示按鍵(SDL_KEYDOWN)或放開(SDL_KEYUP)。state是冗餘的,和含義type相同只是符號不同(SDL_PRESSED,SDL_RELEASED)。 

讀取鍵盤訊息

使用訊息迴圈用SDL_PollEvent()從訊息佇列裡讀取,並用switch-case檢測SDL_KEYUP和SDL_KEYDOWN。下面是個基本的例子。

例3-10 讀鍵盤訊息

SDL_Event event; . . /* Poll for events. SDL_PollEvent() returns 0 when there are no */ /* more events on the event queue, our while l will exit when */ /* that occurs. */ while( SDL_PollEvent( &event ) ){ /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */ switch( event.type ){ case SDL_KEYDOWN: printf( "Key press detectedn" ); break; case SDL_KEYUP: printf( "Key release detectedn" ); break; default: break; } }


更詳細的內容

我們已經知道要用SDL_Init和SDL_SetVoMode初始化,但我們還得用另外兩個取得必要資訊。要SDL_EnableUNICODE(1)以允許unicode變換,還要用SDL_GetKeyName將SDLKey轉換成可列印字元。注意:小於0x80的unicode字元直接對映到ASCII碼。下例用到這一點。

例3-11 解釋按鍵訊息

#include "SDL.h" /* Function s */ void PrintKeyInfo( SDL_KeyboardEvent *key ); void PrintModifiers( SDLMod mod ); /* main */ int main( int argc, char *argv[] ){ SDL_Event event; int quit = 0; /* Initialise SDL */ if( SDL_Init( SDL_INIT_VIDEO ) ){ fprintf( stderr, "Could not initialise SDL: %sn", SDL_GetError() ); exit( -1 ); } /* Set a video mode */ if( !SDL_SetVideoMode( 320, 200, 0, 0 ) ){ fprintf( stderr, "Could not set video mode: %sn", SDL_GetError() ); SDL_Quit(); exit( -1 ); } /* Enable Unicode translation */ SDL_EnableUNICODE( 1 ); /* Loop until an SDL_QUIT event is found */ while( !quit ){ /* Poll for events */ while( SDL_PollEvent( &event ) ){ switch( event.type ){ /* Keyboard event */ /* Pass the event data onto PrintKeyInfo() */ case SDL_KEYDOWN: case SDL_KEYUP: PrintKeyInfo( &event.key ); break; /* SDL_QUIT event (window close) */ case SDL_QUIT: quit = 1; break; default: break; } } } /* Clean up */ SDL_Quit(); exit( 0 ); } /* Print all information about a key event */ void PrintKeyInfo( SDL_KeyboardEvent *key ){ /* Is it a release or a press? */ if( key->type == SDL_KEYUP ) printf( "Release:- " ); else printf( "Press:- " ); /* Print the hardware scancode first */ printf( "Scancode: 0x%02X", key->keysym.scancode ); /* Print the name of the key */ printf( ", Name: %s", SDL_GetKeyName( key->keysym.sym ) ); /* We want to print the unicode info, but we need to make */ /* sure its a press event first (remember, release events */ /* don't have unicode info */ if( key->type == SDL_KEYDOWN ){ /* If the Unicode value is less than 0x80 then the */ /* unicode value can be used to get a printable */ /* representation of the key, using (char)unicode. */ printf(", Unicode: " ); if( key->keysym.unicode < 0x80 && key->keysym.unicode > 0 ){ printf( "%c (0x%04X)", (char)key->keysym.unicode, key->keysym.unicode ); } else{ printf( "? (0x%04X)", key->keysym.unicode ); } } printf( "n" ); /* Print modifier info */ PrintModifiers( key->keysym.mod ); } /* Print modifier info */ void PrintModifiers( SDLMod mod ){ printf( "Modifers: " ); /* If there are none then say so and return */ if( mod == KMOD_NONE ){ printf( "Nonen" ); return; } /* Check for the presence of each SDLMod value */ /* This looks messy, but there really isn't */ /* a clearer way. */ if( mod & KMOD_NUM ) printf( "NUMLOCK " ); if( mod & KMOD_CAPS ) printf( "CAPSLOCK " ); if( mod & KMOD_LCTRL ) printf( "LCTRL " ); if( mod & KMOD_RCTRL ) printf( "RCTRL " ); if( mod & KMOD_RSHIFT ) printf( "RSHIFT " ); if( mod & KMOD_LSHIFT ) printf( "LSHIFT " ); if( mod & KMOD_RALT ) printf( "RALT " ); if( mod & KMOD_LALT ) printf( "LALT " ); if( mod & KMOD_CTRL ) printf( "CTRL " ); if( mod & KMOD_SHIFT ) printf( "SHIFT " ); if( mod & KMOD_ALT ) printf( "ALT " ); printf( "n" ); }


遊戲式鍵盤輸入

鍵盤訊息僅在鍵的狀態在按下和放開間變化時才觸發。

設想你用游標鍵控制飛船運動以改變眼前看到的太空景象,當你按左鍵並希望鏡頭向左轉時。看看下面的程式碼,並注意它為什麼是錯的。

/* Alien screen coordinates */ int alien_x=0, alien_y=0; . . /* Initialise SDL and video modes and all that */ . /* Main game loop */ /* Check for events */ while( SDL_PollEvent( &event ) ){ switch( event.type ){ /* Look for a keypress */ case SDL_KEYDOWN: /* Check the SDLKey values and move change the coords */ switch( event.key.keysym.sym ){ case SDLK_LEFT: alien_x -= 1; break; case SDLK_RIGHT: alien_x += 1; break; case SDLK_UP: alien_y -= 1; break; case SDLK_DOWN: alien_y += 1; break; default: break; } } } }


問題在於你必須按100次左以便得到100次鍵盤訊息,才能向左轉100畫素。正確的方法是收到事件時設定標誌,根據標誌來移動。

例3-12 正確的運動控制

/* Alien screen coordinates */ int alien_x=0, alien_y=0; int alien_xvel=0, alien_yvel=0; . . /* Initialise SDL and video modes and all that */ . /* Main game loop */ /* Check for events */ while( SDL_PollEvent( &event ) ){ switch( event.type ){ /* Look for a keypress */ case SDL_KEYDOWN: /* Check the SDLKey values and move change the coords */ switch( event.key.keysym.sym ){ case SDLK_LEFT: alien_xvel = -1; break; case SDLK_RIGHT: alien_xvel = 1; break; case SDLK_UP: alien_yvel = -1; break; case SDLK_DOWN: alien_yvel = 1; break; default: break; } break; /* We must also use the SDL_KEYUP events to zero the x */ /* and y velocity variables. But we must also be */ /* careful not to zero the velocities when we shouldn't*/ case SDL_KEYUP: switch( event.key.keysym.sym ){ case SDLK_LEFT: /* We check to make sure the alien is moving */ /* to the left. If it is then we zero the */ /* velocity. If the alien is moving to the */ /* right then the right key is still press */ /* so we don't tocuh the velocity */ if( alien_xvel < 0 ) alien_xvel = 0; break; case SDLK_RIGHT: if( alien_xvel > 0 ) alien_xvel = 0; break; case SDLK_UP: if( alien_yvel < 0 ) alien_yvel = 0; break; case SDLK_DOWN: if( alien_yvel > 0 ) alien_yvel = 0; break; default: break; } break; default: break; } } . . /* Update the alien position */ alien_x += alien_xvel; alien_y += alien_yvel;


如您所見,我們用了兩個變數alien_xvel和alien_yvel來表示飛船的運動,並在響應鍵盤訊息時它們。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993820/,如需轉載,請註明出處,否則將追究法律責任。

相關文章