HelloWorld.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Copyright (c) 2008-2014 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/Engine/Engine.h>
  24. #include <Urho3D/UI/Font.h>
  25. #include <Urho3D/Input/Input.h>
  26. #include <Urho3D/Core/ProcessUtils.h>
  27. #include <Urho3D/UI/Text.h>
  28. #include <Urho3D/UI/UI.h>
  29. #include "HelloWorld.h"
  30. #include <Urho3D/DebugNew.h>
  31. // Expands to this example's entry-point
  32. DEFINE_APPLICATION_MAIN(HelloWorld)
  33. HelloWorld::HelloWorld(Context* context) :
  34. Sample(context)
  35. {
  36. }
  37. void HelloWorld::Start()
  38. {
  39. // Execute base class startup
  40. Sample::Start();
  41. // Create "Hello World" Text
  42. CreateText();
  43. // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
  44. // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
  45. // could subscribe in the constructor instead.
  46. SubscribeToEvents();
  47. }
  48. void HelloWorld::CreateText()
  49. {
  50. ResourceCache* cache = GetSubsystem<ResourceCache>();
  51. // Construct new Text object
  52. SharedPtr<Text> helloText(new Text(context_));
  53. // Set String to display
  54. helloText->SetText("Hello World from Urho3D!");
  55. // Set font and text color
  56. helloText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  57. helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
  58. // Align Text center-screen
  59. helloText->SetHorizontalAlignment(HA_CENTER);
  60. helloText->SetVerticalAlignment(VA_CENTER);
  61. // Add Text instance to the UI root element
  62. GetSubsystem<UI>()->GetRoot()->AddChild(helloText);
  63. }
  64. void HelloWorld::SubscribeToEvents()
  65. {
  66. // Subscribe HandleUpdate() function for processing update events
  67. SubscribeToEvent(E_UPDATE, HANDLER(HelloWorld, HandleUpdate));
  68. }
  69. void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData)
  70. {
  71. // Do nothing for now, could be extended to eg. animate the display
  72. }