下面是一个简化的代码,显示了我所面临的问题。
该代码的目的是创建一个512x512窗口,并在检测到左键单击时将其顶层表面的Y尺寸更改为512x(512+25)。当检测到另一次左键单击时,我们将尺寸恢复为512x512。
当检测到左击事件时,或者当检测到mouseMotionEvent时,我们(使用printf())显示鼠标坐标。
观察到奇怪的行为:
当我运行代码时,我左键单击了一次,窗口的Y尺寸改变了,但是当我在新创建的区域内移动鼠标时,显示的Y坐标仍然是511。
有时我不会得到这种奇怪的行为,那么Y坐标可以大于511。要获得奇怪的行为,左键单击几次,快速移动鼠标。
编译(linux):
$ gcc -o test test.c `sdl-config --cflags --libs`来源:(test.c)
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
/* Prototypes */
void event_handler(void);
/* global variables declaration */
SDL_Surface *screen=NULL;
/***** MAIN FUNCTION *****/
int main(int argc, char** argv)
{
/* init SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Erreur à l'initialisation de la SDL : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
if ((screen = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE )) == NULL) {
fprintf(stderr, "Graphic mode could not be correctly initialized : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_WM_SetCaption("my window", NULL);
/* handle event & wait until quit_event is raised */
event_handler();
/* quit & return */
SDL_Quit();
return EXIT_SUCCESS;
}
/*** Event handler ***/
void event_handler(void)
{
SDL_Event event;
int quit=0;
char message_is_displayed=0;
SDL_Rect mess_coord = {0,512,512,512+25}; //{x_start,y_start,width,height}
while(!quit)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
quit = 1;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
if(!message_is_displayed)
{
screen = SDL_SetVideoMode(512,512+25, 32, SDL_SWSURFACE); //change the screen size
SDL_FillRect(screen, &mess_coord, SDL_MapRGB(screen->format, 255, 255, 255)); // fill in white the bottom area
}
else
{
screen = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE);
}
message_is_displayed = !message_is_displayed;
SDL_Flip(screen);
}
printf("mouse position: (%d,%d)\n",event.button.x, event.button.y);
break;
case SDL_MOUSEMOTION:
printf("mouse position: (%d,%d)\n",event.motion.x, event.motion.y);
break;
}
}
}发布于 2012-10-16 01:15:37
您可以尝试使用以下函数获取变量oldx和oldy中的x和y坐标,如下所示
int oldx,oldy;
oldx=wherex();
oldy=wherey();在windows中工作。
发布于 2015-05-12 20:54:58
如果你使用的是SDL1,我认为你应该在你的代码中添加以下代码:
int x, y;
SDL_GetMouseState(&x, &y);现在,鼠标位置为x和y。
发布于 2015-05-12 23:29:39
只要按下鼠标按钮,它就会调整大小,然后打印按钮位置。它可能是在窗口裁剪之后裁剪鼠标位置。
尝试仅在SDL_MOUSEBUTTONUP上调整窗口大小。这样,您可以在鼠标真正单击(SDL_MOUSEBUTTONDOWN)时打印当前位置,然后在松开鼠标后调整大小。
https://stackoverflow.com/questions/10584479
复制相似问题