HRESULT Application::Init(HINSTANCE hInstance, bool windowed) { g_debug << "Application Started \n"; //Create Window Class WNDCLASS wc; memset(&wc, 0, sizeof(WNDCLASS)); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WndProc; wc.hInstance = hInstance; wc.lpszClassName = "D3DWND"; RECT rc = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false); //Register Class and Create new Window RegisterClass(&wc); m_mainWindow = ::CreateWindow("D3DWND", "Character Animation with Direct3D: Example 11.2", WS_OVERLAPPEDWINDOW, 0, 0, rc.right - rc.left, rc.bottom - rc.top, 0, 0, hInstance, 0); //SetCursor(NULL); ::ShowWindow(m_mainWindow, SW_SHOW); ::UpdateWindow(m_mainWindow); //Create IDirect3D9 Interface IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION); if (d3d9 == NULL) { g_debug << "Direct3DCreate9() - FAILED \n"; return E_FAIL; } //Check that the Device supports what we need from it D3DCAPS9 caps; d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps); //Check vertex & pixelshader versions if (caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0)) { g_debug << "Warning - Your graphic card does not support vertex and pixelshaders version 2.0 \n"; } //Set D3DPRESENT_PARAMETERS m_present.BackBufferWidth = WINDOW_WIDTH; m_present.BackBufferHeight = WINDOW_HEIGHT; m_present.BackBufferFormat = D3DFMT_A8R8G8B8; m_present.BackBufferCount = 2; m_present.MultiSampleType = D3DMULTISAMPLE_NONE; m_present.MultiSampleQuality = 0; m_present.SwapEffect = D3DSWAPEFFECT_DISCARD; m_present.hDeviceWindow = m_mainWindow; m_present.Windowed = windowed; m_present.EnableAutoDepthStencil = true; m_present.AutoDepthStencilFormat = D3DFMT_D24S8; m_present.Flags = 0; m_present.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; m_present.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //Hardware Vertex Processing int vp = 0; if ( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; //Create the IDirect3DDevice9 if (FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow, vp, &m_present, &g_pDevice))) { g_debug << "Failed to create IDirect3DDevice9 \n"; return E_FAIL; } //Release IDirect3D9 interface d3d9->Release(); //Load Application Specific resources here... D3DXCreateFont(g_pDevice, 50, 0, FW_BOLD, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &g_pFont); //Create Sprite D3DXCreateSprite(g_pDevice, &g_pSprite); //Load Effect ID3DXBuffer *pErrorMsgs = NULL; HRESULT hRes = D3DXCreateEffectFromFile(g_pDevice, "resources/fx/lighting.fx", NULL, NULL, D3DXSHADER_DEBUG, NULL, &g_pEffect, &pErrorMsgs); if (FAILED(hRes) && (pErrorMsgs != NULL)) { //Failed to create Effect g_debug << (char*)pErrorMsgs->GetBufferPointer() << "\n"; return E_FAIL; } m_deviceLost = false; //Setup mesh m_drone.Load("resources/meshes/soldier.x"); m_animController = m_drone.GetController(); //Set Still Anim ID3DXAnimationSet *anim = NULL; m_animController->GetAnimationSet(2, &anim); m_animController->SetTrackAnimationSet(0, anim); anim->Release(); //Setup IK m_pIK = new InverseKinematics(&m_drone); //Set Cursor HCURSOR cursor = LoadCursor(NULL, IDC_ARROW); SetCursor(cursor); return S_OK; }
XAsset::XAsset(const char* resourcePath) { // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 0.0f, Engine::instance->progressCallbackUserData ); } IResource* resource = getCore()->getResource( resourcePath, "rb" ); assert( resource ); // retrieve file size FILE* file = resource->getFile(); fseek( file, 0, SEEK_END ); int fileSize = ftell( file ); fseek( file, 0, SEEK_SET ); // create buffer & load file data in buffer char* buffer = new char[fileSize]; fread( buffer, 1, fileSize, file ); resource->release(); // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 0.25f, Engine::instance->progressCallbackUserData ); } // create hierarchy allocation interface XAllocateHierarchy xAlloc; // load hierarchy from file D3DXFRAME* rootFrame; ID3DXAnimationController* animController; _dxCR( D3DXLoadMeshHierarchyFromXInMemory( buffer, fileSize, D3DXMESH_MANAGED, iDirect3DDevice, &xAlloc, NULL, &rootFrame, &animController ) ); delete[] buffer; // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 0.5f, Engine::instance->progressCallbackUserData ); } // create D3 animation objects Clump* clump = new Clump( "SCENE_ROOT" ); clump->setFrame( static_cast<Frame*>( rootFrame ) ); if( animController ) { ID3DXAnimationSet* animationSet; const char* animationName; animController->GetAnimationSet( 0, &animationSet ); unsigned int numAnimations = animationSet->GetNumAnimations(); float period = float( animationSet->GetPeriod() ); float numFramesPerSecond = 100.0f/3.0f; float frameTime = 1.0f / numFramesPerSecond; unsigned int numFrames = unsigned int( period * numFramesPerSecond ); if( !numFrames ) numFrames++; AnimationSet* as = new AnimationSet( numAnimations ); for( unsigned int i=0; i<numAnimations; i++ ) { SRT srt; animationSet->GetAnimationNameByIndex( i, &animationName ); Animation* animation = new Animation( animationName, numFrames ); for( unsigned int j=0; j<numFrames; j++ ) { srt.time = j * frameTime; _dxCR( animationSet->GetSRT( srt.time, i, &srt.scale, &srt.rotation, &srt.translation ) ); animation->setKey( j, &srt ); } assert( animation->validateKeys() ); as->setAnimation( i, animation ); } clump->setAnimation( as ); animationSet->Release(); animController->Release(); } // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 0.75f, Engine::instance->progressCallbackUserData ); } // create D3 classes for hierarchy createD3HierarchyClasses( clump, rootFrame ); // resolve nameless phenomena resolveNamelessFrames( clump ); _clumps.push_back( clump ); static_cast<Frame*>( rootFrame )->dirty(); // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 1.0f, Engine::instance->progressCallbackUserData ); } }