如何检查鼠标是否在某个区域中

时间:2013-05-01 06:53:38

标签: c++ opengl

好吧所以我在屏幕上放了一些图像,真的你需要知道的是每个glvertex2f()都是一个点,因此连接4个点就是一个区域。每个glVertex2f由X,Y创建。我想保存这个区域范围,所以当我点击鼠标并获得x,y结果时,我可以测试鼠标点击x,y是否在这个区域内。

所以这就是我创建区域的地方

for( int z = 0; z < 6; z++ )
{
if( game->player1.Blockbestand[z] > 0 )
{
    glLoadIdentity();               
    xoff = (z/4.0f);
    yoff = (floor(xoff))/4.0f;

    glBegin(GL_QUADS);
    glTexCoord2f(0/4 + xoff,0/4 + yoff);       glVertex2f( x1,game->camera.height-y1);
    glTexCoord2f(0/4 + xoff,1.0/4 + yoff);    glVertex2f( x2,game->camera.height-y1 );
    glTexCoord2f(1.0/4 + xoff,1.0/4 + yoff); glVertex2f( x2,game->camera.height-y2 );
    glTexCoord2f(1.0/4 + xoff,0/4 + yoff);    glVertex2f( x1,game->camera.height-y2 );
    glEnd();

    x1= x2+10;
    x2 = x1+30;
    xoff = (z/4.0f);
    yoff = (floor(xoff))/4.0f;  
}
}           

这是我点击鼠标的地方

for (std::list<MouseState>::iterator it = clicks->begin(); it != clicks->end(); it++) {
    if (it->leftButton == true){
        std::cout << "Left click!\n";
        std::cout << "x: " << it->x << "\n";
            std::cout << "y: " << it->y << "\n";
    }

}

所以我假设我可以以某种方式保存数组中的区域。然后当左按钮点击为真时,我得到x,y并查看数组以查看它是否在该区域..但不知道如何做到这一点..

1 个答案:

答案 0 :(得分:0)

首先保存数组中的4个点

locationCheck[m][n] = x1;
locationCheck[m][n+1] = x2;
locationCheck[m][n+2] = game->camera.height-y1;
locationCheck[m][n+3] = game->camera.height-y2;


m++;

然后检查鼠标的x / y是否在4点之间。

void GameScreen::menuClickCheck(int x,int y){
int m = 0;
int n=0;
while (m < 32){
    if (locationCheck[m][n] < x && locationCheck[m][n+1] > x) 
        if (locationCheck[m][n+2] > y && locationCheck[m][n+3] < y)
            {
            switch (m)
            {
                case 0 : cout << "Type=DefaultLand \n";
                break;
                case 1 : cout << "Type=Lava \n";
                break;
                case 2 : cout << "Type=Stone \n";
                break;


            }//end switch
        m=32;
    }//endif
    m++;
}//whileloop

}