OSDN Git Service

Boost.MSMでメニュー部分を実装
[shooting3/shootinggame.git] / ShootingGame / GameMain.cpp
1 //// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
2 //// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
3 //// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
4 //// PARTICULAR PURPOSE.
5 ////
6 //// Copyright (c) Microsoft Corporation. All rights reserved
7
8 #include "pch.h"
9 //#include "MainPage.xaml.h"
10 #include "GameMain.h"
11
12 using namespace Microsoft::WRL;
13 //using namespace Windows::Foundation;
14 //using namespace Windows::Foundation::Collections;
15 //using namespace Windows::UI::Core;
16 //using namespace Windows::UI::ViewManagement;
17 //
18 //using namespace Windows::UI::ApplicationSettings;
19 //using namespace Windows::UI::Popups;
20 //using namespace Windows::UI::Xaml::Controls;
21 //using namespace Windows::Graphics::Display;
22 using namespace DirectX;
23 using namespace D2D1;
24 using namespace concurrency;
25 using namespace BasicSprites;
26
27 namespace ShootingGame {
28
29 const float GameMain::BACKBUFFER_WIDTH = 320.0f;
30 const float GameMain::BACKBUFFER_HEIGHT = 240.0f;
31
32
33 GameMain::GameMain() 
34   :backBufferViewPort_(0.0f,0.0f,BACKBUFFER_WIDTH,BACKBUFFER_HEIGHT) , isDestroy_(true),state_(this),
35   eventHolder_(::CreateEventEx(NULL, NULL, 0, EVENT_MODIFY_STATE | SYNCHRONIZE))
36 {
37   //  GameStateMachine machine;
38   soundDriver_.reset(new sf::SoundDriver());
39   soundManager_.reset(new sf::SoundManager(*soundDriver_.get()));
40  
41   //soundThread_ = std::thread(
42   //   [this]() -> void 
43   //  {
44   //      ExecuteSoundThread();
45   //  }
46   //);
47 }
48
49 GameMain::~GameMain()
50 {
51   StopSound();
52   eventHolder_.reset();
53   //if(soundThread_.joinable())
54   //{
55   //    soundThread_.join();
56   //}
57 };
58
59 void GameMain::CreateDeviceIndependentResources()
60 {
61   DirectXBase::CreateDeviceIndependentResources();
62
63   // Create the performance throttler.
64
65   autoThrottle_ = ref new ::AutoThrottle(1.0f / 60.0f);
66 }
67
68 void GameMain::Initialize(
69   _In_ Windows::UI::Core::CoreWindow^ window,
70   _In_ Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ swapChainPanel,
71   _In_ float dpi
72   )
73 {
74   panel_  = swapChainPanel;
75   Initialize(window,dpi);
76   state_.Start();
77   state_.ProcessEvent(Event::Init());
78 }
79
80 void GameMain::CreateDeviceResources()
81 {
82   DirectXBase::CreateDeviceResources();
83
84   // Create the sprite batch.
85
86   spriteBatch_ = ref new BasicSprites::SpriteBatch();
87   unsigned int capacity = 1024;
88   if (m_featureLevel < D3D_FEATURE_LEVEL_9_3)
89   {
90     capacity = min(Parameters::MaximumCapacityCompatible, capacity);
91   }
92   spriteBatch_->Initialize(
93     m_d3dDevice.Get(),
94     capacity
95     );
96
97   // Create the Sample Overlay.
98
99   sampleOverlay_ = ref new ::SampleOverlay();
100
101   sampleOverlay_->Initialize(
102     m_d2dDevice.Get(),
103     m_d2dContext.Get(),
104     m_wicFactory.Get(),
105     m_dwriteFactory.Get(),
106     L"\83V\83\85\81[\83e\83B\83\93\83O\83Q\81[\83\80"
107     );
108
109
110
111 }
112
113 void GameMain::CreateWindowSizeDependentResources()
114 {
115   //    DirectXBase::CreateWindowSizeDependentResources();
116   // Store the window bounds so the next time we get a SizeChanged event we can
117   // avoid rebuilding everything if the size is identical.
118   m_windowBounds = m_window->Bounds;
119
120   // If the swap chain already exists, resize it.
121   if(m_swapChain != nullptr)
122   {
123     DX::ThrowIfFailed(
124       m_swapChain->ResizeBuffers(2, 0, 0, DXGI_FORMAT_B8G8R8A8_UNORM, 0)
125       );
126   }
127   // Otherwise, create a new one.
128   else
129   {
130     // Allocate a descriptor.
131     DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
132     scale_ = floorf(m_window->Bounds.Width * m_dpi / ( 96.0f * BACKBUFFER_WIDTH));
133     swapChainDesc.Width = static_cast<UINT>(m_window->Bounds.Width * m_dpi / 96.0f);    // Can not use 0 to get the default on Composition SwapChain
134     swapChainDesc.Height = static_cast<UINT>(m_window->Bounds.Height * m_dpi / 96.0f);
135     swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;           // this is the most common swapchain format
136     swapChainDesc.Stereo = false; 
137     swapChainDesc.SampleDesc.Count = 1;                          // don't use multi-sampling
138     swapChainDesc.SampleDesc.Quality = 0;
139     swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
140     swapChainDesc.BufferCount = 2;                               // use double buffering to enable flip
141     swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
142     swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all Metro style apps must use this SwapEffect
143     swapChainDesc.Flags = 0;
144
145     // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device
146
147     // First, retrieve the underlying DXGI Device from the D3D Device.
148     ComPtr<IDXGIDevice1>  dxgiDevice;
149     DX::ThrowIfFailed(
150       m_d3dDevice.As(&dxgiDevice)
151       );
152
153     // Identify the physical adapter (GPU or card) this device is running on.
154     ComPtr<IDXGIAdapter> dxgiAdapter;
155     DX::ThrowIfFailed(
156       dxgiDevice->GetAdapter(&dxgiAdapter)
157       );
158
159     // And obtain the factory object that created it.
160     ComPtr<IDXGIFactory2> dxgiFactory;
161     DX::ThrowIfFailed(
162       dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory))
163       );
164
165     // Obtain the final swap chain for this window from the DXGI factory.
166     DX::ThrowIfFailed(
167       dxgiFactory->CreateSwapChainForComposition(
168       m_d3dDevice.Get(),
169       &swapChainDesc,
170       nullptr,    // allow on all displays
171       &m_swapChain
172       )
173       );
174
175     ComPtr<ISwapChainBackgroundPanelNative> dxRootPanelAsNative;
176
177     // set the swap chain on the SwapChainBackgroundPanel
178     reinterpret_cast<IUnknown*>(panel_)->QueryInterface(__uuidof(ISwapChainBackgroundPanelNative), (void**)&dxRootPanelAsNative);
179
180     DX::ThrowIfFailed(
181       dxRootPanelAsNative->SetSwapChain(m_swapChain.Get())
182       );
183
184     // Ensure that DXGI does not queue more than one frame at a time. This both reduces 
185     // latency and ensures that the application will only render after each VSync, minimizing 
186     // power consumption.
187     DX::ThrowIfFailed(
188       dxgiDevice->SetMaximumFrameLatency(1)
189       );
190
191   }
192
193   // Obtain the backbuffer for this window which will be the final 3D rendertarget.
194   ComPtr<ID3D11Texture2D> backBuffer;
195   DX::ThrowIfFailed(
196     m_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer))
197     );
198
199   // Create a view interface on the rendertarget to use on bind.
200   DX::ThrowIfFailed(
201     m_d3dDevice->CreateRenderTargetView(
202     backBuffer.Get(),
203     nullptr,
204     &m_renderTargetView
205     )
206     );
207
208   // Cache the rendertarget dimensions in our helper class for convenient use.
209   D3D11_TEXTURE2D_DESC backBufferDesc = {0};
210   backBuffer->GetDesc(&backBufferDesc);
211   m_renderTargetSize.Width  = static_cast<float>(backBufferDesc.Width);
212   m_renderTargetSize.Height = static_cast<float>(backBufferDesc.Height);
213
214   // Create a descriptor for the depth/stencil buffer.
215   CD3D11_TEXTURE2D_DESC depthStencilDesc(
216     DXGI_FORMAT_D24_UNORM_S8_UINT, 
217     backBufferDesc.Width,
218     backBufferDesc.Height,
219     1,
220     1,
221     D3D11_BIND_DEPTH_STENCIL
222     );
223
224   // Allocate a 2-D surface as the depth/stencil buffer.
225   ComPtr<ID3D11Texture2D> depthStencil;
226   DX::ThrowIfFailed(
227     m_d3dDevice->CreateTexture2D(
228     &depthStencilDesc,
229     nullptr,
230     &depthStencil
231     )
232     );
233
234   // Create a DepthStencil view on this surface to use on bind.
235   DX::ThrowIfFailed(
236     m_d3dDevice->CreateDepthStencilView(
237     depthStencil.Get(),
238     &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D),
239     &m_depthStencilView
240     )
241     );
242
243   // Create a viewport descriptor of the full window size.
244   swapChainViewPort_.Width =         static_cast<float>(backBufferDesc.Width);
245   swapChainViewPort_.Height =         static_cast<float>(backBufferDesc.Height);
246
247   // Set the current viewport using the descriptor.
248   m_d3dContext->RSSetViewports(1, &swapChainViewPort_);
249
250   // Now we set up the Direct2D render target bitmap linked to the swapchain. 
251   // Whenever we render to this bitmap, it will be directly rendered to the 
252   // swapchain associated with the window.
253   D2D1_BITMAP_PROPERTIES1 bitmapProperties = 
254     BitmapProperties1(
255     D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
256     PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
257     m_dpi,
258     m_dpi
259     );
260
261   // Direct2D needs the dxgi version of the backbuffer surface pointer.
262   ComPtr<IDXGISurface> dxgiBackBuffer;
263   DX::ThrowIfFailed(
264     m_swapChain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer))
265     );
266
267   // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
268   DX::ThrowIfFailed(
269     m_d2dContext->CreateBitmapFromDxgiSurface(
270     dxgiBackBuffer.Get(),
271     &bitmapProperties,
272     &m_d2dTargetBitmap
273     )
274     );
275
276   // So now we can set the Direct2D render target.
277   m_d2dContext->SetTarget(m_d2dTargetBitmap.Get());
278
279   // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
280   m_d2dContext->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE);
281
282   // Randomly generate some non-interactive asteroids to fit the screen.
283
284   sampleOverlay_->UpdateForWindowSizeChange();
285   // \83o\83b\83N\83o\83b\83t\83@\82Ì\8dì\90¬
286   //ComPtr<ID3D11Texture2D> backBuffer;
287   //DX::ThrowIfFailed(
288   //    m_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer))
289   //    );
290   //D3D11_TEXTURE2D_DESC backBufferDesc = {0};
291   //backBuffer->GetDesc(&backBufferDesc);
292
293   D3D11_TEXTURE2D_DESC desc = {0};
294   desc.Width = 320;
295   desc.Height = 240;
296   desc.Format = backBufferDesc.Format;
297   desc.MipLevels = 1;
298   desc.SampleDesc.Count = 1;
299   desc.SampleDesc.Quality = 0;
300   desc.ArraySize = 1;
301   desc.Usage = D3D11_USAGE_DEFAULT;
302   desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
303   //    desc.MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
304
305   DX::ThrowIfFailed(m_d3dDevice->CreateTexture2D(&desc,NULL,&backBuffer_));
306
307   DX::ThrowIfFailed(m_d3dDevice->CreateRenderTargetView(backBuffer_.Get(),0,&backBufferRenderTargetView_));
308
309   //   // \90[\93x\83o\83b\83t\83@\82Ì\8dì\90¬
310   //   D3D11_TEXTURE2D_DESC depth = {} ;
311   //   depth.Width = desc.Width;//rc.right - rc.left;client_width_;
312   //   depth.Height = desc.Height;//rc.bottom - rc.top;client_height_;
313   //   depth.MipLevels = 1;
314   //   depth.ArraySize = 1;
315   //   depth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
316   //   depth.SampleDesc.Count = 1;
317   //   depth.SampleDesc.Quality = 0;
318   //   depth.Usage = D3D11_USAGE_DEFAULT;
319   //   depth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
320   //   depth.CPUAccessFlags = 0;
321   //   depth.MiscFlags = 0;
322   //   ComPtr<ID3D11Texture2D> depthT;
323   //DX::ThrowIfFailed(m_d3dDevice->CreateTexture2D( &depth, NULL, &depthT ));
324
325   //   D3D11_DEPTH_STENCIL_VIEW_DESC dsv = {};
326   //   dsv.Format = depth.Format;
327   //   dsv.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
328   //   dsv.Texture2D.MipSlice = 0;
329
330   //DX::ThrowIfFailed(m_d3dDevice->CreateDepthStencilView( depthT.Get(), &dsv, &backBufferDepthStencilView_ ));
331
332   spriteBatch_->AddTexture(backBuffer_.Get(),float2());
333
334   // \90[\93x\83X\83e\83\93\83V\83\8b\83X\83e\81[\83g\82ð\8dì\90¬\82·\82é
335   //D3D11_DEPTH_STENCIL_DESC ddsDesc;
336   //::ZeroMemory( &ddsDesc, sizeof( ddsDesc ) );
337   //ddsDesc.DepthEnable = TRUE;                                     // \90[\93x\83e\83X\83g\82ð\8eg\97p\82·\82é
338   //ddsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
339   //ddsDesc.DepthFunc = D3D11_COMPARISON_LESS;
340   //ddsDesc.StencilEnable = FALSE;
341   //DX::ThrowIfFailed(m_d3dDevice->CreateDepthStencilState( &ddsDesc, &backBufferDepthStencilState_ ));
342
343 }
344
345 void GameMain::Update(float timeTotal, float timeDelta)
346 {
347   // Update the performance throttler.
348
349   auto control = autoThrottle_->Update(timeDelta);
350   state_.ProcessEvent(Event::Update());
351   //if (control == FrameWorkload::Increase)
352   //{
353   //    m_numParticlesToDraw += SampleSettings::Performance::ParticleCountDelta;
354   //}
355   //if (control == FrameWorkload::Decrease)
356   //{
357   //    m_numParticlesToDraw -= SampleSettings::Performance::ParticleCountDelta;
358   //}
359   //if (control != FrameWorkload::Maintain)
360   //{
361   //    m_numParticlesToDraw = max(SampleSettings::Performance::ParticleCountMin, min(SampleSettings::Performance::ParticleCountMax, m_numParticlesToDraw));
362   //    if (m_featureLevel < D3D_FEATURE_LEVEL_9_3)
363   //    {
364   //        m_numParticlesToDraw = min(static_cast<int>(Parameters::MaximumCapacityCompatible - SampleSettings::NumAsteroids - 1), m_numParticlesToDraw);
365   //    }
366   //}
367
368   // Update the non-interactive asteroids.
369   // Their behavior is to drift across the window with a fixed translational and rotational
370   // velocity.  Upon crossing a boundary outside the window, their position wraps.
371
372   //for (auto asteroid = m_asteroidData.begin(); asteroid != m_asteroidData.end(); asteroid++)
373   //{
374   //    static const float border = 100.0f;
375   //    asteroid->pos = asteroid->pos + asteroid->vel * timeDelta;
376   //    if (asteroid->vel.x < 0)
377   //    {
378   //        if (asteroid->pos.x < -border)
379   //        {
380   //            asteroid->pos.x = m_windowBounds.Width + border;
381   //        }
382   //    }
383   //    else
384   //    {
385   //        if (asteroid->pos.x > m_windowBounds.Width + border)
386   //        {
387   //            asteroid->pos.x = -border;
388   //        }
389   //    }
390   //    if (asteroid->vel.y < 0)
391   //    {
392   //        if (asteroid->pos.y < -border)
393   //        {
394   //            asteroid->pos.y = m_windowBounds.Height + border;
395   //        }
396   //    }
397   //    else
398   //    {
399   //        if (asteroid->pos.y > m_windowBounds.Height + border)
400   //        {
401   //            asteroid->pos.y = -border;
402   //        }
403   //    }
404
405   //    asteroid->rot += asteroid->rotVel * timeDelta;
406   //    if (asteroid->rot > static_cast<float>(M_PI))
407   //    {
408   //        asteroid->rot -= 2.0f * static_cast<float>(M_PI);
409   //    }
410   //    if (asteroid->rot < static_cast<float>(-M_PI))
411   //    {
412   //        asteroid->rot += 2.0f * static_cast<float>(M_PI);
413   //    }
414   //}
415
416   //// Update the interactive particles.
417   //// Their behavior is to be gravitationally attracted to two oscillating gravity
418   //// wells and repelled by any pressed pointer points.  Upon reaching the edge of
419   //// the window, the particles bounce.
420
421   //// Add two gravity wells that move throughout the window.
422   //float2 wellPositions[] =
423   //{
424   //    float2(
425   //        (1.0f + 0.8f * cosf(timeTotal / (2.0f * static_cast<float>(M_PI)) + 3.0f)) * m_windowBounds.Width / 2.0f,
426   //        (1.0f + 0.8f * sinf(timeTotal / 5.0f)) * m_windowBounds.Height / 2.0f
427   //        ),
428   //    float2(
429   //        (1.0f + 0.8f * cosf(timeTotal / static_cast<float>(M_PI * M_PI) + 1.0f)) * m_windowBounds.Width / 2.0f,
430   //        (1.0f + 0.8f * sinf(timeTotal / static_cast<float>(M_PI))) * m_windowBounds.Height / 2.0f
431   //        )
432   //};
433
434   //for (auto particle = m_particleData.begin(); particle != m_particleData.begin() + m_numParticlesToDraw; particle++)
435   //{
436   //    if (particle->pos.x < 0)
437   //    {
438   //        particle->vel.x = abs(particle->vel.x);
439   //    }
440   //    if (particle->pos.x > m_windowBounds.Width)
441   //    {
442   //        particle->vel.x = -abs(particle->vel.x);
443   //    }
444   //    if (particle->pos.y < 0)
445   //    {
446   //        particle->vel.y = abs(particle->vel.y);
447   //    }
448   //    if (particle->pos.y > m_windowBounds.Height)
449   //    {
450   //        particle->vel.y = -abs(particle->vel.y);
451   //    }
452
453   //    for (auto repulsor = m_repulsors.begin(); repulsor != m_repulsors.end(); repulsor++)
454   //    {
455   //        float2 delta = particle->pos - repulsor->second;
456   //        float deltaLength = length(delta) + 24.0f; // Offset length to avoid division by zero.
457   //        float deltaLengthCubed = deltaLength * deltaLength * deltaLength;
458   //        particle->vel = particle->vel + SampleSettings::Physics::Gravity * timeDelta * delta / deltaLengthCubed;
459   //    }
460
461   //    for (int i = 0; i < ARRAYSIZE(wellPositions); i++)
462   //    {
463   //        float gravitySign = 1.0f;
464   //        if ((static_cast<int>(timeTotal / 2.0f) + 1) % 10 == 0)
465   //        {
466   //            // Every 20 seconds, "explode" the gravity wells for 2 seconds.
467   //            gravitySign = -1.0f;
468   //        }
469   //        float2 delta = wellPositions[i] - particle->pos;
470   //        float deltaLength = length(delta) + 24.0f;
471   //        float deltaLengthCubed = deltaLength * deltaLength * deltaLength;
472   //        particle->vel = particle->vel + gravitySign * 0.2f * SampleSettings::Physics::Gravity * timeDelta * delta / deltaLengthCubed;
473   //    }
474
475   //    particle->vel = particle->vel * (1.0f - SampleSettings::Physics::Damping);
476
477   //    // Add random noise to the velocity to prevent particles from locking together.
478   //    
479   //    particle->vel.x += RandFloat(-0.5f, 0.5f);
480   //    particle->vel.y += RandFloat(-0.5f, 0.5f);
481
482   //    particle->pos = particle->pos + particle->vel * timeDelta;
483   //}
484 }
485
486 void GameMain::Render()
487 {
488   state_.ProcessEvent(Event::Render());
489
490
491
492   //// Draw the non-interactive asteroids.
493
494   //for (auto asteroid = m_asteroidData.begin(); asteroid != m_asteroidData.end(); asteroid++)
495   //{
496   //    spriteBatch_->Draw(
497   //        m_asteroid.Get(),
498   //        asteroid->pos,
499   //        PositionUnits::DIPs,
500   //        float2(1.0f, 1.0f) * asteroid->scale,
501   //        SizeUnits::Normalized,
502   //        float4(0.8f, 0.8f, 1.0f, 1.0f),
503   //        asteroid->rot
504   //        );
505   //}
506
507   //// Draw the interactive particles.
508
509   //for (auto particle = m_particleData.begin(); particle != m_particleData.begin() + m_numParticlesToDraw; particle++)
510   //{
511   //    float alpha = length(particle->vel) / 200.0f;
512   //    spriteBatch_->Draw(
513   //        m_particle.Get(),
514   //        particle->pos,
515   //        PositionUnits::DIPs,
516   //        float2(32.0f, 32.0f),
517   //        SizeUnits::DIPs,
518   //        float4(0.1f, 0.02f, 0.0f, alpha),
519   //        0.0f,
520   //        BlendMode::Additive
521   //        );
522   //}
523
524 }
525
526
527
528 float GameMain::RandFloat(float min, float max)
529 {
530   return (static_cast<float>(rand() % RAND_MAX) / static_cast<float>(RAND_MAX)) * (max - min) + min;
531 }
532
533 // \83T\83E\83\93\83h\8dÄ\90\83X\83\8c\83b\83h
534 void GameMain::ExecuteSoundThread()
535 {
536   //sf::com_init comInit;
537   //  InitSound();
538   soundManager_->Sequencer().Play();
539   while(!isDestroy_)
540   {
541     soundDriver_->Render();
542     //          Concurrency::wait(800);
543     //  soundDriver_->Update();
544     //  soundDriver_->Render();
545   }
546   ::SetEvent(eventHolder_.get());
547   //isDestroy_ = false;
548 }
549
550 void GameMain::StartSound()
551 {
552   if(isDestroy_) {
553     isDestroy_ = false;
554     // \83T\83E\83\93\83h\8dÄ\90\83X\83\8c\83b\83h\82Ì\8aJ\8en
555     soundTask_ = task<void>(create_async([this]()
556     {
557       ExecuteSoundThread();
558     })
559       );
560   }
561 }
562
563 void GameMain::StopSound()
564 {
565   // \83T\83E\83\93\83h\8dÄ\90\83X\83\8c\83b\83h\82Ì\92â\8e~
566   if(!isDestroy_){
567     isDestroy_ = true;
568     WaitForSingleObjectEx(eventHolder_.get(),INFINITE,FALSE);
569   }
570 }
571
572 void GameMain::ClearScreen()
573 {
574   m_d3dContext->OMSetRenderTargets(
575     1,
576     backBufferRenderTargetView_.GetAddressOf(),
577     nullptr//backBufferDepthStencilView_.Get()
578     );
579   //    m_d3dContext->OMSetDepthStencilState(backBufferDepthStencilState_.Get(),0);
580   m_d3dContext->RSSetViewports(1, &backBufferViewPort_);
581   //m_d3dContext->RSSetState(
582
583
584   m_d3dContext->ClearRenderTargetView(
585     backBufferRenderTargetView_.Get(),
586     reinterpret_cast<float*>(&D2D1::ColorF(D2D1::ColorF::Black))
587     );
588
589   //m_d3dContext->ClearDepthStencilView(backBufferDepthStencilView_.Get(),D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,1.0f,0);
590
591 }
592
593 void GameMain::RenderScreen()
594 {
595   m_d3dContext->OMSetRenderTargets(
596     1,
597     m_renderTargetView.GetAddressOf(),
598     nullptr/*m_depthStencilView.Get()*/
599     );
600
601   m_d3dContext->RSSetViewports(1, &swapChainViewPort_);
602
603
604   m_d3dContext->ClearRenderTargetView(
605     m_renderTargetView.Get(),
606     reinterpret_cast<float*>(&D2D1::ColorF(D2D1::ColorF::MidnightBlue))
607     );
608
609   //m_d3dContext->ClearDepthStencilView(m_depthStencilView.Get(),D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,1.0f,0);
610
611   spriteBatch_->Begin();
612
613   spriteBatch_->Draw(
614     backBuffer_.Get(),
615     float4(m_window->Bounds.Width / 2.0f,m_window->Bounds.Height / 2.0f,0.0f,1.0f),
616     PositionUnits::DIPs,
617     float2(1.0f, 1.0f) * scale_,
618     SizeUnits::Normalized,
619     float4(1.0f, 1.0f, 1.0f, 1.0f),
620     0.0f,
621     BlendMode::Alpha
622     );
623
624   spriteBatch_->End();
625
626   // Render the Sample Overlay.
627   sampleOverlay_->Render();
628
629 }
630
631 void GameMain::LoadTexture(const std::wstring& filename,ID3D11Texture2D** texPtrAddr)
632 {
633   // Load the sprite textures.
634
635   BasicLoader^ loader = ref new BasicLoader(m_d3dDevice.Get(), m_wicFactory.Get());
636   Platform::String^ f = ref new Platform::String(filename.c_str());
637
638   loader->LoadTexture(
639     f,
640     texPtrAddr,
641     nullptr
642     );
643 }
644
645
646 }
647