Moving image

I’m writing a very basic program to move an image, but I don’t know how to
erase the previous one without affecting the background. How can I do
this?–
Roger D. Vargas | "Humans have so many emotions, Artoo. And most
VQt Development Team | seem to cause them pain. I do not understand,
http://vqt.sourceforge.net | but I’m glad I have none. And yet – I can’t help
wondering sometimes… what it would be like."
C-3PO, Protocol Droid. Star Wars 2: World of Fire.

…so update the destination with the background and then your image every
frame.

I think you could also only redraw the part(s) of your background (using the
partial rectangle update features of SDL) that were touched by your image
but that becomes a bad idea as soon as you have a hole in your image.–

Olivier A. Dagenais - Software Architect and Developer

“Roger D. Vargas” wrote in message
news:Pine.LNX.4.10.10103220828330.674-100000 at pesca.esisc.colombus.cu

I’m writing a very basic program to move an image, but I don’t know how to
erase the previous one without affecting the background. How can I do
this?


Roger D. Vargas | "Humans have so many emotions, Artoo. And
most
VQt Development Team | seem to cause them pain. I do not understand,
http://vqt.sourceforge.net | but I’m glad I have none. And yet – I can’t
help
wondering sometimes… what it would be
like."
C-3PO, Protocol Droid. Star Wars 2: World of
Fire.

Here’s a simplistic approach that can be used. Someone else
will probably have some cool tricky way to do it.

Keep a copy of your background image in a separate surface. When
you go to update your image, copy a rect from the background copy
to your screen surface. The rect should be the rectangle describing
your previous image position.

This assumes that your background is not just a solid color. If it
is, you could always just redraw the background with that color provided
you are using double buffering.

Hope this isn’t too far off. It’s the first question I felt I could
probably answer :slight_smile:

JoshOn Thu, Mar 22, 2001 at 08:30:26AM -0500, Roger D. Vargas wrote:

I’m writing a very basic program to move an image, but I don’t know how to
erase the previous one without affecting the background. How can I do
this?


Roger D. Vargas | "Humans have so many emotions, Artoo. And most
VQt Development Team | seem to cause them pain. I do not understand,
http://vqt.sourceforge.net | but I’m glad I have none. And yet – I can’t help
wondering sometimes… what it would be like."
C-3PO, Protocol Droid. Star Wars 2: World of Fire.

I’m writing a very basic program to move an image, but I don’t know how to
erase the previous one without affecting the background. How can I do
this?

If the image you’re moving is being mixed with the background using alpha
blending, you’ll have to copy a portion of the original background image
back to your screen surface to erase.

I can’t think of anything clever that could be done to speed this up
without clever hardware that may or may not be available on your target
system(s). Anybody else?

Greg V. (hmaon)

    "It's today!" said Piglet.
"My favorite day," said Pooh.On Thu, 22 Mar 2001, Roger D. Vargas wrote:

As soon as you draw anything (your image in this case), you’ve already
affected your background. It doesn’t work like a sticker on top of something
else, where you can peel it off later to get you back to your original state.
Thus, you have to draw your background back over your image to get the
background back. So, generally the solution people take is either to have
the background around as an image they can draw to the screen, or else they
save the background before they wipe it out so they can restore it later.On Thursday 22 March 2001 05:30, you wrote:

I’m writing a very basic program to move an image, but I don’t know how to
erase the previous one without affecting the background. How can I do
this?

  1. Copy background rectangle where sprite will be
  2. Draw sprite
  3. Update sprite location
  4. Replace background rectangle
    (proceed to 1)

I’m writing a very basic program to move an image, but I don’t know how to
erase the previous one without affecting the background. How can I do
this?


