I’m new to SDL and I decided to teach myself some basics. I created the window and managed to blit some surfaces to it easily enough but now I’m trying to have a surface that moves six times before the the program closes down. However the surface leaves instances of itself behind when blitted. Any help would be appreciated.
I’m writing in C, not C++.
Code:
#include “SDL/SDL.h”
#include “SDL/SDL_image.h”
#include <stdio.h>
SDL_Surface *loadimage(char *file)
{
SDL_Surface *loaded_img = NULL;
SDL_Surface *optimised_img = NULL;
loaded_img = IMG_Load(file);
if(loaded_img)
{
optimised_img = SDL_DisplayFormatAlpha(loaded_img);
SDL_FreeSurface(loaded_img);
return optimised_img;
}
printf("File not found.");
}
void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, clip, destination, &offset);
}
int main(int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
printf(“SDL could not be initialised.”);
return 1;
}
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *message = NULL;
SDL_Rect msRect;
int counter = 0;
screen = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE);
/*background = loadimage("background.bmp");
apply_surface(0, 0, background, screen);*/
message = loadimage("message.png");
msRect.x = 1024/2;
msRect.y = 1024/2;
apply_surface(msRect.x, msRect.y, message, screen, NULL);
msRect.h = message->w;
msRect.h = message->w;
SDL_Flip(screen);
while(counter < 6)
{
SDL_Delay(3000);
msRect.x -= 100;
msRect.y -= 100;
apply_surface(x, y, message, screen, NULL);
SDL_Flip(screen);
counter++;
}
SDL_FreeSurface(message);
SDL_FreeSurface(background);
SDL_Quit();
return 1;
}