[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] [ Search: ]

4.23.2 Loading Images for Pixmaps

This section describes how you can load an image and use it on a pixmap. A pixmap is a small graphics class that can be used independently from the engine. It may be useful, for instance, to draw a logo on screen or for a heads-up-display (HUD).

Getting the Image Loader Plugin

To load an image you need the image loader plugin. There are two ways to make sure you have this plugin in your application. You can put a line in your configuration file like this:

 
System.Plugins.iImageIO = crystalspace.graphic.image.io.multiplex

Or you can add the following line to the call to csInitializer::RequestPlugins() in your main() function:

 
CS_REQUEST_IMAGELOADER,

To actually use the image loader in your application you need to ask the shared-object registry for a handle to the image loader:

 
csRef<iImageIO> imgldr = csQueryRegistry<iImageIO> (object_reg);

Loading the Image from VFS

Next, you will probably want to load the image from VFS (see section Virtual File System (VFS)). This is accomplished by loading the image as raw data and then asking the image loader to re-interpret the raw data as an actual ‘iImage’ resource.

 
iGraphics3D* g3d = ...
iTextureManager* txtmgr = g3d->GetTextureManager ();
csRef<iDataBuffer> buf = VFS->ReadFile ("/lib/mydata/test.jpg");
csRef<iImage> ifile = imgldr->Load (buf, txtmgr->GetTextureFormat ());
csRef<iTextureHandle> txt =
  txtmgr->RegisterTexture (ifile, CS_TEXTURE_2D);

This code first uses VFS to load the image from the given VFS path. This will only load the image data. VFS doesn't know how to parse images. This is the responsibility of the image loader. This is what the next line does. Here you give the loaded buffer to the image loader which will return the loaded image with an iImage pointer.

Now you have to make sure the image becomes a texture that is usable for drawing on screen. This is done with txtmgr->RegisterTexture(). Because we are going to use the texture for a pixmap (which is 2D) we use the ‘CS_TEXTURE_2D’ flag.

Using the Texture in a Pixmap

To create a pixmap using this texture you can simply do:

 
csSimplePixmap* pixmap = new csSimplePixmap (txt);

Drawing the Pixmap

There are various ways to draw the pixmap. Check out the API documentation for more info. But here is one example:

 
pixmap->DrawScaled (G3D, 10, 10, 400, 400);

This will draw the pixmap at location 10, 10 with dimension 400, 400. If needed it will scale so that the pixmap really fits there.

Include Files

The include files useful for this section are:

 
#include <cstool/cspixmap.h>
#include <igraphic/image.h>
#include <igraphic/imageio.h>
#include <ivideo/texture.h>
#include <ivideo/txtmgr.h>
#include <iutil/vfs.h>

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated using texi2html 1.76.