Navigation
Artikel
Stuff
RSS Feeds
|
Sourcecodes - DirectX Sprite mit ID3DXSPRITESprachenübersicht/C / C++/ C#/Spieleprogrammierung/DirectX Keywords: Sprite, DirectX, 2D Mit diesem Quellcode kann man 2D Sprites unter DirectX mit ID3DXSPRITE darstellen.
sprite.h: #ifndef __SPRITE #define __SPRITE //Header #include <d3d9.h> //File for DirectXGraphics #include <d3dx9.h> //Filfe for DirectXGraphics #include <d3dx9math.h> //Mathlib #include <errorfile.h> //Error file #include <texture_manager.h> //Texture manager file #include <string> //The standard lib string #include <matrix_operations.h> //Matrix & Vector //Uses the standard lib using namespace std; class bn_Sprite { public: //Initialise the objects bn_Sprite(); //Cleans the objects ~bn_Sprite(); //Creates the bn_Sprite void CreateSprite(LPDIRECT3DDEVICE9 lpDevice,bn_TextureManager *pTextureManager,char *pFileName,DWORD dwFlags = D3DXSPRITE_ALPHABLEND,D3DPOOL Pool = D3DPOOL_MANAGED,D3DCOLOR ColorKey = 0); //Set the bn_Sprite on a position void SetXY(int x,int y); //Sets the rotation void SetRotation(float fRotation); //Scales the bn_Sprite void Scale(float fScaleX,float fScaleY); //Sets bAnimated void SetAnimated(bool bAnimated); //Sets the height/width of the animation-rects void SetAnimatedRect(int iNumberOfSteps,int iWidth,int iHeight); //Do an animation step void IncreaseAnimationStep(); //Sets the animation step int SetAnimationStep(int iStep); //Renders the bn_Sprite void Render(int iX,int iY); //Renders a rect of a bn_Sprite void RenderRect(int iX,int iY); //Sets the Alpha void SetAlpha(int iAlpha); //Sets the Rect void SetRect(RECT Rect); //If the device has been reseted void OnResetDevice(); //If the device'll be reseted void OnLostDevice(); protected: //Texture name std::string m_strTextureName; //Texture manager bn_TextureManager *m_pTextureManager; //bn_Sprite class LPD3DXSPRITE m_lpSprite; //actually position bn_Vector m_vPosition; //Rotation center bn_Vector m_vRotationCenter; //Scale bn_Vector m_vScale; //Flags for the bn_Sprite DWORD m_dwFlags; //Sets the alpha value int m_iAlpha; //Rotation angle float m_fRotation; //Variable if the bn_Sprite is animated bool m_bAnimated; //Width;Height; Increase Width of the sprite per Animation Step; MaxWidth int m_iWidth,m_iHeight,m_iWidthPerStep,m_iMaxWidth; //Rect for DrawRect RECT m_DrawRect, m_Rect; }; #endif
sprite.cpp: #include "sprite.h" //Initialise the objects bn_Sprite::bn_Sprite() { //Sets it 0 m_bAnimated = false; m_lpSprite = NULL; m_iAlpha = 255; m_pTextureManager = NULL; m_strTextureName = ""; } //Cleans the objects bn_Sprite::~bn_Sprite() { //Cleans the bn_Sprite if (m_lpSprite != NULL) { m_lpSprite->Release(); m_lpSprite=NULL; } //If the texturemanager exists if (m_strTextureName.c_str() != "") { if (m_pTextureManager) //Deletes the texture if the priority is low m_pTextureManager->DeleteTexture(m_strTextureName.c_str()); } } //Creates the bn_Sprite void bn_Sprite::CreateSprite(LPDIRECT3DDEVICE9 lpDevice,bn_TextureManager *pTextureManager,char *pFileName,DWORD dwFlags,D3DPOOL Pool,D3DCOLOR ColorKey) { D3DXCreateSprite(lpDevice,&m_lpSprite); m_dwFlags = dwFlags; //Sets the texture manager m_pTextureManager = pTextureManager; m_pTextureManager->AddTexture(pFileName,Pool,D3DX_FILTER_NONE,1,ColorKey); m_strTextureName = pFileName; //Gets the information about the struct D3DXIMAGE_INFO ImageInfo; D3DXGetImageInfoFromFile(m_strTextureName.c_str(),&ImageInfo); m_vPosition.x = m_vRotationCenter.x = (float)(ImageInfo.Width/2); m_vPosition.y = m_vRotationCenter.y = (float)(ImageInfo.Height/2); m_vScale = bn_Vector3(1.0f,1.0f,1.0f); } //Set the bn_Sprite on a position void bn_Sprite::SetXY(int x,int y) { m_vPosition.x=(float)x; m_vPosition.y=(float)y; } //Sets die Rotation void bn_Sprite::SetRotation(float fRotation) { //Sets the Rotation state m_fRotation = fRotation; } //Sets bAnimated void bn_Sprite::SetAnimated(bool bAnimated) { m_bAnimated = bAnimated; } //Sets the height/width of the animation-rects void bn_Sprite::SetAnimatedRect(int iNumberOfSteps,int iWidth,int iHeight) { //Sets the maximal width m_iMaxWidth = iNumberOfSteps * iWidth; //Sets the actually width and the width count per step m_iWidthPerStep = m_iWidth = iWidth; //Sets the height m_iHeight = iHeight; //Sets the first rect m_Rect.left = 0; m_Rect.right = iWidth; m_Rect.top = 0; m_Rect.bottom = iHeight; } //Sets the animation step int bn_Sprite::SetAnimationStep(int iStep) { //If step is too high if (iStep > (m_iMaxWidth/m_iWidth)) { return 10; } //Sets the first step SetAnimatedRect((m_iMaxWidth/m_iWidth),m_iWidth,m_iHeight); //Increase the step for (int i = 1; i < iStep;i++) IncreaseAnimationStep(); return 0; } //Do an animation step void bn_Sprite::IncreaseAnimationStep() { //If it was the last animation step if (m_iWidth == m_iMaxWidth) { //Sets the rect and the width m_Rect.left = 0; m_Rect.right = m_iWidthPerStep; m_iWidth = 0; } else { //Increase the step m_iWidth += m_iWidthPerStep; m_Rect.left += m_iWidthPerStep; m_Rect.right += m_iWidthPerStep; } } //Scales the bn_Sprite void bn_Sprite::Scale(float fScaleX,float fScaleY) { m_vScale.x += fScaleX; m_vScale.y += fScaleY; } //Renders the bn_Sprite void bn_Sprite::Render(int iX,int iY) { if (m_pTextureManager->GetTexture(m_strTextureName.c_str()) == 0) { ExceptionGameError("",0,17,"Error, sprite texture doesn't exists.",false); return; } m_vPosition.x = (float)iX + m_vRotationCenter.x; m_vPosition.y = (float)iY + m_vRotationCenter.y; D3DXMATRIX matSpriteMatrix,matTranslation,matScale,matRotation; D3DXMatrixRotationZ(&matRotation,m_fRotation); D3DXMatrixScaling(&matScale,m_vScale.x,m_vScale.y,m_vScale.z); D3DXMatrixTranslation(&matTranslation,m_vPosition.x,m_vPosition.y,m_vPosition.z); matSpriteMatrix = matRotation * matTranslation * matScale; m_lpSprite->Begin(m_dwFlags); m_lpSprite->SetTransform(&matSpriteMatrix); if (m_bAnimated == false) { //Renders it without an animation DXTestForError(m_lpSprite->Draw(m_pTextureManager->GetTexture(m_strTextureName.c_str()),0,(D3DXVECTOR3*)(D3DVECTOR*)&m_vRotationCenter,NULL,D3DCOLOR_ARGB(m_iAlpha,0xff,0xff,0xff)),"Error by drawing the Sprite",16,true); } else { //Renders it with an animation DXTestForError(m_lpSprite->Draw(m_pTextureManager->GetTexture(m_strTextureName.c_str()),&m_Rect,(D3DXVECTOR3*)(D3DVECTOR*)&m_vRotationCenter,NULL,D3DCOLOR_ARGB(m_iAlpha,0xff,0xff,0xff)),"Error by drawing the Sprite",16,true); } //End DXTestForError(m_lpSprite->End(),"Error by drawing the Sprite",16,true); } //If the device has been reseted void bn_Sprite::OnResetDevice() { m_lpSprite->OnResetDevice(); } //If the device'll be reseted void bn_Sprite::OnLostDevice() { m_lpSprite->OnLostDevice(); } //Renders a rect of a bn_Sprite void bn_Sprite::RenderRect(int iX,int iY) { if (m_pTextureManager->GetTexture(m_strTextureName.c_str()) == 0) { ExceptionGameError("",0,17,"Error, sprite texture doesn't exists.",false); return; } m_vPosition.x = (float)iX + m_vRotationCenter.x; m_vPosition.y = (float)iY + m_vRotationCenter.y; D3DXMATRIX matSpriteMatrix,matTranslation,matScale,matRotation; //Sets the matrix D3DXMatrixRotationZ(&matRotation,m_fRotation); D3DXMatrixScaling(&matScale,m_vScale.x,m_vScale.y,m_vScale.z); D3DXMatrixTranslation(&matTranslation,m_vPosition.x,m_vPosition.y,m_vPosition.z); matSpriteMatrix = matRotation * matTranslation * matScale; //Begins with rendering the bn_Sprite m_lpSprite->Begin(m_dwFlags); m_lpSprite->SetTransform(&matSpriteMatrix); //Renders the rect of the bn_Sprite DXTestForError(m_lpSprite->Draw(m_pTextureManager->GetTexture(m_strTextureName.c_str()),&m_DrawRect,(D3DXVECTOR3*)(D3DVECTOR*)&m_vRotationCenter,(D3DXVECTOR3*)(D3DVECTOR*)&m_vPosition,D3DCOLOR_ARGB(m_iAlpha,0xff,0xff,0xff)),"Error by drawing the bn_Sprite",16,true); //End DXTestForError(m_lpSprite->End(),"Error by drawing the Sprite",16,true); } //Sets the Alpha void bn_Sprite::SetAlpha(int iAlpha) { m_iAlpha = iAlpha; } //Sets the Rect void bn_Sprite::SetRect(RECT Rect) { m_DrawRect = Rect; } Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren? Sprachenübersicht/C / C++/ C#/Spieleprogrammierung/DirectX/DirectX Sprite mit ID3DXSPRITE |