Using the mouse

Can anyone point me anywhere where mouse is used in SDL? I’m creating a menu in
my game and I want the user to be able to click on certain buttons. I know how
to capture input and use the keyboard but a bit confused on the mouse. Any help
would be appreciated. Thanks.

Mouse input is just another kind of SDL_Event. Check the documentation on SDL events for the details.>----- Original Message ----

From: Armond Sarkisian <armond.sarkisian at gmail.com>
Subject: [SDL] Using the mouse

Can anyone point me anywhere where mouse is used in SDL? I’m creating a menu in
my game and I want the user to be able to click on certain buttons. I know how
to capture input and use the keyboard but a bit confused on the mouse. Any help
would be appreciated. Thanks.

I do it something like I have below. Note that I’m using Sprig and SDL_ColorDef:

// Coords for the mouse
int mx, my;

// Functions to check the mouse against a rectangle
#ifndef _MouseAt
#define _MouseAt
static inline Uint8 mouseAt(int x1, int y1, int x2, int y2)
{
return (mx >= x1) && (mx <= x2) && (my >= y1) && (my <= y2);
}
static inline Uint8 mouseAt(SDL_Rect rect)
{
return (mx >= rect.x) && (mx <= rect.x + rect.w) && (my >= rect.y) && (my <= rect.y + rect.h);
}
static inline Uint8 mouseAt(SDL_Rect rect, int mx, int my)
{
return (mx >= rect.x) && (mx <= rect.x + rect.w) && (my >= rect.y) && (my <= rect.y + rect.h);
}
#endif

// Now, in my game function:
// Create a button position
SDL_Rect quitButton = SPG_MakeRect(50, 100, 90, 120);

while (!done)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEBUTTONDOWN))
{

            // Get the mouse coords

            SDL_GetMouseState(&mx, &my);

            if (event.button.button == SDL_BUTTON_LEFT)
            {
                   if (mouseAt(quitButton))
                   {
                        done = 1;
                   }
            }
        }
     }
// Clear screen and such
SDL_FillRect(screen, NULL, 0);
//  Draw the button
SPG_Rect(screen, quitButton.x, quitButton.y, quitButton.x + quitbutton.w, quitButton.y + quitButton.h, SDL_RED(screen));
// Show it
SDL_Flip(screen);

}

Hope it helps,
Jonny D> To: sdl at libsdl.org

From: armond.sarkisian at gmail.com
Date: Tue, 22 Jul 2008 01:24:48 +0000
Subject: [SDL] Using the mouse

Can anyone point me anywhere where mouse is used in SDL? I’m creating a menu in
my game and I want the user to be able to click on certain buttons. I know how
to capture input and use the keyboard but a bit confused on the mouse. Any help
would be appreciated. Thanks.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I do it something like I have below. Note that I’m
using Sprig and SDL_ColorDef:

// Coords for the mouse
int mx, my;

// Functions to check the mouse against a rectangle
#ifndef _MouseAt
#define _MouseAt
static inline Uint8 mouseAt(int x1, int y1, int x2, int y2)
{
return (mx >= x1) && (mx <= x2)
&& (my >= y1) && (my <= y2);
}

Although as an asides, for cross-compiler/platform compatibility having SDL’s ‘#include <begin_code.h>’ at the start of your header file (after all the other includes) and ‘#include <close_code.h>’ at the end (before the final ‘#endif’ defines and then undefines a couple of usefule macros.

This means that putting ‘static inline’ before inline functions in C will put in the appropriate replacement for ‘inline’ for the compiler you’re using while 'extern DECLSPEC ’ will deal with the syntax for setting up a file as either a .so or .dll.

It also defines SDLCALL (Windows and OS/2 specific), which forces C calling conventions for a function, and NULL quite nicely which I was once surprised to find is not standard. But then I was once surprised to find that conio.h was not standard :oD— On Tue, 7/22/08, Jonathan Dearborn wrote:

