Smooth Mouse (SDL) ;)

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Nekuz0r
Posts: 3
Joined: Mon Apr 02, 2007 11:13 am

Smooth Mouse (SDL) ;)

Post by Nekuz0r »

last night i made this, to make a mouse cursor with smooth movement

the movement is based on the stick angle (aStickA), 0° = Right
now, you can add accleration in function of circle ray (aStickR)

Code: Select all

int getSqrRadius(int X, int Y) {
	return sqrt((X * X) + (Y * Y));
}

SDL_ShowCursor(0);
SDL_WarpMouse(SCR_WIDTH / 2, SCR_HEIGHT / 2);

SDL_Surface* Mouse = SDL_LoadBMP("ms0:/PSP/GAME150/test/cursor.bmp");
SDL_SetColorKey(Mouse, SDL_SRCCOLORKEY, SDL_MapRGB(Mouse->format,255,255,255));
SDL_Rect coords;

SceCtrlData pad;
int aStickX = 0;
int aStickY = 0;
float aStickR;
float aStickA;
int x,y;
float Precision = 360.0f;
while (1) {
	
	//Deplacement de la souri
	SDL_GetMouseState(&x, &y);
	sceCtrlReadBufferPositive(&pad, 1);
	aStickX = pad.Ly - 128;
	aStickY = pad.Lx - 128;
	
	aStickR = getSqrRadius(aStickX, aStickY);
	if (aStickR > 100) {
		if (getSqrRadius(aStickX, aStickY) > 30) {
			aStickA = ((atan2f(aStickX, -aStickY) + M_PI) / (M_PI * 2)) * Precision;
		} else {
			aStickA = -1;      
		}
	
		//1° a 89°
		if &#40;aStickA >= 1 && aStickA <= &#40;89 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
			x += &#40;10 * &#40;1 - &#40;aStickA / &#40;Precision / 360.0f&#41; / 90&#41;&#41;&#41;;
			y -= &#40;10 * &#40;0 + &#40;aStickA / &#40;Precision / 360.0f&#41; / 90&#41;&#41;&#41;;
		&#125;
		else if &#40;aStickA >= &#40;91 * &#40;Precision / 360.0f&#41;&#41; && aStickA <= &#40;179 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
			x -= &#40;10 * &#40;0 + &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 89&#41; / 90&#41;&#41;&#41;;
			y -= &#40;10 * &#40;1 - &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 89&#41; / 90&#41;&#41;&#41;;
		&#125;
		else if &#40;aStickA >= &#40;181 * &#40;Precision / 360.0f&#41;&#41; && aStickA <= &#40;269 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
			x -= &#40;10 * &#40;1 - &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 179&#41; / 90&#41;&#41;&#41;;
			y += &#40;10 * &#40;0 + &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 179&#41; / 90&#41;&#41;&#41;;
		&#125;
		else if &#40;aStickA >= &#40;271 * &#40;Precision / 360.0f&#41;&#41; && aStickA <= &#40;359 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
			x += &#40;10 * &#40;0 + &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 269&#41; / 90&#41;&#41;&#41;;
			y += &#40;10 * &#40;1 - &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 269&#41; / 90&#41;&#41;&#41;;
		&#125;
		else if &#40;aStickA == 0&#41; &#123;
			x += 10;
		&#125;
		else if &#40;aStickA == &#40;90 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
			y -= 10;
		&#125;
		else if &#40;aStickA == &#40;180 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
			x -= 10;
		&#125;
		else if &#40;aStickR == &#40;270 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
			y += 10;
		&#125;
	&#125;
	
	SDL_WarpMouse&#40;x, y&#41;;
	coords.x = x;
	coords.y = y;
	SDL_BlitSurface&#40;Mouse, NULL, Screen, &coords&#41;;
	SDL_Flip&#40;Screen&#41;;
	sceKernelDelayThread&#40;20000&#41;;
&#125;
Post Reply