SDL2 event.. how ?

Hello,and good night

  • i have an problem with SDL2 and his mouse event /buttons

can you help me:

example:

 ...
 //--------------
  int quit = 1;
  
  while(quit == 1)
 {
  SDL_WaitEvent(&event);
  
  while (SDL_PollEvent(&event))
 {
  switch (event.type)
  {
  case SDL_QUIT:
  quit = 0;
  break;	
  }
  
  // ----------- mouse event test ----
  
      //  works :)
  /*
  if (event.type == SDL_MOUSEMOTION  )
  {
   quit = 0;
  }
  */
  
//  works not :(

  if (event.type == SDL_MOUSEMOTION && event.button.button ==  SDL_BUTTON(SDL_BUTTON_RIGHT))
  {
   quit = 0;
  }


    //  works not :(

  if (event.type == SDL_MOUSEMOTION && event.button.button ==  SDL_BUTTON_RIGHT)
  {
   quit = 0;
  }
  
 } // poll event loop
 ....
 } 
...
.....

Try this structure

while (SDL_PollEvent(&event))
{
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
// Handle mouse button press
break;
case SDL_MOUSEBUTTONUP:
// Handle mouse button release
break;
case SDL_MOUSEMOTION:
// Handle mouse movement
break;
case SDL_MOUSEWHEEL:
// Handle mouse wheel events
break;
}
}

When receiving a SDL_MOUSEMOTION event you should use the SDL_Event member named motion.

See https://wiki.libsdl.org/SDL2/SDL_Event#relationships-between-event-types-and-union-members

1 Like

Should be disconnected to motion if it’s keyboard, mouse. What are you trying to do?

Now i found this:

And edit my code until it works
… but now … cant moving mouse / it freeze from in to you comming mouse in window

#include <SDL2/SDL.h> 

#include <stdio.h>  
#include <stdlib.h> 

int main( int argc, char *argv[] )
{
	
  SDL_Event event; 
  SDL_Init(SDL_INIT_EVERYTHING);
    
//----------------
 
  SDL_Window *window = SDL_CreateWindow("SDL2 Mouse Test" , 50, 50, 480, 252, SDL_WINDOW_SHOWN);
  SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

//----------------

  SDL_Surface *blank_up = SDL_LoadBMP("hwnd_blank_up.bmp");
  SDL_Texture *texture_blank_up = SDL_CreateTextureFromSurface(renderer,blank_up);
   
  int mouse_x = 100;	
  int mouse_y = 100;	   
    
//--------------
  int quit = 1;
  
  while(quit == 1)
  {
   SDL_WaitEvent(&event);
  
   while (SDL_PollEvent(&event))
   {
    switch (event.type)
    {
  	 case SDL_QUIT:
     quit = 0;
     break;
     
     case SDL_MOUSEMOTION:
     if (event.type == SDL_MOUSEMOTION)
     {
      mouse_x = event.motion.x;	
      mouse_y = event.motion.y;	
      break;
     }
     
    case SDL_MOUSEBUTTONDOWN:
    if (event.button.button == SDL_BUTTON_LEFT)
     {
      quit = 0;	
      break;
     }
     

    } // end of switch event
   } // end of pollevent
 
 
    SDL_Rect dstrect_m = { mouse_x, mouse_y, 50 , 50} ;          
    SDL_RenderCopy(renderer,texture_blank_up, NULL, &dstrect_m);
    
     SDL_RenderPresent(renderer);
  } //end of whlie_loop

  //----------
  
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  
 SDL_Quit();
 return 0;
}

Problem 1:

You might want redraw the background using SDL_RenderClear each frame to avoid any garbage/left-over graphics. You can use SDL_SetRenderDrawColor to specify the colour.

SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); // black
SDL_RenderClear(renderer);

Problem 2:

The break statement in the SDL_MOUSEMOTION case is only executed if event.type == SDL_MOUSEMOTION is true. You need to move it outside the if.

