0001: // ----------------------------------------------------------------------------
0002: //
0003: // draw.h - Rendering
0004: // 
0005: // Copyright (c) 2002 IMAGIRE Takashi (imagire@gmail.com)
0006: // All Rights Reserved.
0007: //
0008: // ----------------------------------------------------------------------------
0009: #ifndef _DRAW_H
0010: #define _DRAW_H
0011: 
0012: #include <d3d8.h>
0013: #include <d3dx8.h>
0014: 
0015: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDEV);
0016: void Render(LPDIRECT3DDEVICE8 lpD3DDEV);
0017: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDEV);
0018: 
0019: // ----------------------------------------------------------------------------
0020: // Management of the camera
0021: // ----------------------------------------------------------------------------
0022: class CMyView{
0023:     D3DXMATRIX              m_mView;    // View matrix
0024:     D3DXVECTOR4             m_eye;      // camera position
0025:     D3DXVECTOR4             m_lookAt;   // camera look at
0026:     D3DXVECTOR4             m_up;       // up direction
0027: 
0028:     void Calc(){D3DXMatrixLookAtLH(&m_mView,(D3DXVECTOR3*)&m_eye,(D3DXVECTOR3*)&m_lookAt,(D3DXVECTOR3*)&m_up);}
0029: public:
0030:     CMyView(){Init();}
0031:     void Init(){m_eye.x    = 0.0f; m_eye.y    = 1.0f; m_eye.z    = 2.5f; m_eye.w    = 1.0f;
0032:                 m_lookAt.x = 0.0f; m_lookAt.y = 0.5f; m_lookAt.z = 0.0f; m_lookAt.w = 1.0f; 
0033:                 m_up.x     = 0.0f; m_up.y     = 1.0f; m_up.z     = 0.0f; m_up.w     = 1.0f; 
0034:                 this->Calc();}
0035:     void SetEye   (const D3DXVECTOR4    eye){m_eye   =   eye;this->Calc();}
0036:     void SetLookAt(const D3DXVECTOR4 lookAt){m_lookAt=lookAt;this->Calc();}
0037:     void SetUp    (const D3DXVECTOR4     up){m_up    =    up;this->Calc();}
0038:     const D3DXVECTOR4 &GetEye()    const {return m_eye;}
0039:     const D3DXVECTOR4 &GetLookAt() const {return m_lookAt;}
0040:     const D3DXVECTOR4 &GetUp()     const {return m_up;}
0041:     const D3DXMATRIX  &Get()       const {return m_mView;}
0042: };
0043: 
0044: // ----------------------------------------------------------------------------
0045: // bullet
0046: // ----------------------------------------------------------------------------
0047: #define BULLET_SPEED 5.0f
0048: 
0049: class CBullet{
0050: private:
0051:     BOOL            bActive;
0052:     D3DXVECTOR4     pos;
0053:     D3DXVECTOR4     velosity;
0054: public:
0055:     CBullet():bActive(FALSE){;}
0056: 
0057:     void Reset(){bActive=FALSE;}
0058: 
0059:     DWORD Create(D3DXVECTOR4 &x, D3DXVECTOR4 &v){
0060:         if(bActive) return -1;
0061:         bActive = TRUE;
0062:         pos = x;
0063:         velosity = v;
0064:         velosity.w = 0;
0065:         return 0;
0066:     }
0067:     DWORD Update(DWORD dt){
0068:         if(bActive){
0069:             pos += velosity*0.001f*(float)dt;
0070:             pos.w=0.0f;
0071:             // Dead check
0072:             if(pos.x < -10.0f || 10.0f < pos.x
0073:             || pos.y < - 5.0f || 15.0f < pos.y
0074:             || pos.z < -10.0f || 10.0f < pos.z
0075:             ){
0076:                 bActive = FALSE;
0077:                 return 1;
0078:             }
0079:             return 0;
0080:         }
0081:         return -1;
0082:     }
0083:     void Render(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mVP);
0084:     void Draw(LPDIRECT3DDEVICE8 lpD3DDev);
0085:     
0086:     // ----------------------------------------------------------------------------
0087:     // Geter
0088:     BOOL  IsActive() const {return bActive;}
0089:     D3DXVECTOR4 &GetPosition(){return pos;}
0090:     D3DXVECTOR4 &GetVelosity(){return velosity;}
0091: };
0092: // ----------------------------------------------------------------------------
0093: #define BULLET_MAX 10   // The max count what we can shoot
0094: class CBulletMgr{
0095: private:
0096:     CBullet bullet[BULLET_MAX];
0097: public:
0098:     DWORD Add(D3DXVECTOR4 &x, D3DXVECTOR4 &v){
0099:         DWORD i;
0100:         for(i=0;i<BULLET_MAX;i++){
0101:             if(0==bullet[i].Create(x,v)) return 0;
0102:         }
0103:         return -1;
0104:     }
0105:     void Update(DWORD dt){
0106:         for(DWORD i=0;i<BULLET_MAX;i++){
0107:             bullet[i].Update(dt);
0108:         }
0109:     }
0110:     void Render(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mVP){
0111:         for(DWORD i=0;i<BULLET_MAX;i++){
0112:             bullet[i].Render(lpD3DDev, mVP);
0113:         }
0114:     }
0115:     void Reset(){
0116:         for(DWORD i=0;i<BULLET_MAX;i++){
0117:             bullet[i].Reset();
0118:         }
0119:     }
0120:     DWORD GetNum()const{return BULLET_MAX;}
0121:     BOOL  IsActive(DWORD i) const {return bullet[i].IsActive();}
0122:     D3DXVECTOR4 &GetPosition(DWORD i){return bullet[i].GetPosition();}
0123:     D3DXVECTOR4 &GetVelosity(DWORD i){return bullet[i].GetVelosity();}
0124:     VOID Draw(DWORD i, LPDIRECT3DDEVICE8 lpD3DDev){bullet[i].Draw(lpD3DDev);}
0125: };
0126: 
0127: #endif /* !_DRAW_H */
0128: