8.2 2D objects on top of the screen (Pixmaps)

By Ryan Murphy, ramurphy100@hotmail.com.

To display a 2D image (or Pixmap) is quite simple in Crystal Space. Once loaded, the image can be displayed by itself or over a 3D rendering, like the CS Logo in Walktest.

Firstly, you must include:
    cstool/cspixmap.h
    igraphic/image.h
    igraphic/imageio.h

Then, tell the compiler that you are using the PixMap class:

class csPixMap;

NOTE: You also need to have a texture manager and a loader in place and working to be able to load and display the image.
Then, the definition for any 2D image is placed in the header file, eg:

csPixmap* img_name;

In your application, create a routine called Load2DImages (). This is called after you have loaded a world file and/or initilized your texture handler.

//-----------------------------------------------------------------------------
// Load 2D Images
//-----------------------------------------------------------------------------
bool MyApp::Load2DImages ()
{
  iImage* ifile;
  iTextureHandle* txt;

  // Load the image
  ifile = loader->LoadImage ("/lib/stdtex/filename.jpg");
  if (!ifile) return false;

  // If you need to rescale the image,
  ifile->Rescale (width, height);

  // Register the texture
  txt = txtmgr->RegisterTexture (ifile, CS_TEXTURE_2D);
  if (!txt) return false;

  // Create a PixMap
  img_name = new csSimplePixmap (txt);

  // Tidy up any unused variables
  ifile->DecRef ();
  txt->DecRef ();

  // Tell the calling function we survived
  return true;
}

Then, to draw the image, create the following routine. To display the image OVER the 3D rendering, call this after you have drawn the 3D portion of your display, however if you don't have any 3D rendering, it doesn't matter what order it is called in.

//-----------------------------------------------------------------------------
// Draw the 2D objects
//-----------------------------------------------------------------------------
void MyApp::DrawFrame2D ()
{
  if (!g2d->BeginDraw ())
    return;

  // Draw the HUD
  if (img_HUD)
  {
    img_HUD->DrawTiled (g3d, x1, y1, x2, y2, alpha);
  }
}

You can use other draw routines instead of 'DrawTiled' - see the API reference under csPixMap for other options.