How to remove the left overs of SDL_FillRect?

Started by
5 comments, last by Servant of the Lord 12 years, 2 months ago
Hello I'm trying to figure out how to remove this black crap off my OrangeGhostKnight Surface which you can see clearly in this picture below

UG6jH.png

what would be your suggestions and tips

I've already tried using


SDL_FillRect(Screen,&OrangeGhostKnightRect,(0,0,0));
SDL_BlitSurface(OrangeGhostKnightSurf,NULL,Screen,&OrangeGhostKnightRect);
SDL_SetColorKey(OrangeGhostKnightSurf, SDL_SRCCOLORKEY |SDL_RLEACCEL, SDL_MapRGB(OrangeGhostKnightSurf->format,0, 0, 0));
SDL_Flip(Screen);


SDL_SetColorKey(); Clearly doesn't work in this problem

Any suggestions or tips or am I doing this wrong ?
Advertisement
With SDL_FillRect your are explicitly creating that black rectangle.
And also try setting the color key before you blit the surface.
its still not working ;(



SDL_SetColorKey(OrangeGhostKnightSurf, SDL_SRCCOLORKEY |SDL_RLEACCEL,BlackColor);


what are like the common problems for SDL_FillRect not working?
What I meant is that you should entirely remove [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

SDL_FillRect from your code. (or do you want to have a rectangle there?)

[/font]

[font="helvetica, arial, verdana, tahoma, sans-serif"][size="2"][color="#282828"]What does the [/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

OrangeGhostKnight surface looks like? (or the base image) Is it black in the background?[/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Try this:[/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[/font]


[color=#000000][size=2]

SDL_SetColorKey

[color=#666600][size=2]

(

[color=#660066][size=2]

OrangeGhostKnightSurf

[color=#666600][size=2]

,

[color=#000000][size=2]

SDL_SRCCOLORKEY

[color=#666600][size=2]

|

[color=#000000][size=2]

SDL_RLEACCEL

[color=#666600][size=2]

,

[color=#000000][size=2]

SDL_MapRGB

[color=#666600][size=2]

(

[color=#660066][size=2]

OrangeGhostKnightSurf

[color=#666600][size=2]

->

[color=#000000][size=2]

format

[color=#666600][size=2]

,

[color=#006666][size=2]

0

[color=#666600][size=2]

,

[color=#000000][size=2][color=#006666][size=2]

0

[color=#666600][size=2]

,

[color=#000000][size=2][color=#006666][size=2]

0

[color=#666600][size=2]

));


[color=#000000][size=2]SDL_BlitSurface[color=#666600][size=2]([color=#660066][size=2]OrangeGhostKnightSurf[color=#666600][size=2],[color=#000000][size=2]NULL[color=#666600][size=2],[color=#660066][size=2]Screen[color=#666600][size=2],&[color=#660066][size=2]OrangeGhostKnightRect[color=#666600][size=2]);


[color=#000000][size=2]SDL_Flip[color=#666600][size=2]([color=#660066][size=2]Screen[color=#666600][size=2]);


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[/font]
What I meant is that you should entirely remove SDL_FillRect from your code. (or do you want to have a rectangle there?)

What does the OrangeGhostKnight surface look like? (or the base image) Is it black in the background?

Try this:


SDL_BlitSurface(OrangeGhostKnightSurf,NULL,Screen,&OrangeGhostKnightRect);
SDL_SetColorKey(OrangeGhostKnightSurf, SDL_SRCCOLORKEY |SDL_RLEACCEL, SDL_MapRGB(OrangeGhostKnightSurf->format,0, 0, 0));
SDL_Flip(Screen);
the orange Coloured character in the image on my first post is that ghostknight and what I want to do is basically change his frames without it overlaying constantly like if it were a trail of images so I'm filling that Orange Coloured character rectangle and then trying to get rid of it because the fill colour is still showing as you can see in the above image

what I want to do is basically change his frames without it overlaying constantly like if it were a trail of images


There is no efficient way to do this. As soon as you write pixels to the Screen, the previous pixels are lost. You can track what used to be behind the sprite by copying those pixels first, then drawing them over your sprite with them afterwards. However, reading from video memory is quite slow (read: slower than redrawing the entire screen by several orders of magnitude), and keeping track of the "background pixels" quickly becomes complicated once you have sprites intersecting.

The easiest (and fastest) solution that most games go for is to just redraw the entire scene on top of what was already there.

the orange Coloured character in the image on my first post is that ghostknight and what I want to do is basically change his frames without it overlaying constantly like if it were a trail of images so I'm filling that Orange Coloured character rectangle and then trying to get rid of it because the fill colour is still showing as you can see in the above image


Think about this for a second: (this is what you are currently doing)

1) You draw the background.
2) You draw your character.
3) You draw a black rectangle to erase your character.
4) You draw your character again.
5) You complain there is a black rectangle that you just drew two steps before.

What you are wanting to do is "undraw" your character... but that's not possible. You can't undraw something. Instead, you need to draw over it. What you have to do, is draw over the character with the background, and then draw the character again afresh.
You can do that in two different ways:

The easy way: (every loop/frame of your game)
1) Draw the entire background.
2) Draw all the objects over the background.
3) Apply to screen.

Basically, your drawing code looks like this:
1) Erase the entire screen. (so no images from the previous frame shows through)
2) Draw the background.
3) Draw any trees or objects North of the knight.
4) Draw the knight. (so the knight appears over those trees)
5) Draw any trees or objects south of the Knight (so the southern trees appear over the knight, letting him "walk behind" them)
6) Draw the GUI (buttons, menus, healthbars, text, etc...)

Except, you don't actually need to do step 1 ("Erase the entire screen"), because step 2 covers the entire screen anyway.

The hard way: (Don't do this)
1) Draw the background once only.
2) Every time an object moves, calculate and redraw just the piece of the background that covers where the object used to be.
3) Redraw the object.
4) Apply to the screen.

This is called 'Dirty rectangles'. Don't do it like this - it complicates things, and makes things unnecessarily hard while bringing you a benefit that you won't need.
The point of dirty rectangles is to get a little extra speed - but you already have enough speed in your computer, so no matter how slow you think your game is running, the solution is to find the real slowdown, not to use dirty rectangles. There are much easier, safer, and stabler ways to boost a program's speed.

This is how your game should be laid out.

This topic is closed to new replies.

Advertisement