Advertisement
Guest User

Untitled

a guest
Oct 13th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. class Screen {
  2. CanvasElement _canvas;
  3. CanvasRenderingContext2D _context;
  4. List<Drawable> _objects = new List<Drawable>();
  5. int _timeoflastfpscount = 0, _frameCount = 0, _prevInfo = 0, _sizeX, _sizeY, previousTime = new Date.now().millisecondsSinceEpoch;
  6. bool _fullscreen = false, _schResize = false;
  7. Random rng = new Random();
  8. Screen.create (CanvasElement this._canvas, int this._sizeX, int this._sizeY, bool this._fullscreen) {
  9. if (_fullscreen) {
  10. _sizeX = window.innerWidth;
  11. _sizeY = window.innerHeight;
  12. window.on.resize.add((event) {
  13. _sizeX = window.innerWidth;
  14. _sizeY = window.innerHeight;
  15. _schResize = true;
  16. });
  17. }
  18. //Initialise the Canvas
  19. _canvas.height = _sizeY;
  20. _canvas.width = _sizeX;
  21. _context = _canvas.context2d;
  22. }
  23. int getAppWidth () {
  24. return _sizeX;
  25. }
  26. int getAppHeight () {
  27. return _sizeY;
  28. }
  29. void updateFPS (int time, CanvasRenderingContext2D ctx) {
  30. _frameCount++;
  31. int sinceLast = (time - _timeoflastfpscount);
  32. if (sinceLast >= 1000) {
  33. _prevInfo = ((_frameCount / sinceLast) * 1000).round().toInt();
  34. _frameCount = 0;
  35. _timeoflastfpscount = time;
  36. }
  37. ctx.font = "12px 'Open Sans'";
  38. ctx.fillStyle = "red";
  39. ctx.fillText("${_prevInfo.toString()} FPS - Frames drawn since last: ${_frameCount}", 0, 16, this._sizeX);
  40. }
  41.  
  42. void clear(CanvasRenderingContext2D ctx) {
  43. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  44. }
  45.  
  46. void spawn (Drawable d) {
  47. _objects.addLast(d);
  48. }
  49. void updateObjs (List<Drawable> obj, double secondsSinceLastFrame) {
  50. if (!obj.isEmpty()) {
  51. int i = 0;
  52. obj.forEach((Drawable d) {
  53. if (d.needsUpdate) { d.update(secondsSinceLastFrame); }
  54. if (d.markDead) {
  55. obj.removeAt(i);
  56. i--;
  57. }
  58. i++;
  59. });
  60. }
  61. }
  62. void draw (List<Drawable> obj, CanvasRenderingContext2D ctx) {
  63. if (!obj.isEmpty()) {
  64. obj.forEach((Drawable d) {
  65. d.draw(ctx);
  66. });
  67. }
  68. }
  69. void update (int time) {
  70. if (time == null) {
  71. time = new Date.now().millisecondsSinceEpoch;
  72. }
  73. //Draw the frame
  74. if (!_schResize) clear(_context);
  75. else {
  76. _canvas.height = _sizeY;
  77. _canvas.width = _sizeX;
  78. }
  79. updateObjs(_objects, ((time - previousTime).toDouble() / 1000));
  80. draw(_objects, _context);
  81. //Show our FPS on the canvas passing along the time at the begining
  82. updateFPS(time, _context);
  83. //Request the next frame!
  84. previousTime = time;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement