Mouse + Button

Hey, iam back

I Show you my mouse/touch Code with an Button where you can push it
(Android compatible)

nclude <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 = 118;  // 48    on landsacpe: 118
  int hwnd_size_y = 106;  // 42    on landscape 106

   int mouse_x = 100;	
   int mouse_y = 100;	   
   
   int hwnd_button1_x = hwnd_size_x;
     int hwnd_button1_y = hwnd_size_y;
   
   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;

  // case SDL_MOUSEBUTTONDOWN:
     if (event.type == SDL_MOUSEBUTTONDOWN)
     
     {     

   if(((( event.type == SDL_MOUSEBUTTONDOWN && mouse_x + 6 >= hwnd_button1_x) &&
       (mouse_x +6  <= hwnd_button1_x + hwnd_size_x)) ||
       
((hwnd_button1_x + hwnd_size_x >= mouse_x) &&
(hwnd_button1_x + hwnd_size_x <= mouse_x + 6))) &&

 (((mouse_y + 12 >= hwnd_button1_y)&&
(mouse_y + 12 <= hwnd_button1_y + hwnd_size_y)) ||

((hwnd_button1_y + hwnd_size_y >= mouse_y) &&
(hwnd_button1_y + hwnd_size_y <= mouse_y+ 12))))
{
 // collision
  quit = 0;
  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 = { hwnd_button1_x, hwnd_button1_y, 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;
}

Hope you like it

hwnd_blank_up.bmp (6.0 KB)
hwnd_blank_down.bmp (6.0 KB)

some feedback

  1. if you call SDL_PollEvent right after SDL_WaitEvent, you lose one event
  2. indentation is terrible
    • those comments such as // end of switch event would be unnecessary if you had proper indentation
  3. don’t test event.type inside an if when inside a case testing the same value
  4. empty if that does nothing
  5. refactor the giant expression inside if into a function that does AABB

otherwise, nice!

1 Like

If you don’t want SDL_WaitEvent to remove one event from the event queue, you can pass NULL as argument.

nice, I didn’t know, thanks

Some feedback extending from elefantinho’s point 5:

refactor the giant expression inside if into a function that does AABB

  1. Since you have hwnd_button1_x, hwnd_button1_y, hwnd_size_x and hwnd_size_y for the button’s position and size, you can change those four variables into one variable: an SDL_Rect, or SDL_FRect if you want to use floats instead of integers.

    The SDL_Rect’s / SDL_FRect’s fields can be initialized like this:

    Integer version:
    SDL_Rect button = {118, 106, 118, 106};

    Float version:
    SDL_FRect button = {118.0f, 106.0f, 118.0f, 106.0f};

    Then when you want to check if the mouse pointer is clicking on the button, you can check it like this:

	case SDL_MOUSEBUTTONDOWN:
	{
		// Integer version
		if (SDL_PointInRect({mouse_x, mouse_y}, &button))
			quit = 0;

		// Float version
		if (SDL_PointInFRect({mouse_x, mouse_y}, &button))
			quit = 0;

		break;
	}
  1. Since you have mouse_x and mouse_y for the mouse position, you can change those two variables into one variable: an SDL_Point, or SDL_FPoint if you want to use floats instead of integers.

    The SDL_Point’s / SDL_FPoint’s fields can be initialized like this:

    Integer version:
    SDL_Point mousePosition = {100, 100};

    Float version:
    SDL_FPoint mousePosition = {100.0f, 100.0f};

    Then whenever the mouse moves and you want to store the mouse’ position, you can do it like this:

	case SDL_MOUSEMOTION:
	{
		mousePosition.x = event.motion.x;
		mousePosition.y = event.motion.y;

		// Or like this:
		mousePosition = {event.motion.x, event.motion.y};
	}

Hey Patty1991,

I’d suggest moving your dst rectangle variables above your game loop so that you can use them as controllers to your textures. You can use those rects to change the position and scale of the textures at will. (Like making the button expand or temporarily move up when a finger touches it to show that it is interactive)

Keep improving, we can see you are making progress.