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.


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

Screenshot After

  • \$\begingroup\$ Are you sure that your calculated X values position the image in the visible area of the screen? \$\endgroup\$ – Zack The Human Oct 22 '12 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\$ – extropic-engine Oct 22 '12 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 Oct 22 '12 at 18:39
  • \$\begingroup\$ @Asakeron That was it! I didn't expect where to be modified. You should make your comment an answer! \$\endgroup\$ – extropic-engine Oct 22 '12 at 18:42

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.