HelloWorld.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Core/ProcessUtils.h>
  5. #include <Urho3D/Input/Input.h>
  6. #include <Urho3D/UI/Font.h>
  7. #include <Urho3D/UI/Text.h>
  8. #include <Urho3D/UI/UI.h>
  9. #include "HelloWorld.h"
  10. #include <Urho3D/DebugNew.h>
  11. // Expands to this example's entry-point
  12. URHO3D_DEFINE_APPLICATION_MAIN(HelloWorld)
  13. HelloWorld::HelloWorld(Context* context) :
  14. Sample(context)
  15. {
  16. }
  17. void HelloWorld::Start()
  18. {
  19. // Execute base class startup
  20. Sample::Start();
  21. // Create "Hello World" Text
  22. CreateText();
  23. // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
  24. // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
  25. // could subscribe in the constructor instead.
  26. SubscribeToEvents();
  27. // Set the mouse mode to use in the sample
  28. Sample::InitMouseMode(MM_FREE);
  29. }
  30. void HelloWorld::CreateText()
  31. {
  32. auto* cache = GetSubsystem<ResourceCache>();
  33. // Construct new Text object
  34. SharedPtr<Text> helloText(new Text(context_));
  35. // Set String to display
  36. helloText->SetText("Hello World from Urho3D!");
  37. // Set font and text color
  38. helloText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  39. helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
  40. // Align Text center-screen
  41. helloText->SetHorizontalAlignment(HA_CENTER);
  42. helloText->SetVerticalAlignment(VA_CENTER);
  43. // Add Text instance to the UI root element
  44. GetSubsystem<UI>()->GetRoot()->AddChild(helloText);
  45. }
  46. void HelloWorld::SubscribeToEvents()
  47. {
  48. // Subscribe HandleUpdate() function for processing update events
  49. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HelloWorld, HandleUpdate));
  50. }
  51. void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData)
  52. {
  53. // Do nothing for now, could be extended to eg. animate the display
  54. }