00001
00002 #include "DxBaseClass.h"
00003
00004
00005 using namespace jbxwl;
00006
00007
00009
00010
00011 BEGIN_MESSAGE_MAP(jbxwl::CDxBaseView, CExView)
00012 END_MESSAGE_MAP()
00013
00014
00015 CDxBaseView::CDxBaseView()
00016 {
00017 lpD3D = NULL;
00018 lpD3DDevice = NULL;
00019 lpBackBuffer = NULL;
00020 lpSurface = NULL;
00021 lpTexture = NULL;
00022
00023 lpDInput = NULL;
00024 lpDMouse = NULL;
00025 lpDKeyBoard = NULL;
00026
00027 bufferSize.x = bufferSize.y = 0;
00028 }
00029
00030
00031
00032 CDxBaseView::~CDxBaseView()
00033 {
00034
00035 if (pApp!=NULL) pApp->ViewDestructor(this);
00036
00037
00038
00039 DXRELEASE(lpTexture);
00040 DXRELEASE(lpSurface);
00041 DXRELEASE(lpBackBuffer);
00042 DXRELEASE(lpD3DDevice);
00043 }
00044
00045
00046
00053 BOOL CDxBaseView::InitDevice(int xsize, int ysize, CWnd* cwnd)
00054 {
00055 lpD3D = GpD3D;
00056 if (cwnd ==NULL) cwnd = this;
00057 SetParameter(&d3dParam, xsize, ysize);
00058
00059 lpD3DDevice = Dx9CreateGraphic(lpD3D, &d3dParam, cwnd->m_hWnd);
00060 if (lpD3DDevice==NULL) return FALSE;
00061
00062
00063 lpDMouse = GpDMouse;
00064 lpDKeyBoard = GpDKeyBoard;
00065
00066 return TRUE;
00067 }
00068
00069
00070
00071 void CDxBaseView::ClearObject()
00072 {
00073 hasViewData = FALSE;
00074 DXRELEASE(lpTexture);
00075 DXRELEASE(lpBackBuffer);
00076 DXRELEASE(lpSurface);
00077 }
00078
00079
00080
00081
00082
00083 void CDxBaseView::SetParameter(D3DPRESENT_PARAMETERS* d3dParam, int xsize, int ysize)
00084 {
00085 RECT rect;
00086
00087 if (xsize>0 && ysize>0) {
00088 rect.right = xsize;
00089 rect.bottom = ysize;
00090 }
00091 else {
00092 this->GetClientRect(&rect);
00093 }
00094 rect.right = (int)(rect.right*1.5);
00095 rect.bottom = (int)(rect.bottom*1.5);
00096
00097 ZeroMemory(d3dParam, sizeof(D3DPRESENT_PARAMETERS));
00098 d3dParam->BackBufferWidth = rect.right;
00099 d3dParam->BackBufferHeight = rect.bottom;
00100 d3dParam->BackBufferCount = 1;
00101 d3dParam->Windowed = TRUE;
00102 d3dParam->SwapEffect = D3DSWAPEFFECT_DISCARD;
00103
00104 d3dParam->BackBufferFormat = D3DFMT_UNKNOWN;
00105 d3dParam->EnableAutoDepthStencil = TRUE;
00106 d3dParam->AutoDepthStencilFormat = D3DFMT_D16;
00107 }
00108
00109
00110
00111
00112
00113 POINT CDxBaseView::GetWindowDisplaySize(POINT pt)
00114 {
00115 D3DDISPLAYMODE disp;
00116
00117 lpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &disp);
00118 int scrSize = Min(disp.Width, disp.Height)*2/3;
00119
00120 int sz = Max(pt.x, pt.y);
00121 if (sz<=0) return pt;
00122
00123 double rs = 1.0;
00124 if (sz>scrSize) {
00125 rs = scrSize/sz;
00126 pt.x = (int)(pt.x*rs);
00127 pt.y = (int)(pt.y*rs);
00128 }
00129
00130
00131 return pt;
00132 }
00133
00134
00147 int CDxBaseView::GetMouseButton()
00148 {
00149 int ret = 0;
00150 DIMOUSESTATE dims;
00151
00152 if (lpDMouse!=NULL && activeWin) {
00153 lpDMouse->Acquire();
00154 HRESULT hr = lpDMouse->GetDeviceState(sizeof(DIMOUSESTATE), &dims);
00155 if (SUCCEEDED(hr)) {
00156 if (dims.rgbButtons[0]==0x80) ret = 1;
00157 else if (dims.rgbButtons[1]==0x80) ret = 2;
00158 else if (dims.rgbButtons[2]==0x80) ret = 3;
00159 }
00160
00161 }
00162 return ret;
00163 }
00164
00165
00166 void CDxBaseView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
00167 {
00168 if (lpDMouse!=NULL) {
00169 lpDMouse->Unacquire();
00170 if (bActivate && pActivateView==this) {
00171 lpDMouse->Acquire();
00172 }
00173 }
00174
00175 CExView::OnActivateView(bActivate, pActivateView, pDeactiveView);
00176 }
00177
00178
00179
00180
00182
00183
00184
00185 BEGIN_MESSAGE_MAP(jbxwl::CDxVTXBaseView, CDxBaseView)
00186 ON_WM_TIMER()
00187 END_MESSAGE_MAP()
00188
00189
00190 CDxVTXBaseView::CDxVTXBaseView()
00191 {
00192 vb = lb = NULL;
00193
00194 rotation = false;
00195 ctrlMouse = true;
00196 reverseZ = false;
00197
00198 angleX = angleY = angleZ = 0.0;
00199 angle = 0.0;
00200 rTime = gTime = 0.0;
00201 lTime = 1.0;
00202 mWheelAc = 0.0;
00203 mWheelNotch = 1.0;
00204
00205 startTime = (double)timeGetTime();
00206
00207 D3DXMatrixIdentity(&matRotate);
00208 D3DXMatrixIdentity(&matTrans);
00209 D3DXMatrixIdentity(&matTemp);
00210 }
00211
00212
00213
00214 CDxVTXBaseView::~CDxVTXBaseView()
00215 {
00216
00217 if (pApp!=NULL) pApp->ViewDestructor(this);
00218
00219 ReleaseVB();
00220 }
00221
00222
00223
00224 void CDxVTXBaseView::SwitchRotation()
00225 {
00226 if (rotation) {
00227 rotation = false;
00228 }
00229 else {
00230 gTime = rTime = (double)timeGetTime() - startTime;
00231 rotation = true;
00232 }
00233 angleX = angleY = angleZ = 0.0;
00234 }
00235
00236
00237
00243 void CDxVTXBaseView::ExecRotation()
00244 {
00245 HRESULT hr;
00246
00247 rTime = (double)timeGetTime() - startTime;
00248
00249 if (rotation) {
00250 angle = angle + (rTime - gTime);
00251 D3DXMatrixRotationY(&matRotateY, (float)angle/(200.0f*10));
00252 D3DXMatrixRotationX(&matRotateX, (float)angle/(100.0f*10));
00253 matRotate = matTemp*matRotateX*matRotateY;
00254 }
00255
00256 else {
00257 DIMOUSESTATE dims;
00258 double mwheel = 0.0;
00259
00260 if (lpDMouse!=NULL && activeWin) {
00261 lpDMouse->Acquire();
00262 hr = lpDMouse->GetDeviceState(sizeof(DIMOUSESTATE), &dims);
00263 if (SUCCEEDED(hr)) {
00264 if (ctrlMouse) {
00265 mwheel = (double)dims.lZ;
00266 if (dims.rgbButtons[0]==0x80) {
00267 POINT pt = GetMousePos();
00268 if (pt.x>=0 && pt.y>=0) {
00269 angleX = (double)dims.lX;
00270 angleY = (double)dims.lY;
00271 ExRotationAngle();
00272 lTime = rTime - gTime;
00273 }
00274 else ctrlMouse = false;
00275 }
00276 }
00277 if (dims.rgbButtons[0]!=0x80 && !ctrlMouse) {
00278 POINT pt = GetMousePos();
00279 if (pt.x>=0 && pt.y>=0) ctrlMouse = true;
00280 }
00281 }
00282 }
00283 if (mwheel!=0.0) {
00284 mWheelAc = mWheelAc - mWheelNotch*mwheel/2000.f;
00285 ExMouseWheel();
00286 }
00287
00288 D3DXMatrixRotationX(&matRotateX, (float)(angleX/150.0*((rTime-gTime)/lTime)));
00289 D3DXMatrixRotationY(&matRotateY, (float)(angleY/150.0*((rTime-gTime)/lTime)));
00290 D3DXMatrixRotationZ(&matRotateZ, (float)(angleZ/150.0*((rTime-gTime)/lTime)));
00291 matRotate = matTemp = matRotate*matRotateX*matRotateY*matRotateZ;
00292 angle = 0.0;
00293 }
00294 gTime = rTime;
00295
00296 matWorld = matTrans*matRotate;
00297 lpD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
00298 }
00299
00300
00301
00303
00304
00305 void CDxVTXBaseView::OnTimer(UINT_PTR nIDEvent)
00306 {
00307 if (hasViewData) {
00308 if (doReSize) ExecWindowReSize(clientRect.right, clientRect.bottom);
00309 else ExecRotation();
00310 ExecRender();
00311 }
00312 else DEBUG_ERROR("CDxVTXBaseView::OnTimer(): ERROR: 表示可能なデータがありません\n");
00313 CDxBaseView::OnTimer(nIDEvent);
00314 }
00315
00316
00317
00318 void CDxVTXBaseView::OnDraw(CDC* pDC)
00319 {
00320 if (hasViewData && timerID==-1) {
00321 if (doReSize) ExecWindowReSize(clientRect.right, clientRect.bottom);
00322 else ExecRotation();
00323 ExecRender();
00324 }
00325 else if (!hasViewData) DEBUG_ERROR("CDxVTXBaseView::OnDraw(): ERROR: 表示可能なデータがありません\n");
00326 }
00327
00328
00329
00331
00350 HRESULT jbxwl::ResetDx9Device(LPDIRECT3DDEVICE9 lpD3DDevice, D3DPRESENT_PARAMETERS* pd3dParam, CDxBaseView* pviw)
00351 {
00352 HRESULT hr = lpD3DDevice->TestCooperativeLevel();
00353 if (hr==D3D_OK) return hr;
00354
00355 do {
00356 Sleep(100);
00357 hr = lpD3DDevice->TestCooperativeLevel();
00358 } while (hr==D3DERR_DEVICELOST);
00359
00360 if (hr==D3DERR_DEVICENOTRESET) {
00361 if (pviw!=NULL) pviw->ClearObject();
00362
00363 hr = lpD3DDevice->Reset(pd3dParam);
00364 if (hr==D3D_OK) {
00365 DEBUG_INFO("jbxwl::ResetDx9Device(): Reseted Dx9 Device.\n");
00366 if (pviw!=NULL) pviw->InitObject();
00367 }
00368 else {
00369 DEBUG_ERROR("jbxwl::ResetDx9Device(): Failed to Reset.\n");
00370 }
00371 }
00372 return hr;
00373 }
00374