Search code examples
cursormousesdlrect

SDL - Move cursor with rect x and y smaller than zero


I implemented a custom cursor on my SDL game. When moving it across the screen I can go as far as I want to the right and to the bottom. But the cursor will not go beyond the ledt or the top wall. I'm using SDL_GetMouseState and passing the current x and y values to it.

How do I manage to allow a surface to move to the position (-5, 0)?

Here's some code:

typedef struct {
    SDL_Surface *image;
    SDL_Rect frame;
} Cursor;



void moveCursor(Cursor *cursor) {

    Sint16 *x = &cursor->frame.x;
    Sint16 *y = &cursor->frame.y;

    Uint16 cursorWidth = cursor->frame.w;
    Uint16 cursorHeight = cursor->frame.h;

    SDL_GetMouseState((int *)x, (int *)y); 

    cursor->frame.w = cursorWidth;
    cursor->frame.h = cursorHeight;

    SDL_Rect temporaryFrame = cursor->frame;

    SDL_BlitSurface(cursor->image, NULL, bufferSurface.surface, &temporaryFrame);
}

Solution

  • SDL doesn't detect mouse movements outside its window, so you cannot have negative mouse coordinates.

    To simulate a mouse that can move offscreen, always keep the mouse centered and store it's relative motion. The relative motion is translated to your virtual mouse that can now move anywhere and is represented by a sprite.