0
votes

How do i make so that positions adapts to the new window position when i resize my window in SDL2 and with SDL_RenderSetLogicalSize?

I want to be able to hover a text and make it change color but whenever i resize the window its still in the same window cords. Is there a way to adapt the mouse?

void MainMenu::CheckHover()
{
    for (std::list<MenuItem>::iterator it = menuItems.begin(); it != menuItems.end(); it++)
    {
        Text* text = (*it).text;
        float Left = text->GetX();
        float Right = text->GetX() + text->GetWidth();
        float Top = text->GetY();
        float Bottom = text->GetY() + text->GetHeight();

        if (mouseX < Left ||
            mouseX > Right ||
            mouseY < Top ||
            mouseY > Bottom)
        {
            //hover = false
            text->SetTextColor(255, 255, 255);
        }
        else
        {
            //hover = true
            text->SetTextColor(100, 100, 100);
        }
    }
}
1
How do you resize the window and how do you set mouseX and mouseY? - olevegard
I use RenderSetLogicalSize and SDL_SetWindowSize to resize but because of logical size everything fits to the screen and that makes the coordinates of the mouse off. - SundayBrowsing

1 Answers

0
votes

I had a similar problem some time ago, and it was due to multiple updates of my mouse position in one SDL eventloop. I wanted to move a SDL_Texture around by dragging with the mouse but it failed after resizing, because somehow the mouse coordinates were messed up.

What I did was rearrange my code to have only one event handling the mouse position update. Also I'm not using any calls to SDL_SetWindowSize(). When the user resizes the window the renderer is resized appropriately due to SDL_RenderSetLogicalSize().

The relevant code parts look like this - some stuff is adapted to your case. I would also suggest to use a SDL_Rect to detect if the mouse is inside your text area, because the SDL_Rects will be resized internally if the the window/renderer changes size.

  //Declarations
  //...
  SDL_Point mousePosRunning;

  // Picture in picture texture I wanted to move
  SDL_Rect  pipRect;

  // Init resizable sdl window
  window = SDL_CreateWindow(
     "Window",
     SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex),
     SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex),
     defaultW, defaultH,
     SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE );
  renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");    // This one is optional
  SDL_RenderSetLogicalSize(renderer, defaultW, defaultH);

  // SDL main loop
  while(SDL_PollEvent(&event) && running)
  {
     switch (event.type)
     {
      // Some event handling here
      // ...
      // Handle mouse motion event
      case SDL_MOUSEMOTION:
           // Update mouse pos
           mousePosRunning.x = event.button.x;
           mousePosRunning.y = event.button.y;

           // Check if mouse is inside the pip region
           if (SDL_EnclosePoints(&mousePosRunning, 1, &pipRect, NULL))
           {
              // Mouse is inside the pipRect
              // do some stuff... i.e. change color
           }
           else
           {
              // Mouse left rectangle 
           }
           break;
     }
  }