Coordinate System

Hi guys,
i want to change coordinate system from top left to center.
how is that possible?
i have search in some sites but didn’t get a clear answer:
http://www.gamedev.net/topic/475067-sdl-coordinate-system/

thanks.

Those answers are fine, but you seem to want something more specific. What
are you using SDL for? What parts of SDL are you using already? Are you
porting some prior code to SDL?

For instance, if all you are doing is blitting image data to the screen
with SDL_BlitSurface(), then you just need to write a function that flips
y-coordinates and then performs the blit call (as stated in the link you
provided). However, there are some other functions that use coordinate
data, so you would have to write such a wrapper function for each one that
you intend to use.

To flip the y-coordinate…
newY = surfaceHeight - oldY;

Jonny D

i’m using it for drawing some circles and lines. i have decided to use
SDL_draw library. i’m programming from scratch. the picture is attached as
explained in picture, circles have the same color and their radius change
every second. as you see there are some lines to draw but each line have
different pixel colors. these lines also change every second.On Mon, Apr 16, 2012 at 1:36 AM, Jonathan Dearborn wrote:

Those answers are fine, but you seem to want something more specific.
What are you using SDL for? What parts of SDL are you using already? Are
you porting some prior code to SDL?

For instance, if all you are doing is blitting image data to the screen
with SDL_BlitSurface(), then you just need to write a function that flips
y-coordinates and then performs the blit call (as stated in the link you
provided). However, there are some other functions that use coordinate
data, so you would have to write such a wrapper function for each one that
you intend to use.

To flip the y-coordinate…
newY = surfaceHeight - oldY;

Jonny D


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

-------------- next part --------------
A non-text attachment was scrubbed…
Name: pic2
Type: application/octet-stream
Size: 13490 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20120416/45434b20/attachment.obj

Hi guys,
i want to change coordinate system from top left to center.
how is that possible?

You need to add on your own level of coordinate abstraction.

So instead of your program calling Draw_Pixel, then you need to get your
program to call (e.g.) myDraw_Pixel and then write myDraw_Pixel to look
like this:

void myDraw_Pixel(SDL_Surface *super, Sint16 x, Sint16 y, Uint32 color)
{
x += myXoffset ;
y += myYoffset ;
Draw_Pixel (super, x, y, color) ;
}

and in your program declare globals for myXoffset and myYoffset and set
them acordingly.

You could implment it as a macro too.

This is pretty fundamental stuff. Don’t expect the computer to do
everything for you! E.g. in my BASIC interpreter environment, (0,0) is
bottom left, so to cater for this, I need to invert the Y axis, so my
"Draw_Pixel" code has this in it:

 x += xOrigin ;
 y += yOrigin ;
 y  = hgHeight - 1 - y ;

GordonOn Mon, 16 Apr 2012, Mohsen Jamali wrote:

*i think this the function that i should use to change the coordinate
system:
(if size of the screen is 800x600)
*void transXY(int &x, int &y){
if(x==0 && y==0){
x=400;
y=300;
return;
}
if(x<0){
if(y<0){
x=x+400;
y=300-y;
}else{
x=x+400;
y=300-y;
}
}else{
if(y<0){
x=x+400;
y=300-y;
}else{
x=x+400;
y=y-300;
}
}
}
*is there any better solution ?
i’m sure that there exist a better idea.

in some other libraries like GDI they just use : *

SetViewportOrg(0, 0);

*is there any simple function like this in SDl?*On Mon, Apr 16, 2012 at 12:30 PM, Gordon Henderson <gordon+sdl at drogon.net>wrote:

On Mon, 16 Apr 2012, Mohsen Jamali wrote:

Hi guys,

i want to change coordinate system from top left to center.
how is that possible?

You need to add on your own level of coordinate abstraction.

So instead of your program calling Draw_Pixel, then you need to get your
program to call (e.g.) myDraw_Pixel and then write myDraw_Pixel to look
like this:

void myDraw_Pixel(SDL_Surface *super, Sint16 x, Sint16 y, Uint32 color)
{
x += myXoffset ;
y += myYoffset ;
Draw_Pixel (super, x, y, color) ;
}

and in your program declare globals for myXoffset and myYoffset and set
them acordingly.

You could implment it as a macro too.

This is pretty fundamental stuff. Don’t expect the computer to do
everything for you! E.g. in my BASIC interpreter environment, (0,0) is
bottom left, so to cater for this, I need to invert the Y axis, so my
"Draw_Pixel" code has this in it:

x += xOrigin ;
y += yOrigin ;
y = hgHeight - 1 - y ;

Gordon

_____________**
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/**listinfo.cgi/sdl-libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

i think this the function that i should use to change the coordinate system:
(if size of the screen is 800x600)
void transXY(int &x, int &y){
? if(x==0 && y==0){
??? x=400;
??? y=300;
??? return;
? }
? if(x<0){
??? if(y<0){
??? x=x+400;
??? y=300-y;
??? }else{
??? x=x+400;
??? y=300-y;
??? }
? }else{
??? if(y<0){
??? x=x+400;
??? y=300-y;
??? }else{
??? x=x+400;
??? y=y-300;
??? }
? }
}
is there any better solution ?

Why not simply:

x -= 400;
y = (600 - y) - 300;

This assumes that your input x,y points are cartiasian positions and the screen
size is 800x600.

i’m sure that there exist a better idea.

in some other libraries like GDI they just use :

SetViewportOrg(0, 0);
is there any simple function like this in SDl?

No. I think GDI is a higher level library than SDL.

Regards,
HartleyOn Monday, April 16, 2012 1:53 PM, Mohsen Jamali wrote:

Why not simply:

x -= 400;

Oops…

x += 400;

y = (600 - y) - 300;

This assumes that your input x,y points are cartiasian positions and the screen
size is 800x600.

HartleyOn Monday, April 16, 2012 2:18 PM, H Hartley Sweeten wrote:

Look at your code:

  1. X < 0, Y < 0
    Xnew = X + 400
    Ynew = 300 - Y
  2. X < 0, Y > 0
  • Xnew = X + 400*
    Ynew = 300 - Y
  1. X > 0, Y < 0
    Xnew = X + 400
    Ynew = 300-y
  2. X > 0, Y > 0
    Xnew = X + 400
    Ynew = y - 300

The value of X does not depend on any of the if() statements, you
*always *compute
it as Xnew = X + 400.

  1. X < 0, Y < 0
    • Xnew = X + 400
      Ynew = 300 - Y
  1. X < 0, Y > 0
    Xnew = X + 400
    Ynew = 300 - Y
  2. X > 0, Y < 0
  • *Xnew = X + 400
    Ynew = 300 - Y
  1. X > 0, Y > 0
    Xnew = X + 400
    Ynew = Y - 300*

Judging by the fact that the first 3 are the same the 4th one isn’t, this
is probably a typo. You probably meant Ynew = 300 - Y. I don’t think this
will do what you want. You seem to want it so that when ‘Y’ increases, the
vertical pixel location increases. If so, then what you want is 300 - Y.

Explanation:
Let’s use ‘Yc’ to mean "Y coordinates based on the center of the screen"
and ‘Ys’ to mean “Y coordinates based on SDL’s coordinates”.

So:

Yc = 0 ---------> Ys = 300
Yc = 1 ---------> Ys = 299
Yc = -1 ---------> Ys = 301

600 - (Y + 300) = 600 - 300 - Y = 300 - Y

Your function should then be:

void transXY(int& x, int& y) {
x += 400;
y = 300 - y;
}

On Mon, Apr 16, 2012 at 3:52 PM, Mohsen Jamali <mohsen.aria at gmail.com>wrote:

*i think this the function that i should use to change the coordinate
system:
(if size of the screen is 800x600)
*void transXY(int &x, int &y){
if(x==0 && y==0){
x=400;
y=300;
return;
}
if(x<0){
if(y<0){
x=x+400;
y=300-y;
}else{
x=x+400;
y=300-y;
}
}else{
if(y<0){
x=x+400;
y=300-y;
}else{
x=x+400;
y=y-300;
}
}
}
*is there any better solution ?
i’m sure that there exist a better idea.

in some other libraries like GDI they just use : *

SetViewportOrg(0, 0);

is there any simple function like this in SDl?

On Mon, Apr 16, 2012 at 12:30 PM, Gordon Henderson <gordon+sdl at drogon.net>wrote:

On Mon, 16 Apr 2012, Mohsen Jamali wrote:

Hi guys,

i want to change coordinate system from top left to center.
how is that possible?

You need to add on your own level of coordinate abstraction.

So instead of your program calling Draw_Pixel, then you need to get your
program to call (e.g.) myDraw_Pixel and then write myDraw_Pixel to look
like this:

void myDraw_Pixel(SDL_Surface *super, Sint16 x, Sint16 y, Uint32 color)
{
x += myXoffset ;
y += myYoffset ;
Draw_Pixel (super, x, y, color) ;
}

and in your program declare globals for myXoffset and myYoffset and set
them acordingly.

You could implment it as a macro too.

This is pretty fundamental stuff. Don’t expect the computer to do
everything for you! E.g. in my BASIC interpreter environment, (0,0) is
bottom left, so to cater for this, I need to invert the Y axis, so my
"Draw_Pixel" code has this in it:

x += xOrigin ;
y += yOrigin ;
y = hgHeight - 1 - y ;

Gordon

_____________**
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/**listinfo.cgi/sdl-libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I don’t think this will do what you want.

That is to say, I don’t think the Y - 300 is what you want to do.

you are right Patrick, yeah it was a typo.
thanks to all for answers. :-xOn Tue, Apr 17, 2012 at 1:50 AM, Patrick Baggett <baggett.patrick at gmail.com>wrote:

Look at your code:

  1. X < 0, Y < 0
    Xnew = X + 400
    Ynew = 300 - Y
  2. X < 0, Y > 0
  • Xnew = X + 400*
    Ynew = 300 - Y
  1. X > 0, Y < 0
    Xnew = X + 400
    Ynew = 300-y
  2. X > 0, Y > 0
    Xnew = X + 400
    Ynew = y - 300

The value of X does not depend on any of the if() statements, you *always
*compute it as Xnew = X + 400.

  1. X < 0, Y < 0
    • Xnew = X + 400
      Ynew = 300 - Y
  1. X < 0, Y > 0
    Xnew = X + 400
    Ynew = 300 - Y
  2. X > 0, Y < 0
  • *Xnew = X + 400
    Ynew = 300 - Y
  1. X > 0, Y > 0
    Xnew = X + 400
    Ynew = Y - 300

Judging by the fact that the first 3 are the same the 4th one isn’t, this
is probably a typo. You probably meant Ynew = 300 - Y. I don’t think this
will do what you want. You seem to want it so that when ‘Y’ increases, the
vertical pixel location increases. If so, then what you want is 300 - Y.

Explanation:
Let’s use ‘Yc’ to mean “Y coordinates based on the center of the
screen” and ‘Ys’ to mean “Y coordinates based on SDL’s coordinates”.

So:

Yc = 0 ---------> Ys = 300
Yc = 1 ---------> Ys = 299
Yc = -1 ---------> Ys = 301

600 - (Y + 300) = 600 - 300 - Y = 300 - Y

Your function should then be:

void transXY(int& x, int& y) {
x += 400;
y = 300 - y;
}

On Mon, Apr 16, 2012 at 3:52 PM, Mohsen Jamali <@Mohsen_Jamali>wrote:

*i think this the function that i should use to change the coordinate
system:
(if size of the screen is 800x600)
*void transXY(int &x, int &y){
if(x==0 && y==0){
x=400;
y=300;
return;
}
if(x<0){
if(y<0){
x=x+400;
y=300-y;
}else{
x=x+400;
y=300-y;
}
}else{
if(y<0){
x=x+400;
y=300-y;
}else{
x=x+400;
y=y-300;
}
}
}
*is there any better solution ?
i’m sure that there exist a better idea.

in some other libraries like GDI they just use : *

SetViewportOrg(0, 0);

is there any simple function like this in SDl?

On Mon, Apr 16, 2012 at 12:30 PM, Gordon Henderson <gordon+sdl at drogon.net wrote:

On Mon, 16 Apr 2012, Mohsen Jamali wrote:

Hi guys,

i want to change coordinate system from top left to center.
how is that possible?

You need to add on your own level of coordinate abstraction.

So instead of your program calling Draw_Pixel, then you need to get your
program to call (e.g.) myDraw_Pixel and then write myDraw_Pixel to look
like this:

void myDraw_Pixel(SDL_Surface *super, Sint16 x, Sint16 y, Uint32 color)
{
x += myXoffset ;
y += myYoffset ;
Draw_Pixel (super, x, y, color) ;
}

and in your program declare globals for myXoffset and myYoffset and set
them acordingly.

You could implment it as a macro too.

This is pretty fundamental stuff. Don’t expect the computer to do
everything for you! E.g. in my BASIC interpreter environment, (0,0) is
bottom left, so to cater for this, I need to invert the Y axis, so my
"Draw_Pixel" code has this in it:

x += xOrigin ;
y += yOrigin ;
y = hgHeight - 1 - y ;

Gordon

_____________**
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/**listinfo.cgi/sdl-libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

To improve the function, you might want to remove the hard coded
resoltuion. For example, you could pass the “screen” surface as a
parameter, or use SDL_GetVideoSurface(). The implementation can use
screen->w and screen->h instead of 400 and 300. Alternatively, you
might create named constants, SCREEN_WIDTH and SCREEN_HEIGHT, which
can be used both in this function and as parameters to
SDL_SetVideoMode().

This way, if you end up changing the resolution you won’t need to
hunt down errant references to the old values.

good idea, i would use it. Thanks.On Tue, Apr 17, 2012 at 1:34 PM, Brian Barrett <brian.ripoff at gmail.com>wrote:

To improve the function, you might want to remove the hard coded
resoltuion. For example, you could pass the “screen” surface as a
parameter, or use SDL_GetVideoSurface(). The implementation can use
screen->w and screen->h instead of 400 and 300. Alternatively, you
might create named constants, SCREEN_WIDTH and SCREEN_HEIGHT, which
can be used both in this function and as parameters to
SDL_SetVideoMode().

This way, if you end up changing the resolution you won’t need to
hunt down errant references to the old values.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org