i build a maze. it has walls of brown colour and floor is of white colour. player is a rectangular box controlled by arrows keys. on pressing arrow keys i want to first check tht the area my object will occupy is a floor or not, if it is a floor i allow it to move otherwise not.
i check this by
1. reading pixels of the next occupied area using glReadPixels
2. compare the readed pixels with white color, if it is white increment in the object direction otherwise not
the problem is this when i press left key, the object does not move even if it is on the floor
some of my global variables
float objectx, objecty, objectSize = 20;
RGBApixmap block(10,20);
bool isFloorColor = false;
objectx and objecty is initialzed in generateMaze()function which is in main
this is my object code
void Object(float objX, float objY)
{
glColor3f(64/255.0,0,64/255.0);
glRectf(objX,objY,objX+objectSize,objY+objectSize);
}
arrow keys handler(i just mention left arrow key)
void mySpecialKeyFn(int key, int mx, int my)
{
switch(key)
{
case GLUT_KEY_LEFT:
glReadPixels(objectx-10, objecty, block.nCols, block.nRows, GL_RGBA, GL_UNSIGNED_BYTE,block.getPixmap());
isFloorColor = block.checkfloor();
if(objectx-10>95 && isFloorColor==true)
objectx-=10;
glutPostRedisplay();
break;
}
void display(void)
{
....
Object(objectx,objecty);
glFlush();
}
function in headerfile RGBA.h
bool checkfloor()
{
int count = 0;
for(int i =0 ; i<10; i++)
{
if(this->pixel[i].r ==(uchar)255.0 && this->pixel[i].g ==(uchar)255.0 && this->pixel[i].b ==(uchar)255.0)
count++;
}
if(count>=5)
return true;
else
return false;
}