Hey guys, this is gonna be a really stupid thing, but I've been starring at this for a while now and I've not sussed it...
1) Yes I have used sprites... no I don't want to use textured quads, I will in future, but not this time... :)
2) This is in D3D 9
3) There isn't one, here's the description...
I've written a class, I called it CNumber. It's job is to create a *simple* imaged number (By taking individual number textures and 'building' the number - left aligned).
I pass the 'Draw' method a number as an interger, it splits it up into individual components (I've maxed the number at '999 999' - simply as it's impossible to score even near it in the game I'm creating (simple space invaders clone). Also passed to the method is the draw position on the screen x, and y.
'Number.h'
#ifndef NUMBER_H
#define NUMBER_H
#include <d3d9.h>
#include <d3dx9.h>
#include "utils.h"
namespace NTextures {
typedef D3DXIMAGE_INFO IINFO;
static IDirect3DTexture9* _p0Texture;
static IDirect3DTexture9* _p1Texture;
static IDirect3DTexture9* _p2Texture;
static IDirect3DTexture9* _p3Texture;
static IDirect3DTexture9* _p4Texture;
static IDirect3DTexture9* _p5Texture;
static IDirect3DTexture9* _p6Texture;
static IDirect3DTexture9* _p7Texture;
static IDirect3DTexture9* _p8Texture;
static IDirect3DTexture9* _p9Texture;
void Load(LPDIRECT3DDEVICE9, IINFO);
void Clean();
IDirect3DTexture9* ChooseTexture(int i);
}
static const int UNITS = 1;
static const int TENS = 2;
static const int HUNDREDS = 3;
static const int THOUSANDS = 4;
static const int TENS_OF_THOUSANDS = 5;
static const int HUNDREDS_OF_THOUSANDS = 6;
class CNumber {
public:
CNumber(LPDIRECT3DDEVICE9 dev, bool detailed);
~CNumber();
void Draw(int number, int xPos, int yPos);
private:
void CalculateSize(int num);
void CreateSprites(LPDIRECT3DDEVICE9 dev);
void CleanSprites();
int _iSize;
bool _bIsDetailed;
short _sUn;
short _sTe;
short _sHu;
short _sTh;
short _sTeTh;
short _sHuTh;
ID3DXSprite* _pUnitsSprite ;
ID3DXSprite* _pTensSprite ;
ID3DXSprite* _pHundredsSprite ;
ID3DXSprite* _pThousandsSprite ;
ID3DXSprite* _pTensThousandsSprite;
ID3DXSprite* _pHundThousandsSprite;
};
#endif
'Number.cpp'
#include "Number.h"
void NTextures::Load(LPDIRECT3DDEVICE9 _pd3dDevice, IINFO d3dxImageInfo){
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p0Texture, "./Images/Text/0.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p1Texture, "./Images/Text/1.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p2Texture, "./Images/Text/2.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p3Texture, "./Images/Text/3.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p4Texture, "./Images/Text/4.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p5Texture, "./Images/Text/5.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p6Texture, "./Images/Text/6.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p7Texture, "./Images/Text/7.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p8Texture, "./Images/Text/8.jpg", 28, 28);
Util::myLoadTexture(_pd3dDevice, d3dxImageInfo, _p9Texture, "./Images/Text/9.jpg", 28, 28);
}
void NTextures::Clean(){
if( _p0Texture != NULL )
_p0Texture->Release();
if( _p1Texture != NULL )
_p1Texture->Release();
if( _p2Texture != NULL )
_p2Texture->Release();
if( _p3Texture != NULL )
_p3Texture->Release();
if( _p4Texture != NULL )
_p4Texture->Release();
if( _p5Texture != NULL )
_p5Texture->Release();
if( _p6Texture != NULL )
_p6Texture->Release();
if( _p7Texture != NULL )
_p7Texture->Release();
if( _p8Texture != NULL )
_p8Texture->Release();
if( _p9Texture != NULL )
_p9Texture->Release();
}
IDirect3DTexture9* NTextures::ChooseTexture(int i){
switch(i){
case 0:
return _p0Texture;
case 1:
return _p1Texture;
case 2:
return _p2Texture;
case 3:
return _p3Texture;
case 4:
return _p4Texture;
case 5:
return _p5Texture;
case 6:
return _p6Texture;
case 7:
return _p7Texture;
case 8:
return _p8Texture;
case 9:
return _p9Texture;
}
return 0;
}
CNumber::CNumber(LPDIRECT3DDEVICE9 dev, bool detailed){
// do nothing
_bIsDetailed = detailed;
CreateSprites(dev);
}
CNumber::~CNumber(){
// clean up
CleanSprites();
}
void CNumber::Draw(int num, int x, int y){
int i = 0; // position counter
IDirect3DTexture9* tempTexture = 0;
float xR = 1.0f, yR = 1.0f;
if(!_bIsDetailed){
xR = 0.80f; yR = 0.75f;
}
CalculateSize(num);
switch(_iSize){
case HUNDREDS_OF_THOUSANDS:
tempTexture = NTextures::ChooseTexture(_sHuTh);
Util::DrawSprite( _pHundThousandsSprite, tempTexture, (int)(28*xR), (int)(28*yR), (float)x+(28.0f*i), (float)y );
++i;
case TENS_OF_THOUSANDS:
tempTexture = NTextures::ChooseTexture(_sTeTh);
Util::DrawSprite( _pTensThousandsSprite, tempTexture, (int)(28*xR), (int)(28*yR), (float)x+(28.0f*i), (float)y );
++i;
case THOUSANDS:
tempTexture = NTextures::ChooseTexture(_sTh);
Util::DrawSprite( _pThousandsSprite, tempTexture, (int)(28*xR), (int)(28*yR), (float)x+(28.0f*i), (float)y );
++i;
case HUNDREDS:
tempTexture = NTextures::ChooseTexture(_sHu);
Util::DrawSprite( _pHundredsSprite, tempTexture, (int)(28*xR), (int)(28*yR), (float)x+(28.0f*i), (float)y );
++i;
case TENS:
tempTexture = NTextures::ChooseTexture(_sTe);
Util::DrawSprite( _pTensSprite, tempTexture, (int)(28*xR), (int)(28*yR), (float)x+(28.0*i), (float)y );
++i;
case UNITS:
tempTexture = NTextures::ChooseTexture(_sUn);
Util::DrawSprite( _pUnitsSprite, tempTexture, (int)(28*xR), (int)(28*yR), (float)x+(28.0*i), (float)y );
default:
break;
}
}
void CNumber::CalculateSize(int num){
bool bSizeChosen = false;
if( num >= 100000 ){
_iSize = HUNDREDS_OF_THOUSANDS;
bSizeChosen = true;
_sHuTh = (short)(num/100000);
num -= _sHuTh*100000;
}
if( num >= 10000 ){
if(!bSizeChosen){
_iSize = TENS_OF_THOUSANDS;
bSizeChosen = true;
}
_sTeTh = (short)(num/10000);
num -= _sTeTh*10000;
}
if( num >= 1000 ){
if(!bSizeChosen){
_iSize = THOUSANDS;
bSizeChosen = true;
}
_sTh = (short)(num/1000);
num -= _sTh*1000;
}
if( num >= 100 ){
if(!bSizeChosen){
_iSize = HUNDREDS;
bSizeChosen = true;
}
_sHu = (short)(num/100);
num -= _sHu*100;
}
if( num >= 10 ){
if(!bSizeChosen){
_iSize = TENS;
bSizeChosen = true;
}
_iSize = TENS;
_sTe = (short)(num/10);
num -= _sTe*10;
}
if( num >= 0 ){
if(!bSizeChosen){
_iSize = UNITS;
bSizeChosen = true;
}
_sUn = (short)(num/1);
}
}
void CNumber::CreateSprites(LPDIRECT3DDEVICE9 dev){
D3DXCreateSprite( dev, &_pHundThousandsSprite );
D3DXCreateSprite( dev, &_pTensThousandsSprite );
D3DXCreateSprite( dev, &_pThousandsSprite );
D3DXCreateSprite( dev, &_pHundredsSprite );
D3DXCreateSprite( dev, &_pTensSprite );
D3DXCreateSprite( dev, &_pUnitsSprite );
}
void CNumber::CleanSprites(){
if( _pUnitsSprite != NULL )
_pUnitsSprite->Release() ;
if( _pTensSprite != NULL )
_pTensSprite->Release() ;
if( _pHundredsSprite != NULL )
_pHundredsSprite->Release() ;
if( _pThousandsSprite != NULL )
_pThousandsSprite->Release() ;
if( _pTensThousandsSprite != NULL )
_pTensThousandsSprite->Release() ;
if( _pHundThousandsSprite != NULL )
_pHundThousandsSprite->Release() ;
}
'Utils.h'
#ifndef UTILS_H
#define UTILS_H
#include <d3d9.h>
#include <d3dx9.h>
namespace Util {
typedef IDirect3DDevice9* PDEVICE;
typedef LPDIRECT3DTEXTURE9& RPTEXTURE;
typedef D3DXIMAGE_INFO IINFO;
// collection of utility functions.
void MakeRect(RECT* src, int top, int left, int bottom, int right);
void myLoadTexture(PDEVICE, IINFO, RPTEXTURE, char*, int, int);
void DrawSprite(ID3DXSprite* pSprite, RPTEXTURE texture, int xSize, int ySize, float xPos, float yPos);
};
#endif
'Utils.cpp'
#include "utils.h"
void Util::MakeRect(RECT *src, int t, int l, int b, int r){
src->top = t;
src->left = l;
src->bottom = b;
src->right = r;
}
void Util::myLoadTexture(PDEVICE device, IINFO iInfo, RPTEXTURE texture, char* filename, int width, int height){
D3DXCreateTextureFromFileEx( device,
filename,
width,
height,
1,
D3DPOOL_DEFAULT,
D3DFMT_UNKNOWN,
D3DPOOL_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f),
&iInfo,
NULL,
&texture );
}
void Util::DrawSprite(ID3DXSprite* pSprite, RPTEXTURE texture, int xSize, int ySize, float xPos, float yPos){
RECT theRect;
Util::MakeRect(&theRect, 0, 0, ySize, xSize);
D3DXVECTOR3 vCenter( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vTopCorner( xPos, yPos, 0.0f );
pSprite->Begin(D3DXSPRITE_ALPHABLEND);
pSprite->Draw( texture,
&theRect,
&vCenter,
&vTopCorner,
0xffffffff /* Colour Filter A = ff, R = ff, G = ff, B = ff */ );
pSprite->End();
}
The util files listed just in-case any questions arise on these, these are fine though all methods tried and tested. (Might not be the best way, but to get a full game working, they work. Comments welcome though :) )
Ok, the numbers draw fine up-to 99, then things start to go wrong...
All help appreciated,
Many thanks,
Add
[Edited by - Add on May 25, 2005 2:38:50 PM]
"The FFT - an algorithm the whole family can use" ... and for my next joke...