Skip to main content
gamedev.net
🔒 Locked

Drawing code causing me problems

Started by ToastFlambe Mar 13, 2005 at 10:52 PM 2 replies 950+ views
Original Post
ToastFlambe
ToastFlambe
I'm trying to write a program that draws a Pole Position-like landscape using a bitmap. The problem is I'm getting a Segmentation Fault error when I run the program. I've narrowed down the problem to to this chunk of code:

  if(!(TexelX < 0 || TexelX > map->w) && !(TexelY < 0 || TexelY > map->h))
                    {

                         fmt=map->format;
                         SDL_LockSurface(map);
                         pixel=*((Uint32*)map->pixels + y*map->pitch/4 + x);
                         SDL_UnlockSurface(map);

                         /* Get Red component */
                         temp=pixel&fmt->Rmask; /* Isolate red component */
                         temp=temp>>fmt->Rshift;/* Shift it down to 8-bit */
                         temp=temp<<fmt->Rloss; /* Expand to a full 8-bit number */
                         red=(Uint8)temp;

                         /* Get Green component */
                         temp=pixel&fmt->Gmask; /* Isolate green component */
                         temp=temp>>fmt->Gshift;/* Shift it down to 8-bit */
                         temp=temp<<fmt->Gloss; /* Expand to a full 8-bit number */
                         green=(Uint8)temp;

                         /* Get Blue component */
                         temp=pixel&fmt->Bmask; /* Isolate blue component */
                         temp=temp>>fmt->Bshift;/* Shift it down to 8-bit */
                         temp=temp<<fmt->Bloss; /* Expand to a full 8-bit number */
                         blue=(Uint8)temp;

                         SDL_LockSurface(screen);
                         DrawPixel(screen, x, y, red, green, blue);
                         SDL_UnlockSurface(screen);
                    }

I copied some of it from the SDL documentation but I did modify this line. pixel=*((Uint32*)map->pixels + y*map->pitch/4 + x); which used to be pixel=*((Uint32*)map->pixels); in an attempt to read the pixel data from a single pixel in the bitmap but I don't think I did that right. If this is wrong, does somebody know the right way to read data from a single pixel in a bitmap?
Kylotan
Kylotan
Are the values of x and y valid? Apart from that, I don't see anything glaringly wrong. You don't detail the DrawPixel function; does that use the same calculation?

(By the way, /* Expand to a full 8-bit number */ is an incorrect comment.)
ToastFlambe
ToastFlambe
Yes, the X and Y values should be valid, that's what the if statement is for.

Here is the DrawPixel function

void DrawPixel(SDL_Surface *screen, int x, int y,                                    Uint8 R, Uint8 G, Uint8 B){  Uint32 color = SDL_MapRGB(screen->format, R, G, B);  switch (screen->format->BytesPerPixel)  {    case 1: // Assuming 8-bpp      {        Uint8 *bufp;        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;        *bufp = color;      }      break;    case 2: // Probably 15-bpp or 16-bpp      {        Uint16 *bufp;        bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;        *bufp = color;      }      break;    case 3: // Slow 24-bpp mode, usually not used      {        Uint8 *bufp;        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;        if(SDL_BYTEORDER == SDL_LIL_ENDIAN)        {          bufp[0] = color;          bufp[1] = color >> 8;          bufp[2] = color >> 16;        } else {          bufp[2] = color;          bufp[1] = color >> 8;          bufp[0] = color >> 16;        }      }      break;    case 4: // Probably 32-bpp      {        Uint32 *bufp;        bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;        *bufp = color;      }      break;  }}


Its right off of the Cone#D tutorial. The program is only using the 32-bpp part.

I think I know what the problem is, I'm just having a hard time understanding it. The line I said I modified in my first post,

pixel=*((Uint32*)map->pixels + y*map->pitch/4 + x);

I modified to make it look like this line in the DrawPixel function,

bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;

since that is the line that pinpoints the location in memory for the pixel you want to display. The problem is that first line sets "pixel" equal to the value stored at the address of (Uint32*)blah blah, the second line does the correct thing. But I'm not sure what the "correct thing" is. In other words, I'm not quite sure how to get the RGB value of a single pixel of the "map" surface.


Kylotan
Kylotan
That's very well and good regarding the if statement, except that statement doesn't check the actual x and y values that you're drawing with. Perhaps you should change this:
pixel=*((Uint32*)map->pixels + y*map->pitch/4 + x);
to this:
pixel=*((Uint32*)map->pixels + TexelY*map->pitch/4 + TexelX);

The 2 lines you give in your last post are equivalent, except one yields a pointer and one yield's the pointer's value. If one leads to a crash, the other would too.

Maybe you could comment out some lines and isolate the exact line where it crashes. Or run in windowed mode where you can probably use the debugger.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.