0001:
0002:
0003:
0004:
0005:
0006: #define STRICT
0007: #include <windows.h>
0008: #include <commctrl.h>
0009: #include <commdlg.h>
0010: #include <basetsd.h>
0011: #include <math.h>
0012: #include <stdio.h>
0013: #include <d3dx9.h>
0014: #include <dxerr9.h>
0015: #include <tchar.h>
0016: #include "Common/DXUtil.h"
0017: #include "Common/D3DEnumeration.h"
0018: #include "Common/D3DSettings.h"
0019: #include "Common/D3DApp.h"
0020: #include "Common/D3DFont.h"
0021: #include "Common/D3DFile.h"
0022: #include "Common/D3DUtil.h"
0023: #include "resource.h"
0024: #include "main.h"
0025:
0026:
0027: #define RS m_pd3dDevice->SetRenderState
0028: #define TSS m_pd3dDevice->SetTextureStageState
0029: #define SAMP m_pd3dDevice->SetSamplerState
0030:
0031: #define frand() ((float)rand()/RAND_MAX)
0032:
0033: #if 0
0034: #define TEX_WIDTH 2
0035: #define TEX_HEIGHT 2
0036: #define LOG_SIZE 2
0037: #else
0038: #if 0
0039: #define TEX_WIDTH 4
0040: #define TEX_HEIGHT 4
0041: #define LOG_SIZE 4
0042: #else
0043: #if 0
0044: #define TEX_WIDTH 16
0045: #define TEX_HEIGHT 16
0046: #define LOG_SIZE 8
0047: #else
0048: #if 0
0049: #define TEX_WIDTH 256
0050: #define TEX_HEIGHT 256
0051: #define LOG_SIZE 16
0052: #else
0053: #if 1
0054: #define TEX_WIDTH 512
0055: #define TEX_HEIGHT 512
0056: #define LOG_SIZE 18
0057: #else
0058: #if 1
0059: #define TEX_WIDTH 1024
0060: #define TEX_HEIGHT 1024
0061: #define LOG_SIZE 20
0062: #else
0063: #endif
0064: #endif
0065: #endif
0066: #endif
0067: #endif
0068: #endif
0069:
0070:
0071:
0072:
0073: typedef struct {
0074: FLOAT p[4];
0075: FLOAT tu, tv;
0076: } TVERTEX;
0077:
0078:
0079:
0080:
0081: CMyD3DApplication* g_pApp = NULL;
0082: HINSTANCE g_hInst = NULL;
0083:
0084:
0085:
0086:
0087:
0088:
0089: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0090: {
0091: CMyD3DApplication d3dApp;
0092:
0093: g_pApp = &d3dApp;
0094: g_hInst = hInst;
0095:
0096: InitCommonControls();
0097: if( FAILED( d3dApp.Create( hInst ) ) )
0098: return 0;
0099:
0100: return d3dApp.Run();
0101: }
0102:
0103:
0104:
0105:
0106:
0107:
0108:
0109:
0110: CMyD3DApplication::CMyD3DApplication()
0111: {
0112: m_pSeedTex = NULL;
0113:
0114: m_pSortTex[0] = NULL;
0115: m_pSortTex[1] = NULL;
0116: m_pSortSurf[0] = NULL;
0117: m_pSortSurf[1] = NULL;
0118: m_id = 0;
0119: this->SetState(STATE_INIT);
0120:
0121: m_pEffect = NULL;
0122: m_hTechnique = NULL;
0123: m_htSrcMap = NULL;
0124: m_hstage = NULL;
0125: m_hstepno = NULL;
0126: m_hoffset = NULL;
0127:
0128: m_dwCreationWidth = 512+20;
0129: m_dwCreationHeight = 512+20;
0130: m_strWindowTitle = TEXT( "main" );
0131: m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
0132: m_bStartFullscreen = false;
0133: m_bShowCursorWhenFullscreen = false;
0134:
0135: m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0136: m_bLoadingApp = TRUE;
0137: m_bDispText = FALSE;
0138:
0139: ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0140: }
0141:
0142:
0143:
0144:
0145:
0146:
0147:
0148:
0149: VOID WINAPI CreateSeedTex (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord,
0150: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0151: {
0152: UNREFERENCED_PARAMETER( pTexCoord );
0153: UNREFERENCED_PARAMETER( pTexelSize );
0154: UNREFERENCED_PARAMETER( pData );
0155:
0156: #if 0
0157: float c = pTexCoord->x/pTexelSize->x-0.5;
0158: c = TEX_HEIGHT-c;
0159: pOut->x =
0160: pOut->y =
0161: pOut->z =
0162: pOut->w = c/TEX_HEIGHT;
0163: #else
0164: pOut->x =
0165: pOut->y =
0166: pOut->z =
0167: pOut->w = frand();
0168: #endif
0169: }
0170:
0171:
0172:
0173:
0174:
0175:
0176:
0177:
0178: CMyD3DApplication::~CMyD3DApplication()
0179: {
0180: }
0181:
0182:
0183:
0184:
0185:
0186:
0187:
0188:
0189:
0190:
0191: HRESULT CMyD3DApplication::OneTimeSceneInit()
0192: {
0193:
0194: SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0195:
0196: m_bLoadingApp = FALSE;
0197:
0198: return S_OK;
0199: }
0200:
0201:
0202:
0203:
0204:
0205:
0206:
0207:
0208: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0209: DWORD dwBehavior, D3DFORMAT Format )
0210: {
0211: UNREFERENCED_PARAMETER( Format );
0212: UNREFERENCED_PARAMETER( dwBehavior );
0213: UNREFERENCED_PARAMETER( pCaps );
0214:
0215:
0216:
0217: if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0218: return E_FAIL;
0219:
0220: return S_OK;
0221: }
0222:
0223:
0224:
0225:
0226:
0227:
0228:
0229:
0230:
0231:
0232:
0233: HRESULT CMyD3DApplication::InitDeviceObjects()
0234: {
0235: HRESULT hr;
0236:
0237:
0238: LPD3DXBUFFER pErr;
0239: if( FAILED( hr = D3DXCreateEffectFromFile(
0240: m_pd3dDevice, "hlsl.fx", NULL, NULL,
0241: D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0242: MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0243: , "ERROR", MB_OK);
0244: return DXTRACE_ERR( "CreateEffectFromFile", hr );
0245: }
0246: m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0247: m_htSrcMap = m_pEffect->GetParameterByName( NULL, "SrcMap" );
0248: m_hstage = m_pEffect->GetParameterByName( NULL, "stage" );
0249: m_hstepno = m_pEffect->GetParameterByName( NULL, "stepno" );
0250: m_hoffset = m_pEffect->GetParameterByName( NULL, "offset" );
0251:
0252: D3DXVECTOR4 BufInfo = D3DXVECTOR4(TEX_WIDTH, TEX_HEIGHT, 0,0);
0253: m_pEffect->SetVector("BufInfo", &BufInfo);
0254:
0255:
0256: m_pFont->InitDeviceObjects( m_pd3dDevice );
0257:
0258: return S_OK;
0259: }
0260:
0261:
0262:
0263:
0264:
0265:
0266:
0267:
0268:
0269: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0270: {
0271: HRESULT hr;
0272: int i;
0273:
0274:
0275: this->SetState(STATE_INIT);
0276:
0277:
0278: m_pEffect->OnResetDevice();
0279:
0280:
0281: if( FAILED(hr = m_pd3dDevice->CreateTexture(TEX_WIDTH, TEX_HEIGHT, 1
0282: , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8
0283: , D3DPOOL_DEFAULT, &m_pSeedTex, NULL)))
0284: return hr;
0285:
0286:
0287:
0288: for(i=0;i<2;i++){
0289: if (FAILED(m_pd3dDevice->CreateTexture(TEX_WIDTH, TEX_HEIGHT, 1
0290: , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8
0291: , D3DPOOL_DEFAULT, &m_pSortTex[i], NULL)))
0292: return E_FAIL;
0293: if (FAILED(m_pSortTex[i]->GetSurfaceLevel(0, &m_pSortSurf[i])))
0294: return E_FAIL;
0295: }
0296:
0297:
0298:
0299: RS( D3DRS_DITHERENABLE, FALSE );
0300: RS( D3DRS_SPECULARENABLE, FALSE );
0301: RS( D3DRS_ZENABLE, FALSE );
0302: RS( D3DRS_LIGHTING, FALSE );
0303:
0304: TSS(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
0305: TSS(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0306: TSS(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0307:
0308: SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
0309: SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
0310: SAMP( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0311: SAMP( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0312:
0313:
0314: m_pFont->RestoreDeviceObjects();
0315:
0316: return S_OK;
0317: }
0318:
0319:
0320:
0321:
0322:
0323:
0324:
0325:
0326: HRESULT CMyD3DApplication::FrameMove()
0327: {
0328:
0329: UpdateInput( &m_UserInput );
0330:
0331: return S_OK;
0332: }
0333:
0334:
0335:
0336:
0337: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0338: {
0339: pUserInput->bRotateUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000) == 0x8000 );
0340: pUserInput->bRotateDown = ( m_bActive && (GetAsyncKeyState( VK_DOWN ) & 0x8000) == 0x8000 );
0341: pUserInput->bRotateLeft = ( m_bActive && (GetAsyncKeyState( VK_LEFT ) & 0x8000) == 0x8000 );
0342: pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0343: }
0344:
0345:
0346:
0347:
0348:
0349:
0350: BOOL CMyD3DApplication::Sort()
0351: {
0352:
0353:
0354:
0355: int step = m_cnt;
0356: int rank;
0357: for(rank = 0; rank<step ; rank++){
0358: step -= rank+1;
0359: }
0360:
0361: float stepno = (float)(1<<(rank+1));
0362: float offset = (float)(1<<(rank-step));
0363: float stage = 2*offset;
0364:
0365: if(LOG_SIZE<=rank) return FALSE;
0366:
0367:
0368:
0369:
0370: m_id=1-m_id;
0371: m_pd3dDevice->SetRenderTarget(0, m_pSortSurf[m_id]);
0372: m_pd3dDevice->SetDepthStencilSurface(NULL);
0373:
0374: D3DVIEWPORT9 viewport = {0,0
0375: , TEX_WIDTH
0376: , TEX_HEIGHT
0377: , 0.0f,1.0f};
0378: m_pd3dDevice->SetViewport(&viewport);
0379:
0380: if( m_pEffect != NULL )
0381: {
0382:
0383:
0384:
0385: m_pEffect->SetTechnique( m_hTechnique );
0386: m_pEffect->Begin( NULL, 0 );
0387: m_pEffect->Pass( 0 );
0388:
0389:
0390:
0391:
0392: static CONST FLOAT w = TEX_WIDTH;
0393: static CONST FLOAT h = TEX_HEIGHT;
0394: static CONST TVERTEX Vertex1[4] = {
0395:
0396: { 0, 0, 0.1f, 1, 0, 0,},
0397: { w, 0, 0.1f, 1, 1, 0,},
0398: { w, h, 0.1f, 1, 1, 1,},
0399: { 0, h, 0.1f, 1, 0, 1,},
0400: };
0401: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1);
0402: m_pEffect->SetTexture( m_htSrcMap, m_pSortTex [1-m_id]);
0403:
0404: m_pEffect->SetFloat(m_hstage, stage);
0405: m_pEffect->SetFloat(m_hstepno, stepno);
0406: m_pEffect->SetFloat(m_hoffset, offset);
0407:
0408: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0409: , 2, Vertex1, sizeof( TVERTEX ) );
0410:
0411: m_pEffect->End();
0412: }
0413:
0414: return TRUE;
0415: }
0416:
0417:
0418:
0419:
0420: int CMyD3DApplication::RenderStateSort()
0421: {
0422: int ret = 0;
0423: HRESULT hr;
0424:
0425: LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0426: D3DVIEWPORT9 oldViewport;
0427:
0428:
0429:
0430:
0431: Sleep(50);
0432:
0433:
0434:
0435:
0436: m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0437: m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0438: m_pd3dDevice->GetViewport(&oldViewport);
0439:
0440:
0441:
0442:
0443: if(0==m_cnt){
0444:
0445: if( FAILED(hr = D3DXFillTexture(m_pSeedTex, CreateSeedTex, NULL))) return hr;
0446:
0447:
0448: m_pd3dDevice->SetRenderTarget(0, m_pSortSurf[m_id]);
0449: m_pd3dDevice->SetDepthStencilSurface(NULL);
0450:
0451: D3DVIEWPORT9 viewport = {0,0
0452: , TEX_WIDTH
0453: , TEX_HEIGHT
0454: , 0.0f,1.0f};
0455: m_pd3dDevice->SetViewport(&viewport);
0456:
0457:
0458: TVERTEX VertexSeed[4] = {
0459:
0460: { 0, 0, 0, 1, 0, 0,},
0461: { TEX_WIDTH, 0, 0, 1, 1, 0,},
0462: { TEX_WIDTH,TEX_HEIGHT, 0, 1, 1, 1,},
0463: { 0,TEX_HEIGHT, 0, 1, 0, 1,},
0464: };
0465: m_pd3dDevice->SetFVF(D3DFVF_XYZRHW| D3DFVF_TEX1);
0466: m_pd3dDevice->SetTexture( 0, m_pSeedTex );
0467: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0468: , 2, VertexSeed, sizeof( TVERTEX ) );
0469: }
0470:
0471:
0472:
0473:
0474: BOOL bDo;
0475: bDo = this->Sort();
0476:
0477:
0478:
0479:
0480: m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0481: m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0482: m_pd3dDevice->SetViewport(&oldViewport);
0483:
0484: pOldBackBuffer->Release();
0485: pOldZBuffer->Release();
0486:
0487:
0488:
0489:
0490: if(bDo){
0491:
0492: m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(50,100,200,0), 0, 0);
0493: }else{
0494:
0495: m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(255,50,50,0), 0, 0);
0496: }
0497:
0498: float w = (float)this->m_d3dsdBackBuffer.Width -20;
0499: float h = (float)this->m_d3dsdBackBuffer.Height-20;
0500: TVERTEX VertexFinal[4] = {
0501:
0502: { 10+0, 10, 0, 1, 0, 0,},
0503: { 10+w, 10, 0, 1, 1, 0,},
0504: { 10+w, 10+h,0, 1, 1, 1,},
0505: { 10+0, 10+h,0, 1, 0, 1,},
0506: };
0507: m_pd3dDevice->SetFVF(D3DFVF_XYZRHW| D3DFVF_TEX1);
0508: m_pd3dDevice->SetTexture( 0, m_pSortTex[m_id] );
0509: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0510: , 2, VertexFinal, sizeof( TVERTEX ) );
0511:
0512:
0513:
0514:
0515: int max = LOG_SIZE*(LOG_SIZE+1)/2;
0516: if(max+100<=m_cnt+1){ret = -1;}
0517:
0518:
0519: return ret;
0520: }
0521:
0522:
0523:
0524:
0525: HRESULT CMyD3DApplication::Render()
0526: {
0527:
0528:
0529:
0530: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0531: {
0532: switch(m_iState){
0533: case STATE_INIT:
0534: m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0, 0, 0);
0535: this->SetState(STATE_SORT);
0536: break;
0537: case STATE_SORT:
0538: if(this->RenderStateSort()) this->SetState(STATE_INIT);
0539: break;
0540: }
0541:
0542:
0543: if(m_bDispText)RenderText();
0544:
0545:
0546: m_pd3dDevice->EndScene();
0547: }
0548:
0549: m_cnt++;
0550:
0551: return S_OK;
0552: }
0553:
0554:
0555:
0556:
0557:
0558:
0559:
0560:
0561: HRESULT CMyD3DApplication::RenderText()
0562: {
0563: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
0564: TCHAR szMsg[MAX_PATH] = TEXT("");
0565:
0566: FLOAT fNextLine = 40.0f;
0567:
0568:
0569: fNextLine = (FLOAT) m_d3dsdBackBuffer.Height;
0570: lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0571: fNextLine -= 20.0f;
0572: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0573:
0574: lstrcpy( szMsg, m_strDeviceStats );
0575: fNextLine -= 20.0f;
0576: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0577: lstrcpy( szMsg, m_strFrameStats );
0578: fNextLine -= 20.0f;
0579: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0580:
0581: return S_OK;
0582: }
0583:
0584:
0585:
0586:
0587:
0588:
0589:
0590:
0591: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
0592: WPARAM wParam, LPARAM lParam )
0593: {
0594: switch( msg )
0595: {
0596: case WM_PAINT:
0597: {
0598: if( m_bLoadingApp )
0599: {
0600:
0601: HDC hDC = GetDC( hWnd );
0602: TCHAR strMsg[MAX_PATH];
0603: wsprintf(strMsg, TEXT("Loading... Please wait"));
0604: RECT rct;
0605: GetClientRect( hWnd, &rct );
0606: DrawText( hDC, strMsg, -1, &rct
0607: , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0608: ReleaseDC( hWnd, hDC );
0609: }
0610: break;
0611: }
0612: case WM_COMMAND:
0613: switch( LOWORD(wParam) ){
0614: case ID_MENUITEM_INFO:
0615:
0616: m_bDispText ^= 1;
0617: MENUITEMINFO menu;
0618: ZeroMemory(&menu, sizeof(MENUITEMINFO));
0619: menu.cbSize = sizeof(MENUITEMINFO);
0620: menu.fMask = MIIM_STATE;
0621: if(m_bDispText)menu.fState = MFS_CHECKED;
0622: SetMenuItemInfo(GetMenu(this->m_hWnd), ID_MENUITEM_INFO, FALSE, &menu);
0623: break;
0624: }
0625:
0626: }
0627:
0628: return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0629: }
0630:
0631:
0632:
0633:
0634:
0635:
0636:
0637:
0638: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0639: {
0640: SAFE_RELEASE(m_pSortSurf[1]);
0641: SAFE_RELEASE(m_pSortSurf[0]);
0642: SAFE_RELEASE(m_pSortTex[1]);
0643: SAFE_RELEASE(m_pSortTex[0]);
0644:
0645: SAFE_RELEASE( m_pSeedTex );
0646:
0647:
0648: if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
0649:
0650: m_pFont->InvalidateDeviceObjects();
0651:
0652: return S_OK;
0653: }
0654:
0655:
0656:
0657:
0658:
0659:
0660:
0661:
0662: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0663: {
0664:
0665: SAFE_RELEASE( m_pEffect );
0666:
0667:
0668: m_pFont->DeleteDeviceObjects();
0669:
0670: return S_OK;
0671: }
0672:
0673:
0674:
0675:
0676:
0677:
0678:
0679:
0680: HRESULT CMyD3DApplication::FinalCleanup()
0681: {
0682: SAFE_DELETE( m_pFont );
0683:
0684: return S_OK;
0685: }
0686:
0687:
0688:
0689:
0690: