Jump to content
  • Advertisement
Sign in to follow this  
justin12343

Create Texture from SDL_Surface

This topic is 2340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm putting together a bitmap font system with SDL_TTF, SDL_image, and DirectX 9 but I'm having some problems converting a SDL_Surface returned from IMG_Load into a IDirect3DTexture9.


bool Font::CreateFromBitmap()
{
SDL_Surface *surf = IMG_Load( mFname.c_str());
//SDL_SetColorKey(surf, SDL_RLEACCEL, surf->format->colorkey);
Uint16 i = mFirstChar;
for (int y = 0; y < surf->h; y += mHeight)
{
for (int x = 0; x < surf->w; x += mWidth)
{
SDL_Surface *pCharSurf = SDL_CreateRGBSurface( SDL_SWSURFACE, mWidth, mHeight, surf->format->BitsPerPixel, surf->format->Rmask, surf->format->Gmask, surf->format->Bmask, surf->format->Amask);
surf->refcount++;
pCharSurf->refcount++;
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = mWidth;
rect.h = mHeight;
if (SDL_BlitSurface( surf, &rect, pCharSurf, NULL) == -1)
{
printf( "Failed to load font at '%s' in %s", i, mName);
SDL_FreeSurface( surf);
Release();
return false;
}
Character *pChar = CreateChar( pCharSurf); //<------------------
if (pChar == NULL)
{
printf( "Failed to load font at '%s' in %s", i, mName);
SDL_FreeSurface( surf);
Release();
return false;
}
chars = pChar;

if (i == mLastChar)
{
goto end;
}
else
{
++i;
}
}
}
end:
SDL_FreeSurface( surf);
return true;
}


This function is used when creating a font from an image file instead of a .ttf font. It's supposed to copy a portion from the image onto a small surface using SDL_BlitSurface then pass that to a overloaded Font::CreateChar function.


Character *DX9Font::CreateChar( SDL_Surface *surf)
{
DX9Character *pChar = new DX9Character;
pChar->surface = surf;
pChar->w = surf->w;
D3DFORMAT format = D3DFMT_A8R8G8B8;
if (surf->format->BitsPerPixel == 32)
{
if (surf->format->Rmask == 0x000000ff)
{
format = D3DFMT_A8B8G8R8;

if (surf->format->Amask == 0x00000000)
{
format = D3DFMT_X8B8G8R8;
}
}
else
{
format = D3DFMT_A8R8G8B8;

if (surf->format->Amask == 0x00000000)
{
format = D3DFMT_X8R8G8B8;
}
}
}
if (surf->format->BitsPerPixel == 24)
{
format = D3DFMT_R8G8B8;
}
if (surf->format->BitsPerPixel == 16)
{
format = D3DFMT_R5G6B5;
}
if (surf->format->BitsPerPixel == 8)
{
format = D3DFMT_R3G3B2;
}
D3DXCreateTexture( pEngine->GetDevice(), surf->w, surf->h, 1, D3DUSAGE_DYNAMIC, format, D3DPOOL_DEFAULT, &pChar->texture);
D3DLOCKED_RECT rect;
SDL_LockSurface( surf);
if FAILED( pChar->texture->LockRect( 0, &rect, NULL, D3DLOCK_DISCARD))
{
delete pChar;
return 0;
}
char *src = (char*)surf->pixels;

Uint32 rows = surf->h;
Uint32 size = surf->w * surf->format->BytesPerPixel;
char *dest = (char*)rect.pBits;
while(rows--)
{
memcpy( dest, src, size);
src += surf->pitch;
dest += rect.Pitch;
}
pChar->texture->UnlockRect( 0);
SDL_UnlockSurface( surf);
return pChar;
}


Here is where the surface is memcpy'ed into a DirectX texture. The problem is when the texture is rendered, all I get is black boxes or completely transparent black boxes if the image had an alpha channel. The ttf font surfaces use the exact same function above and works fine, but not surfaces returned from IMG_Load. What could be the problem? Edited by justin12343

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!