沒上班日子無聊,沒得上班又沒錢的日子即是痛苦,在上海又換了個地,生活不容易,新地方還不錯,上網用無線,下午準備接着寫SDL系列,沒想到的是B450上fedora12 無線網卡驅動沒整上,而後我在XP和fedora12之間不停的開關機,最終驅動裝上去(lsmod 看到了),可是無線網卡仍是配置上,(iwconfig)居然找不到設備(是否是真沒裝上)。看來得去好看看網卡驅動模塊,而後回到XP上,在虛擬機上裝了個fedora17,後來發現環境什麼的都沒搞好,gcc都沒有^=^!但仍然擋不住我這顆火熱的心。接着昨天的繼續,在事件驅動中鼠標有兩個事件,分別是SDL_MouseMotionEvent 和SDL_MouseButtonEvent,前者指是在整個顯示屏上所佔的位置(x,y)關於X,Y是如何定義的,能夠這麼理解,在顯示器的左上方是原點,水平方向是X軸,垂直方向是Y軸。後者表示鼠標的點擊(單擊雙擊左擊右擊)事件,還有滾動事件,下面是關於兩個事件的結構體。關於二者具體的解釋請看結構體註釋。 數組
typedef struct{ Uint8 type; /* SDL_MOUSEMOTION */ Uint8 state; /* 當前鼠標的狀態 */ Uint16 x, y; /* 鼠標此時XY的座標*/ Sint16 xrel, yrel; /*在X / Y方向的相對運動 */ } SDL_MouseMotionEvent;
typedef struct{ Uint8 type; /* SDL_MOUSEBUTTONDOWN OR SDL_MOUSEBUTTONUP */ Uint8 button; /* 當前鼠標的索引它們分別是SDL_BUTTON_LEFT SDL_BUTTON_MIDDLE, SDL_BUTTON_RIGHT */ Uint8 state; /* SDL_PRESSED OR SDL_RELEASED */ Uint16 x, y; /*記錄鼠標按下或釋放時X,Y時間,(翻譯過來是這樣,可是我也不肯定)*/ } SDL_MouseButtonEvent;大概就是這樣,鼠標的事件就如此。從鍵盤到鼠標我都關注有幾點值得注意的地方,其一是Uint8 type;其二都有Uint state;前者對應的宏還不同,從字面意思就可知道是一種型號,因此固然不一樣啦,但不一樣之間有共性那就是有DOWN和UP,當面SDL_MOUSEMOTION有點特殊。其實也就是狀態,一回事嘍!因此也就有了接下來的這個例子,Key STATE;
#include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_ttf.h" #include <string> //Screen attributes const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //The surfaces SDL_Surface *background = NULL; SDL_Surface *up = NULL; SDL_Surface *down = NULL; SDL_Surface *left = NULL; SDL_Surface *right = NULL; SDL_Surface *screen = NULL; //The event structure SDL_Event event; //The font TTF_Font *font = NULL; //The color of the font SDL_Color textColor = { 0, 0, 0 }; SDL_Surface *load_image( std::string filename ) { //The image that's loaded SDL_Surface* loadedImage = NULL; //The optimized surface that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = IMG_Load( filename.c_str() ); //If the image loaded if( loadedImage != NULL ) { //Create an optimized surface optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old surface SDL_FreeSurface( loadedImage ); //If the surface was optimized if( optimizedImage != NULL ) { //Color key surface SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) ); } } //Return the optimized surface return optimizedImage; } void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ) { SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface( source, clip, destination, &offset ); } bool init() { if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); //If there was an error in setting up the screen if( screen == NULL ) { return false; } //Initialize SDL_ttf if( TTF_Init() == -1 ) { return false; } //Set the window caption SDL_WM_SetCaption( "Press an Arrow Key", NULL ); //If everything initialized fine return true; } bool load_files() { //Load the background image background = load_image( "background.png" ); //Open the font font = TTF_OpenFont( "lazy.ttf", 72 ); //If there was a problem in loading the background if( background == NULL ) { return false; } //If there was an error in loading the font if( font == NULL ) { return false; } //If everything loaded fine return true; } void clean_up() { //Free the surfaces SDL_FreeSurface( background ); SDL_FreeSurface( up ); SDL_FreeSurface( down ); SDL_FreeSurface( left ); SDL_FreeSurface( right ); //Close the font TTF_CloseFont( font ); //Quit SDL_ttf TTF_Quit(); //Quit SDL SDL_Quit(); } int main( int argc, char* args[] ) { //Quit flag bool quit = false; //Initialize if( init() == false ) { return 1; } //Load the files if( load_files() == false ) { return 1; } //Render the text up = TTF_RenderText_Solid( font, "Up", textColor ); down = TTF_RenderText_Solid( font, "Down", textColor ); left = TTF_RenderText_Solid( font, "Left", textColor ); right = TTF_RenderText_Solid( font, "Right", textColor ); //While the user hasn't quit while( quit == false ) { while( SDL_PollEvent( &event ) ) { if( event.type == SDL_QUIT ) { quit = true; } } apply_surface( 0, 0, background, screen ); //獲得全部鍵的狀態 Uint8 *keystates = SDL_GetKeyState( NULL ); //If up is pressed if( keystates[ SDLK_UP ] ) { apply_surface( ( SCREEN_WIDTH - up->w ) / 2, ( SCREEN_HEIGHT / 2 - up->h ) / 2, up, screen ); } //進行判斷 if( keystates[ SDLK_DOWN ] ) { apply_surface( ( SCREEN_WIDTH - down->w ) / 2, ( SCREEN_HEIGHT / 2 - down->h ) / 2 + ( SCREEN_HEIGHT / 2 ), down, screen ); } //If left is pressed if( keystates[ SDLK_LEFT ] ) { apply_surface( ( SCREEN_WIDTH / 2 - left->w ) / 2, ( SCREEN_HEIGHT - left->h ) / 2, left, screen ); } //If right is pressed if( keystates[ SDLK_RIGHT ] ) { apply_surface( ( SCREEN_WIDTH / 2 - right->w ) / 2 + ( SCREEN_WIDTH / 2 ), ( SCREEN_HEIGHT - right->h ) / 2, right, screen ); } //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } } //End while quit //Clean up clean_up(); return 0; }代碼真沒什麼意思,裏面有一個SDL_GetKeyState()函數,獲得了整個鍵盤的當前信息狀態。也就stata值返回給一個數組。整個數組裏面的值是一組鍵盤定義的宏,是通用的。其次還可使用SDL_GetMouseState()獲得鼠標的當前狀態信息。
昨晚寫到這裏,原本準備發表的,可是發表不成功,因此今天上午倒騰到如今總算搞把fedora 無線上網搞點,只不過是換到fedroa17,用上去感受還不錯,至少字體,界面好看多了。有一點小小欣喜。U盤安裝fedora DVD版本(到時候我也把整個安裝過程遇到的問題和你們一塊兒分享下。 app