HelloWorld.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Copyright (c) 2008-2013 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 "Context.h"
  23. #include "Engine.h"
  24. #include "CoreEvents.h"
  25. #include "Font.h"
  26. #include "Input.h"
  27. #include "ProcessUtils.h"
  28. #include "Text.h"
  29. #include "UI.h"
  30. #include "HelloWorld.h"
  31. // Expands to this example's entry-point
  32. DEFINE_APPLICATION_MAIN(HelloWorld)
  33. HelloWorld::HelloWorld(Context* context) :
  34. Application(context),
  35. cache_(GetSubsystem<ResourceCache>())
  36. {
  37. // Modify engine startup parameters
  38. engineParameters_["WindowTitle"] = GetTypeName();
  39. engineParameters_["LogName"] = GetTypeName() + ".log";
  40. engineParameters_["FullScreen"] = false;
  41. }
  42. int HelloWorld::Start()
  43. {
  44. // Create "Hello World" Text
  45. CreateText();
  46. // Finally, hook-up this HelloWorld instance to handle update events
  47. SubscribeToEvents();
  48. // Go on to the main loop
  49. return EXIT_SUCCESS;
  50. }
  51. void HelloWorld::CreateText()
  52. {
  53. // Construct new Text object
  54. SharedPtr<Text> helloText(new Text(context_));
  55. // Set String to display
  56. helloText->SetText("Hello World from Urho3D!");
  57. // Set font and text color
  58. helloText->SetFont(cache_->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  59. helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
  60. // Align Text center-screen
  61. helloText->SetHorizontalAlignment(HA_CENTER);
  62. helloText->SetVerticalAlignment(VA_CENTER);
  63. // Add Text instance to the UI root element
  64. GetSubsystem<UI>()->GetRoot()->AddChild(helloText);
  65. }
  66. void HelloWorld::SubscribeToEvents()
  67. {
  68. // Subscribes HandleUpdate() method for processing update events
  69. SubscribeToEvent(E_UPDATE, HANDLER(HelloWorld, HandleUpdate));
  70. }
  71. void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData)
  72. {
  73. // Determine whether the escape key was pressed...
  74. if (GetSubsystem<Input>()->GetKeyPress(KEY_ESC))
  75. {
  76. // ...if so, request Engine exit
  77. engine_->Exit();
  78. }
  79. }