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 : Batchable {
15 	mixin ComponentBase;
16 
17 	package vec4 _color = vec4(1,1,1,1);
18 	package BatchData* batch;
19 	Material _material;
20 
21 	@property Material material() {
22 		return _material;
23 	};
24 
25 	@property int vertecies() {
26 		return 4;	
27 	};
28 	@property int indecies() {
29 		return 6;	
30 	};
31 		
32 	@property ref vec4 color() {
33 		if (batch !is null) {
34 			batch.MarkCheck(BatchData.Type.Color);
35 		}
36 		return _color;
37 	}
38 
39 	public void OnComponentAdd() {
40 		entity.sprite = this;
41 	}
42 
43 	this(Material material) {
44 		this._material = material;
45 	}
46 
47 	this(Texture texture) {
48 		this(texture.GetMaterial());
49 	}
50 
51 
52 	void OnBatchSetup(BatchData* data) {
53 		batch = data;
54 	}
55 
56 	void UpdateBatch(vec3[] vertex, vec2[] uv, vec4[] color, uint[] index, uint indexPosition)
57 	{
58 		if (vertex !is null) {
59 			vertex[0] = vec3(-0.5f, -0.5f, 0.0f);
60 			vertex[1] = vec3(0.5f,  -0.5f, 0.0f);
61 			vertex[2] = vec3(0.5f,  0.5f, 0.0f);
62 			vertex[3] = vec3(-0.5f,  0.5f, 0.0f);
63 		}
64 		if (color !is null) {
65 			color[0] = _color;
66 			color[1] = _color;
67 			color[2] = _color;
68 			color[3] = _color;
69 		}
70 		if (uv !is null) {
71 			uv[0] = vec2(0,0);
72 			uv[1] = vec2(1,0);
73 			uv[2] = vec2(1,1);
74 			uv[3] = vec2(0,1);
75 		}
76 		if (index !is null) {
77 			index[0] = indexPosition;
78 			index[1] = indexPosition+1;
79 			index[2] = indexPosition+2;
80 			index[3] = indexPosition;
81 			index[4] = indexPosition+2;
82 			index[5] = indexPosition+3;
83 		}
84 	}
85 }