Input.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Context.h"
  24. #include "CoreEvents.h"
  25. #include "Graphics.h"
  26. #include "GraphicsEvents.h"
  27. #include "GraphicsImpl.h"
  28. #include "Input.h"
  29. #include "Log.h"
  30. #include "Mutex.h"
  31. #include "ProcessUtils.h"
  32. #include "Profiler.h"
  33. #include "StringUtils.h"
  34. #include <cstring>
  35. #include <SDL.h>
  36. #include "DebugNew.h"
  37. static HashMap<unsigned, Input*> inputInstances;
  38. /// Return the Input subsystem instance corresponding to an SDL window ID.
  39. Input* GetInputInstance(unsigned windowID)
  40. {
  41. #if !defined(ANDROID) && !defined(IOS)
  42. return windowID ? inputInstances[windowID] : 0;
  43. #else
  44. // On mobile devices only a single Urho3D instance within a process is supported, and the window ID can not be relied on.
  45. return inputInstances.Size() ? inputInstances.Begin()->second_ : 0;
  46. #endif
  47. }
  48. /// Convert SDL keycode if necessary
  49. int ConvertSDLKeyCode(int keySym, int scanCode)
  50. {
  51. if (scanCode == SDL_SCANCODE_AC_BACK)
  52. return KEY_ESC;
  53. else
  54. return SDL_toupper(keySym);
  55. }
  56. OBJECTTYPESTATIC(Input);
  57. Input::Input(Context* context) :
  58. Object(context),
  59. windowID_(0),
  60. toggleFullscreen_(true),
  61. active_(false),
  62. minimized_(false),
  63. activated_(false),
  64. suppressNextMouseMove_(false),
  65. initialized_(false)
  66. {
  67. // Zero the initial state
  68. mouseButtonDown_ = 0;
  69. ResetState();
  70. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  71. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  72. // Try to initialize right now, but skip if screen mode is not yet set
  73. Initialize();
  74. }
  75. Input::~Input()
  76. {
  77. // Remove input instance mapping
  78. if (initialized_)
  79. {
  80. MutexLock lock(GetStaticMutex());
  81. inputInstances.Erase(windowID_);
  82. }
  83. }
  84. void Input::Update()
  85. {
  86. PROFILE(UpdateInput);
  87. if (!graphics_ || !graphics_->IsInitialized())
  88. return;
  89. // Reset input accumulation for this frame
  90. keyPress_.Clear();
  91. mouseButtonPress_ = 0;
  92. mouseMove_ = IntVector2::ZERO;
  93. mouseMoveWheel_ = 0;
  94. // Reset touch delta movement
  95. for (Map<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  96. {
  97. TouchState& state = i->second_;
  98. state.lastPosition_ = state.position_;
  99. state.delta_ = IntVector2::ZERO;
  100. }
  101. {
  102. MutexLock lock(GetStaticMutex());
  103. // Pump SDL events
  104. SDL_Event evt;
  105. SDL_PumpEvents();
  106. while (SDL_PollEvent(&evt))
  107. {
  108. // Dispatch event to the appropriate Input instance. However SDL_QUIT can not at the moment be handled for multiple
  109. // instances properly (other threads' graphics devices can not be closed from this thread), so we handle it only
  110. // for own instance
  111. if (evt.type != SDL_QUIT)
  112. HandleSDLEvent(&evt);
  113. else
  114. graphics_->Close();
  115. }
  116. }
  117. // Check for activation and inactivation from SDL window flags. Must nullcheck the window pointer because it may have
  118. // been closed due to input events
  119. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  120. if (window)
  121. {
  122. unsigned flags = SDL_GetWindowFlags(window) & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS);
  123. if (!active_ && graphics_->GetFullscreen() && flags == (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS))
  124. activated_ = true;
  125. else if (active_ && (flags & SDL_WINDOW_INPUT_FOCUS) == 0)
  126. MakeInactive();
  127. }
  128. else
  129. return;
  130. // Activate input now if necessary
  131. if (activated_)
  132. MakeActive();
  133. // Check for mouse move
  134. if (active_)
  135. {
  136. IntVector2 mousePos = GetCursorPosition();
  137. mouseMove_ = mousePos - lastCursorPosition_;
  138. // Recenter the mouse cursor manually
  139. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  140. SetCursorPosition(center);
  141. lastCursorPosition_ = center;
  142. if (mouseMove_ != IntVector2::ZERO && suppressNextMouseMove_)
  143. {
  144. mouseMove_ = IntVector2::ZERO;
  145. suppressNextMouseMove_ = false;
  146. }
  147. // Send mouse move event if necessary
  148. if (mouseMove_ != IntVector2::ZERO)
  149. {
  150. using namespace MouseMove;
  151. VariantMap eventData;
  152. eventData[P_DX] = mouseMove_.x_;
  153. eventData[P_DY] = mouseMove_.y_;
  154. eventData[P_BUTTONS] = mouseButtonDown_;
  155. eventData[P_QUALIFIERS] = GetQualifiers();
  156. SendEvent(E_MOUSEMOVE, eventData);
  157. }
  158. }
  159. }
  160. void Input::SetToggleFullscreen(bool enable)
  161. {
  162. toggleFullscreen_ = enable;
  163. }
  164. bool Input::GetKeyDown(int key) const
  165. {
  166. return keyDown_.Contains(key);
  167. }
  168. bool Input::GetKeyPress(int key) const
  169. {
  170. return keyPress_.Contains(key);
  171. }
  172. bool Input::GetMouseButtonDown(int button) const
  173. {
  174. return (mouseButtonDown_ & button) != 0;
  175. }
  176. bool Input::GetMouseButtonPress(int button) const
  177. {
  178. return (mouseButtonPress_ & button) != 0;
  179. }
  180. bool Input::GetQualifierDown(int qualifier) const
  181. {
  182. if (qualifier == QUAL_SHIFT)
  183. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  184. if (qualifier == QUAL_CTRL)
  185. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  186. if (qualifier == QUAL_ALT)
  187. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  188. return false;
  189. }
  190. bool Input::GetQualifierPress(int qualifier) const
  191. {
  192. if (qualifier == QUAL_SHIFT)
  193. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  194. if (qualifier == QUAL_CTRL)
  195. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  196. if (qualifier == QUAL_ALT)
  197. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  198. return false;
  199. }
  200. int Input::GetQualifiers() const
  201. {
  202. int ret = 0;
  203. if (GetQualifierDown(QUAL_SHIFT))
  204. ret |= QUAL_SHIFT;
  205. if (GetQualifierDown(QUAL_CTRL))
  206. ret |= QUAL_CTRL;
  207. if (GetQualifierDown(QUAL_ALT))
  208. ret |= QUAL_ALT;
  209. return ret;
  210. }
  211. TouchState Input::GetTouch(unsigned index) const
  212. {
  213. unsigned cmpIndex = 0;
  214. for (Map<int, TouchState>::ConstIterator i = touches_.Begin(); i != touches_.End(); ++i)
  215. {
  216. /// \todo Do not make a value copy
  217. if (cmpIndex == index)
  218. return i->second_;
  219. else
  220. ++cmpIndex;
  221. }
  222. return TouchState();
  223. }
  224. bool Input::IsMinimized() const
  225. {
  226. // Return minimized state also when inactive in fullscreen
  227. if (!active_ && graphics_ && graphics_->GetFullscreen())
  228. return true;
  229. else
  230. return minimized_;
  231. }
  232. void Input::Initialize()
  233. {
  234. Graphics* graphics = GetSubsystem<Graphics>();
  235. if (!graphics || !graphics->IsInitialized())
  236. return;
  237. graphics_ = graphics;
  238. // Set the initial activation
  239. activated_ = true;
  240. initialized_ = true;
  241. {
  242. MutexLock lock(GetStaticMutex());
  243. // Store window ID to direct SDL events to the correct instance
  244. windowID_ = SDL_GetWindowID(graphics_->GetImpl()->GetWindow());
  245. inputInstances[windowID_] = this;
  246. }
  247. LOGINFO("Initialized input");
  248. }
  249. void Input::MakeActive()
  250. {
  251. if (!graphics_ || !graphics_->IsInitialized())
  252. return;
  253. ResetState();
  254. active_ = true;
  255. activated_ = false;
  256. // Re-establish mouse cursor hiding as necessary
  257. SDL_ShowCursor(SDL_FALSE);
  258. suppressNextMouseMove_ = true;
  259. using namespace Activation;
  260. VariantMap eventData;
  261. eventData[P_ACTIVE] = active_;
  262. eventData[P_MINIMIZED] = minimized_;
  263. SendEvent(E_ACTIVATION, eventData);
  264. }
  265. void Input::MakeInactive()
  266. {
  267. if (!graphics_ || !graphics_->IsInitialized())
  268. return;
  269. ResetState();
  270. active_ = false;
  271. activated_ = false;
  272. // Show the mouse cursor when inactive
  273. SDL_ShowCursor(SDL_TRUE);
  274. using namespace Activation;
  275. VariantMap eventData;
  276. eventData[P_ACTIVE] = active_;
  277. eventData[P_MINIMIZED] = minimized_;
  278. SendEvent(E_ACTIVATION, eventData);
  279. }
  280. void Input::ResetState()
  281. {
  282. keyDown_.Clear();
  283. keyPress_.Clear();
  284. // When clearing touch states, send the corresponding touch end events
  285. for (Map<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  286. {
  287. TouchState& state = i->second_;
  288. using namespace TouchEnd;
  289. VariantMap eventData;
  290. eventData[P_TOUCHID] = state.touchID_;
  291. eventData[P_X] = state.position_.x_;
  292. eventData[P_Y] = state.position_.y_;
  293. SendEvent(E_TOUCHEND, eventData);
  294. }
  295. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  296. SetMouseButton(MOUSEB_LEFT, false);
  297. SetMouseButton(MOUSEB_RIGHT, false);
  298. SetMouseButton(MOUSEB_MIDDLE, false);
  299. mouseMove_ = IntVector2::ZERO;
  300. mouseMoveWheel_ = 0;
  301. mouseButtonPress_ = 0;
  302. }
  303. void Input::SetMouseButton(int button, bool newState)
  304. {
  305. // After deactivation in windowed mode, activate by a left-click inside the window
  306. if (initialized_ && !graphics_->GetFullscreen())
  307. {
  308. if (!active_ && newState && button == MOUSEB_LEFT)
  309. activated_ = true;
  310. }
  311. // If we are not active yet, do not react to the mouse button down
  312. if (newState && !active_)
  313. return;
  314. if (newState)
  315. {
  316. if (!(mouseButtonDown_ & button))
  317. mouseButtonPress_ |= button;
  318. mouseButtonDown_ |= button;
  319. }
  320. else
  321. {
  322. if (!(mouseButtonDown_ & button))
  323. return;
  324. mouseButtonDown_ &= ~button;
  325. }
  326. using namespace MouseButtonDown;
  327. VariantMap eventData;
  328. eventData[P_BUTTON] = button;
  329. eventData[P_BUTTONS] = mouseButtonDown_;
  330. eventData[P_QUALIFIERS] = GetQualifiers();
  331. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  332. }
  333. void Input::SetKey(int key, bool newState)
  334. {
  335. // If we are not active yet, do not react to the key down
  336. if (newState && !active_)
  337. return;
  338. bool repeat = false;
  339. if (newState)
  340. {
  341. if (!keyDown_.Contains(key))
  342. {
  343. keyDown_.Insert(key);
  344. keyPress_.Insert(key);
  345. }
  346. else
  347. repeat = true;
  348. }
  349. else
  350. {
  351. if (!keyDown_.Erase(key))
  352. return;
  353. }
  354. using namespace KeyDown;
  355. VariantMap eventData;
  356. eventData[P_KEY] = key;
  357. eventData[P_BUTTONS] = mouseButtonDown_;
  358. eventData[P_QUALIFIERS] = GetQualifiers();
  359. if (newState)
  360. eventData[P_REPEAT] = repeat;
  361. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  362. if (key == KEY_RETURN && newState && !repeat && toggleFullscreen_ && (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  363. graphics_->ToggleFullscreen();
  364. }
  365. void Input::SetMouseWheel(int delta)
  366. {
  367. // If we are not active yet, do not react to the wheel
  368. if (!active_)
  369. return;
  370. if (delta)
  371. {
  372. mouseMoveWheel_ += delta;
  373. using namespace MouseWheel;
  374. VariantMap eventData;
  375. eventData[P_WHEEL] = delta;
  376. eventData[P_BUTTONS] = mouseButtonDown_;
  377. eventData[P_QUALIFIERS] = GetQualifiers();
  378. SendEvent(E_MOUSEWHEEL, eventData);
  379. }
  380. }
  381. void Input::SetCursorPosition(const IntVector2& position)
  382. {
  383. if (!graphics_)
  384. return;
  385. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  386. }
  387. IntVector2 Input::GetCursorPosition() const
  388. {
  389. IntVector2 ret = lastCursorPosition_;
  390. if (!graphics_ || !graphics_->IsInitialized())
  391. return ret;
  392. SDL_GetMouseState(&ret.x_, &ret.y_);
  393. return ret;
  394. }
  395. void Input::HandleSDLEvent(void* sdlEvent)
  396. {
  397. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  398. Input* input = 0;
  399. switch (evt.type)
  400. {
  401. case SDL_KEYDOWN:
  402. // Convert to uppercase to match Win32 virtual key codes
  403. input = GetInputInstance(evt.key.windowID);
  404. if (input)
  405. input->SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), true);
  406. break;
  407. case SDL_KEYUP:
  408. input = GetInputInstance(evt.key.windowID);
  409. if (input)
  410. input->SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), false);
  411. break;
  412. case SDL_TEXTINPUT:
  413. input = GetInputInstance(evt.text.windowID);
  414. if (input && evt.text.text[0])
  415. {
  416. String text(&evt.text.text[0]);
  417. unsigned unicode = text.AtUTF8(0);
  418. if (unicode)
  419. {
  420. using namespace Char;
  421. VariantMap keyEventData;
  422. keyEventData[P_CHAR] = unicode;
  423. keyEventData[P_BUTTONS] = input->mouseButtonDown_;
  424. keyEventData[P_QUALIFIERS] = input->GetQualifiers();
  425. input->SendEvent(E_CHAR, keyEventData);
  426. }
  427. }
  428. break;
  429. case SDL_MOUSEBUTTONDOWN:
  430. input = GetInputInstance(evt.button.windowID);
  431. if (input)
  432. input->SetMouseButton(1 << (evt.button.button - 1), true);
  433. break;
  434. case SDL_MOUSEBUTTONUP:
  435. input = GetInputInstance(evt.button.windowID);
  436. if (input)
  437. input->SetMouseButton(1 << (evt.button.button - 1), false);
  438. break;
  439. case SDL_MOUSEWHEEL:
  440. input = GetInputInstance(evt.wheel.windowID);
  441. if (input)
  442. input->SetMouseWheel(evt.wheel.y);
  443. break;
  444. case SDL_FINGERDOWN:
  445. input = GetInputInstance(evt.tfinger.windowID);
  446. if (input)
  447. {
  448. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  449. TouchState& state = input->touches_[touchID];
  450. state.touchID_ = touchID;
  451. state.lastPosition_ = state.position_ = IntVector2(evt.tfinger.x * input->graphics_->GetWidth() / 32768,
  452. evt.tfinger.y * input->graphics_->GetHeight() / 32768);
  453. state.delta_ = IntVector2::ZERO;
  454. state.pressure_ = evt.tfinger.pressure;
  455. using namespace TouchBegin;
  456. VariantMap eventData;
  457. eventData[P_TOUCHID] = touchID;
  458. eventData[P_X] = state.position_.x_;
  459. eventData[P_Y] = state.position_.y_;
  460. eventData[P_PRESSURE] = state.pressure_;
  461. input->SendEvent(E_TOUCHBEGIN, eventData);
  462. }
  463. break;
  464. case SDL_FINGERUP:
  465. input = GetInputInstance(evt.tfinger.windowID);
  466. if (input)
  467. {
  468. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  469. input->touches_.Erase(touchID);
  470. using namespace TouchEnd;
  471. VariantMap eventData;
  472. eventData[P_TOUCHID] = touchID;
  473. eventData[P_X] = evt.tfinger.x * input->graphics_->GetWidth() / 32768;
  474. eventData[P_Y] = evt.tfinger.y * input->graphics_->GetHeight() / 32768;
  475. input->SendEvent(E_TOUCHEND, eventData);
  476. }
  477. break;
  478. case SDL_FINGERMOTION:
  479. input = GetInputInstance(evt.tfinger.windowID);
  480. if (input)
  481. {
  482. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  483. TouchState& state = input->touches_[touchID];
  484. state.touchID_ = touchID;
  485. state.position_ = IntVector2(evt.tfinger.x * input->graphics_->GetWidth() / 32768,
  486. evt.tfinger.y * input->graphics_->GetHeight() / 32768);
  487. state.delta_ = state.position_ - state.lastPosition_;
  488. state.pressure_ = evt.tfinger.pressure;
  489. using namespace TouchMove;
  490. VariantMap eventData;
  491. eventData[P_TOUCHID] = touchID;
  492. eventData[P_X] = state.position_.x_;
  493. eventData[P_Y] = state.position_.y_;
  494. eventData[P_DX] = evt.tfinger.dx * input->graphics_->GetWidth() / 32768;
  495. eventData[P_DY] = evt.tfinger.dy * input->graphics_->GetHeight() / 32768;
  496. eventData[P_PRESSURE] = state.pressure_;
  497. input->SendEvent(E_TOUCHMOVE, eventData);
  498. }
  499. break;
  500. case SDL_WINDOWEVENT:
  501. input = GetInputInstance(evt.window.windowID);
  502. if (input)
  503. {
  504. switch (evt.window.event)
  505. {
  506. case SDL_WINDOWEVENT_CLOSE:
  507. input->GetSubsystem<Graphics>()->Close();
  508. break;
  509. case SDL_WINDOWEVENT_MINIMIZED:
  510. input->minimized_ = true;
  511. break;
  512. case SDL_WINDOWEVENT_RESTORED:
  513. input->minimized_ = false;
  514. break;
  515. #ifdef ANDROID
  516. case SDL_WINDOWEVENT_SURFACE_LOST:
  517. // Mark GPU objects lost
  518. input->graphics_->Release(false, false);
  519. break;
  520. case SDL_WINDOWEVENT_SURFACE_CREATED:
  521. // Restore GPU objects
  522. input->graphics_->Restore();
  523. break;
  524. #endif
  525. }
  526. }
  527. break;
  528. }
  529. }
  530. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  531. {
  532. // Reset input state on subsequent initializations
  533. if (!initialized_)
  534. Initialize();
  535. else
  536. ResetState();
  537. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  538. // mouse move event. Also get the new window ID in case it changed
  539. unsigned newWindowID = SDL_GetWindowID(graphics_->GetImpl()->GetWindow());
  540. if (newWindowID != windowID_)
  541. {
  542. MutexLock lock(GetStaticMutex());
  543. inputInstances.Erase(windowID_);
  544. inputInstances[newWindowID] = this;
  545. windowID_ = newWindowID;
  546. }
  547. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  548. SetCursorPosition(center);
  549. lastCursorPosition_ = center;
  550. activated_ = true;
  551. // After setting new screen mode we should not be minimized
  552. minimized_ = false;
  553. }
  554. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  555. {
  556. // Update input right at the beginning of the frame
  557. if (initialized_)
  558. Update();
  559. }