screenshot.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. // formatImageNumber
  24. // Preceeds a number with zeros to make it 6 digits long.
  25. //---------------------------------------------------------------------------------------------
  26. function formatImageNumber(%number)
  27. {
  28. if(%number < 10)
  29. %number = "0" @ %number;
  30. if(%number < 100)
  31. %number = "0" @ %number;
  32. if(%number < 1000)
  33. %number = "0" @ %number;
  34. if(%number < 10000)
  35. %number = "0" @ %number;
  36. return %number;
  37. }
  38. //---------------------------------------------------------------------------------------------
  39. // formatSessionNumber
  40. // Preceeds a number with zeros to make it 4 digits long.
  41. //---------------------------------------------------------------------------------------------
  42. function formatSessionNumber(%number)
  43. {
  44. if(%number < 10)
  45. %number = "0" @ %number;
  46. if(%number < 100)
  47. %number = "0" @ %number;
  48. return %number;
  49. }
  50. //---------------------------------------------------------------------------------------------
  51. // recordMovie
  52. // Records a movie file from the Canvas content using the specified fps.
  53. // Possible encoder values are "PNG" and "THEORA" (default).
  54. //---------------------------------------------------------------------------------------------
  55. $RecordingMovie = false;
  56. function recordMovie(%movieName, %fps, %encoder)
  57. {
  58. // If the canvas doesn't exist yet, setup a flag so it'll
  59. // start capturing as soon as it's created
  60. if (!isObject(Canvas))
  61. return;
  62. if (%encoder $= "")
  63. %encoder = "THEORA";
  64. %resolution = Canvas.getVideoMode();
  65. // Start the movie recording
  66. ChatHud.AddLine( "\c4Recording movie file to [\c2" @ %movieName @ "\cr].ogv.");
  67. echo("Recording movie to: " @ %movieName);
  68. startVideoCapture(Canvas, %movieName, %encoder, %fps);
  69. $RecordingMovie = true;
  70. }
  71. function stopMovie()
  72. {
  73. // Stop the current recording
  74. ChatHud.AddLine( "\c4Recording movie file finished.");
  75. echo("Stopped movie recording");
  76. stopVideoCapture();
  77. $RecordingMovie = false;
  78. }
  79. /// This is bound in initializeCommon() to take
  80. /// a screenshot on a keypress.
  81. function doScreenShot( %val )
  82. {
  83. // This can be bound, so skip key up events.
  84. if ( %val == 0 )
  85. return;
  86. _screenShot( 1 );
  87. }
  88. /// A counter for screen shots used by _screenShot().
  89. $screenshotNumber = 0;
  90. /// Internal function which generates unique filename
  91. /// and triggers a screenshot capture.
  92. function _screenShot( %tiles, %overlap )
  93. {
  94. if ( $pref::Video::screenShotSession $= "" )
  95. $pref::Video::screenShotSession = 0;
  96. if ( $screenshotNumber == 0 )
  97. $pref::Video::screenShotSession++;
  98. if ( $pref::Video::screenShotSession > 999 )
  99. $pref::Video::screenShotSession = 1;
  100. %name = "screenshot_" @ formatSessionNumber($pref::Video::screenShotSession) @ "-" @
  101. formatImageNumber($screenshotNumber);
  102. %name = expandFileName( %name );
  103. $screenshotNumber++;
  104. if ( ( $pref::Video::screenShotFormat $= "JPEG" ) ||
  105. ( $pref::video::screenShotFormat $= "JPG" ) )
  106. screenShot( %name, "JPEG", %tiles, %overlap );
  107. else
  108. screenShot( %name, "PNG", %tiles, %overlap );
  109. }
  110. /// This will close the console and take a large format
  111. /// screenshot by tiling the current backbuffer and save
  112. /// it to the root game folder.
  113. ///
  114. /// For instance a tile setting of 4 with a window set to
  115. /// 800x600 will output a 3200x2400 screenshot.
  116. function tiledScreenShot( %tiles, %overlap )
  117. {
  118. // Pop the console off before we take the shot.
  119. Canvas.popDialog( ConsoleDlg );
  120. _screenShot( %tiles, %overlap );
  121. }