Create texture from surface

Hello,

Is it possible to create a texture from a part of a surface.
If not, this would be useful when having a tileset image.
Julien

As far as I know, it’s not. But you can load a surface from a file, blit it off into multiple
small surfaces in memory, and create textures from them, or you can load a surface,
create a texture, and have a sprite object associated with it that knows what area
of the texture belongs to it.________________________________
From: clementj2005@yahoo.fr (Julien Clement)
Subject: [SDL] Create texture from surface

Hello,

Is it possible to create a texture from a part of a surface.
If not, this would be useful when having a tileset image.
Julien

Back in SDL 1.2, before SDL had textures, this is the same technique I
used for hardware-accelerated 2D-tile-based game graphics. Works like
a charm, but watch out for the blend modes and tex coords. You may see
the edges of your tiles blending into their neighboring tiles if
you’re not careful!On Sun, Jan 3, 2010 at 9:31 AM, Mason Wheeler wrote:

or you can load a surface,
create a texture, and have a sprite object associated with it that knows
what area of the texture belongs to it.


http://codebad.com/

What I want to do is: I have a surface with tiles on it.
Then, I want to be able to build a texture from a part of that surface.
I’ve tried to create another surface containing the part I want and
build a texture from it. The problem is that the resulting surface, created
with SDL_CreateRGBSurface, seems to be blank.
here is what I did:

// Here, "bat" contains the whole graphics, it's my tileset image
// and contains 3 tiles of 16x16 horizontally layed out.
SDL_Surface* sbat = Level::get_bat();

// This surface will contain the part I want, so it's 16x16
// and has the same properties as the original surface
SDL_Surface* surf = SDL_CreateRGBSurface (
  sbat->flags, 16, 16, sbat->format->BitsPerPixel,
  sbat->format->Rmask, sbat->format->Gmask, sbat->format->Bmask, sbat->format->Amask
);

// This check passes 
if (surf == NULL) {
  cerr << "*** ERROR: Cannot create a 16x16 surface for BatPart." << endl;
}
else {
  SDL_Rect dst =  {0, 0, 16, 16};
  // I blit the part of the tileset into the newly created tile surface:
  // It seems that the blit contains nothing.
  // The texrect is 16x16 and correctly set.
  SDL_UpperBlit(sbat, &texrect, surf, &dst);
  // Build a texture from it
  SDL_TextureID texid = SDL_CreateTextureFromSurface(0, sbat);
  // Ensure there is no scale mode
  SDL_SetTextureScaleMode(texid, SDL_TEXTURESCALEMODE_NONE);
  // This test passes
  if (texid == 0) {
    cerr << "*** ERROR: Cannot create texture for BatPart." << endl;
  }
  else {
    set_texture(texid, true);
  }
  // Free the tile surface
  SDL_FreeSurface(surf);
}

Yet, as I read, SDL_CreateRGBSurface() works only if SDL_SetVideoMode() has been
called previously, but I don’t want to stick with the old SDL 1.2 way of programming.
And, by the way, don’t want to bother with dummy driver, this operation on surfaces
must be independent from the driver, it’s only memory operations, correct me if I’m wrong !

(maybe there’s a ridiculous and evident mistake on my souces though …)

Thanks guys,

Julien________________________________
De : “sdl-request at lists.libsdl.org
? : sdl at lists.libsdl.org
Envoy? le : Dim 3 Janvier 2010, 21 h 05 min 20 s
Objet : SDL Digest, Vol 37, Issue 4

Send SDL mailing list submissions to
sdl at lists.libsdl.org

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
or, via email, send a message with subject or body ‘help’ to
sdl-request at lists.libsdl.org

You can reach the person managing the list at
sdl-owner at lists.libsdl.org

When replying, please edit your Subject line so it is more specific
than “Re: Contents of SDL digest…”

Today’s Topics:

  1. Create texture from surface (julien CLEMENT)
  2. Re: Create texture from surface (Mason Wheeler)
  3. SDL_CreateRGBSurface (julien CLEMENT)
  4. Re: Create texture from surface (Donny Viszneki)
  5. Re: SDL_CreateRGBSurface (Donny Viszneki)

Message: 1
Date: Sun, 3 Jan 2010 12:22:20 +0000 (GMT)
From: @Julien_Clement1 (Julien Clement)
To: SDL
Subject: [SDL] Create texture from surface
Message-ID: <892300.2187.qm at web28615.mail.ukl.yahoo.com>
Content-Type: text/plain; charset=“utf-8”

Hello,

Is it possible to create a texture from a part of a surface.
If not, this would be useful when having a tileset image.
Julien

-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20100103/a17a61ed/attachment.html


