Sample.as 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Common sample initialization as a framework for all samples.
  2. // - Create Urho3D logo at screen;
  3. // - Set custom window title and icon;
  4. // - Create Console and Debug HUD, and use F1 and F2 key to toggle them;
  5. // - Toggle rendering options from the keys 1-8;
  6. // - Take screenshots with key 9;
  7. // - Handle Esc key down to hide Console or exit application;
  8. Sprite@ logoSprite;
  9. void SampleStart()
  10. {
  11. // Create logo
  12. CreateLogo();
  13. // Set custom window Title & Icon
  14. SetWindowTitleAndIcon();
  15. // Create console and debug HUD
  16. CreateConsoleAndDebugHud();
  17. // Subscribe key down event
  18. SubscribeToEvent("KeyDown", "HandleKeyDown");
  19. }
  20. void SetLogoVisible(bool enable)
  21. {
  22. if (logoSprite !is null)
  23. logoSprite.visible = enable;
  24. }
  25. void CreateLogo()
  26. {
  27. // Get logo texture
  28. Texture2D@ logoTexture = cache.GetResource("Texture2D", "Textures/LogoLarge.png");
  29. if (logoTexture is null)
  30. return;
  31. // Create logo sprite and add to the UI layout
  32. logoSprite = ui.root.CreateChild("Sprite");
  33. // Set logo sprite texture
  34. logoSprite.texture = logoTexture;
  35. int textureWidth = logoTexture.width;
  36. int textureHeight = logoTexture.height;
  37. // Set logo sprite scale
  38. logoSprite.SetScale(256.0f / textureWidth);
  39. // Set logo sprite size
  40. logoSprite.SetSize(textureWidth, textureHeight);
  41. // Set logo sprite hot spot
  42. logoSprite.SetHotSpot(0, textureHeight);
  43. // Set logo sprite alignment
  44. logoSprite.SetAlignment(HA_LEFT, VA_BOTTOM);
  45. // Make logo not fully opaque to show the scene underneath
  46. logoSprite.opacity = 0.75f;
  47. // Set a low priority for the logo so that other UI elements can be drawn on top
  48. logoSprite.priority = -100;
  49. }
  50. void SetWindowTitleAndIcon()
  51. {
  52. Image@ icon = cache.GetResource("Image", "Textures/UrhoIcon.png");
  53. graphics.windowIcon = icon;
  54. graphics.windowTitle = "Urho3D Sample";
  55. }
  56. void CreateConsoleAndDebugHud()
  57. {
  58. // Get default style
  59. XMLFile@ xmlFile = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  60. if (xmlFile is null)
  61. return;
  62. // Create console
  63. Console@ console = engine.CreateConsole();
  64. console.defaultStyle = xmlFile;
  65. // Create debug HUD
  66. DebugHud@ debugHud = engine.CreateDebugHud();
  67. debugHud.defaultStyle = xmlFile;
  68. }
  69. void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  70. {
  71. int key = eventData["Key"].GetInt();
  72. // Close console (if open) or exit when ESC is pressed
  73. if (key == KEY_ESC)
  74. {
  75. if (!console.visible)
  76. engine.Exit();
  77. else
  78. console.visible = false;
  79. }
  80. // Toggle console with F1
  81. else if (key == KEY_F1)
  82. console.Toggle();
  83. // Toggle debug HUD with F2
  84. else if (key == KEY_F2)
  85. debugHud.ToggleAll();
  86. // Common rendering quality controls, only when UI has no focused element
  87. if (ui.focusElement is null)
  88. {
  89. // Texture quality
  90. if (key == '1')
  91. {
  92. int quality = renderer.textureQuality;
  93. ++quality;
  94. if (quality > QUALITY_HIGH)
  95. quality = QUALITY_LOW;
  96. renderer.textureQuality = quality;
  97. }
  98. // Material quality
  99. else if (key == '2')
  100. {
  101. int quality = renderer.materialQuality;
  102. ++quality;
  103. if (quality > QUALITY_HIGH)
  104. quality = QUALITY_LOW;
  105. renderer.materialQuality = quality;
  106. }
  107. // Specular lighting
  108. else if (key == '3')
  109. renderer.specularLighting = !renderer.specularLighting;
  110. // Shadow rendering
  111. else if (key == '4')
  112. renderer.drawShadows = !renderer.drawShadows;
  113. // Shadow map resolution
  114. else if (key == '5')
  115. {
  116. int shadowMapSize = renderer.shadowMapSize;
  117. shadowMapSize *= 2;
  118. if (shadowMapSize > 2048)
  119. shadowMapSize = 512;
  120. renderer.shadowMapSize = shadowMapSize;
  121. }
  122. // Shadow depth and filtering quality
  123. else if (key == '6')
  124. {
  125. int quality = renderer.shadowQuality;
  126. ++quality;
  127. if (quality > SHADOWQUALITY_HIGH_24BIT)
  128. quality = SHADOWQUALITY_LOW_16BIT;
  129. renderer.shadowQuality = quality;
  130. }
  131. // Occlusion culling
  132. else if (key == '7')
  133. {
  134. bool occlusion = renderer.maxOccluderTriangles > 0;
  135. occlusion = !occlusion;
  136. renderer.maxOccluderTriangles = occlusion ? 5000 : 0;
  137. }
  138. // Instancing
  139. else if (key == '8')
  140. renderer.dynamicInstancing = !renderer.dynamicInstancing;
  141. // Take screenshot
  142. else if (key == '9')
  143. {
  144. Image@ screenshot = Image();
  145. graphics.TakeScreenShot(screenshot);
  146. // Here we save in the Data folder with date and time appended
  147. screenshot.SavePNG(fileSystem.programDir + "Data/Screenshot_" +
  148. time.timeStamp.Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
  149. }
  150. }
  151. }