Background Drawing Efficiency

I am working on a game that demands the player stay centered on the
screen and it be the Background that moves every time. Therefore, i
can’t use dirty rectangles to update. Are there any tricks/does anyone
have any ideas on how to handle a game where the background moves and
the player stays static?

[that was a general question. i’d also like to ask if my current method
could be improved, so bear with me: ]
The game world is divided up into what i call “sectors”, roughly
2100x1500, because the game will consist of one large world. Blitting
from one big background image is faster than re-tiling, so i create a
number of SDL_Surfaces the size of a sector to buffer the player’s
current and neighboring sectors. A voice (conscience?) keeps telling me
this is a horrible way to go about drawing a background
Does anyone have ideas on how one could better implement sectors like
this?

thanks, john.

Hi John,

I am working on a game that demands the player stay centered on the
screen and it be the Background that moves every time. Therefore, i

I’m working on a game similar to Age Of Empires, so i have to deal with a huge
background surface - the landscape.

can’t use dirty rectangles to update. Are there any tricks/does anyone
have any ideas on how to handle a game where the background moves and
the player stays static?

I realize moving by blitting that rectangle to the screen that should
currently be visible. The user moves around the world by pressing the arrow
keys or moving the mouse to any screen border.

[that was a general question. i’d also like to ask if my current method
could be improved, so bear with me: ]
The game world is divided up into what i call “sectors”, roughly
2100x1500, because the game will consist of one large world. Blitting
from one big background image is faster than re-tiling, so i create a
number of SDL_Surfaces the size of a sector to buffer the player’s

Why creating many surfaces ? Are the sectors not part of the world ?

If your world is infinite, you can use the following code example to blit all
visible parts of the world to the screen (compile it with gcc tile.c -o
tile):

[code on]
#include <stdio.h>

int main(int argc, char* argv[])
{
int bg_width, bg_height, /* width and height of background surface
/
screen_width, screen_height, /
width and height of screen /
x, y, /
actual coordinates within background /
width, height, /
width and height of background blit /
screen_x, screen_y, /
actual screen coordinates /
bg_left; /
storage of original x */

if (argc < 6) {
printf(“USAGE: tile
\n”);
exit(1);
}

/* get parameters */
bg_width = atoi(argv[1]);
bg_height = atoi(argv[2]);
screen_width = atoi(argv[3]);
screen_height = atoi(argv[4]);
bg_left = atoi(argv[5]);
y = atoi(argv[6]);

/* don’t exceed background’s width and height */
bg_left %= bg_width;
y %= bg_height;

/* as long vertical blitting is needed /
for (height = screen_y = 0; screen_y < screen_height; screen_y += height) {
/
get the background height to use for blitting */
height = bg_height - y;

/* don't exceed screen's height */
if ((screen_y + height) > screen_height) {
  height = screen_height - screen_y;
}

/* restore original x offset within background */
x = bg_left;

/* as long as horizontal blitting is needed */
for (width = screen_x = 0; screen_x < screen_width; screen_x += width) {
  /* get the background width to use for blittin */
  width = bg_width - x;
  
  /* don't exceed screen's width */
  if ((screen_x + width) > screen_width) {
    width = screen_width - screen_x;
  }
  
  /* &[..] means pointer to SDL_Rect */
  printf(
    "SDL_BlitSurface(background, &[%i, %i, %i, %i], screen, &[%i, %i])\n",
    x, y, width, height, screen_x, screen_y
  );
  
  /* step to next x offset */
  x = (x + width) % bg_width;
}

/* step to next y offset */
y = (y + height) % bg_height;

}
}

[code off]> current and neighboring sectors. A voice (conscience?) keeps telling me

this is a horrible way to go about drawing a background
Does anyone have ideas on how one could better implement sectors like
this?

thanks, john.


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