Message: 2
Date: Sun, 3 Jan 2010 06:31:55 -0800 (PST)
From: masonwheeler@yahoo.com (Mason Wheeler)
To: SDL Development List
Subject: Re: [SDL] Create texture from surface
Message-ID: <389861.67196.qm at web53206.mail.re2.yahoo.com>
Content-Type: text/plain; charset=“us-ascii”

As far as I know, it’s not. But you can load a surface from a file, blit it off into multiple
small surfaces in memory, and create textures from them, or you can load a surface,
create a texture, and have a sprite object associated with it that knows what area
of the texture belongs to it.


From: @Julien_Clement1 (Julien Clement)
Subject: [SDL] Create texture from surface

Hello,

Is it possible to create a texture from a part of a surface.
If not, this would be useful when having a tileset image.
Julien


Message: 3
Date: Sun, 3 Jan 2010 06:52:34 -0800 (PST)
From: @Julien_Clement1 (Julien Clement)
To: SDL
Subject: [SDL] SDL_CreateRGBSurface
Message-ID: <486482.42589.qm at web28606.mail.ukl.yahoo.com>
Content-Type: text/plain; charset=“utf-8”

By the way, it seems that SDL_CreateRGBSurface() works
only when SDL_SetVideoMode has been called previously.
What if I need it and don’t use the old 1.2 API ?

There is no way to blit from a part of a texture to another, etc… ?

Julien


Message: 4
Date: Sun, 3 Jan 2010 11:31:55 -0500
From: donny.viszneki@gmail.com (Donny Viszneki)
To: SDL Development List
Subject: Re: [SDL] Create texture from surface
Message-ID:
<44acbb801001030831o5b9cf19cnf4ec752357a0ca67 at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Jan 3, 2010 at 9:31 AM, Mason Wheeler wrote:

or you can load a surface,
create a texture, and have a sprite object associated with it that knows
what area of the texture belongs to it.

Back in SDL 1.2, before SDL had textures, this is the same technique I
used for hardware-accelerated 2D-tile-based game graphics. Works like
a charm, but watch out for the blend modes and tex coords. You may see
the edges of your tiles blending into their neighboring tiles if
you’re not careful!


http://codebad.com/


Message: 5
Date: Sun, 3 Jan 2010 11:33:42 -0500
From: donny.viszneki@gmail.com (Donny Viszneki)
To: SDL Development List
Subject: Re: [SDL] SDL_CreateRGBSurface
Message-ID:
<44acbb801001030833o78941338i5e1b9bd7695701f3 at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

I think you can use the dummy video driver if you don’t want to
SetVideoMode before working with graphics. This sort of thing is
sometimes called “headless” operation. Is that what you want?

On Sun, Jan 3, 2010 at 9:52 AM, julien CLEMENT <@Julien_Clement1> wrote:

By the way, it seems that SDL_CreateRGBSurface() works
only when SDL_SetVideoMode has been called previously.
What if I need it and don’t use the old 1.2 API ?
There is no way to blit from a part of a texture to another, etc… ?

Julien


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


http://codebad.com/



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

End of SDL Digest, Vol 37, Issue 4


Have you called:

  m_nWindowID = SDL_CreateWindow(

        ?MyApp?,

        SDL_WINDOWPOS_UNDEFINED,

        SDL_WINDOWPOS_UNDEFINED,

        g_nDisplayWidth,

        g_nDisplayHeight,

        SDL_WINDOW_SHOWN );

I do exactly the same thing you are doing to take an image that has my
bitmapped font characters and split them up into individual textures. It
works fine. The only two things I call before doing any SDL_Surface or
SDL_TextureID related code is the above function along with creating a
renderer. I think you can skip the renderer creation and it will use a
default renderer.

KenFrom: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of julien CLEMENT
Sent: Sunday, January 03, 2010 2:36 PM
To: sdl at lists.libsdl.org
Subject: [SDL] Create texture from surface

What I want to do is: I have a surface with tiles on it.

Then, I want to be able to build a texture from a part of that surface.

I’ve tried to create another surface containing the part I want and

build a texture from it. The problem is that the resulting surface, created

with SDL_CreateRGBSurface, seems to be blank.

here is what I did:

// Here, "bat" contains the whole graphics, it's my tileset image

// and contains 3 tiles of 16x16 horizontally layed out.

SDL_Surface* sbat = Level::get_bat();



// This surface will contain the part I want, so it's 16x16

// and has the same properties as the original surface

SDL_Surface* surf = SDL_CreateRGBSurface (

  sbat->flags, 16, 16, sbat->format->BitsPerPixel,

  sbat->format->Rmask, sbat->format->Gmask, sbat->format->Bmask,

sbat->format->Amask

);



