canvas.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //------------------------------------------------------------------------------
  23. // initializeCanvas
  24. // Constructs and initializes the default canvas window.
  25. //------------------------------------------------------------------------------
  26. $canvasCreated = false;
  27. function initializeCanvas(%windowName)
  28. {
  29. // Don't duplicate the canvas.
  30. if($canvasCreated)
  31. {
  32. error("Cannot instantiate more than one canvas!");
  33. return;
  34. }
  35. videoSetGammaCorrection($pref::OpenGL::gammaCorrection);
  36. if ( !createCanvas(%windowName) )
  37. {
  38. error("Canvas creation failed. Shutting down.");
  39. quit();
  40. }
  41. $pref::iOS::ScreenDepth = 32;
  42. if ( $pref::iOS::DeviceType !$= "" )
  43. {
  44. %resolution = iOSResolutionFromSetting($pref::iOS::DeviceType, $pref::iOS::ScreenOrientation);
  45. }
  46. else if ($platform $= "Android")
  47. {
  48. %resolution = GetAndroidResolution();
  49. }
  50. else
  51. {
  52. if ( $pref::Video::windowedRes !$= "" )
  53. %resolution = $pref::Video::windowedRes;
  54. else
  55. %resolution = $pref::Video::defaultResolution;
  56. }
  57. if ($platform $= "windows" || $platform $= "macos")
  58. {
  59. setScreenMode( %resolution._0, %resolution._1, %resolution._2, $pref::Video::fullScreen );
  60. }
  61. else
  62. {
  63. setScreenMode( %resolution._0, %resolution._1, %resolution._2, false );
  64. }
  65. $canvasCreated = true;
  66. }
  67. //------------------------------------------------------------------------------
  68. // resetCanvas
  69. // Forces the canvas to redraw itself.
  70. //------------------------------------------------------------------------------
  71. function resetCanvas()
  72. {
  73. if (isObject(Canvas))
  74. Canvas.repaint();
  75. }
  76. //------------------------------------------------------------------------------
  77. // iOSResolutionFromSetting
  78. // Helper function that grabs resolution strings based on device type
  79. //------------------------------------------------------------------------------
  80. function iOSResolutionFromSetting( %deviceType, %deviceScreenOrientation )
  81. {
  82. // A helper function to get a string based resolution from the settings given.
  83. %x = 0;
  84. %y = 0;
  85. %scaleFactor = $pref::iOS::RetinaEnabled ? 2 : 1;
  86. switch(%deviceType)
  87. {
  88. case $iOS::constant::iPhone:
  89. if(%deviceScreenOrientation == $iOS::constant::Landscape)
  90. {
  91. %x = $iOS::constant::iPhoneWidth * %scaleFactor;
  92. %y = $iOS::constant::iPhoneHeight * %scaleFactor;
  93. }
  94. else
  95. {
  96. %x = $iOS::constant::iPhoneHeight * %scaleFactor;
  97. %y = $iOS::constant::iPhoneWidth * %scaleFactor;
  98. }
  99. case $iOS::constant::iPad:
  100. if(%deviceScreenOrientation == $iOS::constant::Landscape)
  101. {
  102. %x = $iOS::constant::iPadWidth * %scaleFactor;
  103. %y = $iOS::constant::iPadHeight * %scaleFactor;
  104. }
  105. else
  106. {
  107. %x = $iOS::constant::iPadHeight * %scaleFactor;
  108. %y = $iOS::constant::iPadWidth * %scaleFactor;
  109. }
  110. case $iOS::constant::iPhone5:
  111. if(%deviceScreenOrientation == $iOS::constant::Landscape)
  112. {
  113. %x = $iOS::constant::iPhone5Width;
  114. %y = $iOS::constant::iPhone5Height;
  115. }
  116. else
  117. {
  118. %x = $iOS::constant::iPhone5Height;
  119. %y = $iOS::constant::iPhone5Width;
  120. }
  121. }
  122. return %x @ " " @ %y;
  123. }