1 module Engine.Sprite;
2 
3 import Engine.Component;
4 import derelict.opengl3.gl;
5 import Engine.Texture;
6 import Engine.Core;
7 import Engine.Buffer;
8 import Engine.Batch;
9 import Engine.Input;
10 import Engine.Material;
11 import Engine.Shader;
12 import std.stdio;
13 
14 class Sprite : Component, Batchable {
15 	package vec4 _color = vec4(1,1,1,1);
16 	package BatchData* batch;
17 	Material _material;
18 
19 	@property Material material() {
20 		return _material;
21 	};
22 
23 	@property int vertecies() {
24 		return 4;	
25 	};
26 	@property int indecies() {
27 		return 6;	
28 	};
29 		
30 	@property ref vec4 color() {
31 		if (batch !is null) {
32 			batch.MarkCheck(BatchData.Type.Color);
33 		}
34 		return _color;
35 	}
36 
37 	public override void OnComponentAdd() {
38 		entity.sprite = this;
39 	}
40 
41 	this(Material material) {
42 		this._material = material;
43 	}
44 
45 	this(Texture texture) {
46 		this(texture.GetMaterial());
47 	}
48 
49 	override void Awake() {
50 		Core.AddBatch(entity, this);
51 	}
52 
53 	void OnBatchSetup(BatchData* data) {
54 		batch = data;
55 	}
56 
57 	void UpdateBatch(vec3[] vertex, vec2[] uv, vec4[] color, uint[] index, uint indexPosition)
58 	{
59 		if (vertex !is null) {
60 			vertex[0] = vec3(-0.5f, -0.5f, 0.0f);
61 			vertex[1] = vec3(0.5f,  -0.5f, 0.0f);
62 			vertex[2] = vec3(0.5f,  0.5f, 0.0f);
63 			vertex[3] = vec3(-0.5f,  0.5f, 0.0f);
64 		}
65 		if (color !is null) {
66 			color[0] = _color;
67 			color[1] = _color;
68 			color[2] = _color;
69 			color[3] = _color;
70 		}
71 		if (uv !is null) {
72 			uv[0] = vec2(0,0);
73 			uv[1] = vec2(1,0);
74 			uv[2] = vec2(1,1);
75 			uv[3] = vec2(0,1);
76 		}
77 		if (index !is null) {
78 			index[0] = indexPosition;
79 			index[1] = indexPosition+1;
80 			index[2] = indexPosition+2;
81 			index[3] = indexPosition;
82 			index[4] = indexPosition+2;
83 			index[5] = indexPosition+3;
84 		}
85 	}
86 }