I suppose you are making a tile-based map/level editor? Here is a simple example program for you, I think it does what you described. Make a "trial.jpg" image or substitute your own sprite name into the program and you can run it.
#include "DarkGDK.h"
void DarkGDK (void)
{
dbSyncOn();
dbSyncRate(60);
const int SPR = 1;
dbLoadImage("trial.jpg", SPR);
dbSprite(SPR, 100, 50, SPR);
bool bMouseDown = false;
bool bSpriteMoving = false;
int diffX = 0, diffY = 0;
const int GRIDX = dbSpriteWidth(SPR);
const int GRIDY = dbSpriteHeight(SPR);
const int GRIDNUMX = dbScreenWidth() / GRIDX;
const int GRIDNUMY = dbScreenHeight() / GRIDY;
while (LoopGDK())
{
// for testing, draw the grid
for (int i = 0; i < dbScreenWidth(); i += GRIDX) {
dbLine(i, 0, i, dbScreenHeight());
}
for (int i = 0; i < dbScreenHeight(); i += GRIDY) {
dbLine(0, i, dbScreenWidth(), i);
}
if (dbMouseClick()) {
bMouseDown = true;
} else {
// if the mouse button has just been released:
if (bMouseDown) {
bMouseDown = false;
// if no sprite is being dragged:
if (!bSpriteMoving) {
// test if the cursor is within the sprite
int mx = dbMouseX();
int my = dbMouseY();
if (mx >= dbSpriteX(SPR)
&& mx < dbSpriteX(SPR) + dbSpriteWidth(SPR)
&& my >= dbSpriteY(SPR)
&& my < dbSpriteY(SPR) + dbSpriteHeight(SPR)) {
// if yes, set up the sprite for dragging
bSpriteMoving = true;
// we can do this, but it's more intuitive to
// grab the sprite by the middle
//diffX = dbMouseX() - dbSpriteX(SPR);
//diffY = dbMouseY() - dbSpriteY(SPR);
diffX = dbSpriteWidth(SPR) / 2;
diffY = dbSpriteHeight(SPR) / 2;
}
// if there is a dragged sprite:
// release it and snap to grid
} else {
bSpriteMoving = false;
int x = (dbMouseX() / GRIDX) * GRIDX;
int y = (dbMouseY() / GRIDY) * GRIDY;
dbSprite(SPR, x, y, SPR);
}
}
}
// if the sprite is being dragged, move it with the mouse cursor,
// keeping a constant coordinate difference between the two
if (bSpriteMoving) {
dbSprite(SPR, dbMouseX() - diffX, dbMouseY() - diffY, SPR);
}
dbSync();
}
return;
}
This program uses a simple coordinate check to see if the mouse is within the sprite. Note that this won't work if there are several sprites on top of each other, because then you need to deal somehow with sprites covering each other, to avoid picking up the lower one. Also, this is for one sprite only, of course you will need to adapt it to your own environment, but since there can only be one moving sprite at a time, it shouldn't be too difficult to adapt. You just need to keep track of which sprite is moving at the moment (e.g. store its ID number instead of the boolean flag that I use in this program).