Transparent PNG leaves copies of itself when blitting

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;

}

you must blit the background again (alternatively, use SDL_FillRect on its old location with the window’s background color) before blitting the surface in its new location.

Before blitting to the screen, you need to clear the screen surface with a
call to SDL_FillRect.

I thinkFrom: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Ploe
Sent: Thursday, November 26, 2009 8:38 AM
To: sdl at lists.libsdl.org
Subject: [SDL] Transparent PNG leaves copies of itself when blitting

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

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;

}

So I have to blit everything to the screen again that I don’t want to disappear?

Yes.

Usually you always have to clear the surface, and redraw everything that you
still want
to be visible.

If using 2D programming, you can use dirty rects to avoid redrawing
everything.–
Paulo

On Fri, Nov 27, 2009 at 6:02 PM, Ploe wrote:

So I have to blit everything to the screen again that I don’t want to
disappear?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You should’nt think as “surfaces instances”, because it is not what they
are. You aren’t really ‘moving’ a “surface instance” from one point of the
screen to another, you are just drawing the same image again and again in a
different position.

When you blit something on another surface, it will be blitted on the top of
what was already there, and that won’t be changed or erased unless you asks
it. That’s why you should draw the background again, before you draw your
surface. Notice when i said background, I mean everything that is below,
since for the surface being drawed, it is just ‘a background’.

2009/11/27 Paulo Pinto > Yes.

Usually you always have to clear the surface, and redraw everything that
you still want
to be visible.

If using 2D programming, you can use dirty rects to avoid redrawing
everything.


Paulo

On Fri, Nov 27, 2009 at 6:02 PM, Ploe wrote:

So I have to blit everything to the screen again that I don’t want to
disappear?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Leonardo