Advertisement

Some Mouse input?

Started by January 14, 2005 01:16 PM
1 comment, last by hms117 20 years, 1 month ago
Hello! In SDL how do you check for some muose input? Im talking about having a picture or a "hotspot" (if you dont understand what im meaning whit hotspot forget about it) So when you click on it some thing would happen. And by the way how do you show a label in SDL? Im hoping so much that i will hear from you! Regards Rasmus Hamberg
Well for mouse input you can use this:
int m_mx;int m_my;SDL_GetMouseState(&m_mx, &m_my);


That will store the current mouse position into mx and my. You can test for mouse presses with:
if( SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){}		if( SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)){}


Idealy though you will wrap it all into an input class so your code is not littered with those if statements.

Take a look at my old input class - I just posted the mouse sections for you to get a better idea of how it works.
class Immortal_Input{private:	int m_lmbdown;	int m_lmbclick;	int m_rmbdown;		int m_rmbclick;	int m_mx;	int m_my;public:	Immortal_Input();	~Immortal_Input();	void Update();	inline bool LMBClick() const;	inline bool RMBClick() const;		inline bool LMBDown() const;	inline bool RMBDown() const;		inline int MouseX() const;	inline int MouseY() const;};


[source lang = "cpp"]Immortal_Input::Immortal_Input(){	m_lmbclick = 0;	m_rmbclick = 0;	m_mx = 0;	m_my = 0;	m_lmbdown = 0;	m_rmbdown = 0;}Immortal_Input::~Immortal_Input(){}inline bool Immortal_Input::LMBClick() const{	return (m_lmbclick == 2);}inline bool Immortal_Input::RMBClick() const{	return (m_rmbclick == 2);}inline bool Immortal_Input::LMBDown() const{	return (m_lmbdown == 1);}inline bool Immortal_Input::RMBDown() const{	return (m_rmbdown == 1);}inline int Immortal_Input::MouseX() const{	return m_mx;}inline int Immortal_Input::MouseY() const{	return m_my;}void Immortal_Input::Update(){	if(m_lmbclick != 1)		m_lmbclick = 0;	if(m_rmbclick != 1)		m_rmbclick = 0;	m_keys = SDL_GetKeyState(NULL);		SDL_GetMouseState(&m_mx, &m_my);		if( SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT))	{		SDL_GetMouseState(&m_mx, &m_my);		m_lmbdown = 1;		m_lmbclick = 1;	}	else	{		if(m_lmbclick == 1)			m_lmbclick = 2;		m_lmbdown = 0;	}		if( SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT))	{		SDL_GetMouseState(&m_mx, &m_my);		m_rmbdown = 1;		m_rmbclick = 1;	}	else	{		if(m_rmbclick == 1)			m_rmbclick  = 2;		m_rmbdown = 0;	}}


Basicaly you just would use:
Immortal_Input input;

in your update loop:
input.Update();


Then when you are handling the updating:
if( input.LMBClick() ){   ... code ...}if( input.LMBDown() ){   ... code ...}


There is a difference between the Down and Click functions. Click will only return true per click - release of the mouse button. Down will return true while the mouse button is pressed down. This code is modeled from Cone3D's lesson 6.

Now for your hot spot question. Imagine if you had some rectangle that represented an image. It's coordinates were X = 0 ,Y = 0, W= 100, H = 100. YOu know the four corners are 0,0 100,0 100,100 and 0,100. Now everytime there is a mouseclick, you will use the MouseX() function and see if it is in that range of [0-100] for the width. If it is then you test to see if the MouseY() is in the height range of [0-100]. If it is, then you know the user clicked on the image. For hoovering - all you have to do is do the same thing, except instead of checking everytime a mouse button is clicked, you will check every frame. I hope this helps, if you need some more guidance, feel free to ask. As for your label question, you will have to use fonts. Take a look at this Cone3D tutorial on using them.

- Drew
Advertisement
WOW thank you for a so good repley!

This topic is closed to new replies.

Advertisement