Created
          May 28, 2019 10:58 
        
      - 
      
- 
        Save rdeioris/3c959b719cbcaf9b3a7494c672420faa to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include "winrt/Windows.ApplicationModel.Core.h" | |
| #include "winrt/Windows.UI.Core.h" | |
| #include "winrt/Windows.UI.ViewManagement.h" | |
| #include <dxgi1_6.h> | |
| #include <d3d11_4.h> | |
| #include <Xinput.h> | |
| #include <vector> | |
| #include <string> | |
| #include <exception> | |
| #include <memory> | |
| #include <fstream> | |
| #include "vs001.h" | |
| #include "ps001.h" | |
| using namespace winrt::Windows::ApplicationModel::Core; | |
| using namespace winrt::Windows::UI::Core; | |
| using namespace winrt::Windows::UI::ViewManagement; | |
| class ViewProvider : public winrt::implements<ViewProvider, IFrameworkView> | |
| { | |
| public: | |
| // IFrameworkView methods | |
| void Initialize(CoreApplicationView const & applicationView) | |
| { | |
| } | |
| void Uninitialize() | |
| { | |
| } | |
| void SetWindow(CoreWindow const & window) | |
| { | |
| window.Activate(); | |
| auto windowPtr = static_cast<::IUnknown*>(winrt::get_abi(window)); | |
| // fix b button | |
| auto navigation = SystemNavigationManager::GetForCurrentView(); | |
| navigation.BackRequested([](auto, auto args) | |
| { | |
| args.Handled(true); | |
| }); | |
| IDXGIFactory6* factory = nullptr; | |
| if (CreateDXGIFactory2(0, __uuidof(IDXGIFactory6), (void **)&factory) != S_OK) | |
| { | |
| throw std::exception("unable to create factory"); | |
| } | |
| UINT i = 0; | |
| IDXGIAdapter1* adapter = nullptr; | |
| while (factory->EnumAdapters1(i++, &adapter) != DXGI_ERROR_NOT_FOUND) | |
| { | |
| DXGI_ADAPTER_DESC1 desc; | |
| adapter->GetDesc1(&desc); | |
| OutputDebugString(desc.Description); | |
| OutputDebugString(L"\n"); | |
| } | |
| factory->EnumAdapters1(0, &adapter); | |
| D3D_FEATURE_LEVEL levels[] = | |
| { | |
| D3D_FEATURE_LEVEL_11_1, | |
| D3D_FEATURE_LEVEL_11_0, | |
| D3D_FEATURE_LEVEL_10_1, | |
| D3D_FEATURE_LEVEL_10_0, | |
| }; | |
| D3D_FEATURE_LEVEL level; | |
| if (D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, | |
| levels, _countof(levels), D3D11_SDK_VERSION, (ID3D11Device**)&device, | |
| &level, (ID3D11DeviceContext**)&context) != S_OK) | |
| { | |
| throw std::exception("unable to create device"); | |
| } | |
| DXGI_SWAP_CHAIN_DESC1 sc_desc = {}; | |
| sc_desc.BufferCount = 2; | |
| sc_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; | |
| sc_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | |
| sc_desc.SampleDesc.Count = 1; | |
| sc_desc.SampleDesc.Quality = 0; | |
| sc_desc.Width = 1920; | |
| sc_desc.Height = 1080; | |
| sc_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; | |
| if (factory->CreateSwapChainForCoreWindow(device, windowPtr, &sc_desc, | |
| nullptr, (IDXGISwapChain1**)&swap_chain) != S_OK) | |
| { | |
| throw std::exception("unable to create the swap chain"); | |
| } | |
| ID3D11Texture2D* swap_chain_texture = nullptr; | |
| if (swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)&swap_chain_texture) != S_OK) | |
| { | |
| throw std::exception("unable to get the swap chain texture"); | |
| } | |
| if (device->CreateRenderTargetView1(swap_chain_texture, nullptr, &rtv) != S_OK) | |
| { | |
| throw std::exception("unable to create the render target view"); | |
| } | |
| } | |
| void Load(winrt::hstring const &) | |
| { | |
| } | |
| void Run() | |
| { | |
| ID3D11RasterizerState2* rs = nullptr; | |
| D3D11_RASTERIZER_DESC2 r_desc = {}; | |
| r_desc.CullMode = D3D11_CULL_NONE; | |
| r_desc.FillMode = D3D11_FILL_SOLID; | |
| if (device->CreateRasterizerState2(&r_desc, &rs) != S_OK) | |
| { | |
| throw std::exception("unable to createrasterizer state"); | |
| } | |
| float vertices[] = { | |
| 0, 1, 0, | |
| -1, -1, 0, | |
| 1, -1, 0 | |
| }; | |
| D3D11_BUFFER_DESC b_desc = {}; | |
| b_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; | |
| b_desc.ByteWidth = sizeof(vertices); | |
| b_desc.Usage = D3D11_USAGE_DEFAULT; | |
| D3D11_SUBRESOURCE_DATA sr_data = {}; | |
| sr_data.pSysMem = vertices; | |
| ID3D11Buffer* buffer = nullptr; | |
| if (device->CreateBuffer(&b_desc, &sr_data, &buffer) != S_OK) | |
| { | |
| throw std::exception("unable to create buffer"); | |
| } | |
| ID3D11VertexShader* vertex_shader = nullptr; | |
| if (device->CreateVertexShader(vs001, sizeof(vs001), nullptr, &vertex_shader) != S_OK) | |
| { | |
| throw std::exception("unable to create vertex shader"); | |
| } | |
| ID3D11PixelShader* pixel_shader = nullptr; | |
| if (device->CreatePixelShader(ps001, sizeof(ps001), nullptr, &pixel_shader) != S_OK) | |
| { | |
| throw std::exception("unable to create pixel shader"); | |
| } | |
| D3D11_INPUT_ELEMENT_DESC elements[] = | |
| { | |
| {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0} | |
| }; | |
| ID3D11InputLayout* layout = nullptr; | |
| if (device->CreateInputLayout(elements, _countof(elements), vs001, sizeof(vs001), &layout) != S_OK) | |
| { | |
| throw std::exception("unable to create input layout"); | |
| } | |
| context->RSSetState(rs); | |
| D3D11_VIEWPORT viewport = {}; | |
| viewport.Height = 1080; | |
| viewport.Width = 1920; | |
| context->RSSetViewports(1, &viewport); | |
| context->VSSetShader(vertex_shader, nullptr, 0); | |
| context->PSSetShader(pixel_shader, nullptr, 0); | |
| UINT striding = { sizeof(float) * 3 }; | |
| UINT offset = { 0 }; | |
| context->IASetVertexBuffers(0, 1, &buffer, &striding, &offset); | |
| context->IASetInputLayout(layout); | |
| context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); | |
| for (;;) | |
| { | |
| CoreWindow::GetForCurrentThread().Dispatcher().ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); | |
| float red[] = { 1, 0, 0, 1 }; | |
| float green[] = { 0, 1, 0, 1 }; | |
| float yellow[] = { 1, 1, 0, 1 }; | |
| float blue[] = { 0, 0, 1, 1 }; | |
| float black[] = { 0,0,0,0 }; | |
| float *color = black; | |
| XINPUT_STATE xpad; | |
| XInputGetState(0, &xpad); | |
| XINPUT_VIBRATION vibration = {}; | |
| if (xpad.Gamepad.wButtons & XINPUT_GAMEPAD_A) | |
| { | |
| color = green; | |
| vibration.wLeftMotorSpeed = 10000; | |
| vibration.wRightMotorSpeed = 10000; | |
| } | |
| if (xpad.Gamepad.wButtons & XINPUT_GAMEPAD_B) | |
| { | |
| color = red; | |
| vibration.wLeftMotorSpeed = 20000; | |
| vibration.wRightMotorSpeed = 10000; | |
| } | |
| if (xpad.Gamepad.wButtons & XINPUT_GAMEPAD_Y) | |
| { | |
| color = yellow; | |
| vibration.wLeftMotorSpeed = 10000; | |
| vibration.wRightMotorSpeed = 20000; | |
| } | |
| if (xpad.Gamepad.wButtons & XINPUT_GAMEPAD_X) | |
| { | |
| color = blue; | |
| vibration.wLeftMotorSpeed = 30000; | |
| vibration.wRightMotorSpeed = 30000; | |
| } | |
| XInputSetState(0, &vibration); | |
| context->ClearRenderTargetView(rtv, color); | |
| context->OMSetRenderTargets(1, (ID3D11RenderTargetView **)&rtv, nullptr); | |
| context->Draw(3, 0); | |
| swap_chain->Present(1, 0); | |
| } | |
| } | |
| ID3D11RenderTargetView1* rtv; | |
| ID3D11DeviceContext4* context; | |
| IDXGISwapChain4* swap_chain; | |
| ID3D11Device5* device; | |
| }; | |
| class ViewProviderFactory : public winrt::implements<ViewProviderFactory, IFrameworkViewSource> | |
| { | |
| public: | |
| IFrameworkView CreateView() | |
| { | |
| return winrt::make<ViewProvider>(); | |
| } | |
| }; | |
| // Entry point | |
| int WINAPI wWinMain( | |
| _In_ HINSTANCE hInstance, | |
| _In_ HINSTANCE hPrevInstance, | |
| _In_ LPWSTR lpCmdLine, | |
| _In_ int nCmdShow | |
| ) | |
| { | |
| auto viewProviderFactory = winrt::make<ViewProviderFactory>(); | |
| CoreApplication::Run(viewProviderFactory); | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment