HelloWorld.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Core/CoreEvents.h>
  23. #include <Urho3D/Core/ProcessUtils.h>
  24. #include <Urho3D/Input/Input.h>
  25. #include <Urho3D/UI/Font.h>
  26. #include <Urho3D/UI/Text.h>
  27. #include <Urho3D/UI/UI.h>
  28. #include "HelloWorld.h"
  29. #include <Urho3D/DebugNew.h>
  30. // Expands to this example's entry-point
  31. URHO3D_DEFINE_APPLICATION_MAIN(HelloWorld)
  32. HelloWorld::HelloWorld(Context* context) :
  33. Sample(context)
  34. {
  35. }
  36. void HelloWorld::Start()
  37. {
  38. // Execute base class startup
  39. Sample::Start();
  40. // Create "Hello World" Text
  41. CreateText();
  42. // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
  43. // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
  44. // could subscribe in the constructor instead.
  45. SubscribeToEvents();
  46. // Set the mouse mode to use in the sample
  47. Sample::InitMouseMode(MM_FREE);
  48. }
  49. void HelloWorld::CreateText()
  50. {
  51. auto* cache = GetSubsystem<ResourceCache>();
  52. // Construct new Text object
  53. SharedPtr<Text> helloText(new Text(context_));
  54. // Set String to display
  55. helloText->SetText("Hello World from Urho3D!");
  56. // Set font and text color
  57. helloText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  58. helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
  59. // Align Text center-screen
  60. helloText->SetHorizontalAlignment(HA_CENTER);
  61. helloText->SetVerticalAlignment(VA_CENTER);
  62. // Add Text instance to the UI root element
  63. GetSubsystem<UI>()->GetRoot()->AddChild(helloText);
  64. }
  65. void HelloWorld::SubscribeToEvents()
  66. {
  67. // Subscribe HandleUpdate() function for processing update events
  68. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HelloWorld, HandleUpdate));
  69. }
  70. void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData)
  71. {
  72. // Do nothing for now, could be extended to eg. animate the display
  73. }