Quote:Original post by TheRamon
Your way gives me a trail behind my mouse with the text I'm trying to draw.
:(
'cause Mouserect has the X and Y and the width and height of my mouse. *custom cursor*
Ok, You have two alternatives. If the background is fairly static (your text isn't moving or changing, likewise with the surfaces), you can use a double buffered surface for your mouse:
SDL_Surface *mouse = SDL_LoadBMP(...);SDL_PixelFormat *format = screen->format;// this surface saves the piece of the screen// that was there before the mouse was blittedSDL_Surface *mouseBackground = SDL_CreateRGBSurface(SDL_SWSURFACE,mouse->w,mouse->h,format->BitsPerPixel,format->Rmask,format->Gmask,format->Bmask,format->Amask);// blit surface 1, surface 2 and ttftext// set up initial mouseSDL_Rect mouseoffset = {...};SDL_BlitSurface(screen,&mouseoffset,mouseBackground,0);SDL_BlitSurface(mouse,0,screen,&mouseoffset);while(running){ if( mouse_moved ) { // restore original backgroud SDL_BlitSurface(mouseBackground,0,screen,&mouseoffset); // change mouse position mouseoffset.x = // ask SDL for mouse position mouseoffset.y = // ditto // save the background of the new position SDL_BlitSurface(screen,&mouseoffset,mouseBackground,0); // blit the mouse in its new position SDL_BlitSurface(mouse,0,screen,&mouseoffset); } }
If you do start moving the background surfaces though, you'll have to be careful. The easiest way to do that would be to restore the background, blit whatever has moved/changed, then save the new background and re-blit the mouse.
The other way is more complex IMO.
You need to calculate the position of the mouse on the surface. I am guessing that surface1 and surface2 both start at (0,0). Your text is somewhere in the middle. You would need to calculate the relative position of the mouse on these surfaces. For example, if the text is at 100,100 and is of width 100 and height 30, and the mouse is at 110,110 and has a width and height of 20, then we can use the following code to work out the relative co-ordinates of the mouse:
SDL_Rect relativeMouseCoordinates( SDL_Surface *surface, const SDL_Rect &position, const SDL_Rect &mouse ){ int xoffset = std::max(0,mouse.x - position.x); int yoffset = std::max(0,mouse.y - position.y); int width = std::min(surface->w, xoffset + mouse.w); int height= std::min(surface->h, yoffset + mouse.h); SDL_Rect rectangle = {xoffset,yoffset,width,height}; return rectangle;}
You can write another function to check if the mouse rectangle is over the other surface. Then your code becomes:
while(running){ if( mouse_moved ) { mouseoffset = // use SDL to get mouse position for_each( surface_on_screen ) { if( mouseOverSurface(surface,surfacerect,mouseoffset) ) { SDL_Rect relativeRectangle = relativeMouseCoordinates(surface,surfacerect,mouseoffset); SDL_BlitSurface(surface,&relativeRectangle,screen,&mouseoffset); } } SDL_BlitSurface(mousesurface,0,screen,&mouseoffset); }}
As you can see, not updating the entire screen is considerably more complex. Please note that I could not test any of this code, it is all written from memory (with pseudo-code where appropriate) so you should double check all of it.
Another consideration, if you are doing an input-driven program rather than a continuous game loop consider using SDL_WaitEvent instead of PollEvent. Your program will then only do processing when the user requests it. For example, a turn based game could be implemented this way.