// ---------- framework : setup dx ---------- bool rtvsD3dApp::setupDX (LPDIRECT3DDEVICE9 pd3dDevice) { pd3dDevice->SetRenderState( D3DRS_LIGHTING , TRUE ); pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_GAUSSIANQUAD ); // ---- create a texture object ---- //D3DXCreateTextureFromFile( pd3dDevice, "image/baboon.jpg", &pTexture ); // get mode D3DDISPLAYMODE mode; returnvalue = pd3dDevice->GetDisplayMode(0, &mode); if (FAILED(returnvalue)) return E_FAIL; // try create texture returnvalue = pd3dDevice->CreateTexture(WIDTH, HEIGHT, 1, 0, mode.Format, D3DPOOL_MANAGED, &pTexture, NULL); if (FAILED(returnvalue)) return E_FAIL; // ---- QUAD ---- void *pVertices = NULL; // ---- initialise quad vertex data ---- QuadVertex quadVertices[] = { // x y z nx ny nz tu tv { 1.28f, 0.93f, 2.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, //top right { -1.28f, 0.93f, 2.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f }, //top left { 1.28f, -0.93f, 2.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }, { -1.28f, -0.93f, 2.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f } }; // ---- create quad vertex buffer ---- int numQuadVertices = sizeof(quadVertices) / ( sizeof(float) * 8 /* + sizeof(DWORD)*/); numQuadTriangles = numQuadVertices / 2; pd3dDevice->CreateVertexBuffer( numQuadVertices*sizeof(QuadVertex), D3DUSAGE_WRITEONLY, QuadVertex::FVF_Flags, D3DPOOL_MANAGED, // does not have to be properly Released before calling IDirect3DDevice9::Reset //D3DPOOL_DEFAULT, // must be Released properly before calling IDirect3DDevice9::Reset &pQuadVertexBuffer, NULL ); // ---- block copy into quad vertex buffer ---- pVertices = NULL; pQuadVertexBuffer->Lock( 0, sizeof(quadVertices), (void**)&pVertices, 0 ); memcpy( pVertices, quadVertices, sizeof(quadVertices) ); pQuadVertexBuffer->Unlock(); setupAntTW(pd3dDevice); // ok return true; }
String screengrab(String filename) { if(filename=="" || filename == " () ") { filename.sprintf("%s_%5.5d_.png",(const char*)screengrabprefix,screengrabcount); screengrabcount++; } if(!IsOneOf('.',filename)) filename << ".png"; HRESULT hr; // get display dimensions // this will be the dimensions of the front buffer D3DDISPLAYMODE mode; if (FAILED(hr=g_pd3dDevice->GetDisplayMode(0,&mode))) { return "fail getdisplaymode"; } // create the image surface to store the front buffer image // note that call to GetFrontBuffer will always convert format to A8R8G8B8 static LPDIRECT3DSURFACE9 surf=NULL; if (!surf && FAILED(hr=g_pd3dDevice->CreateOffscreenPlainSurface(mode.Width,mode.Height, D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&surf,NULL))) { return "fail createimagesurface"; } // Next, this surface is passed to the GetFrontBuffer() method of the device, which will copy the entire screen into our image buffer: // read the front buffer into the image surface if (FAILED(hr=g_pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&surf))) { surf->Release(); return "fail getfrontbuffer"; } //Finally, we call D3DXSaveSurfaceToFile() to create the BMP file, and release the temporary image surface: // write the entire surface to the requested file hr=D3DXSaveSurfaceToFile(filename,D3DXIFF_PNG,surf,NULL,NULL); // release the image surface // surf->Release(); // return status of save operation to caller return (hr==D3D_OK)? filename + " exported!" : "something failed"; }
int resetdevice(const char *) { if(windowed==0) { if(screenwidth==0 || screenheight==0) { D3DDISPLAYMODE displaymode; g_pd3dDevice->GetDisplayMode(0,&displaymode); screenwidth = displaymode.Width; screenheight = displaymode.Height; } Width = screenwidth; Height= screenheight; } else // windowed { Width = (int)(windowwidth ); Height= (int)(windowheight); } oneoverwidth=1.0f/Width; oneoverheight=1.0f/Height; d3dpp.BackBufferWidth = Width; d3dpp.BackBufferHeight = Height; d3dpp.Windowed = (windowed)?1:0; d3dpp.BackBufferFormat = (d3dpp.Windowed)?D3DFMT_UNKNOWN:D3DFMT_X8R8G8B8 ; d3dpp.BackBufferCount = backbuffercount; d3dpp.PresentationInterval = vsync ? D3DPRESENT_INTERVAL_DEFAULT : D3DPRESENT_INTERVAL_IMMEDIATE; vsync_current = vsync; effectlostdevice(); cuberelease(); HRESULT hr = g_pd3dDevice->Reset(&d3dpp); assert(hr==0); effectresetdevice(); if(windowed) SetWindowPos( hWnd, HWND_NOTOPMOST,windowx+window_inset.left,windowy+window_inset.top,windowwidth+window_inset.right-window_inset.left, windowheight+window_inset.bottom-window_inset.top,SWP_SHOWWINDOW ); return hr; }
HRESULT HookIDirect3DDevice9::GetDisplayMode(LPVOID _this, UINT iSwapChain,D3DDISPLAYMODE* pMode) { LOG_API(); return pD3Dev->GetDisplayMode(iSwapChain, pMode); }
void Graphics::init(int windowId, int depthBufferBits, int stencilBufferBits) { if (!hasWindow()) return; hWnd = (HWND)System::windowHandle(windowId); long style = GetWindowLong(hWnd, GWL_STYLE); resizable = false; if ((style & WS_SIZEBOX) != 0) { resizable = true; } if ((style & WS_MAXIMIZEBOX) != 0) { resizable = true; } // TODO (DK) just setup the primary window for now and ignore secondaries // -this should probably be implemented via swap chain for real at a later time // -http://www.mvps.org/directx/articles/rendering_to_multiple_windows.htm if (windowId > 0) { return; } #ifdef SYS_WINDOWS // TODO (DK) convert depthBufferBits + stencilBufferBits to: d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = (!fullscreen) ? TRUE : FALSE; if (resizable) { d3dpp.SwapEffect = D3DSWAPEFFECT_COPY; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = Kore::System::desktopWidth(); d3dpp.BackBufferHeight = Kore::System::desktopHeight(); } else { d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferCount = 2; d3dpp.BackBufferWidth = System::windowWidth(windowId); d3dpp.BackBufferHeight = System::windowHeight(windowId); } d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; if (antialiasingSamples() > 1) { for (int samples = min(antialiasingSamples(), 16); samples > 1; --samples) { if (SUCCEEDED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_A8R8G8B8, FALSE, (D3DMULTISAMPLE_TYPE)samples, nullptr))) { d3dpp.MultiSampleType = (D3DMULTISAMPLE_TYPE)samples; break; } } } #endif #ifdef SYS_XBOX360 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); XVIDEO_MODE VideoMode; XGetVideoMode(&VideoMode); // g_bWidescreen = VideoMode.fIsWideScreen; d3dpp.BackBufferWidth = min(VideoMode.dwDisplayWidth, 1280); d3dpp.BackBufferHeight = min(VideoMode.dwDisplayHeight, 720); d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; #endif #ifdef SYS_XBOX360 d3d->CreateDevice(0, D3DDEVTYPE_HAL, nullptr, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device); #else if (!SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device))) d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device); // d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device); #endif #ifdef SYS_WINDOWS if (System::hasShowWindowFlag(/*windowId*/)) { ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); } #endif initDeviceStates(); #ifdef SYS_WINDOWS if (fullscreen) { // hz = d3dpp.FullScreen_RefreshRateInHz; D3DDISPLAYMODE mode; device->GetDisplayMode(0, &mode); hz = mode.RefreshRate; } if (!fullscreen || hz == 0) { DEVMODE devMode; EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode); hz = devMode.dmDisplayFrequency; } #endif #ifdef SYS_XBOX360 hz = 60; #endif // vsync = d3dpp.PresentationInterval != D3DPRESENT_INTERVAL_IMMEDIATE; System::ticks test1 = Kore::System::timestamp(); for (int i = 0; i < 3; ++i) swapBuffers(windowId); System::ticks test2 = Kore::System::timestamp(); if (test2 - test1 < (1.0 / hz) * System::frequency()) { vsync = false; } else vsync = true; #ifdef USE_SHADER vertexShader = new Shader("standard", SHADER_VERTEX); pixelShader = new Shader("standard", SHADER_FRAGMENT); vertexShader->set(); pixelShader->set(); setFragmentBool(L"lighting", false); #endif _width = System::windowWidth(windowId); _height = System::windowHeight(windowId); System::makeCurrent(windowId); }
void Graphics::init() { if (!hasWindow()) return; hWnd = (HWND)System::createWindow(); d3d = Direct3DCreate9(D3D_SDK_VERSION); //if (!d3d) throw Exception("Could not initialize Direct3D9"); #ifdef SYS_WINDOWS D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = (!fullscreen) ? TRUE : FALSE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferCount = 2; d3dpp.BackBufferWidth = Application::the()->width(); d3dpp.BackBufferHeight = Application::the()->height(); d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; //d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; if (antialiasingSamples() > 1) { for (int samples = min(antialiasingSamples(), 16); samples > 1; --samples) { if (SUCCEEDED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_A8R8G8B8, FALSE, (D3DMULTISAMPLE_TYPE)samples, nullptr))) { d3dpp.MultiSampleType = (D3DMULTISAMPLE_TYPE)samples; break; } } } #endif #ifdef SYS_XBOX360 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); XVIDEO_MODE VideoMode; XGetVideoMode( &VideoMode ); //g_bWidescreen = VideoMode.fIsWideScreen; d3dpp.BackBufferWidth = min(VideoMode.dwDisplayWidth, 1280); d3dpp.BackBufferHeight = min(VideoMode.dwDisplayHeight, 720); d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; #endif #ifdef SYS_XBOX360 d3d->CreateDevice(0, D3DDEVTYPE_HAL, nullptr, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device); #else if (!SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device))) d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device); //d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device); #endif #ifdef SYS_WINDOWS if (Application::the()->showWindow()) { ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); } #endif initDeviceStates(); #ifdef SYS_WINDOWS if (fullscreen) { //hz = d3dpp.FullScreen_RefreshRateInHz; D3DDISPLAYMODE mode; device->GetDisplayMode(0, &mode); hz = mode.RefreshRate; } if (!fullscreen || hz == 0) { DEVMODE devMode; EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode); hz = devMode.dmDisplayFrequency; } #endif #ifdef SYS_XBOX360 hz = 60; #endif //vsync = d3dpp.PresentationInterval != D3DPRESENT_INTERVAL_IMMEDIATE; System::ticks test1 = Kore::System::timestamp(); for (int i = 0; i < 3; ++i) swapBuffers(); System::ticks test2 = Kore::System::timestamp(); if (test2 - test1 < (1.0 / hz) * System::frequency()) { vsync = false; } else vsync = true; #ifdef USE_SHADER vertexShader = new Shader("standard", SHADER_VERTEX); pixelShader = new Shader("standard", SHADER_FRAGMENT); vertexShader->set(); pixelShader->set(); setFragmentBool(L"lighting", false); #endif }