Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Screen {
- CanvasElement _canvas;
- CanvasRenderingContext2D _context;
- List<Drawable> _objects = new List<Drawable>();
- int _timeoflastfpscount = 0, _frameCount = 0, _prevInfo = 0, _sizeX, _sizeY, previousTime = new Date.now().millisecondsSinceEpoch;
- bool _fullscreen = false, _schResize = false;
- Random rng = new Random();
- Screen.create (CanvasElement this._canvas, int this._sizeX, int this._sizeY, bool this._fullscreen) {
- if (_fullscreen) {
- _sizeX = window.innerWidth;
- _sizeY = window.innerHeight;
- window.on.resize.add((event) {
- _sizeX = window.innerWidth;
- _sizeY = window.innerHeight;
- _schResize = true;
- });
- }
- //Initialise the Canvas
- _canvas.height = _sizeY;
- _canvas.width = _sizeX;
- _context = _canvas.context2d;
- }
- int getAppWidth () {
- return _sizeX;
- }
- int getAppHeight () {
- return _sizeY;
- }
- void updateFPS (int time, CanvasRenderingContext2D ctx) {
- _frameCount++;
- int sinceLast = (time - _timeoflastfpscount);
- if (sinceLast >= 1000) {
- _prevInfo = ((_frameCount / sinceLast) * 1000).round().toInt();
- _frameCount = 0;
- _timeoflastfpscount = time;
- }
- ctx.font = "12px 'Open Sans'";
- ctx.fillStyle = "red";
- ctx.fillText("${_prevInfo.toString()} FPS - Frames drawn since last: ${_frameCount}", 0, 16, this._sizeX);
- }
- void clear(CanvasRenderingContext2D ctx) {
- ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
- }
- void spawn (Drawable d) {
- _objects.addLast(d);
- }
- void updateObjs (List<Drawable> obj, double secondsSinceLastFrame) {
- if (!obj.isEmpty()) {
- int i = 0;
- obj.forEach((Drawable d) {
- if (d.needsUpdate) { d.update(secondsSinceLastFrame); }
- if (d.markDead) {
- obj.removeAt(i);
- i--;
- }
- i++;
- });
- }
- }
- void draw (List<Drawable> obj, CanvasRenderingContext2D ctx) {
- if (!obj.isEmpty()) {
- obj.forEach((Drawable d) {
- d.draw(ctx);
- });
- }
- }
- void update (int time) {
- if (time == null) {
- time = new Date.now().millisecondsSinceEpoch;
- }
- //Draw the frame
- if (!_schResize) clear(_context);
- else {
- _canvas.height = _sizeY;
- _canvas.width = _sizeX;
- }
- updateObjs(_objects, ((time - previousTime).toDouble() / 1000));
- draw(_objects, _context);
- //Show our FPS on the canvas passing along the time at the begining
- updateFPS(time, _context);
- //Request the next frame!
- previousTime = time;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement