Page 1 of 1

SDL_BUTTON Help

Posted: Thu Jun 17, 2010 11:49 am
by xiphirx

Code: Select all

void map::handleEvents(SDL_Event &event, char *keys)
{
	ms = SDL_GetMouseState( &mouseX, &mouseY );
	//Scrolling
	if (keys[SDLK_w] == 1) { scrTop -= 10; }
	if (keys[SDLK_s] == 1) { scrTop += 10; }
	if (keys[SDLK_a] == 1) { scrLeft -= 10;}
	if (keys[SDLK_d] == 1) { scrLeft += 10;}

	//TileSelector
	tileSelecX = ( ( mouseX + scrLeft ) / TILE_DIM ) * TILE_DIM - TILE_DIM - scrLeft;
	tileSelecY = ( ( mouseY + scrTop ) / TILE_DIM ) * TILE_DIM - TILE_DIM - scrTop;

	switch (state)
	{
	case 'T':
		handleTilingEvents(event, keys, ms);
		break;
	}
}

void map::handleTilingEvents(SDL_Event &event, char *keys, Uint8 ms)
{
	if (ms && SDL_BUTTON(SDL_BUTTON_LEFT) && state == 'T')
	{
		//set tile to selected
		if ( ((mouseX+scrLeft)/TILE_DIM) >= 0 &&
			 ((mouseX+scrLeft)/TILE_DIM) < tileData.numcols() &&
			 ((mouseY+scrTop)/TILE_DIM) >= 0 &&
			 ((mouseY+scrTop)/TILE_DIM) < tileData.numrows() 
		   )
		{
			tileData[(mouseY+scrTop)/TILE_DIM][(mouseX+scrLeft)/TILE_DIM] = selTile;
			cerr << "left m button" << endl;
		}
	}

	//Drag Fill
	if ( ms && SDL_BUTTON(SDL_BUTTON_MIDDLE) && mmb == 0 ) 
	{
		if ( ( (mouseX+scrLeft)/TILE_DIM ) >= 0 &&
			 ( (mouseX+scrLeft)/TILE_DIM ) < tileData.numcols() &&
			 ( (mouseY+scrTop)/TILE_DIM ) >= 0 &&
			 ( (mouseY+scrTop)/TILE_DIM ) < tileData.numrows() 
		   )
		{
			mmb=1;
		}
	}
	if (mmb == 1)
	{

		dragFillX1 = (mouseX+scrLeft)/TILE_DIM;
		dragFillY1 = (mouseY+scrTop)/TILE_DIM;
		mmb = 2;
	}
	if ( !( ms & SDL_BUTTON(SDL_BUTTON_MIDDLE) ) && mmb ==2 ) 
	{
		if (
			( (mouseX+scrLeft)/TILE_DIM ) >= 0 &&
			( (mouseX+scrLeft)/TILE_DIM ) < tileData.numcols() &&
			( (mouseY+scrTop)/TILE_DIM ) >= 0 &&
			( (mouseY+scrTop)/TILE_DIM ) < tileData.numrows() 
			)
		{
			mmb = 0;
			dragFillX2 = (mouseX+scrLeft)/TILE_DIM;
			dragFillY2 = (mouseY+scrTop)/TILE_DIM;
			int temp;
			//Swap rect coordinates if needed
			if (dragFillX2 < dragFillX1) { temp = dragFillX1; dragFillX1 = dragFillX2; dragFillX2 = temp; }
			if (dragFillY2 < dragFillY1) { temp = dragFillY1; dragFillY1 = dragFillY2; dragFillY2 = temp; }

			for (int l=dragFillY1; l<=dragFillY2; l++)
			{
				for (int k=dragFillX1; k<=dragFillX2;k++)
				{
					tileData[l][k] = selTile;
				}
			}
			dragFillX1 = dragFillX2 = dragFillY1 = dragFillY2 = -1;
		}
	}
}
For some reason, my middle button triggers left mouse button and the middle button ...

This is reused code from an old project that doesn't have this problem, I simply copy and pasted it and made it look nicer :/

Re: SDL_BUTTON Help

Posted: Thu Jun 17, 2010 1:27 pm
by Ginto8
IIRC SDL_BUTTON(0) should be left, SDL_BUTTON(1) should be middle, and SDL_BUTTON(2) should be right. I forget what SDL_BUTTON_LEFT etc. actually are used for (I think mouse events, not state).

Re: SDL_BUTTON Help

Posted: Thu Jun 17, 2010 1:33 pm
by xiphirx
Ginto8 wrote:IIRC SDL_BUTTON(0) should be left, SDL_BUTTON(1) should be middle, and SDL_BUTTON(2) should be right. I forget what SDL_BUTTON_LEFT etc. actually are used for (I think mouse events, not state).
I think they're enumerations right?

Re: SDL_BUTTON Help

Posted: Thu Jun 17, 2010 1:39 pm
by Ginto8
xiphirx wrote:
Ginto8 wrote:IIRC SDL_BUTTON(0) should be left, SDL_BUTTON(1) should be middle, and SDL_BUTTON(2) should be right. I forget what SDL_BUTTON_LEFT etc. actually are used for (I think mouse events, not state).
I think they're enumerations right?
They're enumerations for mouse event types, not mouse states. Use SDL_BUTTON(n) for the n+1'th button (going left, middle, right and so on) SDL_BUTTON_* is for when you are getting a mouse event from SDL_PollEvent()

