class KSpriteCache

The main KDE game object. More...

Definition#include <kspritecache.h>
InheritsQObject (unknown) [public ]
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Methods

Protected Methods

Protected Members


Detailed Description

The KSpriteCache class is used to load and cache sprites. Loading is done via a KConfig file which contains the definitions of the sprite in text form. Usng this approach allows you to tun the sprites without chaning the sourcecode of the program. This is especially useful if the graphics team is independent of the programmer or if you want to write external themes for your game. Furhtermore the class keeps sprites in memory so that they are fastly reloaded when you use more than one sprite of a given type.

Example:


 # Sprite with three frames and a common mask for them
 # z position given but x,y set manually in the program
  [arrow]
  file=arrow%d.png
  mask=arrow_mask.png
  number=3
  offset=19,5
  rtti=32
  z=9.0

 # Simple sprite which is already positioned correcly. No mask just one file
  [board]
  file=board.png
  rtti=32
  x=15.0
  y=40.0
  z=0.0
 
 # Sprite with one cyclic (2) animation of  5 frames (0-4) and
 # a slow delay of 8
  [star]
  anim0=0,4,2,8
  file=star%d.png
  mask=star%d_mask.png
  number=5
  offset=19,20
  rtti=32
  z=100.0


@todo Support single sprites (only one copy in memory) Support more sprite types (currently KSprite and QCanvasText)

 KSpriteCache (QString grafixdir, QObject* parent=0,const char * name=0)

KSpriteCache

Create a sprite cache. Usuzally you will need one per program only.

Parameters:
grafixdir- the directory where the configuration file and the graphics reside

 ~KSpriteCache ()

~KSpriteCache

Delete the sprite cache

bool  setGrafixDir (QString dir)

setGrafixDir

Change the grafichs directory.

@todo this does not flush the cache or so...

void  setRcFile (QString file)

setRcFile

Change the name of the config file. Its default is grafix.rc

QString  grafixDir ()

grafixDir

return the graphics directory

QString  rcFile ()

rcFile

return the rc/configuration file

QCanvas * canvas ()

canvas

[const]

returns the canvas which belongs to the cache

void  setCanvas (QCanvas *c)

setCanvas

sets the canvas belonging to the cache

@todo could be done in the constructor

KConfig * config ()

config

returns the KConfig configuration file where thegraphical data is read. Access to this is necessary if you want to store general game infos in theis file to or if you want to read additional sprite data which are not read be the default functions.

QCanvasItem * getItem (QString name, int no)

getItem

Main function to create a sprite. You call this like


 KSprite *sprite=(KSprite *)(yourcahce->getItem("hello",1));
 if (sprite) sprite->show();

Calling this function will load the given sprite from the configuration file. Its type is determined by the rtti= entry of the section [hello] in that file. Default is a KSprite. This file defines all data of the sprite so that you just have to show it. Each copy of the sprite gets its own number (1,2,...) Note: The sprite is hidden upon creation and you need to show it explicitely. TODO: What definitions are possible in the rc file

@todo support call without number argument as single sprite's @todo support loading of frame sequence via one big pixmap

Parameters:
name- the name of the sprite resp. the secion in the rc file
no- the unique number of the sprite

Returns: sprite - returns the sprite pointer as QCanvasItem

QPixmap *  loadPixmap (QString file,QString mask=QString::null,QString dir=QString::null)

loadPixmap

This function loads a pixmap from the given file. Optional you can also provide a mask file. Also optinal you can provide the directory. Default is the directory which is set with this KSpriteCache

void  deleteItem (QCanvasItem *item)

deleteItem

Deletes a item form the sprite cache given as a pointer to it

void  deleteItem (QString s,int no)

deleteItem

Same as above but delete the item with the name and number

void  deleteAllItems ()

deleteAllItems

Deletes all items in the cache

void  configureCanvasItem (KConfig *config,QCanvasItem *item)

configureCanvasItem

[protected]

Loads the default properties for all QCanvasItems from the given config file. This is at the moment


 x=(double)
 y=(double)
 z=(double)

void  configureCanvasItem (QCanvasItem *original,QCanvasItem *item)

configureCanvasItem

[protected]

Copies the default properties for all QCanvasItems from another sprite. Same as above.

QCanvasItem * loadItem (KConfig *config,QString name)

loadItem

[protected virtual]

Loads an item with the given name form the given config file. From the rtti entry it is determined what type it is and then it is loaded.

QCanvasItem * cloneItem (QCanvasItem *original)

cloneItem

[protected virtual]

Clone the sprite from another sprite, mostly from the copy stored in the cache.

QCanvasPixmapArray * createPixmapArray (KConfig *config,QString name)

createPixmapArray

[protected virtual]

Creates a pixmap array for a KSprite from the given config file for the sprite with the given name (is the name necessary?). Parameters are


   offset=(QPoint)       : The sprites offset (where 0,0 is)
   pixmaps=(QStringList) : List of operations to create frames (TODO *   rename)
                           if ommited one operation without name is used

All following calls have to be preceeded by every given string of the pixmaps section. If this section is not supplied they can be used without prefix but only one frame sequence is created.


   method=(QString)  : load, scale (default=load)
                       load: loads number frames from file
                       scale: scales  number frames from one loaded file
   number=(int)      : how many frames to generate
   file=(Qstring)    : the filename to load (can contain printf format args
                       as %d which are replaced, e.g. file=hello_%d.png
   mask=(QString)    : Same for the mask of the pixmaps
   axis=(int)        : (scale only): scale axis (1=x,2=y,3=x+y)
   final=(double)    : final scale in percent (default 0.0, i.e. complete scaling)
   colorfilter=1,h,s,v: make a HSV transform of all sprite images
   colorfilter=2     :  make it gray (lighter=100 is default)
   colorfilter=2,g   : make it gray and lighter (positiv) or darker (negativ)

void  createAnimations (KConfig *config,KSprite *sprite)

createAnimations

[protected]

Reads the animations from the config file and calls the corresponding KSprite function to create them.


  anim0=a,b,c,d
  anim1=e,f,g,h

Where the animations have to to be in sequence starting with 0 (i.e. anim0). a is the first frame of the animation. b is the last frame of the animation. c is the mode of the animations, see KSprite::createAnimation and d is the delay in cycles of the qcanvas animnation.

Parameters:
config- the config file the sprite is read from
sprite- the sprite whose animations are set

void  createAnimations (KSprite *original,KSprite *sprite)

createAnimations

[protected]

Same as above but to copy the animations from an existing sprite

void  changeGrey (QPixmap *pixmap,int lighter=100)

changeGrey

[protected virtual]

Change a pixmap to grey values. If the second argument is bigger than 100 the pixmap is made lighter and if it less then 100 it is made darker too

void  changeHSV (QPixmap *pixmap,int dh,int ds,int dv)

changeHSV

[protected virtual]

Change the HAS value of the pixmap by dH, dS and dV

void  applyFilter (QPixmap *pixmap,KConfig *config,QString name)

applyFilter

[protected virtual]

Apply the filters as defined in the config file to the sprite name (TODO is this argument needed) to the pixmap.

void  reset ()

reset

[protected]

resets the cache (?)

QDict mItemDict

mItemDict

[protected]

QDict mCloneDict

mCloneDict

[protected]

QString mGrafixDir

mGrafixDir

[protected]

QString mRcFile

mRcFile

[protected]

KConfig * mConfig

mConfig

[protected]

QCanvas * mCanvas

mCanvas

[protected]