Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Create Texture from SDL_Surface


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 justin12343   Members   -  Reputation: 156

Like
0Likes
Like

Posted 16 July 2012 - 10:01 AM

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[i] = 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, 16 July 2012 - 10:02 AM.


Ad:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS