Sign in to follow this  

Don't understand something

This topic is 2029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there. I'm trying to make a simple SDL program.
After setting the video mode and loading the background and sprite circle, basically it takes the mouse pointer coordinates and draws the circle at that location.
This is the code
[source lang="cpp"]#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>

int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *screen=NULL;
SDL_Surface *circle=NULL;
SDL_Surface *backgrd=NULL;

SDL_Rect crc;
crc.w=160;
crc.h=120;
crc.x=crc.y=0;

circle=IMG_Load("circle.png");
backgrd=IMG_Load("img.bmp");

screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
bool run = true;
SDL_Rect mouser;

mouser.x=0;mouser.y=0;

SDL_Event event;


SDL_BlitSurface(backgrd,NULL,screen,NULL);
SDL_Flip(screen);

while(run)
{
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
run = false;
break;
case SDL_MOUSEBUTTONDOWN:

mouser.x=event.button.x;
mouser.y=event.button.y;
break;
}
}

SDL_BlitSurface(backgrd,NULL,screen,NULL);
SDL_BlitSurface(circle,NULL,backgrd,&mouser);
SDL_Flip(screen);
SDL_Delay(2000);
}

SDL_FreeSurface(backgrd);
SDL_FreeSurface(circle);
SDL_Quit();
return 0;
}[/source]
I've set the delay so I can see the sprite before it gets deleted.
The problem is, sprites do not get deleted, that is what I don't understand.
I mean , shouldn't they be "overwritten" each time I call SDL_Blitsurface at line 46 ?
Isn't that how games are rendered ? (Load background, load tiles, load sprite etc etc ) Edited by cold_heats_.--.

Share this post


Link to post
Share on other sites
You draw the background over top of the screen, then draw the circle over top of the background. At no time does your code actually clear what's already been drawn to the background :-)

Share this post


Link to post
Share on other sites
What ApochPiQ said. For what you're trying to do, you should be blitting the Sprite to the screen. So, when you blit the background again, it will overwrite the old background, and the sprite that was drawn there.

Share this post


Link to post
Share on other sites
Sign in to follow this