OSDN Git Service

Release Preview 対応
[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 "SoundDriver.h"
10 #include "GameMain.h"
11 #include "BasicLoader.h"
12 #include "windows.ui.xaml.media.dxinterop.h"
13
14 using namespace Microsoft::WRL;
15 using namespace Windows::Foundation;
16 using namespace Windows::Foundation::Collections;
17 using namespace Windows::UI::Core;
18 using namespace Windows::UI::ViewManagement;
19
20 using namespace Windows::UI::ApplicationSettings;
21 using namespace Windows::UI::Popups;
22 using namespace Windows::UI::Xaml::Controls;
23 using namespace Windows::Graphics::Display;
24 using namespace DirectX;
25 using namespace D2D1;
26 using namespace concurrency;
27
28
29 using namespace BasicSprites;
30
31 const float GameMain::BACKBUFFER_WIDTH = 320.0f;
32 const float GameMain::BACKBUFFER_HEIGHT = 240.0f;
33
34
35 GameMain::GameMain() : backBufferViewPort_(0.0f,0.0f,BACKBUFFER_WIDTH,BACKBUFFER_HEIGHT) , isDestroy_(false)
36 {
37   soundDriver_.reset(new sf::SoundDriver());
38         soundManager_.reset(new sf::SoundManager(*soundDriver_.get()));
39   // \83T\83E\83\93\83h\8dÄ\90\83X\83\8c\83b\83h\82Ì\8aJ\8en
40   soundTask_ = task<void>(create_async([this]()
41   {
42     ExecuteSoundThread();
43   })
44   );
45         //soundThread_ = std::thread(
46  //   [this]() -> void 
47         //  {
48         //        ExecuteSoundThread();
49         //  }
50         //);
51 }
52
53 GameMain::~GameMain()
54 {
55   // \83T\83E\83\93\83h\8dÄ\90\83X\83\8c\83b\83h\82Ì\92â\8e~
56         isDestroy_ = true;
57   soundTask_.wait();
58  
59         //if(soundThread_.joinable())
60         //{
61         //      soundThread_.join();
62         //}
63 };
64
65 void GameMain::CreateDeviceIndependentResources()
66 {
67     DirectXBase::CreateDeviceIndependentResources();
68
69     // Create the performance throttler.
70
71     autoThrottle_ = ref new AutoThrottle(1.0f / 60.0f);
72 }
73
74 void GameMain::Initialize(
75         _In_ Windows::UI::Core::CoreWindow^ window,
76         _In_ Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ swapChainPanel,
77         _In_ float dpi
78         )
79 {
80         panel_  = swapChainPanel;
81         Initialize(window,dpi);
82 }
83
84 void GameMain::CreateDeviceResources()
85 {
86     DirectXBase::CreateDeviceResources();
87
88     // Create the sprite batch.
89
90     spriteBatch_ = ref new SpriteBatch();
91     unsigned int capacity = 1024;
92     if (m_featureLevel < D3D_FEATURE_LEVEL_9_3)
93     {
94         capacity = min(Parameters::MaximumCapacityCompatible, capacity);
95     }
96     spriteBatch_->Initialize(
97         m_d3dDevice.Get(),
98         capacity
99         );
100
101     // Load the sprite textures.
102
103     BasicLoader^ loader = ref new BasicLoader(m_d3dDevice.Get(), m_wicFactory.Get());
104
105     loader->LoadTexture(
106         "explosion.png",
107         &test_,
108         nullptr
109         );
110
111         spriteBatch_->AddTexture(test_.Get(),float2(64.0,64.0));
112
113     // Create the Sample Overlay.
114     
115     sampleOverlay_ = ref new SampleOverlay();
116
117     sampleOverlay_->Initialize(
118         m_d2dDevice.Get(),
119         m_d2dContext.Get(),
120         m_wicFactory.Get(),
121         m_dwriteFactory.Get(),
122         L"\83V\83\85\81[\83e\83B\83\93\83O\83Q\81[\83\80"
123         );
124
125
126
127 }
128
129 void GameMain::CreateWindowSizeDependentResources()
130 {
131 //      DirectXBase::CreateWindowSizeDependentResources();
132     // Store the window bounds so the next time we get a SizeChanged event we can
133     // avoid rebuilding everything if the size is identical.
134     m_windowBounds = m_window->Bounds;
135
136     // If the swap chain already exists, resize it.
137     if(m_swapChain != nullptr)
138     {
139         DX::ThrowIfFailed(
140             m_swapChain->ResizeBuffers(2, 0, 0, DXGI_FORMAT_B8G8R8A8_UNORM, 0)
141             );
142     }
143     // Otherwise, create a new one.
144     else
145     {
146         // Allocate a descriptor.
147         DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
148                 scale_ = floorf(m_window->Bounds.Width * m_dpi / ( 96.0f * BACKBUFFER_WIDTH));
149         swapChainDesc.Width = static_cast<UINT>(m_window->Bounds.Width * m_dpi / 96.0f);    // Can not use 0 to get the default on Composition SwapChain
150         swapChainDesc.Height = static_cast<UINT>(m_window->Bounds.Height * m_dpi / 96.0f);
151         swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;           // this is the most common swapchain format
152         swapChainDesc.Stereo = false; 
153         swapChainDesc.SampleDesc.Count = 1;                          // don't use multi-sampling
154         swapChainDesc.SampleDesc.Quality = 0;
155         swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
156         swapChainDesc.BufferCount = 2;                               // use double buffering to enable flip
157         swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
158         swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all Metro style apps must use this SwapEffect
159         swapChainDesc.Flags = 0;
160
161         // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device
162
163         // First, retrieve the underlying DXGI Device from the D3D Device.
164         ComPtr<IDXGIDevice1>  dxgiDevice;
165         DX::ThrowIfFailed(
166             m_d3dDevice.As(&dxgiDevice)
167             );
168
169         // Identify the physical adapter (GPU or card) this device is running on.
170         ComPtr<IDXGIAdapter> dxgiAdapter;
171         DX::ThrowIfFailed(
172             dxgiDevice->GetAdapter(&dxgiAdapter)
173             );
174
175         // And obtain the factory object that created it.
176         ComPtr<IDXGIFactory2> dxgiFactory;
177         DX::ThrowIfFailed(
178             dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory))
179             );
180
181         // Obtain the final swap chain for this window from the DXGI factory.
182         DX::ThrowIfFailed(
183             dxgiFactory->CreateSwapChainForComposition(
184                 m_d3dDevice.Get(),
185                 &swapChainDesc,
186                 nullptr,    // allow on all displays
187                 &m_swapChain
188                 )
189             );
190
191                 ComPtr<ISwapChainBackgroundPanelNative> dxRootPanelAsNative;
192
193         // set the swap chain on the SwapChainBackgroundPanel
194         reinterpret_cast<IUnknown*>(panel_)->QueryInterface(__uuidof(ISwapChainBackgroundPanelNative), (void**)&dxRootPanelAsNative);
195
196         DX::ThrowIfFailed(
197             dxRootPanelAsNative->SetSwapChain(m_swapChain.Get())
198             );
199
200         // Ensure that DXGI does not queue more than one frame at a time. This both reduces 
201         // latency and ensures that the application will only render after each VSync, minimizing 
202         // power consumption.
203         DX::ThrowIfFailed(
204             dxgiDevice->SetMaximumFrameLatency(1)
205             );
206
207     }
208
209     // Obtain the backbuffer for this window which will be the final 3D rendertarget.
210     ComPtr<ID3D11Texture2D> backBuffer;
211     DX::ThrowIfFailed(
212         m_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer))
213         );
214
215     // Create a view interface on the rendertarget to use on bind.
216     DX::ThrowIfFailed(
217         m_d3dDevice->CreateRenderTargetView(
218             backBuffer.Get(),
219             nullptr,
220             &m_renderTargetView
221             )
222         );
223
224     // Cache the rendertarget dimensions in our helper class for convenient use.
225     D3D11_TEXTURE2D_DESC backBufferDesc = {0};
226     backBuffer->GetDesc(&backBufferDesc);
227     m_renderTargetSize.Width  = static_cast<float>(backBufferDesc.Width);
228     m_renderTargetSize.Height = static_cast<float>(backBufferDesc.Height);
229
230     // Create a descriptor for the depth/stencil buffer.
231     CD3D11_TEXTURE2D_DESC depthStencilDesc(
232         DXGI_FORMAT_D24_UNORM_S8_UINT, 
233         backBufferDesc.Width,
234         backBufferDesc.Height,
235         1,
236         1,
237         D3D11_BIND_DEPTH_STENCIL
238         );
239
240     // Allocate a 2-D surface as the depth/stencil buffer.
241     ComPtr<ID3D11Texture2D> depthStencil;
242     DX::ThrowIfFailed(
243         m_d3dDevice->CreateTexture2D(
244             &depthStencilDesc,
245             nullptr,
246             &depthStencil
247             )
248         );
249
250     // Create a DepthStencil view on this surface to use on bind.
251     DX::ThrowIfFailed(
252         m_d3dDevice->CreateDepthStencilView(
253             depthStencil.Get(),
254             &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D),
255             &m_depthStencilView
256             )
257         );
258
259     // Create a viewport descriptor of the full window size.
260         swapChainViewPort_.Width =         static_cast<float>(backBufferDesc.Width);
261         swapChainViewPort_.Height =         static_cast<float>(backBufferDesc.Height);
262
263     // Set the current viewport using the descriptor.
264     m_d3dContext->RSSetViewports(1, &swapChainViewPort_);
265
266     // Now we set up the Direct2D render target bitmap linked to the swapchain. 
267     // Whenever we render to this bitmap, it will be directly rendered to the 
268     // swapchain associated with the window.
269     D2D1_BITMAP_PROPERTIES1 bitmapProperties = 
270         BitmapProperties1(
271             D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
272             PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
273             m_dpi,
274             m_dpi
275             );
276
277     // Direct2D needs the dxgi version of the backbuffer surface pointer.
278     ComPtr<IDXGISurface> dxgiBackBuffer;
279     DX::ThrowIfFailed(
280         m_swapChain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer))
281         );
282
283     // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
284     DX::ThrowIfFailed(
285         m_d2dContext->CreateBitmapFromDxgiSurface(
286             dxgiBackBuffer.Get(),
287             &bitmapProperties,
288             &m_d2dTargetBitmap
289             )
290         );
291
292     // So now we can set the Direct2D render target.
293     m_d2dContext->SetTarget(m_d2dTargetBitmap.Get());
294
295     // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
296     m_d2dContext->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE);
297
298     // Randomly generate some non-interactive asteroids to fit the screen.
299
300     sampleOverlay_->UpdateForWindowSizeChange();
301         // \83o\83b\83N\83o\83b\83t\83@\82Ì\8dì\90¬
302     //ComPtr<ID3D11Texture2D> backBuffer;
303     //DX::ThrowIfFailed(
304     //    m_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer))
305     //    );
306     //D3D11_TEXTURE2D_DESC backBufferDesc = {0};
307     //backBuffer->GetDesc(&backBufferDesc);
308
309         D3D11_TEXTURE2D_DESC desc = {0};
310     desc.Width = 320;
311     desc.Height = 240;
312     desc.Format = backBufferDesc.Format;
313     desc.MipLevels = 1;
314     desc.SampleDesc.Count = 1;
315     desc.SampleDesc.Quality = 0;
316     desc.ArraySize = 1;
317     desc.Usage = D3D11_USAGE_DEFAULT;
318     desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
319 //    desc.MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
320
321     DX::ThrowIfFailed(m_d3dDevice->CreateTexture2D(&desc,NULL,&backBuffer_));
322
323         DX::ThrowIfFailed(m_d3dDevice->CreateRenderTargetView(backBuffer_.Get(),0,&backBufferRenderTargetView_));
324
325  //   // \90[\93x\83o\83b\83t\83@\82Ì\8dì\90¬
326  //   D3D11_TEXTURE2D_DESC depth = {} ;
327  //   depth.Width = desc.Width;//rc.right - rc.left;client_width_;
328  //   depth.Height = desc.Height;//rc.bottom - rc.top;client_height_;
329  //   depth.MipLevels = 1;
330  //   depth.ArraySize = 1;
331  //   depth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
332  //   depth.SampleDesc.Count = 1;
333  //   depth.SampleDesc.Quality = 0;
334  //   depth.Usage = D3D11_USAGE_DEFAULT;
335  //   depth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
336  //   depth.CPUAccessFlags = 0;
337  //   depth.MiscFlags = 0;
338  //   ComPtr<ID3D11Texture2D> depthT;
339         //DX::ThrowIfFailed(m_d3dDevice->CreateTexture2D( &depth, NULL, &depthT ));
340
341  //   D3D11_DEPTH_STENCIL_VIEW_DESC dsv = {};
342  //   dsv.Format = depth.Format;
343  //   dsv.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
344  //   dsv.Texture2D.MipSlice = 0;
345
346         //DX::ThrowIfFailed(m_d3dDevice->CreateDepthStencilView( depthT.Get(), &dsv, &backBufferDepthStencilView_ ));
347
348         spriteBatch_->AddTexture(backBuffer_.Get(),float2());
349
350         // \90[\93x\83X\83e\83\93\83V\83\8b\83X\83e\81[\83g\82ð\8dì\90¬\82·\82é
351    //D3D11_DEPTH_STENCIL_DESC ddsDesc;
352    //::ZeroMemory( &ddsDesc, sizeof( ddsDesc ) );
353    //ddsDesc.DepthEnable = TRUE;                                     // \90[\93x\83e\83X\83g\82ð\8eg\97p\82·\82é
354    //ddsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
355    //ddsDesc.DepthFunc = D3D11_COMPARISON_LESS;
356    //ddsDesc.StencilEnable = FALSE;
357    //DX::ThrowIfFailed(m_d3dDevice->CreateDepthStencilState( &ddsDesc, &backBufferDepthStencilState_ ));
358
359 }
360
361 void GameMain::Update(float timeTotal, float timeDelta)
362 {
363     // Update the performance throttler.
364
365     auto control = autoThrottle_->Update(timeDelta);
366
367     //if (control == FrameWorkload::Increase)
368     //{
369     //    m_numParticlesToDraw += SampleSettings::Performance::ParticleCountDelta;
370     //}
371     //if (control == FrameWorkload::Decrease)
372     //{
373     //    m_numParticlesToDraw -= SampleSettings::Performance::ParticleCountDelta;
374     //}
375     //if (control != FrameWorkload::Maintain)
376     //{
377     //    m_numParticlesToDraw = max(SampleSettings::Performance::ParticleCountMin, min(SampleSettings::Performance::ParticleCountMax, m_numParticlesToDraw));
378     //    if (m_featureLevel < D3D_FEATURE_LEVEL_9_3)
379     //    {
380     //        m_numParticlesToDraw = min(static_cast<int>(Parameters::MaximumCapacityCompatible - SampleSettings::NumAsteroids - 1), m_numParticlesToDraw);
381     //    }
382     //}
383
384     // Update the non-interactive asteroids.
385     // Their behavior is to drift across the window with a fixed translational and rotational
386     // velocity.  Upon crossing a boundary outside the window, their position wraps.
387
388     //for (auto asteroid = m_asteroidData.begin(); asteroid != m_asteroidData.end(); asteroid++)
389     //{
390     //    static const float border = 100.0f;
391     //    asteroid->pos = asteroid->pos + asteroid->vel * timeDelta;
392     //    if (asteroid->vel.x < 0)
393     //    {
394     //        if (asteroid->pos.x < -border)
395     //        {
396     //            asteroid->pos.x = m_windowBounds.Width + border;
397     //        }
398     //    }
399     //    else
400     //    {
401     //        if (asteroid->pos.x > m_windowBounds.Width + border)
402     //        {
403     //            asteroid->pos.x = -border;
404     //        }
405     //    }
406     //    if (asteroid->vel.y < 0)
407     //    {
408     //        if (asteroid->pos.y < -border)
409     //        {
410     //            asteroid->pos.y = m_windowBounds.Height + border;
411     //        }
412     //    }
413     //    else
414     //    {
415     //        if (asteroid->pos.y > m_windowBounds.Height + border)
416     //        {
417     //            asteroid->pos.y = -border;
418     //        }
419     //    }
420
421     //    asteroid->rot += asteroid->rotVel * timeDelta;
422     //    if (asteroid->rot > static_cast<float>(M_PI))
423     //    {
424     //        asteroid->rot -= 2.0f * static_cast<float>(M_PI);
425     //    }
426     //    if (asteroid->rot < static_cast<float>(-M_PI))
427     //    {
428     //        asteroid->rot += 2.0f * static_cast<float>(M_PI);
429     //    }
430     //}
431
432     //// Update the interactive particles.
433     //// Their behavior is to be gravitationally attracted to two oscillating gravity
434     //// wells and repelled by any pressed pointer points.  Upon reaching the edge of
435     //// the window, the particles bounce.
436
437     //// Add two gravity wells that move throughout the window.
438     //float2 wellPositions[] =
439     //{
440     //    float2(
441     //        (1.0f + 0.8f * cosf(timeTotal / (2.0f * static_cast<float>(M_PI)) + 3.0f)) * m_windowBounds.Width / 2.0f,
442     //        (1.0f + 0.8f * sinf(timeTotal / 5.0f)) * m_windowBounds.Height / 2.0f
443     //        ),
444     //    float2(
445     //        (1.0f + 0.8f * cosf(timeTotal / static_cast<float>(M_PI * M_PI) + 1.0f)) * m_windowBounds.Width / 2.0f,
446     //        (1.0f + 0.8f * sinf(timeTotal / static_cast<float>(M_PI))) * m_windowBounds.Height / 2.0f
447     //        )
448     //};
449
450     //for (auto particle = m_particleData.begin(); particle != m_particleData.begin() + m_numParticlesToDraw; particle++)
451     //{
452     //    if (particle->pos.x < 0)
453     //    {
454     //        particle->vel.x = abs(particle->vel.x);
455     //    }
456     //    if (particle->pos.x > m_windowBounds.Width)
457     //    {
458     //        particle->vel.x = -abs(particle->vel.x);
459     //    }
460     //    if (particle->pos.y < 0)
461     //    {
462     //        particle->vel.y = abs(particle->vel.y);
463     //    }
464     //    if (particle->pos.y > m_windowBounds.Height)
465     //    {
466     //        particle->vel.y = -abs(particle->vel.y);
467     //    }
468
469     //    for (auto repulsor = m_repulsors.begin(); repulsor != m_repulsors.end(); repulsor++)
470     //    {
471     //        float2 delta = particle->pos - repulsor->second;
472     //        float deltaLength = length(delta) + 24.0f; // Offset length to avoid division by zero.
473     //        float deltaLengthCubed = deltaLength * deltaLength * deltaLength;
474     //        particle->vel = particle->vel + SampleSettings::Physics::Gravity * timeDelta * delta / deltaLengthCubed;
475     //    }
476
477     //    for (int i = 0; i < ARRAYSIZE(wellPositions); i++)
478     //    {
479     //        float gravitySign = 1.0f;
480     //        if ((static_cast<int>(timeTotal / 2.0f) + 1) % 10 == 0)
481     //        {
482     //            // Every 20 seconds, "explode" the gravity wells for 2 seconds.
483     //            gravitySign = -1.0f;
484     //        }
485     //        float2 delta = wellPositions[i] - particle->pos;
486     //        float deltaLength = length(delta) + 24.0f;
487     //        float deltaLengthCubed = deltaLength * deltaLength * deltaLength;
488     //        particle->vel = particle->vel + gravitySign * 0.2f * SampleSettings::Physics::Gravity * timeDelta * delta / deltaLengthCubed;
489     //    }
490
491     //    particle->vel = particle->vel * (1.0f - SampleSettings::Physics::Damping);
492
493     //    // Add random noise to the velocity to prevent particles from locking together.
494     //    
495     //    particle->vel.x += RandFloat(-0.5f, 0.5f);
496     //    particle->vel.y += RandFloat(-0.5f, 0.5f);
497
498     //    particle->pos = particle->pos + particle->vel * timeDelta;
499     //}
500 }
501
502 void GameMain::Render()
503 {
504         static int frame_count = 0;
505
506     m_d3dContext->OMSetRenderTargets(
507         1,
508         backBufferRenderTargetView_.GetAddressOf(),
509         nullptr//backBufferDepthStencilView_.Get()
510         );
511 //      m_d3dContext->OMSetDepthStencilState(backBufferDepthStencilState_.Get(),0);
512           m_d3dContext->RSSetViewports(1, &backBufferViewPort_);
513         //m_d3dContext->RSSetState(
514
515
516         m_d3dContext->ClearRenderTargetView(
517         backBufferRenderTargetView_.Get(),
518         reinterpret_cast<float*>(&D2D1::ColorF(D2D1::ColorF::Black))
519         );
520
521         //m_d3dContext->ClearDepthStencilView(backBufferDepthStencilView_.Get(),D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,1.0f,0);
522
523
524     spriteBatch_->Begin();
525
526     // Draw the background.
527
528     spriteBatch_->Draw(
529         test_.Get(),
530         float4(160.0f, 120.0f,0.7f,1.0f),
531         PositionUnits::Pixels,
532         float2(64.0f, 64.0f),
533         SizeUnits::Pixels,
534         float4(1.0f, 1.0f, 1.0f, 1.0f),
535                 0.0f,
536                 BlendMode::Additive,
537                 float2((float)((frame_count >> 3) % 4) * 64.0f,(float)((frame_count >> 3) / 4) * 64.0f)
538         );
539
540         spriteBatch_->Draw(
541         test_.Get(),
542         float4(128.0f, 100.0f,0.7f,1.0f),
543         PositionUnits::Pixels,
544         float2(32.0f, 32.0f),
545         SizeUnits::Pixels,
546         float4(1.0f, 1.0f, 1.0f, 1.0f),
547                 0.0f,
548                 BlendMode::Additive,
549                 float2((float)((frame_count >> 3) % 4) * 64.0f,(float)((frame_count >> 3) / 4) * 64.0f)
550         );
551
552         spriteBatch_->Draw(
553         test_.Get(),
554         float4(128.0f, 50.0f,0.7f,1.0f),
555         PositionUnits::Pixels,
556         float2(96.0f, 96.0f),
557         SizeUnits::Pixels,
558         float4(1.0f, 1.0f, 1.0f, 1.0f),
559                 0.0f,
560                 BlendMode::Additive,
561                 float2((float)((frame_count >> 3) % 4) * 64.0f,(float)((frame_count >> 3) / 4) * 64.0f)
562         );
563
564         frame_count = (frame_count + 1) & (0x7f);
565
566     //// Draw the non-interactive asteroids.
567
568     //for (auto asteroid = m_asteroidData.begin(); asteroid != m_asteroidData.end(); asteroid++)
569     //{
570     //    spriteBatch_->Draw(
571     //        m_asteroid.Get(),
572     //        asteroid->pos,
573     //        PositionUnits::DIPs,
574     //        float2(1.0f, 1.0f) * asteroid->scale,
575     //        SizeUnits::Normalized,
576     //        float4(0.8f, 0.8f, 1.0f, 1.0f),
577     //        asteroid->rot
578     //        );
579     //}
580
581     //// Draw the interactive particles.
582
583     //for (auto particle = m_particleData.begin(); particle != m_particleData.begin() + m_numParticlesToDraw; particle++)
584     //{
585     //    float alpha = length(particle->vel) / 200.0f;
586     //    spriteBatch_->Draw(
587     //        m_particle.Get(),
588     //        particle->pos,
589     //        PositionUnits::DIPs,
590     //        float2(32.0f, 32.0f),
591     //        SizeUnits::DIPs,
592     //        float4(0.1f, 0.02f, 0.0f, alpha),
593     //        0.0f,
594     //        BlendMode::Additive
595     //        );
596     //}
597
598     spriteBatch_->End();
599
600     m_d3dContext->OMSetRenderTargets(
601         1,
602         m_renderTargetView.GetAddressOf(),
603         nullptr/*m_depthStencilView.Get()*/
604         );
605
606         m_d3dContext->RSSetViewports(1, &swapChainViewPort_);
607
608
609         m_d3dContext->ClearRenderTargetView(
610         m_renderTargetView.Get(),
611         reinterpret_cast<float*>(&D2D1::ColorF(D2D1::ColorF::MidnightBlue))
612         );
613
614         //m_d3dContext->ClearDepthStencilView(m_depthStencilView.Get(),D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,1.0f,0);
615
616         spriteBatch_->Begin();
617
618     spriteBatch_->Draw(
619         backBuffer_.Get(),
620         float4(m_window->Bounds.Width / 2.0f,m_window->Bounds.Height / 2.0f,0.0f,1.0f),
621         PositionUnits::DIPs,
622         float2(1.0f, 1.0f) * scale_,
623         SizeUnits::Normalized,
624         float4(1.0f, 1.0f, 1.0f, 1.0f),
625         0.0f,
626         BlendMode::Alpha
627         );
628
629         spriteBatch_->End();
630
631     // Render the Sample Overlay.
632         sampleOverlay_->Render();
633 }
634
635
636
637 float GameMain::RandFloat(float min, float max)
638 {
639     return (static_cast<float>(rand() % RAND_MAX) / static_cast<float>(RAND_MAX)) * (max - min) + min;
640 }
641
642 // \83T\83E\83\93\83h\8dÄ\90\83X\83\8c\83b\83h
643 void GameMain::ExecuteSoundThread()
644 {
645         //sf::com_init comInit;
646   InitSound();
647   soundManager_->Sequencer().Play();
648         while(!isDestroy_)
649         {
650     soundDriver_->Render();
651 //              Concurrency::wait(800);
652         //      soundDriver_->Update();
653         //      soundDriver_->Render();
654         }
655 }
656
657 void GameMain::InitSound()
658 {
659       // \8b\98\94g\83e\81[\83u\83\8b\82ð\8dì\82é
660       {
661         sf::Synthesizer::WaveTable sawtbl;
662         sawtbl.sampleRate = 440.0f;
663         sawtbl.basePitch = 0.0f;
664         sawtbl.stereo = false;
665
666         float v = -1.0f,d = 2.0f / 1024.0f;
667         for(int  i = 0;i < 1024;++i)
668         {
669           //        if(i < 15) v = -1.0f; else v = 1.0f; 
670           sawtbl.waveData.push_back(v) ;
671           v += d;
672         }
673         soundManager_->Synthesizer().WaveTables().push_back(std::move(sawtbl));
674       }
675
676       // \8bé\8c`\94g\83e\81[\83u\83\8b\82ð\8dì\82é
677       {
678         sf::Synthesizer::WaveTable squaretbl;
679         squaretbl.sampleRate = 440.0f;
680         squaretbl.basePitch = 0.0f;
681         squaretbl.stereo = false;
682
683         float v = 0.0f;
684         for(int  i = 0;i < 32;++i)
685         {
686           if(i < 15) v = -1.0f; else v = 1.0f; 
687           squaretbl.waveData.push_back(v) ;
688         }
689         soundManager_->Synthesizer().WaveTables().push_back(std::move(squaretbl));
690       }
691
692       // \8eO\8ap\94g\83e\81[\83u\83\8b
693       {
694         sf::Synthesizer::WaveTable tritbl;
695         tritbl.sampleRate = 440.0f;
696         tritbl.basePitch = 0.0f;
697         tritbl.stereo = false;
698
699         float v = -1.0f,d = 2.0f / 16.0f;
700         for(int  i = 0;i < 32;++i)
701         {
702           if(i < 15) d = -d; 
703           tritbl.waveData.push_back(v) ;
704           v += d;
705         }
706         soundManager_->Synthesizer().WaveTables().push_back(std::move(tritbl));
707       }
708
709       // sin\83e\81[\83u\83\8b
710       {
711         sf::Synthesizer::WaveTable sintbl;
712         sintbl.sampleRate = 440.0f;
713         sintbl.basePitch = 0.0f;
714         sintbl.stereo = false;
715
716         float v = 0.0f,d = 2.0f * M_PI / 128.0f;
717         for(int  i = 0;i < 128;++i)
718         {
719           sintbl.waveData.push_back(sinf(v));
720           v += d;
721         }
722         soundManager_->Synthesizer().WaveTables().push_back(std::move(sintbl));
723       }
724
725       // LFO\97p Sin\83e\81[\83u\83\8b
726       {
727         sf::Synthesizer::WaveTable sintbl;
728         sintbl.sampleRate = 440.0f;
729         sintbl.basePitch = 0.0f;
730         sintbl.stereo = false;
731
732         float v = 0.0f,d = 2.0f * M_PI / 128.0f;
733         for(int  i = 0;i < 128;++i)
734         {
735           sintbl.waveData.push_back(sinf(v) / 2.0f + 0.5f);
736           v += d;
737         }
738         soundManager_->Synthesizer().WaveTables().push_back(std::move(sintbl));
739       }
740
741
742
743
744       //      ::OutputDebugStringW((boost::wformat(L"waveTable size: %d") % osc_[0].WaveData().size()).str().c_str());
745
746       // timber\82Ì\83Z\83b\83g\83A\83b\83v
747       for(int i = 0; i < 4;++i){
748
749         sf::Synthesizer::Timber timber;
750         timber.oscillator.reset(new sf::Synthesizer::WaveTableOscillator(&soundManager_->Synthesizer().WaveTables().at(0)));
751         timber.amplitude.gain = 1.0f;
752         timber.amplitude.envelope.releaseNoteOff = true;
753         timber.amplitude.envelope.attackTime = 0.01f;
754         timber.amplitude.envelope.decayTime = 0.02f;
755         timber.amplitude.envelope.sustainLevel = 0.5f;
756         timber.amplitude.envelope.releaseTime = 0.2f;
757         timber.amplitude.envelope.gain = 1.0f;
758         timber.amplitude.lfo.waveForm = &(soundManager_->Synthesizer().WaveTables()[4]);
759         timber.amplitude.lfo.freq = 5.0f;
760         timber.amplitude.lfo.gain = 0.0f;
761
762         timber.pitch.lfo.freq = 10.0f;
763         timber.pitch.lfo.gain = 0.000f;
764         timber.pitch.lfo.waveForm = &(soundManager_->Synthesizer().WaveTables()[4]);
765         timber.pitch.lfo.startNoteOn = true;
766         timber.pitch.envelope.attackTime = 0.0f;
767         timber.pitch.envelope.decayTime = 0.02f;
768         timber.pitch.envelope.sustainLevel = 0.5f;
769         timber.pitch.envelope.gain =0.0f;
770         timber.pitch.pitch = 0.0f;
771
772         timber.pan.lfo.freq = 2.0f;
773         timber.pan.lfo.gain = 1.0f;
774         timber.pan.lfo.waveForm = &(soundManager_->Synthesizer().WaveTables()[3]);
775         timber.pan.lfo.startNoteOn = true;
776         timber.pan.envelope.enable = false;
777         timber.pan.lfo.envelope.enable = false;
778         timber.pan.pan = 0.0f;
779
780         timber.filter.lfo.freq = 5.0f;
781         timber.filter.lfo.gain = 1.0f;
782         timber.filter.lfo.waveForm = &(soundManager_->Synthesizer().WaveTables()[3]);
783         timber.filter.lfo.startNoteOn = true;
784         timber.filter.cutoff = 10000.0f;
785         timber.filter.resonance = 0.0f;
786         timber.filter.envelope.attackTime = 0.01f;
787         timber.filter.envelope.decayTime = 0.03f;
788         timber.filter.envelope.sustainLevel = 0.05f;
789         timber.filter.envelope.releaseTime = 0.2f;
790         timber.filter.envelope.gain = 10000.0f;
791
792         soundManager_->Synthesizer().AddProgram(sf::Synthesizer::Program(std::move((boost::wformat(L"Program No.%d") % i).str()),std::move(timber)));   
793         //        timbers_.push_back(std::move(timber));
794       }
795
796       for(int i = 0,end = soundManager_->Synthesizer().Voices();i < end;++i)
797       {
798          soundManager_->Synthesizer().AssignProgramToVoice(0,i);
799       }
800
801       soundManager_->Synthesizer().isEnable(true);
802 }
803