Reputation: 83
In creating the mouse events to click in a rectangle, how would I reformat the if statement so that I no longer get an error for taking the address of a temporary object of type 'SDL_Rect'.
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
SDL_Point point ={x, y};
bool isPointInTileMap = false;
for (int i = 0; i < TILE_HEIGHT; i++ )
{
for (int j = 0; j < TILE_WIDTH; j++)
{
if (SDL_PointInRect(&point, &(tileMap_[i][j].getBoundRect())))
{
isPointInTileMap = true;
break;
}
}
}
Upvotes: 0
Views: 49
Reputation: 155145
Simply use a local:
for( int y = 0; y < TILE_HEIGHT; y++ )
{
for( int x = 0; x < TILE_WIDTH; x++ )
{
SDL_Rect rect = tileMap_[y][x].getBoundRect();
if( SDL_PointInRect( &point, &rect ) )
{
isPointInTileMap = true;
break;
}
}
}
This can be simplified further:
for( int y = 0; y < TILE_HEIGHT; y++ )
for( int x = 0; x < TILE_WIDTH; x++ )
{
SDL_Rect rect = tileMap_[y][x].getBoundRect();
if( isPointInTileMap = SDL_PointInRect( &point, &rect ) ) break;
}
Don't worry about allocation as SDL_Rect
is a "POCO" (Plain Ol' C Object) struct, so it lives on the stack and is reclaimed automatically - just make sure you never use use the value of &rect
outside its scope as it will be dangling pointer.
Upvotes: 1