SDL_BUTTON Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

SDL_BUTTON Help

Post 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 :/
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL_BUTTON Help

Post 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).
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL_BUTTON Help

Post 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?
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL_BUTTON Help

Post 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()
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: SDL_BUTTON Help

Post 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();
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL_BUTTON Help

Post 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)?
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: SDL_BUTTON Help

Post 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".
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL_BUTTON Help

Post 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
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL_BUTTON Help

Post 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
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: SDL_BUTTON Help

Post by dandymcgee »

Try this:

Code: Select all

if (ms && SDL_BUTTON(0) && (state == 'T'))
 
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL_BUTTON Help

Post 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...
:/
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL_BUTTON Help

Post 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))
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL_BUTTON Help

Post 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 :(
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL_BUTTON Help

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply