/src/net/flashpunk/Screen.as

https://bitbucket.org/DDRKirbyISQ/minimalistmayhem
ActionScript | 239 lines | 144 code | 25 blank | 70 comment | 19 complexity | b0322b60ae149675d6ebcf743ac38045 MD5 | raw file
  1. package net.flashpunk
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.PixelSnapping;
  6. import flash.display.Sprite;
  7. import flash.geom.Matrix;
  8. import net.flashpunk.graphics.Image;
  9. /**
  10. * Container for the main screen buffer. Can be used to transform the screen.
  11. */
  12. public class Screen
  13. {
  14. /**
  15. * Constructor.
  16. */
  17. public function Screen()
  18. {
  19. FP.engine.addChild(_sprite);
  20. resize();
  21. update();
  22. }
  23. /**
  24. * Initialise buffers to current screen size.
  25. */
  26. public function resize():void
  27. {
  28. if (_bitmap[0]) {
  29. _sprite.removeChild(_bitmap[0]);
  30. _sprite.removeChild(_bitmap[1]);
  31. _bitmap[0].bitmapData.dispose();
  32. _bitmap[1].bitmapData.dispose();
  33. }
  34. // create screen buffers
  35. _bitmap[0] = new Bitmap(new BitmapData(FP.width, FP.height, false, _color), PixelSnapping.NEVER);
  36. _bitmap[1] = new Bitmap(new BitmapData(FP.width, FP.height, false, _color), PixelSnapping.NEVER);
  37. _sprite.addChild(_bitmap[0]).visible = true;
  38. _sprite.addChild(_bitmap[1]).visible = false;
  39. FP.buffer = _bitmap[0].bitmapData;
  40. _width = FP.width;
  41. _height = FP.height;
  42. _current = 0;
  43. }
  44. /**
  45. * Swaps screen buffers.
  46. */
  47. public function swap():void
  48. {
  49. _current = 1 - _current;
  50. FP.buffer = _bitmap[_current].bitmapData;
  51. }
  52. /**
  53. * Refreshes the screen.
  54. */
  55. public function refresh():void
  56. {
  57. // refreshes the screen
  58. FP.buffer.fillRect(FP.bounds, _color);
  59. }
  60. /**
  61. * Redraws the screen.
  62. */
  63. public function redraw():void
  64. {
  65. // refresh the buffers
  66. _bitmap[_current].visible = true;
  67. _bitmap[1 - _current].visible = false;
  68. }
  69. /** @private Re-applies transformation matrix. */
  70. public function update():void
  71. {
  72. _matrix.b = _matrix.c = 0;
  73. _matrix.a = _scaleX * _scale;
  74. _matrix.d = _scaleY * _scale;
  75. _matrix.tx = -_originX * _matrix.a;
  76. _matrix.ty = -_originY * _matrix.d;
  77. if (_angle != 0) _matrix.rotate(_angle);
  78. _matrix.tx += _originX * _scaleX * _scale + _x;
  79. _matrix.ty += _originY * _scaleX * _scale + _y;
  80. _sprite.transform.matrix = _matrix;
  81. }
  82. /**
  83. * Refresh color of the screen.
  84. */
  85. public function get color():uint { return _color; }
  86. public function set color(value:uint):void { _color = 0xFF000000 | value; }
  87. /**
  88. * X offset of the screen.
  89. */
  90. public function get x():int { return _x; }
  91. public function set x(value:int):void
  92. {
  93. if (_x == value) return;
  94. _x = value;
  95. update();
  96. }
  97. /**
  98. * Y offset of the screen.
  99. */
  100. public function get y():int { return _y; }
  101. public function set y(value:int):void
  102. {
  103. if (_y == value) return;
  104. _y = value;
  105. update();
  106. }
  107. /**
  108. * X origin of transformations.
  109. */
  110. public function get originX():int { return _originX; }
  111. public function set originX(value:int):void
  112. {
  113. if (_originX == value) return;
  114. _originX = value;
  115. update();
  116. }
  117. /**
  118. * Y origin of transformations.
  119. */
  120. public function get originY():int { return _originY; }
  121. public function set originY(value:int):void
  122. {
  123. if (_originY == value) return;
  124. _originY = value;
  125. update();
  126. }
  127. /**
  128. * X scale of the screen.
  129. */
  130. public function get scaleX():Number { return _scaleX; }
  131. public function set scaleX(value:Number):void
  132. {
  133. if (_scaleX == value) return;
  134. _scaleX = value;
  135. update();
  136. }
  137. /**
  138. * Y scale of the screen.
  139. */
  140. public function get scaleY():Number { return _scaleY; }
  141. public function set scaleY(value:Number):void
  142. {
  143. if (_scaleY == value) return;
  144. _scaleY = value;
  145. update();
  146. }
  147. /**
  148. * Scale factor of the screen. Final scale is scaleX * scale by scaleY * scale, so
  149. * you can use this factor to scale the screen both horizontally and vertically.
  150. */
  151. public function get scale():Number { return _scale; }
  152. public function set scale(value:Number):void
  153. {
  154. if (_scale == value) return;
  155. _scale = value;
  156. update();
  157. }
  158. /**
  159. * Rotation of the screen, in degrees.
  160. */
  161. public function get angle():Number { return _angle * FP.DEG; }
  162. public function set angle(value:Number):void
  163. {
  164. if (_angle == value * FP.RAD) return;
  165. _angle = value * FP.RAD;
  166. update();
  167. }
  168. /**
  169. * Whether screen smoothing should be used or not.
  170. */
  171. public function get smoothing():Boolean { return _bitmap[0].smoothing; }
  172. public function set smoothing(value:Boolean):void { _bitmap[0].smoothing = _bitmap[1].smoothing = value; }
  173. /**
  174. * Width of the screen.
  175. */
  176. public function get width():uint { return _width; }
  177. /**
  178. * Height of the screen.
  179. */
  180. public function get height():uint { return _height; }
  181. /**
  182. * X position of the mouse on the screen.
  183. */
  184. public function get mouseX():int { return _sprite.mouseX; }
  185. /**
  186. * Y position of the mouse on the screen.
  187. */
  188. public function get mouseY():int { return _sprite.mouseY; }
  189. /**
  190. * Captures the current screen as an Image object.
  191. * @return A new Image object.
  192. */
  193. public function capture():Image
  194. {
  195. return new Image(_bitmap[_current].bitmapData.clone());
  196. }
  197. // Screen information.
  198. /** @private */ private var _sprite:Sprite = new Sprite;
  199. /** @private */ private var _bitmap:Vector.<Bitmap> = new Vector.<Bitmap>(2);
  200. /** @private */ private var _current:int = 0;
  201. /** @private */ private var _matrix:Matrix = new Matrix;
  202. /** @private */ private var _x:int;
  203. /** @private */ private var _y:int;
  204. /** @private */ private var _width:uint;
  205. /** @private */ private var _height:uint;
  206. /** @private */ private var _originX:int;
  207. /** @private */ private var _originY:int;
  208. /** @private */ private var _scaleX:Number = 1;
  209. /** @private */ private var _scaleY:Number = 1;
  210. /** @private */ private var _scale:Number = 1;
  211. /** @private */ private var _angle:Number = 0;
  212. /** @private */ private var _color:uint = 0xFF202020;
  213. }
  214. }