Hey everyone,
I’ve been playing around with SDL and I’ve gotten the
Fatal signal: Segmentation Fault (SDL Parachute Deployed) error message.
It seems to happen when I use the SDL_MapRGB() function (commented stuff
out and it only seems to happen with SDL_MapRGB() ). It’s more or less
copied from the SDL docs so I’m not sure what’s going wrong. Anyone
able to help? Code is below.
–Marcin
#include <stdio.h>
#include <stdlib.h>
#include <SDL/sdl.h>
void init(SDL_Surface *screen)
{
if( SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, “Could not initialize SDL Video: %s. \n”,
SDL_GetError());
exit(-1);
}
screen = SDL_SetVideoMode(640,480,16,SDL_SWSURFACE);
if(!screen) {
fprintf(stderr, “Could not set 640x480x16bpp screen: %s\n”,
SDL_GetError());
exit(-1);
}
}
void draw_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x*bpp;
switch(bpp) {
case 1: /* 8 bit color */
*p = pixel;
break;
case 2: /* 16 bit color */
*(Uint16 *)p = pixel;
break;
case 3: /* 24 bit color -- ignore */
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4: *(Uint32 *)p = pixel;
break;
}
}
int main(int argc, char **argv)
{
Uint32 pixel;
SDL_Surface screen;
init(screen);
pixel = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00, 0xff);
/ if you comment out the line above, it doesn’t crash /
if(SDL_MUSTLOCK(screen)) {
if(SDL_LockSurface(screen) < 0) {
fprintf(stderr, “Error locking screen: %s”, SDL_GetError());
return -1;
}
}
draw_pixel(screen, 320,240, pixel); / if you comment this out, it
still crashes with the fatal sig */
if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
SDL_UpdateRect(screen, 320, 240, 1, 1);
return 0;
}