YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
DSMain.cpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2012 - 2013.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #include "DSScreen.h"
29 #include "Host.h"
30 #include "Helper/Initialization.h"
31 #include "YSLib/Adaptor/Font.h"
32 #include "DSWindow.h"
33 #if YCL_MULTITHREAD == 1
34 # include <thread> // for std::this_thread::*;
35 #endif
36 #ifdef YCL_DS
37 # include "YSLib/Service/yblit.h" // for Drawing::FillPixel;
38 #endif
39 #include "YCLib/Debug.h"
40 
42 
43 using namespace Drawing;
44 
45 namespace
46 {
47 
48 #if YCL_MINGW32
49 yconstexpr double g_max_free_fps(1000);
50 std::chrono::nanoseconds host_sleep(u64(1000000000 / g_max_free_fps));
51 #endif
52 
53 
54 //注册的应用程序指针。
55 DSApplication* pApp;
56 
57 
58 #if YCL_MINGW32
59 
60 ::HWND
61 InitializeMainWindow(const wchar_t* wnd_title, u16 wnd_w, u16 wnd_h,
62  ::DWORD wstyle = WS_TILED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
63 {
64  ::RECT rect{0, 0, wnd_w, wnd_h};
65 
66  ::AdjustWindowRect(&rect, wstyle, FALSE);
67  return ::CreateWindowW(Host::WindowClassName, wnd_title, wstyle,
68  CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left,
69  rect.bottom - rect.top, HWND_DESKTOP, nullptr,
70  ::GetModuleHandleW(nullptr), nullptr);
71 }
72 #endif
73 
74 } // unnamed namespace;
75 
76 
78  : GUIApplication(),
79  scrs()
80 #if YCL_MINGW32
81  , p_wnd_thrd()
82 #endif
83 {
84  using Devices::DSScreen;
85 
86  YAssert(!YSLib::pApp, "Duplicate instance found.");
87 
88  //注册全局应用程序实例。
89  YSLib::pApp = this;
90  //初始化系统设备。
91 #if YCL_DS
92  InitVideo();
93 #endif
95 #if YCL_DS
96  scrs[0]->Update(ColorSpace::Blue),
97  scrs[1]->Update(ColorSpace::Green);
98 #elif YCL_MINGW32
99 
100  using namespace Host;
101 
102  YAssert(IsScreenReady(), "Screen is not ready.");
103 
104  p_wnd_thrd.reset(new WindowThread([this]{
105  return unique_ptr<Window>(new DSWindow(InitializeMainWindow(
106  L"YSTest", 256, 384), *scrs[0], *scrs[1], GetHost()));
107  }));
108  // FIXME: Reduce possible data race.
109  while(!p_wnd_thrd->GetWindowPtr())
110  // TODO: Resolve magic sleep duration.
111  std::this_thread::sleep_for(host_sleep);
112 
113  const auto h_wnd(p_wnd_thrd->GetWindowPtr()->GetNativeHandle());
114 
115  yunseq(scrs[0]->WindowHandle = h_wnd, scrs[1]->WindowHandle = h_wnd);
116 #endif
117 }
118 
120 {
121 #if YCL_MINGW32
122  p_wnd_thrd.reset();
123  YCL_DEBUG_PUTS("Host thread dropped.");
124 #endif
125  //等待并确保所有 Shell 被释放。
126 // hShell = nullptr;
127  //释放全局非静态资源。
128  //当主 Shell 句柄为静态存储期对象时需要通过 reset 释放。
129  //释放设备。
130  reset(scrs[0]),
131  reset(scrs[1]);
132 }
133 
134 Devices::DSScreen&
135 DSApplication::GetDSScreenUp() const ynothrow
136 {
137  YAssert(bool(scrs[0]), "Null pointer found.");
138 
139  return *scrs[0];
140 }
141 Devices::DSScreen&
142 DSApplication::GetDSScreenDown() const ynothrow
143 {
144  YAssert(bool(scrs[1]), "Null pointer found.");
145 
146  return *scrs[1];
147 }
148 Devices::Screen&
150 {
151  return GetDSScreenUp();
152 }
153 Devices::Screen&
155 {
156  return GetDSScreenDown();
157 }
158 
159 
160 bool
161 #if YCL_DS
162 InitConsole(Devices::Screen& scr, Drawing::PixelType fc, Drawing::PixelType bc)
163 {
164  using namespace platform;
165 
166  if(&FetchGlobalInstance<DSApplication>().GetScreenUp() == &scr)
167  YConsoleInit(true, fc, bc);
168  else if(&FetchGlobalInstance<DSApplication>().GetScreenDown() == &scr)
169  YConsoleInit(false, fc, bc);
170  else
171  return false;
172 #elif YCL_MINGW32
174 {
175 #else
176 # error Unsupported platform found!
177 #endif
178  return true;
179 }
180 
181 void
182 ShowFatalError(const char* s)
183 {
184  using namespace platform;
185 
186  YDebugSetStatus();
187  YDebugBegin();
188  std::printf("Fatal Error:\n%s\n", s);
189  terminate();
190 }
191 
192 #if YCL_MINGW32
193 YSL_BEGIN_NAMESPACE(MinGW32)
194 
195 void
196 TestFramework(size_t idx)
197 {
198  YCL_DEBUG_PUTS(("Test beginned, idx = " + to_string(idx) + " .").c_str());
199  YCL_DEBUG_PUTS("Test ended.");
200 }
201 
202 YSL_END_NAMESPACE(MinGW32)
203 #endif
204 
205 YSL_END
206