Object position Relative to the screen

Hi

Im using an SDL_Surface to draw the object player. Now i want to know his position relative to the screen for scrolling. Is there any way for me to know that?

Thanks!

You need to keep track of it. The best way to do this is to create a data structure (call it Sprite or something) that contains a pointer to the SDL_Surface, plus information about where and how it’s drawn, and use that data in your call to SDL_BlitSurface.________________________________
From: fernandeslacerda@hotmail.com (Tabris)
Subject: [SDL] Object position Relative to the screen

Hi

Im using an SDL_Surface to draw the object player. Now i want to know his position relative to the screen for scrolling. Is there any way for me to know that?

Thanks!

I’m not sure I understand. You have the X, Y position of the player, since
you have to draw that surface yourself. The screen is drawn at 0,0 with a
width, height of whatever size you created it.

Since you mention scrolling, I am guessing you have a larger background than
would fit on the screen and are drawing it to the screen with an offset to
give the effect of scrolling. Either that or you are using tiles to draw
the screen (typical in large scrolling worlds). In either case you can
figure out the players TRUE position in the world by taking your Background
X,Y and adding the player’s screen X,Y. Or if you need his screen position
given a 2D world position and background offset, you can just subtract his
position from the background offset.+===================================================+

| |

| ------------------------- |

| | | |

| | | |

| | P | |

| | | |

| | | |

| ------------------------- |

| |

| |

| |

+===================================================+

The larger rectangle represents the full size of your 2D world. Let’s
assume a world size of 1200x800

The smaller rectangle inside is the size of the screen. Let’s assume a
screen size of 640x480

Let’s say the player is at 600,360 in the world.

Assuming the screen is at world offset 150,100 then the player would be on
the SCREEN at 450,260.

The way we get this is to take the player world pos, subtract the screen
world offset.

Of course this may not be what you were asking, but since you didn’t give a
good explanation that is the best I can do with what you provided.

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Tabris
Sent: Monday, December 28, 2009 1:40 PM
To: sdl at lists.libsdl.org
Subject: [SDL] Object position Relative to the screen

Hi

Im using an SDL_Surface to draw the object player. Now i want to know his
position relative to the screen for scrolling. Is there any way for me to
know that?

Thanks!