C++ 双倍';按钮';SDL_事件成员访问?
假设我有这个简单的事件处理代码,我想通过左键点击得到X&Y值:C++ 双倍';按钮';SDL_事件成员访问?,c++,sdl,game-engine,sdl-2,C++,Sdl,Game Engine,Sdl 2,假设我有这个简单的事件处理代码,我想通过左键点击得到X&Y值: SDL_Event = event; SDL_PollEvent(&event); //get the event switch (event.type) { case SDL_QUIT://if the X is pressed then end the game isRunning = false; break; case SDL_MOUSEBUTTONDOWN:
SDL_Event = event;
SDL_PollEvent(&event); //get the event
switch (event.type) {
case SDL_QUIT://if the X is pressed then end the game
isRunning = false;
break;
case SDL_MOUSEBUTTONDOWN:
int x, y;
if (event.button.button == SDL_BUTTON_LEFT) {
SDL_GetMouseState(&x, &y);
std::cout << "Xpos is: " << x << std::endl;
std::cout << "Ypos is: " << y << std::endl;
}
default:
break;
}
SDL_事件=事件;
SDL_PollEvent(事件和事件)//获取事件
开关(事件类型){
case SDL_QUIT://如果按下X,则结束游戏
isRunning=false;
打破
外壳SDL_鼠标按钮向下:
int x,y;
if(event.button.button==SDL_按钮左){
SDL_GetMouseState(x&y);
std::cout是一个大的“ole”:
第二个按钮是SDL\u MouseButtonEvent
SDL\u Event
中的实际鼠标按钮索引,也就是说事件。按钮是作为SDL\u MouseButtonEvent
访问事件数据的方式。然后,SDL\u MouseButtonEvent::button
提供实际的鼠标按钮索引
你可以这样澄清一下:
SDL_Event = event;
SDL_PollEvent(&event); //get the event
switch (event.type) {
. . .
case SDL_MOUSEBUTTONDOWN:
{
const SDL_MouseButtonEvent &mouse_event = event.button;
int x, y;
if (mouse_event.button == SDL_BUTTON_LEFT) {
SDL_GetMouseState(&x, &y);
std::cout << "Xpos is: " << x << std::endl;
std::cout << "Ypos is: " << y << std::endl;
}
break;
}
. . .
}
SDL_事件=事件;
SDL_polleevent(&event);//获取事件
开关(事件类型){
. . .
外壳SDL_鼠标按钮向下:
{
const SDL_MouseButtonEvent&mouse_event=event.button;
int x,y;
如果(鼠标左键==SDL左键){
SDL_GetMouseState(x&y);
std::难道我不明白你的问题。event.button.button
没有调用,它与你获得x
和y
的方式无关。你使用event.button.button
只是为了点击鼠标按钮。至于为什么按钮在那里出现两次,那只是库的设计。event.button
表示一个按钮事件和事件。button.button
表示事件发生的按钮。好吧,我刚才说我调用了event.button…我只是想理解你为什么需要。button.button,而不是一个。button,你评论的后半部分和两个a下面的回答为我澄清一下。
typedef struct SDL_MouseButtonEvent
{
Uint32 type; /**< ::SDL_MOUSEBUTTONDOWN or ::SDL_MOUSEBUTTONUP */
Uint32 timestamp; /**< In milliseconds, populated using SDL_GetTicks() */
Uint32 windowID; /**< The window with mouse focus, if any */
Uint32 which; /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
Uint8 button; /**< The mouse button index */
Uint8 state; /**< ::SDL_PRESSED or ::SDL_RELEASED */
Uint8 clicks; /**< 1 for single-click, 2 for double-click, etc. */
Uint8 padding1;
Sint32 x; /**< X coordinate, relative to window */
Sint32 y; /**< Y coordinate, relative to window */
} SDL_MouseButtonEvent;
SDL_Event = event;
SDL_PollEvent(&event); //get the event
switch (event.type) {
. . .
case SDL_MOUSEBUTTONDOWN:
{
const SDL_MouseButtonEvent &mouse_event = event.button;
int x, y;
if (mouse_event.button == SDL_BUTTON_LEFT) {
SDL_GetMouseState(&x, &y);
std::cout << "Xpos is: " << x << std::endl;
std::cout << "Ypos is: " << y << std::endl;
}
break;
}
. . .
}