David Olsen <jolynsbass hotmail.com> writes:>
Gary,
I’ll bet you’ve got your GetRelativeMouseState inside your event loop.
Moving the mouse creates an amazing number of events, and it is getting
"trapped" inside the event loop until you stop moving the mouse. My
suggestion is that you only read the relative mouse position(or absolute for
that matter) BEFORE or AFTER the event processing loop, and before your
screen drawing routine, so that the mouse position is only updated once per
video frame drawn. If you have a mouse button click event, you can also poll
the cursor location in the code for the click event, for extra precision.
If you post a little bit of your code, it would probably be clear to the
gurus around here(of which I am not!) exactly what’s happening.
-Dave
----- Original Message -----
From: “Gary Fletcher” <fleck12jobs hotmail.com>
To: <sdl libsdl.org>
Sent: Monday, May 23, 2005 12:36 AM
Subject: [SDL] Cursor control problems with SDL
Hi all.
I’m having problems with my cursor control modules. I’ve decided that my
engine
will take over cursor processing when ever it is instansiated. The reasons
for
this is that SDL (my API of choice at the moment) doesn’t automatically
provide
cursor processing in fullscreen mode.
Anyway. I’ve got a cursor class that is dervived from my base sprite
class. I’m
calling the cursor control during each iteration of the game loop,
clearing the
old position, calling pumpevents and the SDL_GetRelativeMouseState() for
the X
and Y deltas, adding the new X and Y to the current X and Y values and
then
redrawing the cursor sprite.
The old position of the cursor isn’t being removed, the new cursors Y
coordinates doesn’t seem to change and the cursor movement isn’t smooth at
all.
I’ve also noticed that if something is happening on screen, ie. I’m
drawing
something else, then that stops while the cursor is being moved. I’m
assuming
that the get mouse state functions wait until there is no mouse
movement/action
before returning?
Does anyone know of any tutorials regarding SDL and cursor controls. The
SDL
documentation is pretty scant for this?
I’m using SDL on windows with C++.
SDL mailing list
SDL libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
David.
You’re right I am. I’m not too hot with the SDL cursor event stuff so I’ll post
some code if thats okay, it’s missing some inherited stuff and animation stuff
and a few other bits but hope it’s enough.
Loop that’s calling cursor:
void SiSE::m_gameLoop(void)
{
/* The game loop. Loop until the game stack is empty /
while(!m_StateStack.empty())
{
if ((SDL_GetTicks() - m_Timer) >= FRAME_RATE)
{
/ In order to use the function pointed to on top of the stack
/
/ it needs to be called with an object of the appropriate type
/
/ In this case a SiSE object. Pass the function pointer /
/ to a member function that will execute it as (this->*funcPtr)()
*/
ExecuteFunc(m_StateStack.top().funcPtr);
// Do cursor processing
cerr << "About to process cursor" << endl;
cursor->mProcessCursor();
cerr << "Cursor processed" << endl;
m_Timer = SDL_GetTicks();
}
}
return;
}
Cursor class:
#ifndef INCLUDE_CURSOR_H
#define INCLUDE_CURSOR_H
#include “SiSEBaseSprite.h”
#include “SiSEVector2D.h”
const std::string CURSKEY = “CURSKEY”;
const std::string CLEARKEY = “CURSCLEARKEY”;
class SiSECursor : public SiSEBaseSprite
{
public:
SiSECursor(SDL_Surface *screen, float x = 0.0f, float y = 0.0f, float
speed = 0.0f);
~SiSECursor();
void mProcessCursor(void);
inline void mEnableCurs(void){mEnabled = true;}
inline void mDisableCurs(void){mEnabled = false;}
inline void mToggleCurs(void){mEnabled = !mEnabled;}
inline bool mIsEnabled(void){return mEnabled;}
inline void mSetClearKey(std::string& clrKey){mClearKey = clrKey;}
inline void mSetCursKey(std::string& cursKey){mCursAnimKey = cursKey;}
inline void mSetDefaults(void){mClearKey = CLEARKEY; mCursAnimKey =
CLEARKEY;}
private:
void mSetCursPos(void);
void mAddRelCursPos(void);
void mGetCursPos(void);
bool mEnabled;
int mouseAX;
int mouseAY;
int mouseRX;
int mouseRY;
int button;
std::string mClearKey;
std::string mCursAnimKey;
};
#endif
#include “SiSECursor.h”
#include “SDL/SDL.h”
enum MBUTTONS{NONPRESSED,LPRESSED,RPRESSED};
SiSECursor::SiSECursor(SDL_Surface *screen, float x, float y, float speed) :
SiSEBaseSprite(screen,x,y,speed)
{
SDL_ShowCursor(SDL_DISABLE);
mEnabled = true;
mouseAX=0;
mouseAY=0;
mouseRX=0;
mouseRY=0;
button=NONPRESSED;
mCursAnimKey = CURSKEY;
mClearKey = CLEARKEY;
}
SiSECursor::~SiSECursor()
{
mEnabled = false;
SDL_ShowCursor(SDL_ENABLE);
}
void SiSECursor::mProcessCursor()
{
if (mEnabled)
{
mGetCursPos();
mAddRelCursPos();
mAnimKey = mClearKey;
clearBG();
mAnimKey = mCursAnimKey;
draw();
}
return;
}
void SiSECursor::mAddRelCursPos()
{
std::cerr << "mAddRelCursPos() - mouseRX: " << mouseRX << std::endl;
std::cerr << "mAddRelCursPos() - mouseRY: " << mouseRY << std::endl;
xyAdd(mouseRX,mouseRY);
}
void SiSECursor::mSetCursPos()
{
xySet(mouseAX,mouseAY);
}
void SiSECursor::mGetCursPos()
{
mouseAX=0;
mouseAY=0;
mouseRX=0;
mouseRY=0;
button=NONPRESSED;
int height = GetHeight();
int width = GetWidth();
SDL_PumpEvents();
SDL_GetMouseState(&mouseAX,&mouseAY);
if (mouseAX < 0)
{
mouseAX = 0;
}
else if (mouseAX > height)
{
mouseAX = height;
}
if (mouseAY < 0)
{
mouseAY = 0;
}
else if (mouseAY > width)
{
mouseAY = width;
}
SDL_PumpEvents();
SDL_GetRelativeMouseState(&mouseRX,&mouseRY);
if (SDL_GetMouseState(NULL,NULL)&SDL_BUTTON(SDL_BUTTON_LEFT))
{
button = LPRESSED;
}
else if (SDL_GetMouseState(NULL,NULL)&SDL_BUTTON(SDL_BUTTON_RIGHT))
{
button = RPRESSED;
}
}