YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
DSMain.cpp
浏览该文件的文档.
1 /*
2  © 2012-2014 FrankHB.
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 YFM_Helper_HostRenderer // for Host::WindowThread;
30 #include YFM_Helper_Initialization
31 #include YFM_YSLib_Adaptor_Font
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 YFM_YCLib_Debug
40 #include YFM_Helper_Host
41 
42 namespace YSLib
43 {
44 
45 using namespace Drawing;
46 
47 namespace
48 {
49 
50 #if YCL_Win32
51 yconstexpr double g_max_free_fps(1000);
52 std::chrono::nanoseconds host_sleep(u64(1000000000 / g_max_free_fps));
53 #endif
54 
55 
57 DSApplication* ApplicationPtr;
58 
59 } // unnamed namespace;
60 
61 
62 DSApplication::DSApplication()
63  : GUIApplication(), DSVideoState(),
64  scrs()
65 #if YCL_Win32
66  , p_wnd_thrd()
67 #endif
68 {
69  using Devices::DSScreen;
70 
71  YAssert(!ApplicationPtr, "Duplicate instance found.");
72  //注册全局应用程序实例。
73  ApplicationPtr = this;
74  //初始化系统设备。
75 #if YCL_DS
77  InitVideo();
78 #endif
79  Devices::InitDSScreen(scrs[0], scrs[1]);
80 #if YCL_DS
81  FillPixel<PixelType>(scrs[0]->GetCheckedBufferPtr(),
82  GetAreaOf(scrs[0]->GetSize()), ColorSpace::Blue),
83  FillPixel<PixelType>(scrs[1]->GetCheckedBufferPtr(),
84  GetAreaOf(scrs[1]->GetSize()), ColorSpace::Green);
85 #elif YCL_Win32
86 
87  using namespace Host;
88 
89  YAssert(IsScreenReady(), "Screen is not ready.");
90  p_wnd_thrd.reset(new WindowThread([this]{
91  return unique_ptr<Window>(new DSWindow(CreateNativeWindow(
92  WindowClassName, {256, 384}, L"YSTest", WS_TILED | WS_CAPTION
93  | WS_SYSMENU | WS_MINIMIZEBOX), *scrs[0], *scrs[1], GetHost()));
94  }));
95  while(!p_wnd_thrd->GetWindowPtr())
96  // TODO: Resolve magic sleep duration.
97  std::this_thread::sleep_for(host_sleep);
98 
99  const auto h_wnd(p_wnd_thrd->GetWindowPtr()->GetNativeHandle());
100 
101  yunseq(scrs[0]->WindowHandle = h_wnd, scrs[1]->WindowHandle = h_wnd);
102 #endif
103 }
104 
105 DSApplication::~DSApplication()
106 {
107 #if YCL_Win32
108  p_wnd_thrd.reset();
109  YTraceDe(Notice, "Host thread dropped.");
110 #endif
111  //等待并确保所有 Shell 被释放。
112 // hShell = {};
113  //释放全局非静态资源。
114  //当主 Shell 句柄为静态存储期对象时需要通过 reset 释放。
115  //释放设备。
116  reset(scrs[0]),
117  reset(scrs[1]);
118  ApplicationPtr = {};
119 }
120 
121 Devices::DSScreen&
122 DSApplication::GetDSScreenUp() const ynothrow
123 {
124  YAssertNonnull(scrs[0]);
125  return *scrs[0];
126 }
127 Devices::DSScreen&
128 DSApplication::GetDSScreenDown() const ynothrow
129 {
130  YAssertNonnull(scrs[1]);
131  return *scrs[1];
132 }
133 Devices::Screen&
134 DSApplication::GetScreenUp() const ynothrow
135 {
136  return GetDSScreenUp();
137 }
138 Devices::Screen&
139 DSApplication::GetScreenDown() const ynothrow
140 {
141  return GetDSScreenDown();
142 }
143 
144 void
145 DSApplication::SwapScreens()
146 {
148  SwapLCD();
149 #if YF_Hosted
150  std::swap(GetDSScreenUp().Offset, GetDSScreenDown().Offset);
151 # if !YCL_Android
152  if(const auto p_wnd = GetHost().GetForegroundWindow())
153  p_wnd->Invalidate();
154 # endif
155 #endif
156 }
157 
158 
159 bool
160 #if YCL_DS
161 InitConsole(Devices::Screen& scr, Drawing::PixelType fc, Drawing::PixelType bc)
162 {
163  using namespace platform;
164 
165  if(&FetchGlobalInstance<DSApplication>().GetScreenUp() == &scr)
166  YConsoleInit(true, fc, bc);
167  else if(&FetchGlobalInstance<DSApplication>().GetScreenDown() == &scr)
168  YConsoleInit(false, fc, bc);
169  else
170  return false;
171 #elif YCL_Win32 || YCL_Android
172 InitConsole(Devices::Screen&, Drawing::PixelType, Drawing::PixelType)
173 {
174 #else
175 # error "Unsupported platform found."
176 #endif
177  return true;
178 }
179 
180 void
181 ShowFatalError(const char* s)
182 {
183  using namespace platform;
184 
185  YDebugSetStatus();
186  YDebugBegin();
187  std::printf("Fatal Error:\n%s\n", s);
188  terminate();
189 }
190 
191 #if YCL_Win32
192 namespace MinGW32
193 {
194 
195 void
196 TestFramework(size_t idx)
197 {
198  YTraceDe(Notice, ("Test beginned, idx = " + to_string(idx) + " .").c_str());
199  YTraceDe(Notice, "Test ended.");
200  yunused(idx);
201 }
202 
203 } // namespace MinGW32;
204 #endif
205 
206 } // namespace YSLib;
207 
yconstexpr wchar_t WindowClassName[]
YF_API GUIState & FetchGUIState()
取默认图形用户界面公共状态。
Definition: ygui.cpp:442
#define yunused(...)
标记未使用的表达式。
Definition: ydef.h:697
std::uint64_t u64
Definition: yadaptor.h:70
DS 宿主窗口。
DS 屏幕。
YF_API void YDebugBegin()
调试模式:显示控制台。
Definition: Debug.cpp:75
yconstfn auto GetAreaOf(const Size &s) ynothrow-> decltype(s.Width *s.Height)
取面积。
Definition: ygdibase.h:404
YF_API void YDebugSetStatus(bool=true)
调试模式:设置状态。
Definition: Debug.cpp:63
YB_NORETURN YF_API void terminate() ynothrow
异常终止函数。
Definition: ycommon.cpp:39
YF_API NativeWindowHandle CreateNativeWindow(const wchar_t *, const YSLib::Drawing::Size &, const wchar_t *=L"",::DWORD=WS_POPUP,::DWORD=WS_EX_LTRREADING)
按指定窗口类名、客户区大小、标题文本、样式和附加样式创建本机顶层窗口。
Definition: Win32GUI.cpp:222
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YF_API Drawing::FontCache & FetchDefaultFontCache()
取默认字体缓存。
YF_API void YConsoleInit(std::uint8_t dspIndex, Color fc=ColorSpace::White, Color bc=ColorSpace::Black)
启动控制台。
#define YAssertNonnull(_expr)
Definition: cassert.h:81
if(YB_LIKELY(!error)) if(YB_LIKELY(!(error
bool reset(std::unique_ptr< _type > &p)
Definition: memory.hpp:77
平台中立的图像块操作。
std::string to_string(unsigned char val)
转换为字符串。
Definition: string.hpp:353
bool InitVideo()
初始化视频输出。
Definition: Video.cpp:133
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:269
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:462
void InitDSScreen(unique_ptr< DSScreen > &p_up, unique_ptr< DSScreen > &p_dn) ynothrow
初始化 DS 屏幕。
Definition: DSScreen.h:121
void ShowFatalError(const char *s)
Definition: DSMain.cpp:181
void Reset()
复位图形用户界面状态。
Definition: ygui.cpp:185
#define YAssert(_expr, _msg)
Definition: cassert.h:73