Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/ts/BufferedRenderer.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 170 - (show annotations) (download) (as text)
Mon May 4 03:43:41 2015 UTC (9 years ago) by tsugehara
File MIME type: text/texmacs
File size: 4487 byte(s)


1 ///<reference path="all.ts"/>
2 module jg {
3 /**
4 * バッファ上に書き込む特殊なRenderer
5 */
6 export class BufferedRenderer extends Renderer {
7 /** バッファ */
8 buffer: HTMLCanvasElement;
9 /** 描画用context */
10 c: CanvasRenderingContext2D;
11 /** このバッファのサイズ */
12 size: CommonSize;
13
14 /**
15 * コンストラクタ
16 * @param size バッファサイズ
17 */
18 constructor(size:CommonSize) {
19 super();
20 this.size = size;
21 this.refresh();
22 }
23
24 /**
25 * バッファを透明でクリアする
26 */
27 clear() {
28 this.c.clearRect(0, 0, this.size.width, this.size.height);
29 }
30
31 /**
32 * バッファの描画内容を基に画像を生成する
33 * @param area コピー元バッファ領域。省略時は全画面
34 * @param distArea コピー先バッファ領域。省略時はx:0, y:0, width:area.width, height:area.height
35 * @param canavsSize 出力イメージサイズ。省略時はx: 0, y:0, width: area.width, height: area.height
36 */
37 createImage(area?:CommonArea, distArea?:CommonArea, canvasSize?:CommonSize):HTMLCanvasElement {
38 if (! area)
39 area = {x: 0, y: 0, width: this.size.width, height: this.size.height};
40 if (! distArea)
41 distArea = {x: 0, y: 0, width: area.width, height: area.height}
42 if (! canvasSize)
43 canvasSize = area;
44
45 var canvas:HTMLCanvasElement = window.createCanvas(canvasSize.width, canvasSize.height);
46
47 var context = <CanvasRenderingContext2D>canvas.getContext("2d");
48 context.drawImage(
49 this.buffer,
50 area.x,
51 area.y,
52 area.width,
53 area.height,
54 distArea.x,
55 distArea.y,
56 distArea.width,
57 distArea.height
58 );
59 if (this.filter)
60 this.applyFilter(context, distArea);
61 return canvas;
62 }
63
64 /**
65 * バッファの内容を基にSpriteを生成する
66 * @param area コピー元バッファ領域。省略時は全画面
67 * @param distArea コピー先バッファ領域。省略時はx:0, y:0, width:area.width, height:area.height
68 * @param canavsSize Spriteが保持する画像のサイズ。省略時はx: 0, y:0, width: area.width, height: area.height
69 */
70 createSprite(area?:CommonArea, distArea?:CommonArea, canvasSize?:CommonSize) {
71 if (! area)
72 area = {x: 0, y: 0, width: this.size.width, height: this.size.height};
73 if (! distArea)
74 distArea = {x: 0, y: 0, width: area.width, height: area.height}
75 return new Sprite(
76 this.createImage(area, distArea, canvasSize),
77 area.width,
78 area.height
79 );
80 }
81
82 /**
83 * フィルタを適用する
84 * @param c フィルタ適用対象context
85 * @param size フィルタを適用するサイズ
86 */
87 applyFilter(c:CanvasRenderingContext2D, size:CommonSize) {
88 var imageData = c.getImageData(0, 0, size.width, size.height);
89 this.filter.filter(imageData);
90 c.putImageData(imageData, 0, 0);
91 }
92
93 /**
94 * オブジェクトをこのバッファに描画する
95 * @param entity 描画対象オブジェクト
96 */
97 renderUnit(entity:E) {
98 var area:CommonArea = {x: 0, y: 0, width: entity.width, height: entity.height}
99 this.renderEntity(entity, this.c);
100 }
101
102 /**
103 * レイヤーをこのバッファに描画する
104 * @param layer 描画対象レイヤー
105 */
106 renderLayer(layer:Layer) {
107 this.renderParent(layer, this.c);
108 }
109
110 /**
111 * シーンをこのバッファに描画する
112 * @param scene 描画対象シーン
113 */
114 renderScene(scene:Scene) {
115 this.clear();
116 for (var i in scene.layers) {
117 this.c.save();
118 var layer = scene.layers[i];
119 if (layer.x || layer.y)
120 this.c.translate(layer.x, layer.y);
121 this.renderLayer(layer);
122 this.c.restore();
123 }
124 }
125
126 /**
127 * バッファの内容を再生成する。この際、描画済み情報は失われる。
128 * この処理はPCがスリープ状態から復帰した時などに呼び出される可能性があるため、BufferedRendererに描画済みの情報を長期間参照し続けることは推奨されない。。
129 */
130 refresh() {
131 delete this.buffer;
132 this.buffer = window.createCanvas(this.size.width, this.size.height);
133 this.c = <CanvasRenderingContext2D>this.buffer.getContext("2d");
134 }
135
136 /**
137 * このバッファを別の対象に描画する
138 * @param context 描画対象context
139 */
140 draw(context:CanvasRenderingContext2D) {
141 context.drawImage(
142 this.buffer,
143 0,
144 0,
145 this.size.width,
146 this.size.height,
147 0,
148 0,
149 this.size.width,
150 this.size.height
151 );
152 }
153 }
154 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26