Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.89 KB | None | 0 0
  1. //--------------------------------------------------------------------------------------
  2. // File: SimpleSampleMain.cpp
  3. //
  4. // This is a simple Windows Store app for Windows 8.1 Preview showing use of DirectXTK
  5. //
  6. // http://go.microsoft.com/fwlink/?LinkId=248929
  7. //
  8. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  9. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  10. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  11. // PARTICULAR PURPOSE.
  12. //
  13. // Copyright (c) Microsoft Corporation. All rights reserved.
  14. //--------------------------------------------------------------------------------------
  15.  
  16.  
  17. #include "pch.h"
  18. #include "SimpleSampleMain.h"
  19.  
  20. #include <DirectXColors.h>          // For named colors
  21. #include "Common\DirectXHelper.h"   // For ThrowIfFailed
  22.  
  23. using namespace SimpleSample;
  24.  
  25. //Added for Input and Overlay
  26. using namespace Windows::Foundation;
  27. using namespace Windows::System::Threading;
  28. using namespace Concurrency;
  29.  
  30.  
  31. // Loads and initializes application assets when the application is loaded.
  32. SimpleSampleMain::SimpleSampleMain(const std::shared_ptr<DX::DeviceResources>& deviceResources) :
  33.     m_deviceResources(deviceResources)
  34. {
  35.     m_sceneRenderer = std::unique_ptr<DirectXTK3DSceneRenderer>(new DirectXTK3DSceneRenderer(m_deviceResources));
  36.     m_debugTextRenderer = std::shared_ptr<SampleDebugTextRenderer>(new SampleDebugTextRenderer(m_deviceResources));
  37.     //TODO: Insert stuff here - initialization
  38.  
  39.     m_inputManager = std::unique_ptr<InputManager>(new InputManager());
  40.     m_overlayManager = std::unique_ptr<OverlayManager>(new OverlayManager(m_deviceResources));
  41.  
  42.     std::vector<std::shared_ptr<Overlay>> overlays;
  43.     overlays.push_back(m_debugTextRenderer);
  44.  
  45.  
  46.     TouchCapabilities^ pTouchCapabilities = ref new TouchCapabilities();
  47.     if (pTouchCapabilities->TouchPresent != 0)
  48.     {
  49.         // Create and add virtual controller overlay.
  50.         m_virtualControllerRenderer = std::shared_ptr<SampleVirtualControllerRenderer>(new SampleVirtualControllerRenderer(m_deviceResources));
  51.         overlays.push_back(m_virtualControllerRenderer);
  52.  
  53.         // Set up touch regions.
  54.         InitializeTouchRegions();
  55.     }
  56.  
  57.     m_overlayManager->SetOverlays(overlays);
  58.  
  59.     // Note to developer: Apply input to your game.
  60.     // This template supports all control types by default.
  61.     m_inputManager->SetFilter(INPUT_DEVICE_ALL);
  62.     m_inputManager->Initialize(CoreWindow::GetForCurrentThread());
  63.  
  64.     // Setting render target to 30 FPS !!!!!!!!!!!!!!
  65.  
  66.     m_timer.SetFixedTimeStep(true);
  67.     m_timer.SetTargetElapsedSeconds(1.0 / 30);
  68.     // Remember - it is usually not necessary to limit the framerate, it will be capped to display refresh rate anyway, usually 60 Hz, - default Windows Apps behaviour
  69.  
  70.     m_deviceResources->RegisterDeviceNotify(this);
  71. }
  72.  
  73. SimpleSampleMain::~SimpleSampleMain()
  74. {
  75.     // Deregister device notification
  76.     m_deviceResources->RegisterDeviceNotify(nullptr);
  77. }
  78.  
  79. // Updates application state when the window size changes (e.g. device orientation change)
  80. void SimpleSampleMain::CreateWindowSizeDependentResources()
  81. {
  82.     m_sceneRenderer->CreateWindowSizeDependentResources();
  83. }
  84.  
  85. // Updates the application state once per frame.
  86. void SimpleSampleMain::Update()
  87. {
  88.     // Update scene objects.
  89.     m_timer.Tick([&]()
  90.     {
  91.         m_sceneRenderer->Update(m_timer);
  92.         m_overlayManager->Update(m_timer);
  93.         m_inputManager->Update(m_timer);
  94.  
  95.         std::vector<PlayerInputData> playerActions;
  96.         ProcessInput(&playerActions);
  97.  
  98.         m_debugTextRenderer->Update(&playerActions, m_playersConnected);
  99.  
  100.         // Only update the virtual controller if it's present.
  101.         if (m_virtualControllerRenderer != nullptr)
  102.         {
  103.             m_virtualControllerRenderer->Update(&playerActions);
  104.         }
  105.     });
  106. }
  107.  
  108. //// This is the main Render function, all other objects go here
  109. //// Remember to use <vector> for all elements that need rendering - better memory access pattern
  110. // Renders the current frame according to the current application state.
  111. // Returns true if the frame was rendered and is ready to be displayed.
  112. bool SimpleSampleMain::Render()
  113. {
  114.     // Don't try to render anything before the first Update.
  115.     if (m_timer.GetFrameCount() == 0)
  116.     {
  117.         return false;
  118.     }
  119.  
  120.     auto context = m_deviceResources->GetD3DDeviceContext();
  121.  
  122.     // Reset the viewport to target the whole screen.
  123.     auto viewport = m_deviceResources->GetScreenViewport();
  124.     context->RSSetViewports(1, &viewport);
  125.  
  126.     // Reset render targets to the screen.
  127.     ID3D11RenderTargetView *const targets[1] = { m_deviceResources->GetBackBufferRenderTargetView() };
  128.     context->OMSetRenderTargets(1, targets, m_deviceResources->GetDepthStencilView());
  129.  
  130.     // Clear the back buffer and depth stencil view.
  131.     context->ClearRenderTargetView(m_deviceResources->GetBackBufferRenderTargetView(), DirectX::Colors::CornflowerBlue);
  132.     context->ClearDepthStencilView(m_deviceResources->GetDepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
  133.  
  134.     // Render the scene objects.
  135.     m_sceneRenderer->Render();
  136.     m_overlayManager->Render();
  137.  
  138.     return true;
  139. }
  140.  
  141.  
  142. // Signals a new audio device is available
  143. void SimpleSampleMain::NewAudioDevice()
  144. {
  145.     if (m_sceneRenderer)
  146.         m_sceneRenderer->NewAudioDevice();
  147. }
  148.  
  149. // Notifies renderers that device resources need to be released.
  150. void SimpleSampleMain::OnDeviceLost()
  151. {
  152.     m_sceneRenderer->ReleaseDeviceDependentResources();
  153. }
  154.  
  155. // Notifies renderers that device resources may now be recreated.
  156. void SimpleSampleMain::OnDeviceRestored()
  157. {
  158.     m_sceneRenderer->CreateDeviceDependentResources();
  159.     CreateWindowSizeDependentResources();
  160. }
  161.  
  162.  
  163. void SimpleSampleMain::InitializeTouchRegions()
  164. {
  165.     // Here we set up the touch control regions.
  166.     Windows::Foundation::Size logicalSize = m_deviceResources->GetLogicalSize();
  167.     XMFLOAT2 screenTopLeft = XMFLOAT2(0, 0);
  168.     XMFLOAT2 screenTopRight = XMFLOAT2(logicalSize.Width, 0);
  169.     XMFLOAT2 screenBottomLeft = XMFLOAT2(0, logicalSize.Height);
  170.     XMFLOAT2 screenBottomRight = XMFLOAT2(logicalSize.Width, logicalSize.Height);
  171.     float width = screenTopRight.x - screenTopLeft.x;
  172.     float height = screenBottomLeft.y - screenTopLeft.y;
  173.  
  174.     // Clear previous touch regions.
  175.     m_virtualControllerRenderer->ClearTouchControlRegions();
  176.     m_inputManager->ClearTouchRegions();
  177.  
  178.     // The following scoped code region sets up the analog stick.
  179.     {
  180.         const float stickRegionPercent = 0.75f;
  181.         float stickRegionWidth = (width * stickRegionPercent);
  182.         XMFLOAT2 touchRegionBoundary = XMFLOAT2(screenTopLeft.x + stickRegionWidth, screenBottomRight.y);
  183.  
  184.         TouchControlRegion touchControlRegionStick(
  185.             screenTopLeft,
  186.             touchRegionBoundary,
  187.             TOUCH_CONTROL_REGION_ANALOG_STICK,
  188.             PLAYER_ACTION_TYPES::INPUT_MOVE,
  189.             PLAYER_ID::PLAYER_ID_ONE
  190.             );
  191.  
  192.         DWORD errorCode = m_inputManager->SetDefinedTouchRegion(&touchControlRegionStick, m_touchRegionIDs[0]);
  193.  
  194.         if (!errorCode)
  195.         {
  196.             m_virtualControllerRenderer->AddTouchControlRegion(touchControlRegionStick);
  197.         }
  198.     }
  199.  
  200.     // The following scoped code region sets up the buttons.
  201.     {
  202.         const float buttonRegionPercent = 0.2f;
  203.         const float buttonWidthHeight = 80.f;
  204.         const float buttonDesiredOffset = 300.f;
  205.  
  206.         // Control the max location to prevent overlap
  207.         float buttonWidthOffset = (width * buttonRegionPercent) - buttonWidthHeight;
  208.         buttonWidthOffset = buttonWidthOffset < buttonDesiredOffset ? buttonWidthOffset : buttonDesiredOffset;
  209.  
  210.         // Set up button A
  211.         XMFLOAT2 location1 = { width - (buttonWidthOffset + 1.f * buttonWidthHeight), height - buttonDesiredOffset };
  212.         TouchControlRegion touchControlRegionButtonA(
  213.             XMFLOAT2(location1.x, location1.y),
  214.             XMFLOAT2(location1.x + buttonWidthHeight, location1.y + buttonWidthHeight),
  215.             TOUCH_CONTROL_REGION_TYPES::TOUCH_CONTROL_REGION_BUTTON,
  216.             PLAYER_ACTION_TYPES::INPUT_FIRE_DOWN,
  217.             PLAYER_ID::PLAYER_ID_ONE
  218.             );
  219.  
  220.         DWORD errorCode = m_inputManager->SetDefinedTouchRegion(&touchControlRegionButtonA, m_touchRegionIDs[1]);
  221.         if (!errorCode)
  222.         {
  223.             m_virtualControllerRenderer->AddTouchControlRegion(touchControlRegionButtonA);
  224.         }
  225.  
  226.         // Set up button B
  227.         XMFLOAT2 location2 = { width - buttonWidthOffset, height - buttonDesiredOffset - (1.f * buttonWidthHeight) };
  228.         TouchControlRegion touchControlRegionButtonB(
  229.             XMFLOAT2(location2.x, location2.y),
  230.             XMFLOAT2(location2.x + buttonWidthHeight, location2.y + buttonWidthHeight),
  231.             TOUCH_CONTROL_REGION_TYPES::TOUCH_CONTROL_REGION_BUTTON,
  232.             PLAYER_ACTION_TYPES::INPUT_JUMP_DOWN,
  233.             PLAYER_ID::PLAYER_ID_ONE
  234.             );
  235.  
  236.         errorCode = m_inputManager->SetDefinedTouchRegion(&touchControlRegionButtonB, m_touchRegionIDs[2]);
  237.         if (!errorCode)
  238.         {
  239.             m_virtualControllerRenderer->AddTouchControlRegion(touchControlRegionButtonB);
  240.         }
  241.     }
  242. }
  243.  
  244. void SimpleSampleMain::ProcessInput(std::vector<PlayerInputData>* playerActions)
  245. {
  246.     m_playersConnected = m_inputManager->GetPlayersConnected();
  247.  
  248.     *playerActions = m_inputManager->GetPlayersActions();
  249.  
  250.     for (unsigned int j = 0; j < playerActions->size(); j++)
  251.     {
  252.         PlayerInputData playerAction = (*playerActions)[j];
  253.  
  254.  
  255.  
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement