Easy C typecast/SDL problem

I’m just trying to plot one pixel directly to a surface (which has
already been locked, et al) with…

theSurface->pixels[ (( 640 * ( y - 1 )) + x ) ] = SDL_MapRGB

( theSurface->format, 255, 0, 0 );

…unfortunately, I just get reported the error “invalid use of void
expression” - I’ve tried to typecast the plot, but this merely changes
the error reported - any suggestions?

Thanks,

Mark

I’m just trying to plot one pixel directly to a surface (which has
already been locked, et al) with…

theSurface->pixels[ (( 640 * ( y - 1 )) + x ) ] = SDL_MapRGB
( theSurface->format, 255, 0, 0 );

…unfortunately, I just get reported the error “invalid use of void
expression” - I’ve tried to typecast the plot, but this merely changes
the error reported - any suggestions?

This doesn’t do what you think it does. the pixels value is a void*
because it can point to 8,16, 24, and 32 bit pixels. So you must cast it
to the size you need. It will almost work for 8, 16, and 32 bit pixels
with the correct type case for examle ((Uint16 *)theSurface->pixels).
But, of course, that won’t work for 24 bit pixels. Not to mention that
you have to take into account the value of pitch.

Take a look at the following code for an example of how to do it.

BTW, this is part of the code in sgl.c from my polyfonts library
http://gameprogrammer.com/game.html

//---------------------------------------------------
//
// Compute the address of a pixel in any surface
//

static inline void *pixelAddress(SDL_Surface *s, int x, int y)
{
return (void *)(((Uint8 *)s->pixels) +
(y * s->pitch) +
(x * s->format->BytesPerPixel));
}

//---------------------------------------------------
//
// Set a pixel in the surface
//

static inline void putPixel8(SDL_Surface *s, int x, int y, Uint32 pixel)
{
*(Uint8 *)pixelAddress(s, x, y) = pixel;
}

static inline void putPixel16(SDL_Surface *s, int x, int y, Uint32
pixel)
{
*(Uint16 *)pixelAddress(s, x, y) = pixel;
}

static inline void putPixel24(SDL_Surface *s, int x, int y, Uint32
pixel)
{
Uint8 *p = (Uint8 *)pixelAddress(s, x, y);

#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
pixel <<= 8;
#endif

memcpy(p, &pixel, 3);
}

static inline void putPixel32(SDL_Surface *s, int x, int y, Uint32
pixel)
{
*(Uint32 *)pixelAddress(s, x, y) = pixel;
}

static void putPixel(SDL_Surface *s, int x, int y, Uint32 color)
{
//
// first clip to the mode width and height
//
if (x < 0 || x >= (s->w) || y < 0 || y >= (s->h))
{
return;
}

switch (s->format->BytesPerPixel)
{
case 1:
putPixel8(s, x, y, color);
break;
case 2:
putPixel16(s, x, y, color);
break;
case 3:
putPixel24(s, x, y, color);
break;
case 4:
putPixel32(s, x, y, color);
break;
}
}

Thanks,

Mark

Hope that helps
	Bob PendletonOn Fri, 2003-10-10 at 14:59, Mark Bishop wrote:

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

±----------------------------------+

At 08:59 PM 10/10/2003 +0100, you wrote:

I’m just trying to plot one pixel directly to a surface (which has already
been locked, et al) with…

    theSurface->pixels[ (( 640 * ( y - 1 )) + x ) ] = SDL_MapRGB

( theSurface->format, 255, 0, 0 );

…unfortunately, I just get reported the error “invalid use of void
expression” - I’ve

SDL_Surface::pixels is of type void*, so not only are you trying to
dereference void*, you are also trying to index it (which doesnt work
because void has no specified size). I imagine what you are looking for is
something like this:

((unsigned*)theSurface->pixels)[ (( 640 * ( y - 1 )) + x ) ]

However that would be assuming that the pixel format is the same size as an
unsigned. Take a look here at the putpixel example in the docs:
http://sdldoc.csn.ul.ie/guidevideo.php#AEN115

HTH,

Neil.

…unfortunately, I just get reported the error “invalid use of void
expression” - I’ve tried to typecast the plot, but this merely changes
the error reported - any suggestions?

SDL_Surface::pixels is a void*…you cannot use it as an array by itself.

If you know it’s really pointing to a block of 32-bit pixels, you can do:

Uint32 *pixels = (Uint32 *) theSurface->pixels;
pixels[ (( 640 * ( y - 1 )) + x ) ] = blah;

–ryan.