Re: SDL_BUTTON Help

Posted: Thu Jun 17, 2010 1:48 pm
by Bakkon
xiphirx wrote:I think they're enumerations right?
Yes, but they're used like this:

Code: Select all

if(event.button.type == SDL_MOUSEBUTTONDOWN)
	if(event.button.button == SDL_BUTTON_LEFT)
		SelectThing();

Re: SDL_BUTTON Help

Posted: Thu Jun 17, 2010 1:56 pm
by xiphirx
It fixed it but now I am getting a warning

Code: Select all

 warning C4293: '<<' : shift count negative or too big, undefined behavior
line it pertains to

Code: Select all

if (ms && SDL_BUTTON(0) && state == 'T')
I'm not understanding this
Use SDL_BUTTON(n) for the n+1'th button
does that mean I use SDL_BUTTON(-1)?

Re: SDL_BUTTON Help

Posted: Thu Jun 17, 2010 3:55 pm
by dandymcgee
xiphirx wrote: I'm not understanding this
Use SDL_BUTTON(n) for the n+1'th button
does that mean I use SDL_BUTTON(-1)?
The first button (1st = 1) would be SDL_BUTTON(0); so unless you have a 0th button (you don't), no. ;)

It probably would have been better to say "Use SDL_BUTTON(n-1) for the nth button".

Re: SDL_BUTTON Help

Posted: Thu Jun 17, 2010 7:34 pm
by Ginto8
dandymcgee wrote:
xiphirx wrote: I'm not understanding this
Use SDL_BUTTON(n) for the n+1'th button
does that mean I use SDL_BUTTON(-1)?
The first button (1st = 1) would be SDL_BUTTON(0); so unless you have a 0th button (you don't), no. ;)

It probably would have been better to say "Use SDL_BUTTON(n-1) for the nth button".
yeah... that was a bit confusing, and dandy got it right with the restatement

Re: SDL_BUTTON Help

Posted: Fri Jun 18, 2010 12:03 pm
by xiphirx
Yeah, I got it now but its spitting this warning:
warning C4293: '<<' : shift count negative or too big, undefined behavior
line:

Code: Select all

if (ms && SDL_BUTTON(0) && state == 'T')
I know I can just ignore it, but I am trying to get rid of all warnings that come up :P

Re: SDL_BUTTON Help

Posted: Fri Jun 18, 2010 12:21 pm
by dandymcgee
Try this:

Code: Select all

if (ms && SDL_BUTTON(0) && (state == 'T'))
 

Re: SDL_BUTTON Help

Posted: Fri Jun 18, 2010 12:24 pm
by xiphirx
dandymcgee wrote:Try this:

Code: Select all

if (ms && SDL_BUTTON(0) && (state == 'T'))
 
Did not help. I think SDL_BUTTON(0) is causing it, because when I change it to 1, the warning disappears...
:/

Re: SDL_BUTTON Help

Posted: Fri Jun 18, 2010 1:55 pm
by Ginto8
xiphirx wrote:
dandymcgee wrote:Try this:

Code: Select all

if (ms && SDL_BUTTON(0) && (state == 'T'))
 
Did not help. I think SDL_BUTTON(0) is causing it, because when I change it to 1, the warning disappears...
:/
ok sorry I google'd around and it turns out that it IS SDL_BUTTON(n) for the n'th button. :lol:
Also, in case anyone's curious, this is the actual SDL_BUTTON macro:

Code: Select all

#define SDL_BUTTON(X)		(SDL_PRESSED<<(X-1))

Re: SDL_BUTTON Help

Posted: Fri Jun 18, 2010 5:13 pm
by xiphirx
Ginto8 wrote:
xiphirx wrote:
dandymcgee wrote:Try this:

Code: Select all

if (ms && SDL_BUTTON(0) && (state == 'T'))
 
Did not help. I think SDL_BUTTON(0) is causing it, because when I change it to 1, the warning disappears...
:/
ok sorry I google'd around and it turns out that it IS SDL_BUTTON(n) for the n'th button. :lol:
Also, in case anyone's curious, this is the actual SDL_BUTTON macro:

Code: Select all

#define SDL_BUTTON(X)		(SDL_PRESSED<<(X-1))
So then its 1 for LMB and 2 for MMB and 3 for RMB right? I changed accordingly and its acting up again, middle click triggers left click and middle click :(

Re: SDL_BUTTON Help

Posted: Sun Jun 20, 2010 11:07 am
by Ginto8
xiphirx wrote:
Ginto8 wrote:
xiphirx wrote:
dandymcgee wrote:Try this:

Code: Select all

if (ms && SDL_BUTTON(0) && (state == 'T'))
 
Did not help. I think SDL_BUTTON(0) is causing it, because when I change it to 1, the warning disappears...
:/
ok sorry I google'd around and it turns out that it IS SDL_BUTTON(n) for the n'th button. :lol:
Also, in case anyone's curious, this is the actual SDL_BUTTON macro:

Code: Select all

#define SDL_BUTTON(X)		(SDL_PRESSED<<(X-1))
So then its 1 for LMB and 2 for MMB and 3 for RMB right? I changed accordingly and its acting up again, middle click triggers left click and middle click :(
hmm idk that might be something with your mouse config, but according to the code I've seen it's 1 for left, 2 for middle, and 3 for right.