Segfault with SDL_BlitSurface() in loop

I was given a char *map that contains the different elements that you can find on screen. Those elements are e, for electricity, c, for coin, and _, for empty space.

I’ve done a loop that will go through the array and will detect whenever I have one of those characters. Here’s the problem though, when I look to blit the corresponding sprite for each letter my program segfaults.

When I took out SDL_BlitSurface() the program works correctly, doing the amount of loops necesary.

I feel that I’m missing something before SDL_BlitSurface()

Here’s the function where everything goes wrong:

void            print_map()
  int           i;
  SDL_Rect      pos;
  int           x;
  int           y;

  if ((th_client[1].coin = IMG_Load("./images/coin.png")) == NULL)
  y = 0;
  i = 0;
  puts("map loop");
  while (y != 10)
      puts("y loop");
      pos.y = (y * 10) + 5;
      x = 0;
      while (x != 95)
          pos.x = (x * 10) + 5;
          printf("pos x: %dn", pos.x);
          if (th_client[1].map[i] == 'c')
              printf("char : %cn", th_client[1].map[i]);
              if ((SDL_BlitSurface(th_client[i].screen, NULL,th_client[i].coin, &pos)) == -1) // <- it blows up right here
      printf("y : %dn", y);

th_client[1] corresponds to the graphics thread, inside there’s I’ve got a structure with everything needed to initialize SDL.


Source: c#

Leave a Reply