Original Post
I am tring to learn and understand SDL and C. So far I managed to get this far with being able to take a 192x128 image with 32x48 sprites in it. Layed out in a 4x4 grid. Then on button press move the character around the screen. What I don't understand atm is the proper way to limit framerate. I have seen people on the forums metion this delta time using sdl.getticks etc... Atm my program is running way to fast. Its running an a AMD Athlon 64 X2 Dual Core 5400+ 2.61 Ghz, ATI Radeon HD 4670. Would someone on the forums explain to me how to add a framerate limiter to my game. I would like to understand it better. Here is what I have so far.
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
SDL_Surface *screen;
SDL_Event event;
int frame;
int maxframes;
int startanim;
//The frames per second
const int framesSecond = 20;
// Character Struct.
typedef struct{
int x, y; // Sprite Position
int scrollx, scrolly;
int xVel, yVel;
int dir;
}sakuratype;
sakuratype sakura;
// Draw Background.
background(char* string)
{
SDL_Surface *bkg;
SDL_Rect src, dest;
bkg = IMG_Load(string);
if (bkg == NULL) {
printf("Unable to load background.\n");
return 1;
}
src.x = 0;
src.y = 0;
src.w = bkg->w;
src.h = bkg->h;
dest.x = 0;
dest.y = 0;
dest.w = bkg->w;
dest.h = bkg->h;
SDL_BlitSurface(bkg, &src, screen, &dest);
SDL_FreeSurface(bkg);
}
// Draw Sprite
drawsprite(char* string, int row)
{
SDL_Surface *bkg;
SDL_Rect src, dest;
bkg = IMG_Load(string);
if (bkg == NULL) {
printf("Unable to load sprite.\n");
return 1;
}
src.x = frame * 32;
src.y = row * 48;
src.w = 32;
src.h = 48;
dest.x = sakura.x;
dest.y = sakura.y;
dest.w = 32;
dest.h = 48;
SDL_BlitSurface(bkg, &src, screen, &dest);
SDL_Flip(screen);
}
// Move Sakura
movesakura()
{
if( event.type == SDL_KEYDOWN ) {
switch( event.key.keysym.sym )
{
case SDLK_UP: sakura.yVel -= 32; sakura.dir = 3; break;
case SDLK_DOWN: sakura.yVel += 32; sakura.dir = 0; break;
case SDLK_LEFT: sakura.xVel -= 32; sakura.dir = 1; break;
case SDLK_RIGHT: sakura.xVel += 32; sakura.dir = 2; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: sakura.yVel += 32; break;
case SDLK_DOWN: sakura.yVel -= 32; break;
case SDLK_LEFT: sakura.xVel += 32; break;
case SDLK_RIGHT: sakura.xVel -= 32; break;
}
}
}
showsakura()
{
sakura.x += sakura.xVel;
sakura.y += sakura.yVel;
drawsprite("Graphics/000-sakura.bmp", sakura.dir);
}
// Main
int main(int argc, char *argv[])
{
/* Initialize SDL's video system and check for errors. */
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
/* Make sure SDL_Quit gets called when the program exits! */
atexit(SDL_Quit);
/* Set video mode */
screen = SDL_SetVideoMode(800,600,16,0);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
screen = SDL_GetVideoSurface();
sakura.x = 0;
sakura.y = 0;
sakura.dir = 0;
frame = 0;
maxframes = 3;
startanim = 0;
int quit;
quit = 0;
Uint8 *keystate;
while( quit == 0 ) {
while( SDL_PollEvent( &event ) )
{
movesakura();
if( event.type == SDL_QUIT )
{
//Quit the program
quit = 1;
}
}
background("TestBkg.bmp");
showsakura();
/* Ask SDL to update the entire screen. */
SDL_Flip(screen);
}
return 0;
}