case SDL_MOUSEMOTION:
	if (event.type == SDL_MOUSEMOTION)
	{
		mouse_x = event.motion.x;
		mouse_y = event.motion.y;
	}
	break;

The compiler should be able to warn you about this mistake (e.g. “warning: this statement may fall through”). If you didn’t get a warning about this I recommend that you turn on more warnings. If you use GCC or Clang you should at least use -Wall which enables this and many other useful warnings.

Now i have an New way…:rofl:
It works, but only to next Finger Position on Android where i touch on screen​:grin::grin:


#include <SDL2/SDL.h> 

#include <stdio.h>  
#include <stdlib.h> 

int main( int argc, char *argv[] )
{
	
  SDL_Event event; 
  SDL_Init(SDL_INIT_EVERYTHING);
  
//----------------
 
  SDL_Window *window = SDL_CreateWindow("Mouse Test " , 50, 50, 480, 252, SDL_WINDOW_SHOWN);
  SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

//----------------

  SDL_Surface *background = SDL_LoadBMP("background.bmp");
  SDL_Texture *texture_background = SDL_CreateTextureFromSurface(renderer,background);

  SDL_Surface *blank_up = SDL_LoadBMP("hwnd_blank_up.bmp");
  SDL_Texture *texture_blank_up = SDL_CreateTextureFromSurface(renderer,blank_up);

  SDL_Surface *blank_down = SDL_LoadBMP("hwnd_blank_down.bmp");
  SDL_Texture *texture_blank_down = SDL_CreateTextureFromSurface(renderer,blank_down);

//-----------------------------------


  int hwnd_size_x = 48;  // 48    on landsacpe: 118
  int hwnd_size_y = 42;  // 42    on landscape 106

   int mouse_x = 100;	
   int mouse_y = 100;	   
   
   SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
   SDL_RenderClear(renderer); 
    
//--------------
  int quit = 1;
  
  while(quit == 1)
  {
   SDL_WaitEvent(&event);
  
   while (SDL_PollEvent(&event))
   {
    switch (event.type)
    {
     case SDL_QUIT:
     quit = 0;
     break;
     
    case SDL_MOUSEMOTION:
     
     if (event.type == SDL_MOUSEMOTION )
     {
      mouse_x = event.motion.x;	
      mouse_y = event.motion.y;	
     }
   //  break;
       
       
 
   case SDL_MOUSEBUTTONDOWN:
     if (event.type == SDL_MOUSEBUTTONDOWN)
     {     
      mouse_x = event.motion.x;	
      mouse_y = event.motion.y;		
     }
     //break;



    } // end of switch event
   } // end of pollevent
 
    

 //---------------------------------
    
	SDL_Rect dstrect_back = { 0, 0, 480, 252};                  
    SDL_RenderCopy(renderer,texture_background, NULL, &dstrect_back);
  
    SDL_Rect dstrect0 = { 0, 0, hwnd_size_x, hwnd_size_y};                  
    SDL_RenderCopy(renderer,texture_blank_up, NULL, &dstrect0);
  
    SDL_Rect dstrect1 = { hwnd_size_x *1, 0, hwnd_size_x, hwnd_size_y};    
    SDL_RenderCopy(renderer,texture_blank_up, NULL, &dstrect1);
  
    SDL_Rect dstrect2 = { hwnd_size_x *2, 0, hwnd_size_x, hwnd_size_y};    
    SDL_RenderCopy(renderer,texture_blank_up, NULL, &dstrect2);
  
    SDL_Rect dstrect3 = { hwnd_size_x *3, 0, hwnd_size_x, hwnd_size_y};    
    SDL_RenderCopy(renderer,texture_blank_up, NULL, &dstrect3);
  
  
    //.... 
    
    SDL_Rect dstrect_m = { mouse_x, mouse_y, 50 , 50} ;          
    SDL_RenderCopy(renderer,texture_blank_up, NULL, &dstrect_m);

 
 SDL_RenderPresent(renderer);
 } //end of whlie_loop

  //----------
  
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  
 SDL_Quit();
 return 0;
}