Problem with copying surfaces

  1. load a png into draw->work
  2. copy parts(!) of draw->work to tmp
  3. overwrite draw->work with tmp
  4. blit draw->work to screen

[…]
if (draw->work)
{
int xf = 0, yf = 0;
SDL_Surface *tmp = SDL_CreateRGBSurface (SDL_SWSURFACE,
draw->work->w,
draw->work->h,
32,
draw->work->format->Rmask,
draw->work->format->Gmask,
draw->work->format->Bmask,
draw->work->format->Amask);

  for (xf = 0; xf < draw->work->w / 2; xf ++)
    for (yf = 0; yf < draw->work->h; yf ++)
      {
         SDL_Rect a = {xf, yf, 1, 1};
         SDL_Rect b = {draw->work->w  - xf, yf, 1, 1};

         SDL_BlitSurface (draw->work, &a, tmp, &b);
      }

  SDL_BlitSurface (tmp, NULL, draw->work, NULL);
  SDL_FreeSurface (tmp);
}

[…]

when I blit that draw->work to the screen it is like step 2 and 3 didn’t
happen… what is my fault?

Thanks,
Dirk

One thing I notice is that ‘tmp’ has no colorkey, but not all of it gets
written to. Half of it’s left unwritten, hence pure black – this black is
going to ahnnialate…agnigle…wreck the other half of draw->work when you
copy tmp over it.

If you’re trying to mirror it down the middle, which is what I’m guessing this
does, you should only blit half of tmp back, not all of it:

{
SDL_Rect src={(draw->work->w/2)+1, 0, draw->work->w/2, draw->work->h };
SDL_BlitSurface (tmp,&src,draw->work,&src);
}

You can also speedup your code a bit by blitting column per column instead of
pixel per pixel:

for (xf = 0; xf < draw->work->w / 2; xf ++)
{
SDL_Rect a = {xf, 0, 1, draw->work->h};
SDL_Rect b = {draw->work->w - xf, 0, draw->work->h, 1};

SDL_BlitSurface (draw->work, &a, tmp, &b);

}On December 1, 2005 07:19 pm, Dirk wrote:

  1. load a png into draw->work
  2. copy parts(!) of draw->work to tmp
  3. overwrite draw->work with tmp
  4. blit draw->work to screen

[…]
for (xf = 0; xf < draw->work->w / 2; xf ++)
for (yf = 0; yf < draw->work->h; yf ++)
{
SDL_Rect a = {xf, yf, 1, 1};
SDL_Rect b = {draw->work->w - xf, yf, 1, 1};

         SDL_BlitSurface (draw->work, &a, tmp, &b);
      }

  SDL_BlitSurface (tmp, NULL, draw->work, NULL);
  SDL_FreeSurface (tmp);
}

[…]

when I blit that draw->work to the screen it is like step 2 and 3 didn’t
happen… what is my fault?

Thanks,
Dirk

Hello!
Thanks for your answer!

One thing I notice is that ‘tmp’ has no colorkey, but not all of it gets
written to. Half of it’s left unwritten, hence pure black – this black is
going to ahnnialate…agnigle…wreck the other half of draw->work when you
copy tmp over it.

If you’re trying to mirror it down the middle, which is what I’m guessing this
does, you should only blit half of tmp back, not all of it:

{
SDL_Rect src={(draw->work->w/2)+1, 0, draw->work->w/2, draw->work->h };
SDL_BlitSurface (tmp,&src,draw->work,&src);
}

You can also speedup your code a bit by blitting column per column instead of
pixel per pixel:

for (xf = 0; xf < draw->work->w / 2; xf ++)
{
SDL_Rect a = {xf, 0, 1, draw->work->h};
SDL_Rect b = {draw->work->w - xf, 0, draw->work->h, 1};

SDL_BlitSurface (draw->work, &a, tmp, &b);

}

I changed the code as you suggested but draw->work still looks like tmp
wasn’t blitted “over” it… it looks like before… like nothing happened…

if (draw->work)
{
int xf = 0, yf = 0;
SDL_Surface *tmp = SDL_CreateRGBSurface (SDL_SWSURFACE,
draw->work->w,
draw->work->h,
32,
draw->work->format->Rmask,
draw->work->format->Gmask,
draw->work->format->Bmask,
draw->work->format->Amask);

  SDL_SetColorKey (tmp, SDL_SRCCOLORKEY, 0);

  for (xf = 0; xf < draw->work->w / 2; xf ++)
    {
      SDL_Rect a = {xf, 0, 1, draw->work->h};
      SDL_Rect b = {draw->work->w  - xf, 0, 1, draw->work->h};

      SDL_BlitSurface (draw->work, &a, tmp, &b);
    }

  SDL_BlitSurface (tmp, NULL, draw->work, NULL);
  SDL_FreeSurface (tmp);
}

i must admit that i didn’t know that SDL_SetColorKey() would be
necessary… but it didn’t change anything (neither to the worse or the
better…)… it’s just like the image was loaded and blitted to the
screen… :-/ …should i quote more code?

Dirk

I changed the code as you suggested but draw->work still looks like tmp
wasn’t blitted “over” it… it looks like before… like nothing happened…
That’s not quite how I suggested :wink: You don’t need to use a colorkey if you
just don’t blit the half that you don’t want blitted.

i must admit that i didn’t know that SDL_SetColorKey() would be
necessary…
For the record, could you state for the record what exactly you are trying to
do? I’m guessing I guessed right, but you haven’t actually said.

but it didn’t change anything (neither to the worse or the
better…)… it’s just like the image was loaded and blitted to the
screen… :-/ …should i quote more code?
Something else must be going wrong. I actually tested the code and code
modifications.On December 2, 2005 05:46 am, Dirk wrote: