Pointers in SDL

Hello,

Could anybody tell me why I see a BLUE screen with this code when I expected
to see a GREEN one?

SDL_Surface *superficieImagen, *superficieAuxiliar;

int ancho = 640;
int alto = 480;

int main() {

superficieImagen = SDL_SetVideoMode(ancho, alto, 32, SDL_SWSURFACE);
if (superficieImagen == NULL) {
fprintf(stderr, “No se puede establecer el modo de video: %s\n”,
SDL_GetError());
exit(1);
}
superficieAuxiliar = SDL_CreateRGBSurface(SDL_SWSURFACE, ancho, alto, 32,
superficieImagen->format->Rmask, superficieImagen->format->Gmask,
superficieImagen->format->Bmask, superficieImagen->format->Amask);

if (SDL_MUSTLOCK(superficieImagen)) {
if (SDL_LockSurface(superficieImagen) < 0) {
fprintf(stderr, “No puedo bloquear la pantalla: %s\n”, SDL_GetError());
return -1;
}
}

for (int y=0;y<alto;y++) {
for (int x=0;x<ancho;x++) {
*((Uint32 )superficieImagen->pixels + yancho + x) = 0xFF0000FF;
}
}

if (SDL_MUSTLOCK(superficieImagen)) {
SDL_UnlockSurface(superficieImagen);
}

if (SDL_MUSTLOCK(superficieAuxiliar)) {
if (SDL_LockSurface(superficieAuxiliar) < 0) {
fprintf(stderr, “No puedo bloquear la pantalla: %s\n”, SDL_GetError());
return -1;
}
}

for (int y=0;y<alto;y++) {
for (int x=0;x<ancho;x++) {
*((Uint32 )superficieAuxiliar->pixels + yancho + x) = 0xFF00FF00;
}
}

if (SDL_MUSTLOCK(superficieAuxiliar)) {
SDL_UnlockSurface(superficieAuxiliar);
}

superficieImagen->pixels = superficieAuxiliar->pixels;

SDL_UpdateRect(superficieImagen, 0, 0, 0, 0);
pause();
return 0;
}

It seems that the instruction “superficieImagen->pixels =
superficieAuxiliar->pixels;” don’t do anything. I know that I can do this
with SDL_BlitSurface(superficieAuxiliar, NULL, superficieImagen, NULL) and
work correctly, but why it doesn’t work what I do?

ThanQ

Quoth “Manuel D. Lago Reguera” , on 2004-07-21 18:49:58 +0200:

It seems that the instruction “superficieImagen->pixels =
superficieAuxiliar->pixels;” don’t do anything. I know that I can do this
with SDL_BlitSurface(superficieAuxiliar, NULL, superficieImagen, NULL) and
work correctly, but why it doesn’t work what I do?

If I interpret the SDL interface correctly, you’re not supposed to set the
pixels pointer yourself. If you do, the results are undefined. I’m not
precisely sure how this works internally, but it would seem to be a bad
idea at any rate.

Note also that the width may not be the same as the distance between rows
as stored in memory; the latter is called the “pitch” and is expressed in
bytes in the eponymous member of the SDL_Surface structure.

ThanQ

—> Drake Wilson
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040721/920e24d6/attachment.pgp

[snip]

It seems that the instruction “superficieImagen->pixels =
superficieAuxiliar->pixels;” don’t do anything. I know that I can do this
with SDL_BlitSurface(superficieAuxiliar, NULL, superficieImagen, NULL) and
work correctly, but why it doesn’t work what I do?

You shouldn’t change the ->pixels value, as Drake already mentioned. There are numerous reasons not to do that, including the fact that it’s not supported by the SDL spec, and the fact that this can have odd side effects.

Why don’t you simply use two pointers to SDL_Surfaces ? These could be switched to your liking.

Stephane

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stephane Marchesin wrote:
| You shouldn’t change the ->pixels value, as Drake already mentioned.
There are numerous reasons not to do that, including the fact that it’s
not supported by the SDL spec, and the fact that this can have odd side
effects.
|
| Why don’t you simply use two pointers to SDL_Surfaces ? These could be
switched to your liking.
|
| Stephane

I think he/she meant to copy the data across, not the pointer.

Chris E.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFA/qzQRgD2xPOqY+URArgJAKCJDIypr5F0EAZUY74gL58qM33WhQCgqwta
AeonRZkbU/Kk5Be8H11QPbA=
=81ne
-----END PGP SIGNATURE-----

It seems that the instruction “superficieImagen->pixels =
superficieAuxiliar->pixels;” don’t do anything. I know that I can do
this
with SDL_BlitSurface(superficieAuxiliar, NULL, superficieImagen, NULL)
and
work correctly, but why it doesn’t work what I do?

Very simple really, ->pixels member of the SDL_Surface structure is
really just for your personal reference. In other words, ->pixels tells
YOU where the pixel data is stored, it doesn’t tell SDL where the pixel
data is stored.On Jul 21, 2004, at 12:49 PM, Manuel D. Lago Reguera wrote:

Hi there

0xFF0000FF is blue.
If it is still blue, maybe your function to redraw things is not working
properly.

Hope I helped,

J InacioOn Wed, 21 Jul 2004 18:49:58 +0200, Manuel D. Lago Reguera wrote:

Using Opera’s revolutionary e-mail client: http://www.opera.com/m2/

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

j inacio wrote:
| On Wed, 21 Jul 2004 18:49:58 +0200, Manuel D. Lago Reguera
| wrote:
|
| Hi there
|
| 0xFF0000FF is blue.
| If it is still blue, maybe your function to redraw things is not
| working properly.
|
| Hope I helped,
|
| J Inacio

All of those colour values are assuming specific display formats, which
can vary with video mode.
You should use SDL_MapRGB to obtain the colour code.

Chris E.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFA/3A4RgD2xPOqY+URAmFWAJ4zmX8bYwICpmrPc6NDaQmegVHgawCfTRF0
iH7row1cFIGGSjDb99JZKGM=
=jp+M
-----END PGP SIGNATURE-----