Newbie question

Hi,

I have been playing around with SDL for a few days, trying out some 

examples and modifying it. But I am stuck at an error msg that happens
when I run the program:

$ gcc -Wall main.c -o teste -lSDL -lpthread
$ ./teste
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

I saw on other messages in the mailing lists about putting a lot of 

printfs to see if the functions return some error msg, I did it but even
so it does not show anything besides that message.

Can anyone help me out?

Thiago

//--------------------------------------------------------- The Code

#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>

SDL_Surface *screen, *scene, *doll;

// Vari?veis da personagem
SDL_Rect *pers;
short int doll_xvel = 0, doll_yvel = 0;

// Vari?veis de controle
SDL_Event event;
short int quit = 0;

int game_init()
{
if ((SDL_Init(SDL_INIT_VIDEO)) != 0)
{
fprintf(stderr, “N?o foi poss?vel inicializar o SDL: %s.\n”,
SDL_GetError());
exit(1);
}

if ((screen = SDL_SetVideoMode(800, 600, 24, SDL_HWSURFACE |
SDL_FULLSCREEN | SDL_DOUBLEBUF)) == 0)
fprintf(stderr, “Erro ao setar a v?deo: %s.\n”, SDL_GetError());

return(0);
}

int game_update()
{

// Carregando o cen?rio

if ((scene = SDL_LoadBMP(“cenario.bmp”)) == NULL)
fprintf(stderr, “Erro ao carregar o cen?rio: %s.\n”, SDL_GetError());

if ((SDL_BlitSurface(scene, NULL, screen, NULL)) < 0)
fprintf(stderr, “Erro ao blit o cen?rio: %s.\n”, SDL_GetError());
SDL_UpdateRect(screen, 0, 0, scene->w, scene->h);

// Carregando a personagem

if ((doll = SDL_LoadBMP(“pers.bmp”)) == NULL)
fprintf(stderr, “Erro ao carregar a personagem: %s.\n”, SDL_GetError());

pers->x = (screen->w / 2) - 80;
pers->y = (screen->h / 2) - 60;
pers->w = doll->w;
pers->h = doll->h;

if ((SDL_BlitSurface(doll, NULL, screen, pers)) < 0)
fprintf(stderr, “Erro ao blit a personagem: %s.\n”, SDL_GetError());
SDL_UpdateRect(screen, pers->x, pers->y, pers->w, pers->h);

return(0);
}

int game_quit()
{
SDL_FreeSurface(screen);
SDL_FreeSurface(scene);
SDL_FreeSurface(doll);
SDL_Quit();
return(0);
}

int main()
{
game_init();
game_update();
game_quit();
return(0);
}

You are trying to set a value to members of the SDL_Rect *pers when you have
not allocated memory for pers.On Tuesday 05 February 2002 07:49 pm, you wrote:

Hi,

I have been playing around with SDL for a few days, trying out some

examples and modifying it. But I am stuck at an error msg that happens
when I run the program:

$ gcc -Wall main.c -o teste -lSDL -lpthread
$ ./teste
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

I saw on other messages in the mailing lists about putting a lot of

printfs to see if the functions return some error msg, I did it but even
so it does not show anything besides that message.

Can anyone help me out?

Thiago

//--------------------------------------------------------- The Code

#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>

SDL_Surface *screen, *scene, *doll;

// Vari?veis da personagem
SDL_Rect *pers;
short int doll_xvel = 0, doll_yvel = 0;

// Vari?veis de controle
SDL_Event event;
short int quit = 0;

int game_init()
{
if ((SDL_Init(SDL_INIT_VIDEO)) != 0)
{
fprintf(stderr, “N?o foi poss?vel inicializar o SDL: %s.\n”,
SDL_GetError());
exit(1);
}

if ((screen = SDL_SetVideoMode(800, 600, 24, SDL_HWSURFACE |
SDL_FULLSCREEN | SDL_DOUBLEBUF)) == 0)
fprintf(stderr, “Erro ao setar a v?deo: %s.\n”, SDL_GetError());

return(0);
}

int game_update()
{

// Carregando o cen?rio

if ((scene = SDL_LoadBMP(“cenario.bmp”)) == NULL)
fprintf(stderr, “Erro ao carregar o cen?rio: %s.\n”, SDL_GetError());

if ((SDL_BlitSurface(scene, NULL, screen, NULL)) < 0)
fprintf(stderr, “Erro ao blit o cen?rio: %s.\n”, SDL_GetError());
SDL_UpdateRect(screen, 0, 0, scene->w, scene->h);

// Carregando a personagem

if ((doll = SDL_LoadBMP(“pers.bmp”)) == NULL)
fprintf(stderr, “Erro ao carregar a personagem: %s.\n”,
SDL_GetError());

pers->x = (screen->w / 2) - 80;
pers->y = (screen->h / 2) - 60;
pers->w = doll->w;
pers->h = doll->h;

if ((SDL_BlitSurface(doll, NULL, screen, pers)) < 0)
fprintf(stderr, “Erro ao blit a personagem: %s.\n”, SDL_GetError());
SDL_UpdateRect(screen, pers->x, pers->y, pers->w, pers->h);

return(0);
}

int game_quit()
{
SDL_FreeSurface(screen);
SDL_FreeSurface(scene);
SDL_FreeSurface(doll);
SDL_Quit();
return(0);
}

int main()
{
game_init();
game_update();
game_quit();
return(0);
}


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

“Thiago S. Concei??o” wrote in message
news:mailman.1012956722.5191.sdl at libsdl.org

pers->x = (screen->w / 2) - 80;
pers->y = (screen->h / 2) - 60;
pers->w = doll->w;
pers->h = doll->h;

‘pers’ isn’t pointing to anything, so you can’t modify the pointed-to
object. Either allocate it statically:

SDL_Rect r;
pers = &r;

or dynamically:

pers = malloc(sizeof(SDL_Rect)); // Don’t forget to ‘free’.

SDL_FreeSurface(screen);

Don’t do that either.–
Rainer Deyke | root at rainerdeyke.com | http://rainerdeyke.com

int game_quit()
{
SDL_FreeSurface(screen);
SDL_FreeSurface(scene);
SDL_FreeSurface(doll);
SDL_Quit();
return(0);
}

don’t free the screen

SDL_Rect *pers;

int main()
{
game_init();
game_update();
game_quit();
return(0);
}

pers is a pointer, you have to malloc it manually

i.e. pers = (SDL_Rect*)malloc(sizeof(SDL_Rect));