下面是一个简化的代码,显示了我面临的问题. 该代码的目的是创建一个512×512窗口,并在检测到左键时将其顶层表面的Y尺寸更改为512x(512 25).当检测到另一个左击时,我们将尺寸放回到512×
该代码的目的是创建一个512×512窗口,并在检测到左键时将其顶层表面的Y尺寸更改为512x(512 25).当检测到另一个左击时,我们将尺寸放回到512×512.
当检测到左击事件或检测到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; } } }您可以尝试以下函数来获取变量oldx和oldy中的x和y坐标
int oldx,oldy; oldx=wherex(); oldy=wherey();
适用于Windows.