// This check passes 

if (surf == NULL) {

  cerr << "*** ERROR: Cannot create a 16x16 surface for BatPart." <<

endl;

}

else {

  SDL_Rect dst =  {0, 0, 16, 16};

  // I blit the part of the tileset into the newly created tile surface:

  // It seems that the blit contains nothing.

  // The texrect is 16x16 and correctly set.

  SDL_UpperBlit(sbat, &texrect, surf, &dst);

  // Build a texture from it

  SDL_TextureID texid = SDL_CreateTextureFromSurface(0, sbat);

  // Ensure there is no scale mode

  SDL_SetTextureScaleMode(texid, SDL_TEXTURESCALEMODE_NONE);

  // This test passes

  if (texid == 0) {

    cerr << "*** ERROR: Cannot create texture for BatPart." << endl;

  }

  else {

    set_texture(texid, true);

  }

  // Free the tile surface

  SDL_FreeSurface(surf);

}

Yet, as I read, SDL_CreateRGBSurface() works only if SDL_SetVideoMode() has
been

called previously, but I don’t want to stick with the old SDL 1.2 way of
programming.

And, by the way, don’t want to bother with dummy driver, this operation on
surfaces

must be independent from the driver, it’s only memory operations, correct me
if I’m wrong !

(maybe there’s a ridiculous and evident mistake on my souces though …)

Thanks guys,

Julien


De : “sdl-request at lists.libsdl.org
? : sdl at lists.libsdl.org
Envoy? le : Dim 3 Janvier 2010, 21 h 05 min 20 s
Objet : SDL Digest, Vol 37, Issue 4

Send SDL mailing list submissions to
sdl at lists.libsdl.org

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
or, via email, send a message with subject or body ‘help’ to
sdl-request at lists.libsdl.org

You can reach the person managing the list at
sdl-owner at lists.libsdl.org

When replying, please edit your Subject line so it is more specific
than “Re: Contents of SDL digest…”

Today’s Topics:

  1. Create texture from surface (julien CLEMENT)
  2. Re: Create texture from surface (Mason Wheeler)
  3. SDL_CreateRGBSurface (julien CLEMENT)
  4. Re: Create texture from surface (Donny Viszneki)
  5. Re: SDL_CreateRGBSurface (Donny Viszneki)

Message: 1
Date: Sun, 3 Jan 2010 12:22:20 +0000 (GMT)
From: clementj2005@yahoo.fr (Julien Clement)
To: SDL
Subject: [SDL] Create texture from surface
Message-ID: <892300.2187.qm at web28615.mail.ukl.yahoo.com>
Content-Type: text/plain; charset=“utf-8”

Hello,

Is it possible to create a texture from a part of a surface.
If not, this would be useful when having a tileset image.
Julien

-------------- next part --------------
An HTML attachment was scrubbed…
URL:
<http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20100103/a17a6
1ed/attachment.html>


Message: 2
Date: Sun, 3 Jan 2010 06:31:55 -0800 (PST)
From: masonwheeler@yahoo.com (Mason Wheeler)
To: SDL Development List
Subject: Re: [SDL] Create texture from surface
Message-ID: <389861.67196.qm at web53206.mail.re2.yahoo.com>
Content-Type: text/plain; charset=“us-ascii”

As far as I know, it’s not. But you can load a surface from a file, blit it
off into multiple
small surfaces in memory, and create textures from them, or you can load a
surface,
create a texture, and have a sprite object associated with it that knows
what area
of the texture belongs to it.


From: clementj2005@yahoo.fr (Julien Clement)
Subject: [SDL] Create texture from surface

Hello,

Is it possible to create a texture from a part of a surface.
If not, this would be useful when having a tileset image.
Julien
-------------- next part --------------
An HTML attachment was scrubbed…
URL:
<http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20100103/20c32
0ab/attachment-0001.htm>


Message: 3
Date: Sun, 3 Jan 2010 06:52:34 -0800 (PST)
From: clementj2005@yahoo.fr (Julien Clement)
To: SDL
Subject: [SDL] SDL_CreateRGBSurface
Message-ID: <486482.42589.qm at web28606.mail.ukl.yahoo.com>
Content-Type: text/plain; charset=“utf-8”

By the way, it seems that SDL_CreateRGBSurface() works
only when SDL_SetVideoMode has been called previously.
What if I need it and don’t use the old 1.2 API ?

There is no way to blit from a part of a texture to another, etc… ?

Julien

-------------- next part --------------
An HTML attachment was scrubbed…
URL:
<http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20100103/1a91f
173/attachment-0001.htm>


