Some days ago I started to work with ogre. In order to display 2D elements, I have used a nice class written by Hernán Moraldo (Nov 06).
I wrote a small class in order to set pictures using pixel coordinates. I’ve also implementet things like animations, etc.
image.h:
#ifndef image_h
#define image_h
#include “2d.h”
#include <iostream>
#include <ogre .h>
#include <ogretimer .h>
using namespace Ogre;
using namespace std;
class OImage : public Ogre2dManager
{
public:
OImage(Root *r, std::string cF);
~OImage();
void addImage(std::string cF);
void draw();
void setXYByPx(int x, int y);
void setWHByPx(int w, int h);
void moveX(float px);
void moveY(float py);
void setX(float px);
void setY(float py);
float getX();
float getY();
float getW();
float getH();
void setVisible(bool vis);
bool getVisible();
void setAnimated(bool anim);
bool getAnimated();
void setAnimationspeed(int time);
int getAnimationspeed();
protected:
Root *r;
SceneManager *sm; // Scenemanager device
std::string images[24]; // the image array
int curImage; // Which image shall be displayed ?
float x; // x from 0 to 1
float y; // y from 0 to 1
float w; // w from 0 to 1
float h; // h from 0 to 1
float pxw; // W in pixels
float pxh; // H in pixels
bool visible;
bool animated;
int animSpeed;
unsigned long lastTime;
Timer* timer;
};
#endif
image.cpp:
#include “image.h”
OImage::OImage(Root *rp, std::string cF) : Ogre2dManager()
{
timer = new Timer();
visible = 1;
animated = 0;
lastTime = timer->getMilliseconds();
x = 0.5;
y = 0.5;
w = 0.5;
h = 0.5;
r = rp;
sm = r->getSceneManager(”sceneManager”);
images[0] = cF;
curImage = 0;
animSpeed = 30;
init(sm, Ogre::RENDER_QUEUE_OVERLAY, true);
TextureManager::getSingleton().load(images[curImage], “General”);
}
OImage::~OImage()
{
end();
}
void OImage::addImage(std::string cF)
{
for (int i = 0; i < = 31; i++)
{
if (images[i] == “”)
{
images[i] = cF;
TextureManager::getSingleton().load(images[i], “General”);
break;
}
}
}
void OImage::draw()
{
if (animated)
{
if ((timer->getMilliseconds()-lastTime) >= animSpeed)
{
curImage++;
if (curImage >= 32)
curImage = 0;
if (images[curImage] == “”)
curImage = 0;
lastTime = timer->getMilliseconds();
}
}
if (visible)
spriteBltFull(images[curImage], (x*2)-1, ((y*2)-1)*(-1), ((x*2)-1)+(w*2), ((y*2)-1)*(-1)-(h*2));
}
void OImage::setXYByPx(int px, int py)
{
x = ((1.0/r->getAutoCreatedWindow()->getWidth())*px);
y = ((1.0/r->getAutoCreatedWindow()->getHeight())*py);
setWHByPx(pxw, pxh);
}
void OImage::setWHByPx(int pw, int ph)
{
pxw = pw;
pxh = ph;
w = ((1.0/r->getAutoCreatedWindow()->getWidth())*pw);
h = ((1.0/r->getAutoCreatedWindow()->getHeight())*ph);
}
//
// to move the image between -1 and 1
//
void OImage::moveX(float px)
{
x += px;
}
//
// to move the image between -1 and 1
//
void OImage::moveY(float py)
{
y -= py;
}
//
// to set the image
//
void OImage::setX(float px)
{
x = px;
}
//
// to set the image
//
void OImage::setY(float py)
{
y = py;
}
float OImage::getX()
{
return x;
}
float OImage::getY()
{
return y;
}
float OImage::getW()
{
return w;
}
float OImage::getH()
{
return h;
}
//
// sets wether the image is blitted or not
//
void OImage::setVisible(bool vis)
{
visible = vis;
}
//
// will the image be blitted ?
//
bool OImage::getVisible()
{
return visible;
}
//
// changes the images from the image array
//
void OImage::setAnimated(bool anim)
{
animated = anim;
}
//
// is it animated ?
//
bool OImage::getAnimated()
{
return animated;
}
//
// Changes the time (in MS) between the images (animation)
//
void OImage::setAnimationspeed(int time)
{
animSpeed = time;
}
//
// Get the animationsoeed
//
int OImage::getAnimationspeed()
{
return animSpeed;
}
This is a Example:
OImage* anim;
cursor = new OImage(r, “anim1.png”);
cursor->addImage(”anim2.png”);
cursor->addImage(”anim3.png”); …
cursor->setWHByPx(32,32);
// In die mitte des Fensters setzen. r = Root-Objekt.
cursor->setXYByPx(r->getAutoCreatedWindow()->getWidth()/2,r->getAutoCreatedWindow()->getHeight()/2);
// Animation:
cursor->setAnimated(1);
cursor->setAnimationspeed(15);
// In frameStarted():
cursor->draw();