From: Jonathan Dearborn
Subject: Re: [SDL] Using the mouse
To: “A list for developers using the SDL library. (includes SDL-announce)”
Date: Tuesday, July 22, 2008, 2:02 AM

Wow thanks to all of you. You are all awesome!On Tue, Jul 22, 2008 at 5:23 AM, Paul Duffy wrote:

— On Tue, 7/22/08, Jonathan Dearborn wrote:

From: Jonathan Dearborn
Subject: Re: [SDL] Using the mouse
To: “A list for developers using the SDL library. (includes
SDL-announce)”
Date: Tuesday, July 22, 2008, 2:02 AM
I do it something like I have below. Note that I’m
using Sprig and SDL_ColorDef:

// Coords for the mouse
int mx, my;

// Functions to check the mouse against a rectangle
#ifndef _MouseAt
#define _MouseAt
static inline Uint8 mouseAt(int x1, int y1, int x2, int y2)
{
return (mx >= x1) && (mx <= x2)
&& (my >= y1) && (my <= y2);
}

Although as an asides, for cross-compiler/platform compatibility having
SDL’s ‘#include <begin_code.h>’ at the start of your header file (after all
the other includes) and ‘#include <close_code.h>’ at the end (before the
final ‘#endif’ defines and then undefines a couple of usefule macros.

This means that putting ‘static inline’ before inline functions in C
will put in the appropriate replacement for ‘inline’ for the compiler
you’re using while 'extern DECLSPEC ’ will deal
with the syntax for setting up a file as either a .so or .dll.

It also defines SDLCALL (Windows and OS/2 specific), which forces C calling
conventions for a function, and NULL quite nicely which I was once surprised
to find is not standard. But then I was once surprised to find that conio.h
was not standard :oD


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Every time I try to use an SPG function it says unidentified… am I missing
a header or something?On Mon, Jul 21, 2008 at 7:02 PM, Jonathan Dearborn wrote:

I do it something like I have below. Note that I’m using Sprig and
SDL_ColorDef:

// Coords for the mouse
int mx, my;

// Functions to check the mouse against a rectangle
#ifndef _MouseAt
#define _MouseAt
static inline Uint8 mouseAt(int x1, int y1, int x2, int y2)
{
return (mx >= x1) && (mx <= x2) && (my >= y1) && (my <= y2);
}
static inline Uint8 mouseAt(SDL_Rect rect)
{
return (mx >= rect.x) && (mx <= rect.x + rect.w) && (my >= rect.y) &&
(my <= rect.y + rect.h);
}
static inline Uint8 mouseAt(SDL_Rect rect, int mx, int my)
{
return (mx >= rect.x) && (mx <= rect.x + rect.w) && (my >= rect.y) &&
(my <= rect.y + rect.h);
}
#endif

// Now, in my game function:
// Create a button position
SDL_Rect quitButton = SPG_MakeRect(50, 100, 90, 120);

while (!done)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEBUTTONDOWN))
{
// Get the mouse coords
SDL_GetMouseState(&mx, &my);

            if (event.button.button == SDL_BUTTON_LEFT)
            {
                   if (mouseAt(quitButton))
                   {
                        done = 1;
                   }
            }
        }
     }
// Clear screen and such
SDL_FillRect(screen, NULL, 0);
//  Draw the button
SPG_Rect(screen, quitButton.x, quitButton.y, quitButton.x +

quitbutton.w, quitButton.y + quitButton.h, SDL_RED(screen));
// Show it
SDL_Flip(screen);
}

Hope it helps,
Jonny D


To: sdl at libsdl.org
From: @Armond_Sarkisian
Date: Tue, 22 Jul 2008 01:24:48 +0000
Subject: [SDL] Using the mouse

Can anyone point me anywhere where mouse is used in SDL? I’m creating a
menu in
my game and I want the user to be able to click on certain buttons. I
know how
to capture input and use the keyboard but a bit confused on the mouse.
Any help
would be appreciated. Thanks.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org