Message: 4
Date: Sun, 3 Jan 2010 11:31:55 -0500
From: donny.viszneki@gmail.com (Donny Viszneki)
To: SDL Development List
Subject: Re: [SDL] Create texture from surface
Message-ID:
<44acbb801001030831o5b9cf19cnf4ec752357a0ca67 at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Jan 3, 2010 at 9:31 AM, Mason Wheeler wrote:

or you can load a surface,
create a texture, and have a sprite object associated with it that knows
what area of the texture belongs to it.

Back in SDL 1.2, before SDL had textures, this is the same technique I
used for hardware-accelerated 2D-tile-based game graphics. Works like
a charm, but watch out for the blend modes and tex coords. You may see
the edges of your tiles blending into their neighboring tiles if
you’re not careful!


http://codebad.com/


Message: 5
Date: Sun, 3 Jan 2010 11:33:42 -0500
From: donny.viszneki@gmail.com (Donny Viszneki)
To: SDL Development List
Subject: Re: [SDL] SDL_CreateRGBSurface
Message-ID:
<44acbb801001030833o78941338i5e1b9bd7695701f3 at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

I think you can use the dummy video driver if you don’t want to
SetVideoMode before working with graphics. This sort of thing is
sometimes called “headless” operation. Is that what you want?

On Sun, Jan 3, 2010 at 9:52 AM, julien CLEMENT wrote:

By the way, it seems that SDL_CreateRGBSurface() works
only when SDL_SetVideoMode has been called previously.
What if I need it and don’t use the old 1.2 API ?
There is no way to blit from a part of a texture to another, etc… ?

Julien


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


http://codebad.com/



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

End of SDL Digest, Vol 37, Issue 4


Actually there is already some work done in the game,
so the graphical context is ok, works fine, the window & renderer too.
What technic do you use to achieve the same result, Ken ?
Keep a rectangle area and blit only the part of the texture ?
Create a tile surface (like in the code I posted) ?
Another one ?
Julien

Copied from my working code. I pass in the number of frames wide/tall it is and it takes the image width and height and divides by those numbers to figure out how many images to create.

I hope this helps you.

Ken

// Code snippet

              m_nWidth = pSurface->w / nFramesWide;

              m_nHeight = pSurface->h / nFramesTall;



              for( int y=0;y<nFramesTall;y++ )

              {

                    for( int x=0;x<nFramesWide;x++ )

                    {

                          SDL_Rect srcRect;

                          SDL_Surface *pDst;



                          pDst = SDL_CreateRGBSurface(

                                pSurface->flags,

                                m_nWidth,

                                m_nHeight,

                                pSurface->format->BitsPerPixel, 

                                pSurface->format->Rmask,

                                pSurface->format->Gmask,

                                pSurface->format->Bmask,

                                pSurface->format->Amask );



                          srcRect.x = x * m_nWidth;

                          srcRect.y = y * m_nHeight;

                          srcRect.w = m_nWidth;

                          srcRect.h = m_nHeight;



                          SDL_BlitSurface( pSurface,&srcRect,pDst,NULL );



                          if ( m_bHasTransparency )

                          {

                                SDL_SetColorKey( pDst, SDL_SRCCOLORKEY, m_nTransparency );

                          }

#if defined( KS_USE_OLD_SDL_API )

                          m_pSurfaceList[y*nFramesWide+x] = pDst;

#else

                          m_pTextureList[y*nFramesWide+x] = SDL_CreateTextureFromSurface( NULL, pDst );



                          int nBlend = SDL_BLENDMODE_NONE;



                          if ( pSurface->flags & SDL_SRCALPHA )

                                nBlend = SDL_BLENDMODE_BLEND;

                          else if ( m_bHasTransparency )

                                nBlend = SDL_BLENDMODE_MASK;



                          SDL_SetTextureBlendMode( m_pTextureList[y*nFramesWide+x], nBlend );



                          SDL_SetTextureScaleMode( m_pTextureList[y*nFramesWide+x], SDL_TEXTURESCALEMODE_NONE );



                          SDL_FreeSurface( pDst );

#endif

                    }

              }From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On Behalf Of julien CLEMENT

Sent: Sunday, January 03, 2010 2:57 PM
To: SDL
Subject: Re: [SDL] Create texture from surface

Actually there is already some work done in the game,

so the graphical context is ok, works fine, the window & renderer too.

What technic do you use to achieve the same result, Ken ?

Keep a rectangle area and blit only the part of the texture ?

Create a tile surface (like in the code I posted) ?

Another one ?

Julien

Did you try disabling alpha blending, since the destination alpha (fully
transparent new surface) is preserved?

Jonny D

Hello,

Ok, I found what was wrong when creating a texture from a tile surface.
I needed to set the texture blend mode to NONE.

Julien