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

5.7.7 Loading Images for Pixmaps

This howto 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 is very useful for CSWS, to draw a logo on screen or to use for a 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 config 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 finally be able to use the image loader in your application you need to do:

 
iImageIO* imgldr = CS_QUERY_REGISTRY (object_reg, iImageIO);

When you don't need it anymore you must call imgldr->DecRef ().

Loading the Image from VFS

To learn more about VFS go to the see section 7.2 Virtual File System (VFS) documentation. In this section I show you how you can use VFS and the image loader to load an image:

 
iDataBuffer* buf = VFS->ReadFile ("/lib/mydata/test.jpg");
iImage* ifile = imgldr->Load (buf->GetUint8 (),
  buf->GetSize (), txtmgr->GetTextureFormat ());
buf->DecRef ();
iTextureHandle* txt = txtmgr->RegisterTexture (ifile, CS_TEXTURE_2D);
txt->Prepare ();
ifile->DecRef ();

This code will first use 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.

After this is done you free the loaded buffer with buf->DecRef().

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. After registering the texture we have to Prepare() it for use.

When that is done we can free up the loaded image (it is now held by the texture) with ifile->DecRef().

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 "csfx/cspixmap.h"
#include "igraphic/image.h"
#include "igraphic/imageio.h"
#include "ivideo/texture.h"
#include "ivideo/txtmgr.h"
#include "isys/vfs.h"


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

This document was generated using texi2html