YSTest: GUIApplication.cpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
GUIApplication.cpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 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 "Helper/GUIApplication.h"
29 #include "Host.h"
30 #include "Helper/Initialization.h"
31 #include "YSLib/Adaptor/Font.h"
32 #if YCL_MULTITHREAD == 1
33 # include <thread> // for std::this_thread::*;
34 #endif
35
36 YSL_BEGIN
37
38 using namespace Drawing;
39
40 namespace
41 {
42
43 #if YCL_MINGW32
44 yconstexpr double g_max_free_fps(1000);
45 std::chrono::nanoseconds idle_sleep(u64(1000000000 / g_max_free_fps));
46 #endif
47
49 GUIApplication* pApp;
50
51 } // unnamed namespace;
52
53
54 namespace
55 {
56
61 inline Message
62 FetchIdleMessage()
63 {
64 return Message(SM_INPUT);
65 }
66
71 inline void
72 Idle(Messaging::Priority prior)
73 {
74 // Note: Wait for GUI input of any shells. Post message for specific shell
75 // would cause low performance when there are many candidate messages
76 // of distinct shells.
77 PostMessage(FetchIdleMessage(), prior);
78 }
79
80 } // unnamed namespace;
81
82
83 GUIApplication::GUIApplication()
84 : Application(),
85 #if YCL_HOSTED
86 p_hosted(),
87 #endif
88 pFontCache(), UIResponseLimit(0x40), Root()
89 {
90 YAssert(!YSLib::pApp, "Duplicate instance found.");
91
92 YSLib::pApp = this;
93 InitializeEnviornment();
94 #if YCL_HOSTED
95 p_hosted = make_unique<Host::Environment>();
96 #endif
97 Root = InitializeInstalled();
98 try
99 {
100 pFontCache = make_unique<FontCache>();
101 }
102 catch(...)
103 {
104 throw LoggedEvent("Error occurred in creating font cache.");
105 }
106 {
107 const auto& node(Root["YFramework"]);
108
109 InitializeSystemFontCache(*pFontCache, AccessChild<string>(
110 node, "FontFile"), AccessChild<string>(node, "FontDirectory"));
111 }
112 }
113
114 GUIApplication::~GUIApplication()
115 {
116 Uninitialize();
117 }
118
119 FontCache&
120 GUIApplication::GetFontCache() const ynothrow
121 {
122 YAssert(bool(pFontCache), "Null pointer found.");
123
124 return *pFontCache;
125 }
126 #if YCL_HOSTED
127 Host::Environment&
128 GUIApplication::GetHost()
129 {
130 YAssert(bool(p_hosted), "Null pointer found.");
131
132 return *p_hosted;
133 }
134 #endif
135
136 bool
137 GUIApplication::DealMessage()
138 {
139 using namespace Shells;
140
141 if(Queue.IsEmpty())
142 {
143 // Idle(UIResponseLimit);
144 OnGotMessage(FetchIdleMessage());
145 #if YCL_MINGW32
146 // std::this_thread::yield();
147 std::this_thread::sleep_for(idle_sleep);
148 #endif
149 }
150 else
151 {
152 // TODO: Consider the application queue to be locked for thread safety.
153 const auto i(Queue.GetBegin());
154
155 if(YB_UNLIKELY(i->second.GetMessageID() == SM_QUIT))
156 return false;
157 if(i->first < UIResponseLimit)
158 {
159 Idle(UIResponseLimit);
160 #if YCL_MINGW32
161 std::this_thread::sleep_for(idle_sleep);
162 #endif
163 }
164 OnGotMessage(i->second);
165 Queue.Erase(i);
166 }
167 return true;
168 }
169
170
171 GUIApplication&
172 FetchGlobalInstance() ynothrow
173 {
174 YAssert(pApp, "Null pointer found.");
175
176 return *pApp;
177 }
178
179 /* extern */Application&
180 FetchAppInstance() ynothrow
181 {
182 return FetchGlobalInstance();
183 }
184
185
186 void
187 Execute(GUIApplication& app, shared_ptr<Shell> p_shl)
188 {
189 #if YCL_HOSTED
190 Host::FetchEnvironment().ExitOnAllWindowThreadCompleted = true;
191 #endif
192 if(YB_UNLIKELY(!Activate(p_shl)))
193 throw LoggedEvent("Failed launching the main shell;");
194 while(app.DealMessage())
195 ;
196 }
197
198 YSL_END
199