For those of you interested, I've just made a change in my Mouse class. I've made it a singleton so that only one instance of it exists but you can either create a reference to that one instance or a pointer to it in any scope. When you declare a Mouse object you declare it as a pointer or a reference and use the static Instance function to initialize it. Either of the two methods below work
Mouse *myMouse = Mouse::Instance();
Mouse &myMouse = *Mouse::Instance();
I prefer the reference type myself because the pointer variable requires remembering to use the -> operator.
What happens is that the first call to Instance() creates a new instance of the Mouse object. Thereafter, any call to Mouse::Instance() returns the address of the first instance.
The code only has minor changes from my first release of it. The code follows:
Mouse.h
/////////////////////////////////////////////////////
// a Mouse class for handling Mouse functions in //
// GDK //
/////////////////////////////////////////////////////
#pragma once
#ifndef _SMOUSE_H_LAC
#define _SMOUSE_H_LAC
#include "DarkGDK.h"
class Mouse
{
private:
int xPos; // x position
int yPos; // y position
int spriteDrag; // -1 if not being dragged
int mouseStat; // holds click status of all buttons
bool clicked1; // is button 1 clicked?
bool clicked2; // how 'bout button 2?
bool clicked3; // button 3?
bool clicked4; // can't leave out the remaining button
bool justClicked1; // true if just clicked
bool justClicked2; // just clicked is not the same
bool justClicked3; // as being in a down state
bool justClicked4; // it's a transition from up to down
bool justReleased1; // true if just release
bool justReleased2; // just released is not the same
bool justReleased3; // as being in an up state.
bool justReleased4; // it's a transition from down to up
bool visible; // is mouse visible
static Mouse *pInstance;
protected:
Mouse ()
{
mouseStat = dbMouseClick(); // catch status as the mouse is opened
xPos = dbMouseX(); // save initial mouse positions
yPos = dbMouseY();
spriteDrag = -1; // not initially dragging a sprite
visible = true;
clicked1 = mouseStat & 0x01; // update the click status of each button
clicked2 = mouseStat & 0x02;
clicked3 = mouseStat & 0x04;
clicked4 = mouseStat & 0x08;
}
public:
static Mouse *Instance ();
void Sync(void); // catches the mouse up on current status
int X(void); // returns the current mouse x position
int Y(void); // returns the current mouse y postion
bool ClickStat (int buttonnumber); // returns click status of specified button number
bool Released (int buttonnumber); // returns release status of specified button number
bool ButtonUp(int buttonnumber); // true = button up, false = button down
bool ButtonDown (int buttonnumber); // true = button down, false = button up
bool OverSprite (int spritenumber); // returns true if over the specified sprite
void DragSprite (int spritenumber); // tells the mouse to drag the sprite as long as button 1 is held
void DisplayStat (int xpos, int ypos); // Displays status of the mouse at the given position
bool Visible (bool tf); // set the visibility of the mouse on or off
bool Visible (void); // return the visibility of the mouse
};
#endif
Mouse.cpp
#include <Mouse.h>
Mouse * Mouse::Instance()
{
if (pInstance == NULL) {
pInstance = new Mouse;
}
return pInstance;
}
Mouse * Mouse::pInstance;
void Mouse::Sync(void)
{ // gets current state of the mouse and holds it
// the following are the transitory states and get set depending
// the up/down transition of a button
justClicked1 = justClicked2 = justClicked3 = justClicked4 = false;
justReleased1 = justReleased2 = justReleased3 = justReleased4 = false;
mouseStat = dbMouseClick(); // get the status of all buttons
xPos = dbMouseX(); // get the x/y position of the mouse
yPos = dbMouseY();
// get status of button 1
if (clicked1) { // mouse has been in a down state
if (mouseStat & 0x01){ // and remains down
// do nothing
}else{ // but if it's now up
clicked1 = false; // show it now up
justReleased1 = true; // and indicate just released
}
}else { // mouse has been up
if (mouseStat & 0x01){ // but is now down
clicked1 = true;
justClicked1 = true;
}else{ // mouse is still up
// do nothing
}
} // end else
// get status of button 2
if (clicked2) { // mouse has been in a down state
if (mouseStat & 0x02){ // and remains down
// do nothing
}else{ // but if it's now up
clicked2 = false; // show it now up
justReleased2 = true; // and indicate just released
}
}else { // mouse has been up
if (mouseStat & 0x02){ // but is now down
clicked2 = true;
justClicked2 = true;
}else{ // mouse is still up
// do nothing
}
} // end else
// get status of button 3
if (clicked3) { // mouse has been in a down state
if (mouseStat & 0x04){ // and remains down
// do nothing
}else{ // but if it's now up
clicked3 = false; // show it now up
justReleased3 = true; // and indicate just released
}
}else { // mouse has been up
if (mouseStat & 0x04){ // but is now down
clicked3 = true;
justClicked3 = true;
}else{ // mouse is still up
// do nothing
}
} // end else
// get status of button 4
if (clicked4) { // mouse has been in a down state
if (mouseStat & 0x08){ // and remains down
// do nothing
}else{ // but if it's now up
clicked4 = false; // show it now up
justReleased4 = true; // and indicate just released
}
}else { // mouse has been up
if (mouseStat & 0x08){ // but is now down
clicked4 = true;
justClicked4 = true;
}else{ // mouse is still up
// do nothing
}
}
if (spriteDrag != -1 && !clicked1) spriteDrag = -1;
if (spriteDrag != -1 && clicked1) dbSprite (spriteDrag, xPos, yPos, dbSpriteImage(spriteDrag));
}
int Mouse::X(void)
{
return xPos;
}
int Mouse::Y(void)
{
return yPos;
}
bool Mouse::ClickStat(int buttonnumber)
{
switch (buttonnumber){
case 1: return justClicked1;
break;
case 2: return justClicked2;
break;
case 3: return justClicked3;
break;
case 4: return justClicked4;
break;
default: return false;
}
}
bool Mouse::Released(int buttonnumber)
{
switch (buttonnumber) {
case 1: return justReleased1;
break;
case 2: return justReleased2;
break;
case 3: return justReleased3;
break;
case 4: return justReleased4;
break;
default: return false; // return false if invalid button
}
}
bool Mouse::OverSprite(int spritenumber)
{
int sprx = dbSpriteX(spritenumber) - dbSpriteOffsetX(spritenumber); // get the position and dimensions of the sprite
int spry = dbSpriteY(spritenumber) - dbSpriteOffsetY(spritenumber); // must account for offsets
int sprw = dbSpriteWidth(spritenumber); // get width and height
int sprh = dbSpriteHeight(spritenumber);
// see if mouse is between the extremes of the sprite
if (xPos >= sprx && xPos < sprx + sprw && yPos >= spry && yPos < spry + sprh) return true;
return false;
}
bool Mouse::ButtonUp(int buttonnumber)
{
switch (buttonnumber) { // return the status of the specified button
case 1: return !clicked1;
break;
case 2: return !clicked2;
break;
case 3: return !clicked3;
break;
case 4: return !clicked4;
break;
default: return false; // only if a bad button number is passed
}
}
bool Mouse::ButtonDown (int buttonnumber)
{
switch (buttonnumber) {
case 1: return clicked1;
break;
case 2: return clicked2;
break;
case 3: return clicked3;
break;
case 4: return clicked4;
break;
default: return false; // only if a bad button number is passed
}
}
void Mouse::DisplayStat(int xpos, int ypos)
{ // display the mouse position and the click of each button
// does not reflect down state so the numbers will just flash
char MouseString [128]; // buffer in which to format output
dbSetCursor(xpos, ypos); // move the cursor to the co-ordinates
// format the string with the data
sprintf (MouseString, "x = %d, y = %d %c %c %c %c", xPos, yPos, justClicked1 ? '1' : 'X',
justClicked2 ? '2' : 'X', justClicked3 ? '3' : 'X', justClicked4 ? '4' : 'X');
dbPrint (MouseString); // and output it
}
void Mouse::DragSprite(int spritenumber)
{
spriteDrag = spritenumber; // the sprite number to be dragged
}
bool Mouse::Visible(bool tf)
{
if (tf) dbShowMouse();
else dbHideMouse();
return visible = tf;
}bool Mouse::Visible(void)
{
return visible;
}
Lilith, Night Butterfly
I'm not a programmer but I play one in the office