Strange thing

Hi guys,
I want show you a little “trouble (?!)” that I’ve with a blit…

SDL_Surface *grabframe(SDL_Surface *source, SDL_Rect *grabr, int cols,
int frm)
{
SDL_Surface *temp = SDL_ConvertSurface(source, source->format,
SDL_SWSURFACE);

grabr->x = grabr->x + (frm % cols) * grabr->w;
grabr->y = grabr->y + (frm / cols) * grabr->h;

SDL_BlitSurface(source, grabr, temp, NULL);

return temp;
}

I use this function to get my tiles from a surface and:

if ((tiles = IMG_Load(“files/images/tiles.png”)) == NULL)
{
fprintf(stderr, “\n%s\n”, SDL_GetError());
return -1;
}
tr.x = 0;
tr.y = 0;
tr.w = TILEW+1;
tr.h = TILEH+1;

vs = SDL_DisplayFormat(scr);
SDL_GetClipRect(vs, &sr);

pr.w = TILEW;
pr.h = TILEH;

srand(time(NULL));
for (tiley=0; tiley < sr.h; tiley+=TILEH)
{
for (tilex=0; tilex < sr.w; tilex+=TILEW)
{
n = rand() % TILES;
pr.x = tilex;
pr.y = tiley;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, n), &tr, vs, &pr);
}
}

I do that just like a little tutorial.
In teory I will have the screen fill of random tiles but, after the
third tile, I’ve only, I think, NULL image

I tried without the two “for” just for try:
pr.w = TILEW;
pr.h = TILEH;

pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 1), &tr, vs, &pr);

pr.x = 32;
pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 2), &tr, vs, &pr);

pr.x = 64;
pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 3), &tr, vs, &pr);

// the function doesn’t draw anymore
pr.x = 96;
pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 4), &tr, vs, &pr);

pr.x = 128;
pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 5), &tr, vs, &pr);

but after the third tile, the SDL (I think) doesn’t blit anymore…
Can anybody say me why or have anyidea about that?
tnx

Hi.

You do not free your surfaces. That probably isn’t the problem, but be
sure to free the individual tile surface when you finish with it. It’s
also possible that SDL may be objecting to the blit on the basis that
you are attempting to blit offscreen somehow (maybe your tile image x
and y are beyond the w and h). It’s been known to happen. You should
change your grabr width and height to the width and height of the tile
and see if that helps. If it doesn’t, check your other rectangles and
make sure your surface to surface blitting isn’t blitting off the surface.

-TomT64

NighTiger wrote:>Hi guys,

I want show you a little “trouble (?!)” that I’ve with a blit…

SDL_Surface *grabframe(SDL_Surface *source, SDL_Rect *grabr, int cols,
int frm)
{
SDL_Surface *temp = SDL_ConvertSurface(source, source->format,
SDL_SWSURFACE);

grabr->x = grabr->x + (frm % cols) * grabr->w;
grabr->y = grabr->y + (frm / cols) * grabr->h;

SDL_BlitSurface(source, grabr, temp, NULL);

return temp;
}

I use this function to get my tiles from a surface and:

if ((tiles = IMG_Load(“files/images/tiles.png”)) == NULL)
{
fprintf(stderr, “\n%s\n”, SDL_GetError());
return -1;
}
tr.x = 0;
tr.y = 0;
tr.w = TILEW+1;
tr.h = TILEH+1;

vs = SDL_DisplayFormat(scr);
SDL_GetClipRect(vs, &sr);

pr.w = TILEW;
pr.h = TILEH;

srand(time(NULL));
for (tiley=0; tiley < sr.h; tiley+=TILEH)
{
for (tilex=0; tilex < sr.w; tilex+=TILEW)
{
n = rand() % TILES;
pr.x = tilex;
pr.y = tiley;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, n), &tr, vs, &pr);
}
}

I do that just like a little tutorial.
In teory I will have the screen fill of random tiles but, after the
third tile, I’ve only, I think, NULL image

I tried without the two “for” just for try:
pr.w = TILEW;
pr.h = TILEH;

pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 1), &tr, vs, &pr);

pr.x = 32;
pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 2), &tr, vs, &pr);

pr.x = 64;
pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 3), &tr, vs, &pr);

// the function doesn’t draw anymore
pr.x = 96;
pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 4), &tr, vs, &pr);

pr.x = 128;
pr.y = 0;
SDL_BlitSurface(grabframe(tiles, &tr, COLS, 5), &tr, vs, &pr);

but after the third tile, the SDL (I think) doesn’t blit anymore…
Can anybody say me why or have anyidea about that?
tnx


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