summaryrefslogtreecommitdiffstats
path: root/Tests/VSWinStorePhone/Direct3DApp1/Direct3DApp1.cpp
blob: ddcd8bd9344f4ec75687c61ac0c73c799278bc9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "pch.h"

#include "BasicTimer.h"
#include "Direct3DApp1.h"

using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Core;
using namespace Windows::System;
using namespace Windows::Foundation;
using namespace Windows::Graphics::Display;
using namespace concurrency;

Direct3DApp1::Direct3DApp1()
  : m_windowClosed(false)
  , m_windowVisible(true)
{
}

void Direct3DApp1::Initialize(CoreApplicationView ^ applicationView)
{
  applicationView->Activated +=
    ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs ^>(
      this, &Direct3DApp1::OnActivated);

  CoreApplication::Suspending += ref new EventHandler<SuspendingEventArgs ^>(
    this, &Direct3DApp1::OnSuspending);

  CoreApplication::Resuming +=
    ref new EventHandler<Platform::Object ^>(this, &Direct3DApp1::OnResuming);

  m_renderer = ref new CubeRenderer();
}

void Direct3DApp1::SetWindow(CoreWindow ^ window)
{
  window->SizeChanged +=
    ref new TypedEventHandler<CoreWindow ^, WindowSizeChangedEventArgs ^>(
      this, &Direct3DApp1::OnWindowSizeChanged);

  window->VisibilityChanged +=
    ref new TypedEventHandler<CoreWindow ^, VisibilityChangedEventArgs ^>(
      this, &Direct3DApp1::OnVisibilityChanged);

  window->Closed +=
    ref new TypedEventHandler<CoreWindow ^, CoreWindowEventArgs ^>(
      this, &Direct3DApp1::OnWindowClosed);

#ifndef PHONE
  window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
#endif

  window->PointerPressed +=
    ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(
      this, &Direct3DApp1::OnPointerPressed);

  window->PointerMoved +=
    ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(
      this, &Direct3DApp1::OnPointerMoved);

  m_renderer->Initialize(CoreWindow::GetForCurrentThread());
}

void Direct3DApp1::Load(Platform::String ^ entryPoint)
{
}

void Direct3DApp1::Run()
{
  BasicTimer ^ timer = ref new BasicTimer();

  while (!m_windowClosed) {
    if (m_windowVisible) {
      timer->Update();
      CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
        CoreProcessEventsOption::ProcessAllIfPresent);
      m_renderer->Update(timer->Total, timer->Delta);
      m_renderer->Render();
      m_renderer
        ->Present(); // This call is synchronized to the display frame rate.
    } else {
      CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
        CoreProcessEventsOption::ProcessOneAndAllPending);
    }
  }
}

void Direct3DApp1::Uninitialize()
{
}

void Direct3DApp1::OnWindowSizeChanged(CoreWindow ^ sender,
                                       WindowSizeChangedEventArgs ^ args)
{
  m_renderer->UpdateForWindowSizeChange();
}

void Direct3DApp1::OnVisibilityChanged(CoreWindow ^ sender,
                                       VisibilityChangedEventArgs ^ args)
{
  m_windowVisible = args->Visible;
}

void Direct3DApp1::OnWindowClosed(CoreWindow ^ sender,
                                  CoreWindowEventArgs ^ args)
{
  m_windowClosed = true;
}

void Direct3DApp1::OnPointerPressed(CoreWindow ^ sender,
                                    PointerEventArgs ^ args)
{
  // Insert your code here.
}

void Direct3DApp1::OnPointerMoved(CoreWindow ^ sender, PointerEventArgs ^ args)
{
  // Insert your code here.
}

void Direct3DApp1::OnActivated(CoreApplicationView ^ applicationView,
                               IActivatedEventArgs ^ args)
{
  CoreWindow::GetForCurrentThread()->Activate();
}

void Direct3DApp1::OnSuspending(Platform::Object ^ sender,
                                SuspendingEventArgs ^ args)
{
  // Save app state asynchronously after requesting a deferral. Holding a
  // deferral
  // indicates that the application is busy performing suspending operations.
  // Be
  // aware that a deferral may not be held indefinitely. After about five
  // seconds,
  // the app will be forced to exit.
  SuspendingDeferral ^ deferral = args->SuspendingOperation->GetDeferral();
  m_renderer->ReleaseResourcesForSuspending();

  create_task([this, deferral]() {
    // Insert your code here.

    deferral->Complete();
  });
}

void Direct3DApp1::OnResuming(Platform::Object ^ sender,
                              Platform::Object ^ args)
{
  // Restore any data or state that was unloaded on suspend. By default, data
  // and state are persisted when resuming from suspend. Note that this event
  // does not occur if the app was previously terminated.
  m_renderer->CreateWindowSizeDependentResources();
}

IFrameworkView ^ Direct3DApplicationSource::CreateView()
{
  return ref new Direct3DApp1();
}

[Platform::MTAThread] int main(Platform::Array<Platform::String ^> ^)
{
  auto direct3DApplicationSource = ref new Direct3DApplicationSource();
  CoreApplication::Run(direct3DApplicationSource);
  return 0;
}