Roger D. Vargas | "Humans have so many emotions, Artoo. And
most
VQt Development Team | seem to cause them pain. I do not understand,
http://vqt.sourceforge.net | but I’m glad I have none. And yet – I can’t
help
wondering sometimes… what it would be
like."
C-3PO, Protocol Droid. Star Wars 2: World of
Fire.>

  1. Copy background rectangle where sprite will be
  2. Draw sprite
  3. Update sprite location
  4. Replace background rectangle
    (proceed to 1)

Are you suggesting copying the background rectangle from the current
’screen’, or from the background surface itself?

If the first case, what if you move your sprite 1 pixel? Does the
’background copy’ end up copying a piece of the sprite’s last location?

In my current game, most movement takes place above a certain background,
and I just blit pieces of that surface back over the ‘dirty’ sprites. But
that gets me thinking, what if I moves something across the WHOLE screen,
and not just that special background? I’d have to have some notion of
what’s on the ‘screen’ rather than hard-coding which surface(s) my sprite
will be above at any given time. (i.e., my ‘playing field’ is surrounded
by a scoreboard, so my ‘erasing’ operations simply worry about blitting
pieces of the center background rather than testing to see if it’s over
more than one other sprite/surface, because so far they never are)

Jeez, now that I’ve already had my graphics code working for some time
already I guess I haven’t thought much about anything more sophisticated.

Lyle

I’m writing a very basic program to move an image, but I don’t know how to
erase the previous one without affecting the background. How can I do
this?


Roger D. Vargas | "Humans have so many emotions, Artoo. And
[…]On Thu, 22 Mar 2001, Matt Johnson wrote:

Well, the easy way is to make sure the entire screen is free from sprites
before copying anything at at all from it. (Oh, I’m assuming we’re talking
about software surfaces here - blitting from VRAM is not a good idea!)

Other approaches:

* Render the background from background image (or tiles + map data)
  to erase sprites. (Still remove *all* sprites first; then repaint
  all sprites in their now locations, or you'll get problews with
  overlapping sprites.)

* Simply render the whole screen from scratch every frame;
  background, sprites, everything.

The latter is pretty much the only sane way of doing it these days, short of
various VRAM->VRAM blitting tricks (which rely entirely on hardware
acceleration not to just slow things down to a crawl), if you have a
scrolling background.

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Thursday 22 March 2001 23:41, Lyle Hanson wrote:

On Thu, 22 Mar 2001, Matt Johnson wrote:

  1. Copy background rectangle where sprite will be
  2. Draw sprite
  3. Update sprite location
  4. Replace background rectangle
    (proceed to 1)

Are you suggesting copying the background rectangle from the current
’screen’, or from the background surface itself?

If the first case, what if you move your sprite 1 pixel? Does the
’background copy’ end up copying a piece of the sprite’s last location?

Actually, the very best example I’ve ever seen on how to do this simply (and
without re-blitting the entire background each frame) is SL’s Aliens demo:

http://www.libsdl.org/projects/aliens/index.html

I actually use this technique all the time myself.On Thu, 22 Mar 2001, you wrote:

I’m writing a very basic program to move an image, but I don’t know how to
erase the previous one without affecting the background. How can I do
this?


Sam “Criswell” Hart <@Sam_Hart> AIM, Yahoo!:
Homepage: < http://www.geekcomix.com/snh/ >
PGP Info: < http://www.geekcomix.com/snh/contact/ >
Advogato: < http://advogato.org/person/criswell/ >

Looks like the dirty rectangles technique. I’m using this as well in my
scrolling maze game. Sometimes this technique is better, sometimes not. As
always with everything in games, it all depends on what you are trying to do.On Friday 23 March 2001 08:03, you wrote:

On Thu, 22 Mar 2001, you wrote:

I’m writing a very basic program to move an image, but I don’t know how
to erase the previous one without affecting the background. How can I do
this?

Actually, the very best example I’ve ever seen on how to do this simply
(and without re-blitting the entire background each frame) is SL’s Aliens
demo:

http://www.libsdl.org/projects/aliens/index.html

I actually use this technique all the time myself.