Newb?

Hello all,
I’m trying to move a pic of tux around the screen. I made a program for that.
It compiles, but when I run it tux won’t move and the window freezes, causing
me to kill it.

I have attached the code. If someone sees what is wrong please let me know.

Regards,
Rafik
-------------- next part --------------
A non-text attachment was scrubbed…
Name: tut2.cpp
Type: text/x-c++src
Size: 2084 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030323/6d1cf1bf/attachment-0007.cpp

while (done == 0)

{

SDL_Event event;
if (event.type == SDL_QUIT ) { done = 1; }

When and where does ‘event’ ever get set? :^)

You want to do something like:

SDL_WaitEvent(&event);

before you test event.type.

Or:

if (SDL_PollEvent(&event) > 0)

or

while (SDL_PollEvent(&event) > 0)

… around the code where you test event.type.

-bill!On Sun, Mar 23, 2003 at 07:58:39PM -0500, Rafik Rezzik wrote:

Hello all,
I’m trying to move a pic of tux around the screen. I made a program for that.
It compiles, but when I run it tux won’t move and the window freezes, causing
me to kill it.

I have attached the code. If someone sees what is wrong please let me know.


bill at newbreedsoftware.com Hire me!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/

Thanks Bill,

It all works exept for one part. When I move the image (of tux) on a red
background, the red smears. Is this because its not refreshing or what. What
can I do to fix it?

Regards,
RafikOn March 23, 2003 07:58 pm, Rafik Rezzik wrote:

Hello all,
I’m trying to move a pic of tux around the screen. I made a program for
that. It compiles, but when I run it tux won’t move and the window freezes,
causing me to kill it.

I have attached the code. If someone sees what is wrong please let me know.

Regards,
Rafik

Rafik Rezzik wrote:

It all works exept for one part. When I move the image (of tux) on a
red background, the red smears. Is this because its not refreshing or
what. What can I do to fix it?

first, you should sit down and think about it. :wink: sdl does of course
not provide you with layers, so you have to rescue the background before
you blit your tux to the screen surface. then when you want to move it
on, restore the backgraund, save the screen that will be coverd by tux
again and blit tux on that screen position. a.s.o.

best regards …
clemens

Clemens,

I don’t understand this layers stuff. Im just a 14 year old that does’nt know
much. I there something I can read about layers with SDL or to learn how to
do what you said about rescuing my background.

Regards,
RafikOn March 25, 2003 12:41 am, Clemens Kirchgatterer wrote:

Rafik Rezzik wrote:

It all works exept for one part. When I move the image (of tux) on a
red background, the red smears. Is this because its not refreshing or
what. What can I do to fix it?

first, you should sit down and think about it. :wink: sdl does of course
not provide you with layers, so you have to rescue the background before
you blit your tux to the screen surface. then when you want to move it
on, restore the backgraund, save the screen that will be coverd by tux
again and blit tux on that screen position. a.s.o.

best regards …
clemens


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Rafik,

Forget the layers stuff.

Putting an image on the surface of a screen is like using a stamp on a
peice of paper.

If you move your hand and stamp the paper again the old stamp is still
there.

What you want to do is rub out the old stamp first then stamp the new stamp.

To do this on a computer you want to copy a rectange of the background
back over the image you put on the screen. And then put another copy of
the image in a new postion. Refresh the screen and hey presto! It looks
like the image has moved.

If you need some more help on this just post again

Ok I got it to work slowly until a certain point, at wich the app gets killed.
I used the code that is attached. Did I do something backwards?

Thanks for anyhelp.

Regards,
Rafik
-------------- next part --------------
A non-text attachment was scrubbed…
Name: tut2.cpp
Type: text/x-c++src
Size: 2134 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030325/09aeaab0/attachment-0007.cppOn March 25, 2003 07:27 am, Robert Harrison wrote:

Rafik,

Forget the layers stuff.

Putting an image on the surface of a screen is like using a stamp on a
peice of paper.

If you move your hand and stamp the paper again the old stamp is still
there.

What you want to do is rub out the old stamp first then stamp the new
stamp.

To do this on a computer you want to copy a rectange of the background
back over the image you put on the screen. And then put another copy of
the image in a new postion. Refresh the screen and hey presto! It looks
like the image has moved.

If you need some more help on this just post again


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Rafik Rezzik wrote:

Ok I got it to work slowly until a certain point, at wich the app gets
killed. I used the code that is attached. Did I do something
backwards?

hmm, without having looked to close at your code i see the following
problems. first locking - unlocking the surface is unneeded as long as
you don’t try to access the pixel date within the surface directly. just
throw away that code. second you never blit from the screen to a backup
surface befor blitting the tux, but you should. i try to give you some
pseudo code:

SDL_Init();
screen = SDL_SetVideoMode();
bgr = IMG_Load(background);
tux = IMG_Load(tux);
bak = SDL_CreateSurface(); // for background backup
SDL_BlitSurface(background, screen);
SDL_Flip();
while (SDL_PollEvent()) {
SDL_Rect pos;

switch (event) {
	case LEFT: x--; break
	...
}
pos.x = x; pos.y = y; 
SDL_BlitSurface(screen, &pos, bak, NULL); 
SDL_BlitSurface(tux, NULL, screen, &pos);
SDL_UpdateRect(screen, pos.x, pos.y, pos.w, pos.h);

}

just don’t take this to serius, i have left out parameters sometimes,
but you should get the idea.

hope that helps …
clemens

Clemens Kirchgatterer wrote:

i hate to reply to my own mails but …

SDL_Init();
screen = SDL_SetVideoMode();
bgr = IMG_Load(background);
tux = IMG_Load(tux);
bak = SDL_CreateSurface(); // for background backup
SDL_BlitSurface(background, screen);
SDL_Flip();
while (SDL_PollEvent()) {
SDL_Rect pos;

switch (event) {
case LEFT: x–; break

}
pos.x = x; pos.y = y;
SDL_BlitSurface(screen, &pos, bak, NULL);
SDL_BlitSurface(tux, NULL, screen, &pos);
SDL_UpdateRect(screen, pos.x, pos.y, pos.w, pos.h);

// blit the backup back to the screen, but just don't update
SDL_BlitSurface(bak, NULL, screen, &pos);> }