Mouse Cursor Trails

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.

Is there some way to refresh the surface so I can remove the trails ?

thanks,
nasir-------------------------------------------------------------------------------------------------------------------
#include “SDL.h”

static char *crosshair = “./crosshair.bmp”;

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

image = SDL_LoadBMP(bmp);
if(image==NULL)
{
	printf("Failed loading bitmap");
	return;
}

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;

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

// 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(0);

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

SDL_Event event;

for(;;)
{
	while(SDL_PollEvent(&event))
	{
		switch(event.type)
		{
			case SDL_KEYDOWN:										// Press escape to exit
				if(event.key.keysym.sym == SDLK_ESCAPE)
				{
					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
	//			SDL_FillRect(screen,NULL,0);	

				display_bmp(screen,crosshair,event.motion.x,event.motion.y);
				break;
			}
				
			default:
				// unhandled events
				break;
		}
	}
}

return 0;

}

This has nothing to do with your problem, but why do you load the BMP
and free it every time the mouse moves!?!?!?

Load it once (beginning of program), free it once (end of program),
and only blit it when the mouse moves. Don’t load/free, too!!!

:slight_smile:

-bill!On Fri, Aug 27, 2004 at 12:22:21AM +0530, MNH wrote:

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

SDL_UpdateRect(screen,xcord,ycord,image->h,image->w);

Your problem is here, you’re only updating the portion of the screen
that contains the NEW cursor location, not the old one. So even if you
do erase the old cursor, you aren’t updating that portion of the
screen.On Aug 26, 2004, at 2:52 PM, MNH wrote:

By default SDL_mixer allocates 8 mixing channels. In a game I’m
developing I need more than that (it’s a racing game and all the car
engines should play at the same time and at different volumes/pitches).
At least I need 16 channels.

I’ve tried to allocate more channels with this call (just after
initializing the mixer):

nc=Mix_AllocateChannels(16);

The return value if 16 (as it should be). But when I call this code with
ch>=8 the game crashes with a segfault:

ch=Mix_PlayChannel(ch,sample,0);
Mix_Volume(ch,volume);

It’s like the number of channels is not changed with
Mix_AllocateChannels, since for 0 <= ch < 8, the code works perfectly…

btw, I’m using SDL_mixer-1.2.5 ad running Windows XP SP1

Any idea of what can be happening?

?? ??? 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(;:wink:
{
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;
}