10
\$\begingroup\$

I'm trying to write a platforming game where the sprites for the level backgrounds are broken up into 512x512 chunks. I keep 3 chunks in memory at a time and I'm trying to write code to blit all three to the screen. Here is the current code I have:

SDL_Rect where;
where.y = -game->camera->y;

where.x = -game->camera->x - MAP_WIDTH;
SDL_BlitSurface(left_chunk, NULL, screen, &where);

where.x = -game->camera->x;
SDL_BlitSurface(center_chunk, NULL, screen, &where);

where.x = -game->camera->x + MAP_WIDTH;
SDL_BlitSurface(right_chunk, NULL, screen, &where);

The issue I'm running into is that whichever chunk gets blitted first is the only one that shows up. The rest fail to appear onscreen. I think the issue might have something to do with alpha transparency, but even if the chunks don't overlap at all they still fail to blit. In other parts of the code I'm blitting multiple things to the screen at once, such as characters and backgrounds, and they all show up correctly. This particular segment of code is the only area I'm encountering this problem.

Screenshot

If I comment out the line that blits left_chunk, it changes to this:

Screenshot After

\$\endgroup\$
4
  • \$\begingroup\$ Are you sure that your calculated X values position the image in the visible area of the screen? \$\endgroup\$ Commented Oct 22, 2012 at 18:07
  • \$\begingroup\$ @ZackTheHuman They are partially offscreen, since the screen is only 300x175 and the map chunks are 512x512. I thought that may have been the problem at first, but whether they are onscreen or not seems to be irrelevant to whether it is rendering correctly or not. I can upload a screenshot to help illustrate the problem. \$\endgroup\$
    – n s
    Commented Oct 22, 2012 at 18:10
  • 1
    \$\begingroup\$ The SDL documentation says that SDL_BlitSurface saves the final rectangle to dstrect after blitting. Have you tried reseting the where variable after each blit? \$\endgroup\$
    – Asakeron
    Commented Oct 22, 2012 at 18:39
  • \$\begingroup\$ @Asakeron That was it! I didn't expect where to be modified. You should make your comment an answer! \$\endgroup\$
    – n s
    Commented Oct 22, 2012 at 18:42

1 Answer 1

8
\$\begingroup\$

The SDL documentation says that SDL_BlitSurface saves the final rectangle to dstrect after blitting. Reseting the where variable fixes the problem.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .