Copy from a surface to another surface

Hi guy,
I wrote more e-mails for this problem but anyone isn’t published.
I hope that will come published.
The problem is very simple… I can’t copy the result to a_tiles[i] from
finalbmp.
tnx

PS
Following you can find the unfinished work

int main ( int argc, char* argv[] )
{
SDL_Surface bmp, **a_tiles, finalbmp;
SDL_Rect src, dst;
Uint32 rmask, gmask, bmask, amask;
Uint32 a_map, n_square;
Uint8 r[32
32], g[32
32], b[32
32], a[3232], R[3232], G[3232],
B[32
32], A[32*32];
int img_w, img_h ,n_tile, x, y, i;

//////////////////////////////////////////////////////////////////////
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
//////////////////////////////////////////////////////////////////////

if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “Impossibile abilitare le SDL: %s.\n”, SDL_GetError());
return(1);
}

bmp = SDL_LoadBMP(“penguin.bmp”);

if ( bmp == NULL )
{
fprintf(stderr, “Impossibile caricare l’immagine: penguin.bmp.\n”);
return(1);
}

img_w = bmp->w / 32;
img_h = bmp->h / 32;
n_square = img_w * img_h;

a_tiles = calloc( n_square, sizeof(SDL_Surface*) );

a_map = calloc( n_square, sizeof(Uint32) );

src.w = 32;
src.h = 32;

dst.w = 32;
dst.h = 32;
dst.x = 0;
dst.y = 0;

for ( x = 0, n_tile = 0; x < img_w; x++, n_tile++ )
{
for ( y = 0; y < img_h; y++ )
{

  src.x = x * 32;
  src.y = y * 32;

  a_tiles[n_tile] = SDL_CreateRGBSurface(SDL_HWSURFACE, 32, 32, 32,

rmask, gmask, bmask, amask);

  if ( a_tiles[n_tile] == NULL )
  {
    fprintf(stderr, "Impossibile creare la surface: %s.\n",

SDL_GetError());
return(1);
}

  SDL_BlitSurface(bmp, &src, a_tiles[n_tile], &dst);

/*

*/
}
}

finalbmp = SDL_CreateRGBSurface(SDL_HWSURFACE, 32, 32*n_tile, 32, rmask,
gmask, bmask, amask);

src.x = 0;
src.y = 0;

for (i = 0; i < n_tile; i++)
{
dst.y = i * 32;
SDL_BlitSurface (a_tiles[i], &src, finalbmp, &dst);
}

SDL_SaveBMP(finalbmp, “prova.bmp”);

SDL_FreeSurface(bmp);
SDL_FreeSurface(finalbmp);

free(a_map);
free(a_tiles);

return(0);
}

Hi what exactly do you mean by “I can’t copy the result to a_tiles[i] from
finalbmp”, is it crashing ? In your code you copy from a_tile to finalbmp.
To find where the problem is, try to display intermediate surface to see if
everything is going well.

I think the problem is with your n_tile variable. You should increment it in
the y loop, otherwise you copy several image in the same array cell. And
there will be some surface lost in the memory. If it is clear here is the
code modification i sugest :

n_tile = 0;
for ( x = 0; x < img_w; x++)
{
for ( y = 0; y < img_h; y++ )
{
…;
SDL_BlitSurface(bmp, &src, a_tiles[n_tile], &dst);
n_tile++;
}
}

I haven’t checked the rest of the code

Finally, you should start learning some debugging technique to find such
problems yourself, like using GDB or putting printf in your code.

Julien> ----- Original Message -----

From: nightiger_spp@libero.it (NighTiger)
To:
Cc:
Sent: Thursday, June 05, 2003 10:53 PM
Subject: [SDL] Copy from a surface to another surface

Hi guy,
I wrote more e-mails for this problem but anyone isn’t published.
I hope that will come published.
The problem is very simple… I can’t copy the result to a_tiles[i] from
finalbmp.
tnx

PS
Following you can find the unfinished work

int main ( int argc, char* argv[] )
{
SDL_Surface bmp, **a_tiles, finalbmp;
SDL_Rect src, dst;
Uint32 rmask, gmask, bmask, amask;
Uint32 a_map, n_square;
Uint8 r[32
32], g[32
32], b[32
32], a[3232], R[3232], G[3232],
B[32
32], A[32*32];
int img_w, img_h ,n_tile, x, y, i;

//////////////////////////////////////////////////////////////////////
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
//////////////////////////////////////////////////////////////////////

if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “Impossibile abilitare le SDL: %s.\n”,
SDL_GetError());
return(1);
}

bmp = SDL_LoadBMP(“penguin.bmp”);

if ( bmp == NULL )
{
fprintf(stderr, “Impossibile caricare l’immagine: penguin.bmp.\n”);
return(1);
}

img_w = bmp->w / 32;
img_h = bmp->h / 32;
n_square = img_w * img_h;

a_tiles = calloc( n_square, sizeof(SDL_Surface*) );

a_map = calloc( n_square, sizeof(Uint32) );

src.w = 32;
src.h = 32;

dst.w = 32;
dst.h = 32;
dst.x = 0;
dst.y = 0;

for ( x = 0, n_tile = 0; x < img_w; x++, n_tile++ )
{
for ( y = 0; y < img_h; y++ )
{

  src.x = x * 32;
  src.y = y * 32;

  a_tiles[n_tile] = SDL_CreateRGBSurface(SDL_HWSURFACE, 32, 32, 32,

rmask, gmask, bmask, amask);

  if ( a_tiles[n_tile] == NULL )
  {
    fprintf(stderr, "Impossibile creare la surface: %s.\n",

SDL_GetError());
return(1);
}

  SDL_BlitSurface(bmp, &src, a_tiles[n_tile], &dst);

/*

*/
}
}

finalbmp = SDL_CreateRGBSurface(SDL_HWSURFACE, 32, 32*n_tile, 32, rmask,
gmask, bmask, amask);

src.x = 0;
src.y = 0;

for (i = 0; i < n_tile; i++)
{
dst.y = i * 32;
SDL_BlitSurface (a_tiles[i], &src, finalbmp, &dst);
}

SDL_SaveBMP(finalbmp, “prova.bmp”);

SDL_FreeSurface(bmp);
SDL_FreeSurface(finalbmp);

free(a_map);
free(a_tiles);

return(0);
}


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

Hi,

from the docs for SDL_CreateRGBSurface():

“Note: If an alpha-channel is specified (that is, if Amask is nonzero), then
the SDL_SRCALPHA flag is automatically set. You may remove this flag by
calling SDL_SetAlpha after surface creation.”

This isn’t very obvious (unless you carefully read the docs) and
you aren’t the first to be caught by this…-------------------------------------------------------------

for ( x = 0, n_tile = 0; x < img_w; x++ )
{
for ( y = 0; y < img_h; y++, n_tile++ ) // increment here instead!
{

  src.x = x * 32;
  src.y = y * 32;

  a_tiles[n_tile] = SDL_CreateRGBSurface(SDL_HWSURFACE, 32, 32,
                                 32,rmask, gmask, bmask, amask);

  SDL_SetAlpha( a_tiles[n_tile], 0, 0 );// unset SDL_SRCALPHA

  if ( a_tiles[n_tile] == NULL )
  {
    fprintf(stderr, "Impossibile creare la surface: %s.\n",
                                               SDL_GetError());
    return(1);
  }
  SDL_BlitSurface(bmp, &src, a_tiles[n_tile], &dst);
}

}

[Note that I have moved n_tile++ to the correct place.]

Also, you shouldn’t really call SDL_CreateRGBSurface() until after
you have called SDL_SetVideoMode(). Again, see the docs:

http://sdldoc.csn.ul.ie/sdlcreatergbsurface.php

Good luck,

cheers,
John.

----- Original Message -----
From: nightiger_spp@libero.it (NighTiger)
To:
Cc:
Sent: Thursday, June 05, 2003 9:53 PM
Subject: [SDL] Copy from a surface to another surface

Hi guy,
I wrote more e-mails for this problem but anyone isn’t published.
I hope that will come published.
The problem is very simple… I can’t copy the result to a_tiles[i] from
finalbmp.
tnx

PS
Following you can find the unfinished work

int main ( int argc, char* argv[] )
{
SDL_Surface bmp, **a_tiles, finalbmp;
SDL_Rect src, dst;
Uint32 rmask, gmask, bmask, amask;
Uint32 a_map, n_square;
Uint8 r[32
32], g[32
32], b[32
32], a[3232], R[3232], G[3232],
B[32
32], A[32*32];
int img_w, img_h ,n_tile, x, y, i;

//////////////////////////////////////////////////////////////////////
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
//////////////////////////////////////////////////////////////////////

if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “Impossibile abilitare le SDL: %s.\n”,
SDL_GetError());
return(1);
}

bmp = SDL_LoadBMP(“penguin.bmp”);

if ( bmp == NULL )
{
fprintf(stderr, “Impossibile caricare l’immagine: penguin.bmp.\n”);
return(1);
}

img_w = bmp->w / 32;
img_h = bmp->h / 32;
n_square = img_w * img_h;

a_tiles = calloc( n_square, sizeof(SDL_Surface*) );

a_map = calloc( n_square, sizeof(Uint32) );

src.w = 32;
src.h = 32;

dst.w = 32;
dst.h = 32;
dst.x = 0;
dst.y = 0;

for ( x = 0, n_tile = 0; x < img_w; x++, n_tile++ )
{
for ( y = 0; y < img_h; y++ )
{

  src.x = x * 32;
  src.y = y * 32;

  a_tiles[n_tile] = SDL_CreateRGBSurface(SDL_HWSURFACE, 32, 32, 32,

rmask, gmask, bmask, amask);

  if ( a_tiles[n_tile] == NULL )
  {
    fprintf(stderr, "Impossibile creare la surface: %s.\n",

SDL_GetError());
return(1);
}

  SDL_BlitSurface(bmp, &src, a_tiles[n_tile], &dst);

/*

*/
}
}

finalbmp = SDL_CreateRGBSurface(SDL_HWSURFACE, 32, 32*n_tile, 32, rmask,
gmask, bmask, amask);

src.x = 0;
src.y = 0;

for (i = 0; i < n_tile; i++)
{
dst.y = i * 32;
SDL_BlitSurface (a_tiles[i], &src, finalbmp, &dst);
}

SDL_SaveBMP(finalbmp, “prova.bmp”);

SDL_FreeSurface(bmp);
SDL_FreeSurface(finalbmp);

free(a_map);
free(a_tiles);

return(0);
}


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

Hi Julien,
I had wrote wrong.
I copy from a_tiles[i] to finalbmp.
Finalbmp have the correct dimension but is all black.> Hi what exactly do you mean by "I can’t copy the result to a_tiles[i]

from finalbmp", is it crashing ? In your code you copy from a_tile to
finalbmp.
To find where the problem is, try to display intermediate surface to
see if everything is going well.

Hi John Popplewell,
your way is right, now the program work fine.
tnx