Check-in [66dd217f66]

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

Overview
Comment:autotiles
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:66dd217f660d1a2e6e2f91d29db23b67b825ba6e
User & Date: athaudia 2017-07-28 17:54:01
Context
2017-07-28
17:54
autotiles Leaf check-in: 66dd217f66 user: athaudia tags: trunk
2017-07-25
17:25
workaround in deploy.bat check-in: cd80e4b400 user: athaudia tags: trunk
Changes

Added doc/autotile.png.

cannot compute difference between binary files

Changes to lib/playtypus.dart.

33
34
35
36
37
38
39
40
41
42
43
44

45
46
47
48
49
50
51
	}

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







<




>







33
34
35
36
37
38
39

40
41
42
43
44
45
46
47
48
49
50
51
	}

	void _onResize() {
		int scale = min(window.innerWidth~/width, window.innerHeight~/height);
		_canvas.width = width*scale;
		_canvas.height = height*scale;
		_canvas.context2D.scale(scale,scale);

		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';
		_canvas.context2D.imageSmoothingEnabled = false;
	}

	void _step(num timestamp) {
		if(!_preloaded && _load.loaded == 0) {
			_preloaded = true;
			_last_timestamp = timestamp;
			onLoad();

Changes to lib/src/context.dart.

1
2





























3
4
5
6



7
8
9
10








11
12
13
14
15
16
17
..
22
23
24
25
26
27
28
29
30
31
32




































33
34
35
36
37
38
39
..
41
42
43
44
45
46
47
48




import 'dart:html';






























class Tileset {
	ImageElement _img;
	int _tileW, _tileH, _spacing;
	Tileset(this._img, this._tileW, this._tileH, this._spacing);



	int tileCountX() {
		return ((_img.width+_spacing) / (_tileW+_spacing)).floor();
	}
}









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

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




































}

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

	Tileset tileset(String url, int tileWidth, int tileHeight,
	                {int spacing = 0}) {
		return new Tileset(image(url), tileWidth, tileHeight, spacing);
	}
}






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



|
>
>
>




>
>
>
>
>
>
>
>







 







|
|


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







 







|
>
>
>
>
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
..
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
117
118
119
120
121
122
123
124
125
126
127
128
import 'dart:html';

// l + 2*ul + 4*u
// nah, l, ul, l+ul, u, u+l, u+ul, u+ul+l
//const int _autotile_a = [0, 12, 0, 12, 8, 4, 8, 16];
int _autotile_a = [
[0.0,0.0],[3.0,0.0],[0.0,0.0],[3.0,0.0],[2.0,0.0],[1.0,0.0],[2.0,0.0],[4.0,0.0]
];

// u + 2*ur + 4*r
// nah, u, ur, u+ur, r, u+r, ur+r, u+ur+r
//const int _autotile_b = [1, 9, 1, 9, 13, 5, 13, 17];
int _autotile_b = [
[0.5,0.0],[2.5,0.0],[0.5,0.0],[2.5,0.0],[3.5,0.0],[1.5,0.0],[3.5,0.0],[4.5,0.0]
];

// d + 2*dl + 4*l
// nah, d, dl, d+dl, l, d+l, dl+l, d+dl+l
//const int _autotile_c = [2, 10, 2, 10, 14, 6, 14, 18];
int _autotile_c = [
[0.0,0.5],[2.0,0.5],[0.0,0.5],[2.0,0.5],[3.0,0.5],[1.0,0.5],[3.0,0.5],[4.0,0.5]
];

// r + 2*dr + 4*d
// nah, r, dr, r+dr, d, r+d, dr+d, r+dr+d
//const int _autotile_d = [3, 15, 3, 15, 11, 7, 11, 19];
int _autotile_d = [
[0.5,0.5],[3.5,0.5],[0.5,0.5],[3.5,0.5],[2.5,0.5],[1.5,0.5],[2.5,0.5],[4.5,0.5]
];


class Tileset {
	ImageElement _img;
	int _tileW, _tileH, _spacing;
	Tileset(this._img, this._tileW, this._tileH, this._spacing) {
		
	}
	
	int tileCountX() {
		return ((_img.width+_spacing) / (_tileW+_spacing)).floor();
	}
}

class AutoTileset {
	ImageElement _img;
	int _tileW, _tileH;
	AutoTileset(this._img, this._tileW, this._tileH) {
		
	}
}

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

	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);
		var src = new Rectangle(posx, posy, tileset._tileW, tileset._tileH);
		var dst = new Rectangle(x, y, tileset._tileW, tileset._tileH);
		_ctx.drawImageToRect(tileset._img, dst, sourceRect: src);
	}

	void drawAutoTile(AutoTileset tileset, int tile, int x, int y,
		bool ul, bool u, bool ur, bool r, bool dr, bool d, bool dl, bool l)
	{
		var tile_a = _autotile_a[(l?1:0) + (ul?2:0) + (u?4:0)];
		var tile_b = _autotile_b[(u?1:0) + (ur?2:0) + (r?4:0)];
		var tile_c = _autotile_c[(d?1:0) + (dl?2:0) + (l?4:0)];
		var tile_d = _autotile_d[(r?1:0) + (dr?2:0) + (d?4:0)];
		int tw2 = tileset._tileW/2;
		int th2 = tileset._tileH/2;
		
		var srcA = new Rectangle(
			tile_a[0] * tileset._tileW, (tile_a[1]+tile) * tileset._tileW, 
			tileset._tileW/2, tileset._tileH/2);
		var dstA = new Rectangle(x,y,tileset._tileW/2,tileset._tileH/2);

		var srcB = new Rectangle(
			tile_b[0] * tileset._tileW, (tile_b[1]+tile) * tileset._tileW, 
			tileset._tileW/2, tileset._tileH/2);
		var dstB = new Rectangle(x+tw2,y,tileset._tileW/2,tileset._tileH/2);

		var srcC = new Rectangle(
			tile_c[0] * tileset._tileW, (tile_c[1]+tile) * tileset._tileW, 
			tileset._tileW/2, tileset._tileH/2);
		var dstC = new Rectangle(x,y+th2,tileset._tileW/2,tileset._tileH/2);

		var srcD = new Rectangle(
			tile_d[0] * tileset._tileW, (tile_d[1]+tile) * tileset._tileW, 
			tileset._tileW/2, tileset._tileH/2);
		var dstD = new Rectangle(x+tw2,y+th2,tileset._tileW/2,tileset._tileH/2);

		_ctx.drawImageToRect(tileset._img, dstA, sourceRect: srcA);
		_ctx.drawImageToRect(tileset._img, dstB, sourceRect: srcB);
		_ctx.drawImageToRect(tileset._img, dstC, sourceRect: srcC);
		_ctx.drawImageToRect(tileset._img, dstD, sourceRect: srcD);
	}
}

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

	Tileset tileset(String url, int tileWidth, int tileHeight,
	                {int spacing = 0}) {
		return new Tileset(image(url), tileWidth, tileHeight, spacing);
	}

	AutoTileset autoTileset(String url, int tileWidth, int tileHeight) {
		return new AutoTileset(image(url), tileWidth, tileHeight);
	}
}

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
28
29
30
31
32
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;
	}
		
}

void main() {
	new MyGame();
}






>






>

>


>

|

|
|
|
>
|










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

class MyGame extends Game {
	ImageElement img;
	Tileset tileset;
	AutoTileset auto;
	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);
		auto = load.autoTileset("tileset_a1.png", 16, 16);
	}
	
	void onLoad() {
	}
	
	void onRender(Context ctx) {
		ctx.rect(0,0,500,600);
		//ctx.draw(img, 0, 0);
		int tile = (tick ~/ 10);
		for(int x = 0; x < 20; ++x)
			for(int y = 0; y < 11; ++y)
				ctx.drawAutoTile(auto, 1, x*16, y*16, false, false, false, false, false, false, false, false);
				//ctx.drawTile(tileset, tile+x+y*20, x*16, y*16);
	}
	void onTick() {
		tick += 1;
	}
		
}

void main() {
	new MyGame();
}

Added web/tileset_a1.png.

cannot compute difference between binary files