Toastbot
Toastbot

Reputation: 87

Function to move a SDL2 window while mouse down flickers the window and doesn't move 'fast' enough

I made a function to move an borderless SDL window. I use SDL_MOUSEBUTTONDOWN to 'activate' the window movement and SDL_MOUSEBUTTONUP to 'deactivate' it. For some reason it does not just move like it should but instead moves way slower than my mouse and flickers if I moved it by a good distance.

I use SDL2, and I'm on windows 10.

My loop always updates my mouse position, and the function takes the mouse position reduces it by the last mouse position and then moves the window by that distance.

sPos moveClock(int event){
    sPos temPos = setPos(0,0);
    if(tempMoveVar==1){
        temPos = setPos(gvMousePos.x-mPos.x,gvMousePos.y-mPos.y);
        mPos = setPos(gvMousePos.x,gvMousePos.y);
    }else if(event==-1){    //Mouse Down
        mPos = setPos(gvMousePos.x,gvMousePos.y);
        tempMoveVar=1;
    }
    if(event==-65){         //Mouse Up
        tempMoveVar=0;
    }
    return temPos;
}

I just want the window to move 'with' the mouse while my mouse button is down, like you normally can move windows.

Upvotes: 2

Views: 2322

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96579

Rather than moving the window manually, I suggest using SDL_SetWindowHitTest:

int SDL_SetWindowHitTest(SDL_Window* window, SDL_HitTest callback, void* callback_data);

This function lets you specify what dragging specific pixels of a window does (possible actions are moving the window, resizing it, or doing nothing).

You should probably call this function once, after creating your window.

Parameters are:

  • SDL_Window* window speaks for itself.

  • SDL_HitTest callback receives a function that, when given coordinates of a pixel, determines what dragging this pixel does.

  • void* callback_data is described below.

You need to write a function to pass to callback. It has to have following return type and parameter types:

SDL_HitTestResult MyCallback(SDL_Window* win, const SDL_Point* area, void* data)
{
    ...
}

area->x and area->y are the coordinates of the pixel that's being checked. win is the window.

data will receive the same pointer you passed to callback_data when calling SDL_SetWindowHitTest. You can use this pointer to pass arbitrary data to your callback; or, if you don't need it, simply set it to 0.

Your callback should return one of the following:

  • SDL_HITTEST_NORMAL - no action.
  • SDL_HITTEST_DRAGGABLE - dragging this pixel moves the window.
  • SDL_HITTEST_RESIZE_* - dragging this pixel resizes a specific edge (or edges) of the window. (Here * is one of: TOPLEFT, TOP, TOPRIGHT, RIGHT, BOTTOMRIGHT, BOTTOM, BOTTOMLEFT, LEFT).

Upvotes: 5

Related Questions