c++ - Passing an SDL_Surface or clip_rect -
i'm trying write bit of code in check see if mouse has been clicked on sdl surface. in code example below, have sdl_surface background trying check against mouse position.
bool checkcollide(int myx, int myy, sdl_surface* objectone){ if(myx < objectone->clip_rect.x) return false; if(myx > objectone->clip_rect.x + objectone->clip_rect.w) return false; if(myy < objectone->clip_rect.y) return false; if(myy > (objectone->clip_rect.y + objectone->clip_rect.h)) return false; return true; } void main(){ sdl_surface* background; while(sdl_pollevent(&event)){ if(event.type == sdl_mousebuttondown){ if(checkcollide(event.button.x, event.button.y, background){ // run events } } } the problem i'm having when try run shown above, code compiles fine doesn't anything. collision check fails. have tried in lot of various combinations, including changing parameters of checkcollide sdl_rect , passing surface's clip_rect property. successful way i've done have separate sdl_rect same size , location sdl_surface, , check collision against that. when run way, works expect to. why isn't surface (or clip_rect) passing correctly, or if is, doing wrong in checkcollide function cause fail?
the clip_rect property not think is:
from sdl docs:
the clip_rect field clipping rectangle set sdl_setcliprect.
and sdl_setcliprect
sets clipping rectangle surface. when surface destination of blit, area within clip rectangle drawn into.
the rectangle pointed rect clipped edges of surface clip rectangle surface can never fall outside edges of surface.
if rect null clipping rectangle set full size of surface.
the problem is, when test collision later on, clip_rect x,y set 0. surface not know it's position on screen.
a solution create sprite class have rect , surface.
Comments
Post a Comment