Check-in [b9f5dfb290]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:automatic scaling
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b9f5dfb2906ffd5fdf446d5e152a3d9fb0a6109b
User & Date: athaudia 2017-07-25 16:34:44.286
Context
2017-07-25
17:18
deploy.bat check-in: 41c5a8babd user: athaudia tags: trunk
16:34
automatic scaling check-in: b9f5dfb290 user: athaudia tags: trunk
02:36
tileset check-in: 31a1d7564e user: athaudia tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to lib/playtypus.dart.
1

2
3
4
5
6
7
8
9
10
11

12

13



14
15
16
17

18



19


20
21












22
23
24
25
26
27
28
29
30

31
32
33
34




35


36
37
38
39
40
41
42
import 'dart:html';

import 'src/context.dart';
import 'src/loader.dart';

export 'src/context.dart';
export 'src/loader.dart';

abstract class Game {
	bool _preloaded = false;
	Loader _load = new Loader();
	Context _ctx;

	num time_since_last_tick = 0;

	num last_timestamp;




	Game(int width, int height) {
		onPreload(_load);
		var canvas = new CanvasElement(width: width, height: height);

		document.querySelector('body').children.add(canvas);



		_ctx = new Context(canvas.context2D);


		window.requestAnimationFrame(_step);
		}













	void _step(num timestamp) {
		if(!_preloaded && _load.loaded == 0) {
			_preloaded = true;
			last_timestamp = timestamp;
			onLoad();
		}
		if(_preloaded) {
			time_since_last_tick += timestamp - last_timestamp;

			last_timestamp = timestamp;
			const num frametime = 1000.0 / 60.0;
			int ticks = (time_since_last_tick / frametime - 0.5).floor();
			time_since_last_tick -= ticks * frametime;




			print(ticks);


			for(int i = 0; i < ticks; ++i)
				onTick();
			onRender(_ctx);
		}
		window.requestAnimationFrame(_step);
	}


>










>
|
>
|
>
>
>

|

|
>
|
>
>
>
|
>
>

|
>
>
>
>
>
>
>
>
>
>
>
>




|



|
>
|

|
|
>
>
>
>
|
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import 'dart:html';
import 'dart:math';
import 'src/context.dart';
import 'src/loader.dart';

export 'src/context.dart';
export 'src/loader.dart';

abstract class Game {
	bool _preloaded = false;
	Loader _load = new Loader();
	Context _ctx;
	CanvasElement _canvas;
	num _time_since_last_tick = 0;
	num _time_since_last_second = 0;
	num _last_timestamp;
	int _frames_since_last_second = 0;
	int fps = 0;
	int width, height;

	Game(this.width, this.height) {
		onPreload(_load);
		_canvas = new CanvasElement();
		_canvas.style.position = 'absolute';
		var body = document.querySelector('body');
		body.style.backgroundColor = '#000';
		body.children.add(_canvas);
		var canvas_context = _canvas.context2D;
		_ctx = new Context(canvas_context);
		_onResize();
		window.onResize.listen((E) => _onResize());
		window.requestAnimationFrame(_step);
	}

	void _onResize() {
		int scale = min(window.innerWidth~/width, window.innerHeight~/height);
		_canvas.width = width*scale;
		_canvas.height = height*scale;
		_canvas.context2D.scale(scale,scale);
		_canvas.context2D.imageSmoothingEnabled = false;
		var left = (window.innerWidth-_canvas.width) ~/ 2;
		var top = (window.innerHeight-_canvas.height) ~/ 2;
		_canvas.style.left = left.toString() + 'px';
		_canvas.style.top = top.toString() + 'px';
	}

	void _step(num timestamp) {
		if(!_preloaded && _load.loaded == 0) {
			_preloaded = true;
			_last_timestamp = timestamp;
			onLoad();
		}
		if(_preloaded) {
			_time_since_last_tick += timestamp - _last_timestamp;
			_time_since_last_second += timestamp - _last_timestamp;
			_last_timestamp = timestamp;
			const num frametime = 1000.0 / 60.0;
			int ticks = (_time_since_last_tick / frametime - 0.5).floor();
			_time_since_last_tick -= ticks * frametime;
			_frames_since_last_second += 1;
			while(_time_since_last_second >= 1000.0) {
				_time_since_last_second -= 1000.0;
				fps = _frames_since_last_second;
				print(fps);
				_frames_since_last_second = 0;
			}
			for(int i = 0; i < ticks; ++i)
				onTick();
			onRender(_ctx);
		}
		window.requestAnimationFrame(_step);
	}

Changes to lib/src/context.dart.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

	void rect(int x, int y, int w, int h) {
		_ctx.fillRect(x, y, w, h);
	}

	void drawTile(Tileset tileset, int tile, int x, int y) {
		int tcx = tileset.tileCountX();
		int tx = tile & tcx;
		int ty = (tile / tcx).floor();
		int posx = tx*(tileset._tileW+tileset._spacing);
		int posy = ty*(tileset._tileH+tileset._spacing);
		Rectangle src = new Rectangle(posx, posy, tileset._tileW, tileset._tileH);
		Rectangle dst = new Rectangle(x, y, tileset._tileW, tileset._tileH);
		_ctx.drawImageToRect(tileset._img, dst, sourceRect: src);
	}
}







|
|







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

	void rect(int x, int y, int w, int h) {
		_ctx.fillRect(x, y, w, h);
	}

	void drawTile(Tileset tileset, int tile, int x, int y) {
		int tcx = tileset.tileCountX();
		int tx = tile % tcx;
		int ty = tile ~/ tcx;
		int posx = tx*(tileset._tileW+tileset._spacing);
		int posy = ty*(tileset._tileH+tileset._spacing);
		Rectangle src = new Rectangle(posx, posy, tileset._tileW, tileset._tileH);
		Rectangle dst = new Rectangle(x, y, tileset._tileW, tileset._tileH);
		_ctx.drawImageToRect(tileset._img, dst, sourceRect: src);
	}
}
Changes to web/main.dart.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
23
24
25
26
27
import 'dart:html';
import 'package:playtypus/playtypus.dart';

class MyGame extends Game {
	ImageElement img;
	Tileset tileset;
	int tick = 0;
	MyGame() : super(1920, 1080) {}
	
	void onPreload(Loader load) {
		img = load.image("tileset.png");
		tileset = load.tileset("tileset.png", 16, 16, spacing: 1);
	}
	void onLoad() {}

	void onRender(Context ctx) {
		ctx.rect(0,0,tick,600);
		//ctx.draw(img, 0, 0);
		int tile = (tick / 1).floor();
		for(int x = 0; x < 1920/16; ++x)
			for(int y = 0; y < 1080/16; ++y)
				ctx.drawTile(tileset, tile+x+y, x*16, y*16);
	}
	void onTick() {
		tick += 1;
	}
		
}







|





|
>



|
|
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import 'dart:html';
import 'package:playtypus/playtypus.dart';

class MyGame extends Game {
	ImageElement img;
	Tileset tileset;
	int tick = 0;
	MyGame() : super(320,180) {}
	
	void onPreload(Loader load) {
		img = load.image("tileset.png");
		tileset = load.tileset("tileset.png", 16, 16, spacing: 1);
	}
	void onLoad() {
	}
	void onRender(Context ctx) {
		ctx.rect(0,0,tick,600);
		//ctx.draw(img, 0, 0);
		int tile = (tick ~/ 1);
		for(int x = 0; x < 1920/16/2; ++x)
			for(int y = 0; y < 1080/16/2; ++y)
				ctx.drawTile(tileset, tile+x+y, x*16, y*16);
	}
	void onTick() {
		tick += 1;
	}
		
}