Here is some code I posted for Keto(thread still on top page), its for 3D objects but sprites have the relevant commands also.
It may seem more complicated as I have split the collision for the two axis(x and y), you may not want to do that but it does have advantages. You should maybe just look at the X-Component first as the Y-Component incorperates jumping which you probably wont need.
Sorry I have not got time to do it all again for a top down 2D game.
#include "DarkGDK.h"
void DarkGDK()
{
bool jumping = false;
int jc = 0;
float gravity = -1;
int oldX = 0;
int oldY = 0;
int newX = 0;
int newY = 0;
dbSyncOn();
dbSyncRate(60);
dbLoadImage("MartialL.bmp", 1);
dbLoadImage("MartialR.bmp", 2);
dbLoadImage("MartialJ.bmp", 3);
dbLoadImage("ground.bmp", 4);
dbLoadImage("rock.bmp", 5);
dbLoadImage("sky.bmp", 6);
dbMakeObjectPlane(10, 800, 160);
dbMakeObjectBox(1, 10, 20, 0);
dbMakeObjectBox(2, 10, 10, 0);
dbMakeObjectBox(11, 200, 60, 0);
dbPositionObject(1, 0, 2, 0);
dbPositionObject(2, 30, -5, 0);
dbPositionObject(10, 0, 20, 1);
dbPositionObject(11, 0, -40, 0);
dbTextureObject(1, 1);
dbTextureObject(2, 5);
dbTextureObject(11, 4);
dbTextureObject(10, 6);
dbSetAmbientLight(100);
while(LoopGDK())
{
// We deal with collision along the x-axis and y-axis seperately
// This means checking for collision twice each loop, once after moving
// left and right, then once after moving up and down.
// We also check for a collision below feet eack time the user tries to jump
//******************** X-component ***********************//
//check for movement and re-position
if(dbRightKey())
{
newX = newX + 1;
dbTextureObject(1, 2);
}
if(dbLeftKey())
{
newX = newX - 1;
dbTextureObject(1, 1);
}
dbPositionObject(1, newX, dbObjectPositionY(1), dbObjectPositionZ(1));
//check for collision, put object back to where it was if there is a collision
if(dbObjectCollision(1, 0))
{
dbPositionObject(1, oldX, dbObjectPositionY(1), dbObjectPositionZ(1));
newX = oldX;
}
else
{
oldX = newX;
}
//*********************** Y-component **************************//
//check for movement and re-position
if((dbUpKey()) && (jumping == false))
{
//Check for floor below feet by repositioning object, checking collision
//then move it back up.
dbPositionObject(1, dbObjectPositionX(1), dbObjectPositionY(1) - 1, dbObjectPositionZ(1));
bool floorBelow = dbObjectCollision(1, 0);
if(floorBelow)
{
jumping = true;
jc = 15;
dbTextureObject(1, 3);
}
dbPositionObject(1, dbObjectPositionX(1), dbObjectPositionY(1) + 1, dbObjectPositionZ(1));
}
if(jumping)
{
newY = newY + 2;
jc = jc - 1;
}
if(jc == 0)
{
jumping = false;
}
newY = newY + gravity;
dbPositionObject(1, dbObjectPositionX(1), newY, dbObjectPositionZ(1));
//check for collision, put object back to where it was if there is a collision
if(dbObjectCollision(1, 0))
{
dbPositionObject(1, dbObjectPositionX(1), oldY, dbObjectPositionZ(1));
newY = oldY;
}
else
{
oldY = newY;
}
dbPositionCamera(dbObjectPositionX(1), dbObjectPositionY(1), -95);
dbSync();
}
}