![]() |
![]() |
|||||||||||||||||||||||||||||||||
|
SDL de mouse event (olayları) Bu bölümde sdl de mouse olaylarını inceleyeceğiz...
SDL de gerçekleşen olayları tutmak için SDL_Event tipinde bir değişken tutarız.SDL_PollEvent fonksiyonu kullanılarak bu değişkene program üzerinde gerçekleşen olay bilgileri aktarılır.Bizde bu değişkenin tipine bakarak işleme almak istediğimiz olay tipinde ise gerekli işlemleri yaparız. Mouse olayları için sdl de tanımlanmış olan enum değerleri SDL_MOUSEBUTTONDOWN ,SDL_MOUSEBUTTONUP,SDL_MOUSEMOTION ... gibidir.Hangi olay tipini kullanmak istiyorsanız bu karşılaştırmayı yaparak kullanabilirsiniz. Örneğimizde mouse olaylarına bağlı olarak bir ekrana hangi olay gerçekleştiyse onunla ilgili mesaj yazdırma işlmi yapcaz..Bunun için 4 parçadan oluşan bir mesaj resmimiz kullanılmıştır ve önceden anlatıldığı gibi bu resim clip leme şeklinde okunarak ekrana basılmıştır. #include <SDL.h> #include <SDL_image.h> #include <string> using namespace std; const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //Kullanılacak rect dizisi için indeks değerleri. const int CLIP_MOUSEOVER = 0; const int CLIP_MOUSEOUT = 1; const int CLIP_MOUSEDOWN = 2; const int CLIP_MOUSEUP = 3; SDL_Surface *buttonSheet = NULL; SDL_Surface *screen = NULL; SDL_Event _event; //Resmimizin clip lere ayrılması ile her bir rect bilgisinin tutulduğu array. SDL_Rect clips[ 4 ]; //Mouse bilgisinin tutulduğu sınıfımız. //Button sınıfı mouse olaylarının gerçekleşmesi halinde gereken bilgileri taşıyacaktır. //Öncelike bir buton karesi oluşturulur.Bu box ile tutulur. //clip ise bu box üzerinde değişen yüzeydir. class Button { private: SDL_Rect box; SDL_Rect * clip; // public: Button(int x,int y,int w,int h); void handle_events(); void show(); }; SDL_Surface * load_image(string filename) { SDL_Surface* loadedImage = NULL; SDL_Surface* optimizedImage = NULL; loadedImage = IMG_Load( filename.c_str() ); if( loadedImage != NULL ) { optimizedImage = SDL_DisplayFormat( loadedImage ); SDL_FreeSurface( loadedImage ); if( optimizedImage != NULL ) { SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) ); } } 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( screen == NULL ) { return false; } SDL_WM_SetCaption( "Button Test", NULL ); return true; } bool load_files() { //Buton resmi yüklendi. buttonSheet = load_image( "button.png" ); if( buttonSheet == NULL ) { return false; } return true; } void clean_up() { SDL_FreeSurface( buttonSheet ); SDL_Quit(); } //Clipleme işlemi burda yapılmıştır. void set_clips() { clips[ CLIP_MOUSEOVER ].x = 0; clips[ CLIP_MOUSEOVER ].y = 0; clips[ CLIP_MOUSEOVER ].w = 320; clips[ CLIP_MOUSEOVER ].h = 240; clips[ CLIP_MOUSEOUT ].x = 320; clips[ CLIP_MOUSEOUT ].y = 0; clips[ CLIP_MOUSEOUT ].w = 320; clips[ CLIP_MOUSEOUT ].h = 240; clips[ CLIP_MOUSEDOWN ].x = 0; clips[ CLIP_MOUSEDOWN ].y = 240; clips[ CLIP_MOUSEDOWN ].w = 320; clips[ CLIP_MOUSEDOWN ].h = 240; clips[ CLIP_MOUSEUP ].x = 320; clips[ CLIP_MOUSEUP ].y = 240; clips[ CLIP_MOUSEUP ].w = 320; clips[ CLIP_MOUSEUP ].h = 240; } Button::Button( int x, int y, int w, int h ) { box.x = x; box.y = y; box.w = w; box.h = h; clip = &clips[ CLIP_MOUSEOUT ]; } //Olay bilgisinin işlenmesi void Button::handle_events() { int x = 0, y = 0; //Eğer gerçekleşen olay mouse hareketi ise bu durumda box üzerinde olup olmadığına bakıyoruz. //Yani imleç box ile gösterilen alanın üzerinde ise üzerinde dir clibi değilse dışarıdadır clibi kullanılıyor. if( _event.type == SDL_MOUSEMOTION ) { x = _event.motion.x; y = _event.motion.y; if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) ) { clip = &clips[ CLIP_MOUSEOVER ]; } else { clip = &clips[ CLIP_MOUSEOUT ]; } } //Eğer olay mouse tıklama ise bu durumda down clibi kullanılıyor. if( _event.type == SDL_MOUSEBUTTONDOWN ) { if( _event.button.button == SDL_BUTTON_LEFT ) { x = _event.button.x; y = _event.button.y; if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) ) { clip = &clips[ CLIP_MOUSEDOWN ]; } } } //Olay mouse un sol butonunun kaldırılması olayı ise bu durumda up clibi kullanılıyor. if( _event.type == SDL_MOUSEBUTTONUP ) { if( _event.button.button == SDL_BUTTON_LEFT ) { x = _event.button.x; y = _event.button.y; if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) ) { clip = &clips[ CLIP_MOUSEUP ]; } } } } // void Button::show() { apply_surface( box.x, box.y, buttonSheet, screen, clip ); } int main( int argc, char* args[] ) { bool quit = false; if( init() == false ) { return 1; } if( load_files() == false ) { return 1; } set_clips(); Button myButton( 170, 120, 320, 240 ); while( quit == false ) { if( SDL_PollEvent( &_event ) ) { myButton.handle_events(); if( _event.type == SDL_QUIT ) { quit = true; } } SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) ); myButton.show(); if( SDL_Flip( screen ) == -1 ) { return 1; } } clean_up(); return 0; } hakan gedek hgedek@gmail.com Yorumlar
bakan arkadaşlardan yorum beklenmektedir...
Yazan:hgedek | 12/01/2009 21:48:56
hocam yeni üye oldum ama görür görmez dedim aradığım yer ve aradığım bilgiler. BÖTE bölümü okuyorum ve bu konuyu anlatmam gerekiyodu ve bu bilgilerin acayip yararı olcak. Sağolasın.
Yazan:deliii | 05/05/2009 17:18:52
|
||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||