[SDL] Mouse Cursor Trails

Mohammed Yousif mhdyousif at gmx.net
Thu Aug 26 23:50:09 PDT 2004


في خميس 26 أغسطس 2004 21:52, كتب MNH:
> hi,
>
> the following function puts a bitmap(cross-hair) where the current mouse
> pointer is on the surface. So the idea is to move the cross-hair when we
> move the mouse. Its working but the only problem is that ugly pointer
> trails are left behind. I tried clearing the screen (with SDL_FillRect())
> before after Blit but that does'nt seem to work.
>

This is because you don't clear the bitmap in the old position before moving.

try this one:

#include <math.h> // for abs()
#include "SDL.h"

static char *crosshair = "./crosshair.bmp";
SDL_Surface * image;

void display_bmp(SDL_Surface *screen,char *bmp,int xcord,int ycord) {
SDL_Rect src,dest;
static SDL_Rect lastRect;
int x;
SDL_Color black={0,0,0,0};

src.x=0;
src.y=0;
src.h=image->h;
src.w=image->w;

dest.x=xcord;
dest.y=ycord;
dest.h=image->h;
dest.w=image->w;

// clear the last drawn cursor
SDL_FillRect(screen, &lastRect, SDL_MapRGB(screen, 0, 0, 0));
lastRect = dest;

if(SDL_BlitSurface(image,&src,screen,&dest)<0)
{
printf("error blitting : %s\n",SDL_GetError());
return;
}
SDL_Flip(screen);
//SDL_UpdateRect(screen,xcord,ycord,image->h,image->w);

//      SDL_FillRect(screen,&dest,0);
}

int main()
{
SDL_Surface *screen;

if(SDL_Init(SDL_INIT_VIDEO)<0)
{
printf("Failed Initializing Video : %s\n",SDL_GetError());
exit(1);
}

atexit(SDL_Quit);

// Dont show the mouse cursor
SDL_ShowCursor(1);

screen = SDL_SetVideoMode(800,768,16,SDL_HWSURFACE|SDL_ANYFORMAT | 
SDL_DOUBLEBUF);
if(screen==NULL)
{
printf("Error setting video mode : %s",SDL_GetError());
exit(1);
}

image = SDL_LoadBMP(crosshair);
if(image==NULL)
{
printf("Failed loading bitmap");
return;
}
SDL_Event event;
int acc = 10;
for(;;)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE)
{
SDL_FreeSurface(image);
SDL_Quit();
exit(0);
break;
}
case SDL_MOUSEMOTION:// throw current mouse co-ordinates on title bar
{// this is where the cross-hair will be thrown

// don't call display_bmp for every very small move
if (acc > 5) {
acc = 0;
display_bmp(screen,crosshair,event.motion.x,event.motion.y);
} else {
acc += abs(event.motion.xrel + event.motion.yrel);
}
break;
}

default:
// unhandled events
break;
}
}
}

return 0;
}




More information about the SDL mailing list