【发布时间】: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 没有检测到窗口外的鼠标移动。您的解决方案非常简单。让真实的鼠标始终位于窗口的中心,并且只记录它的相对运动。然后在任何你想要的地方展示你的“假”鼠标。