Check-in [edca182ac3]

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

Overview
Comment:context and loader and timing
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:edca182ac34459c18b2f57f2c8dfb6eea7c58ac2
User & Date: athaudia 2017-07-25 01:52:01
Context
2017-07-25
02:36
tileset check-in: 31a1d7564e user: athaudia tags: trunk
01:52
context and loader and timing check-in: edca182ac3 user: athaudia tags: trunk
2017-07-24
17:49
set up dart environment check-in: 2fb3372715 user: athaudia tags: trunk
Changes

Changes to lib/playtypus.dart.

1
2

3












































void say_hello() {
	print("hello world 3");

}












































|
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
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);
	}

	//To override:
	void onPreload(Loader load);
	void onLoad();
	void onRender(Context ctx);
	void onTick();
}

Added lib/src/context.dart.



























>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
import 'dart:html';

class Context {
	CanvasRenderingContext2D _ctx;
	Context(this._ctx);
	void draw(ImageElement img, int x, int y) {
		_ctx.drawImage(img, x, y);
	}

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

Added lib/src/loader.dart.























>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
import 'dart:html';

class Loader {
	int loaded = 0;
	ImageElement image(String url) {
		var img = new ImageElement(src: url);
		loaded -= 1;
		img.onLoad.listen((e) => loaded += 1);
		return img;
	}
}

Changes to web/index.html.

1
2
3
4
5
6

7

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="main.dart.js"></script>
</head>
<body>

</body>




<


>

>
1
2
3

4
5
6
7
8
<!DOCTYPE html>
<html>
<head>

</head>
<body>
<script type="text/javascript" src="main.dart.js"></script>
</body>
</html>

Changes to web/main.dart.


1
2



















3
4
5

6

import 'package:playtypus/playtypus.dart';




















void main() {
	say_hello();
	print('Hello, World!');

}
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

<
<
>

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
import 'dart:html';
import 'package:playtypus/playtypus.dart';

class MyGame extends Game {
	ImageElement img;
	int tick = 0;
	MyGame() : super(800, 600) {}
	
	void onPreload(Loader load) {
		img = load.image("tileset.png");
	}
	void onLoad() {}
	void onRender(Context ctx) {
		ctx.rect(0,0,tick,600);
		ctx.draw(img, 0, 0);
	}
	void onTick() {
		tick += 1;
	}
		
}

void main() {


	new MyGame();
}