ok, heres the deail simply…
even though we think of the screen as 2 dimensional, its only 1 dimension in
RAM.
lets say you had a Grid like this:****
*A
the coordinates of A is (2,1) in the above since the upper left is (0,0). In
RAM, since its really stored in a 1D array it would look like this:
*A
The index of A above is (6) since arrays start at 0.
So…what you need to do is convert from your 2D coordinates to the 1D
coordinates before you can access the pixel your trying to access. In our
grid, the width is 4, the width is also called the PITCH. Every surface in
SDL has a pitch. How you calculate the 1D coordinate is like this:
Index = y * Surface->Pitch + x;
if we plug the coordinates of A into the equation with our pitch of 4 we
get:
Index = 1 * 4 + 2 = 6
wow it works…amazing eh? Sometimes the pitch is bigger than the size of
your screen in width though, so never think “im in 800*600 so my pitch is
800, ill just use 800 instead of pitch”. Always multiply y by pitch,
otherwise your app wont always work.
this 1D array im speaking of, every surface has; It is Surface->pixels. If
you have 8 bit graphics youd do this (note there is no error checking
here!!!):
void DrawPixel(SDL_Surface *Surface, int x, int y, unsigned char Color)
{
unsigned char *Pixel;
Pixel=((char )Surface->Pixels)[ySurface->Pitch+x];
*Pixel=Color;
}
Heres a function also without error checking (for simplicity) to copy a
pixel (also assuming 8 bit color):
void CopyPixel(SDL_Surface *SrcSurface, int SrcX,int SrcY, SDL_Surface
*DestSurface, int DestX, int DestY)
{
unsigned char *Src, *Dest;
Src=((char )SrcSurface->Pixels)[SrcYSrcSurface->Pitch+SrcX];
Dest=((char )DestSurface->Pixels)[DestYDestSurface->Pitch+DestX];
*Dest=*Src;
}
One last point of interest for if you are using more than 8 bit (1 byte/256
colors) color. Your pitch is given in number of bytes. What that means is
if you are using 16 bit color lets say in 800x600 mode, your pitch will be
1600, not 800 since there are 2 bytes per pixel. So… heres a 16 bit draw
pixel function:
void DrawPixel16(SDL_Surface *Surface, int x, int y, unsigned short int
Color)
{
unsigned short int *Pixel;
Pixel=((unsigned short int )Surface->Pixels)[(ySurface->Pitch/2)+x];
*Pixel=Color;
}
and lets say you were in 32 bit color mode (4 bytes):
void DrawPixel16(SDL_Surface *Surface, int x, int y, unsigned int Color)
{
unsigned short int *Pixel;
Pixel=((unsigned int )Surface->Pixels)[(ySurface->Pitch/4)+x];
*Pixel=Color;
}
see how im dividing by the number of bytes? in drawing in 8 bit mode, youd
be dividing by 1 so you can leave that out.
Hope this gives you enough info to get started (:
Atrix
----- Original Message -----
From: nikkoz@gmx.net (Nikolaos Theologou)
To:
Sent: Wednesday, February 12, 2003 9:06 AM
Subject: Re: [SDL] Blitting
So i want to read a pixel from a source surface and put it onto
another surface, and then blit it.
How do i do that? I tried to find info on this (libgfx and rotozoom
and stuff) but all i cant find anything that explains it how its
actually done. Can anyone please give me an example of how this is
done?
Well… You just create another surface, using SDL_CreateRGBSurface(),
and then you get on with your stuff. Where’s the problem, more
specifically?
The problem is “get on with your stuff” =)
I have one surface with the image let us say *source,
and then the *destination surface wich i want to plot on.
And after that i want to blit *destination to the *screen.
I want to copy the pixel (for example x=2 & y=3) from *source and put it
on
the *destination (x=1 & y=1). And thats where i dont get it anymore. I
cant
find any docs on it, just some examples with alot of other confusing
stuff.
Is it something like source.x.y->pixel or what? Please help me to
straighten
this out. Any docs or simple examples. Or maby just type the syntax…
Thank
you!
-N
–
+++ GMX - Mail, Messaging & more http://www.gmx.net +++
Bitte l?cheln! Fotogalerie online mit GMX ohne eigene Homepage!
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl