【问题标题】:SDL - Move cursor with rect x and y smaller than zeroSDL - 移动光标,矩形 x 和 y 小于零
【发布时间】:2013-07-09 11:58:28
【问题描述】:

我在我的 SDL 游戏上实现了一个自定义光标。在屏幕上移动它时,我可以向右和底部移动到任意位置。但光标不会超出 ledt 或顶墙。我正在使用SDL_GetMouseState 并将当前的x 和y 值传递给它。

我如何设法让表面移动到位置(-5, 0)?

这里有一些代码:

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);
}

【问题讨论】:

  • 对不起,我创建帖子时没有将最后一行复制到编辑器。
  • 只用一个关于视频表面的负偏移来blit它。
  • SDL 会自动把我的负坐标变成 0。
  • SDL_Rect 使用有符号变量,因此负坐标是可能的。很明显你的代码有问题,你没有显示出来。
  • 显然 SDL 没有检测到窗口外的鼠标移动。您的解决方案非常简单。让真实的鼠标始终位于窗口的中心,并且只记录它的相对运动。然后在任何你想要的地方展示你的“假”鼠标。

标签: cursor mouse sdl rect


【解决方案1】:

SDL 不会检测到其窗口外的鼠标移动,因此您不能使用负鼠标坐标。

要模拟可以在屏幕外移动的鼠标,请始终保持鼠标居中并存储它的相对运动。相对运动被转换为您的虚拟鼠标,该鼠标现在可以在任何地方移动并由精灵表示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多