1 module Engine.Camera; 2 3 import Engine.Component; 4 import Engine.Texture; 5 import Engine.Core; 6 import Engine.math; 7 import Engine.Input; 8 9 class Camera : Component { 10 // This will identify our vertex buffer 11 12 package mat4 projection; 13 14 float size = 1; 15 recti rect; 16 recti realRect; 17 vec2i center; 18 19 this() { 20 UpdateResolution(); 21 } 22 23 mat4 Projection() { 24 return projection; 25 } 26 27 //Mouse world position 28 vec2 MouseWorldPosition() { 29 auto v = MouseLocalPosition(); 30 return ScreenToWorld(v.x, v.y); 31 } 32 33 //Takes a point on the screen and turns it into point on world 34 vec2 ScreenToWorld(float x, float y) { 35 auto p = transform.position; 36 return vec2(p.x + x,p.y + y); 37 //return (vec4(x,y,0,1) * transform.Matrix()).xy; 38 } 39 40 //Mouse local position 41 vec2 MouseLocalPosition() { 42 auto p = Input.MousePosition(); 43 p *= size; 44 p.x += realRect.min.x; 45 p.y = realRect.max.y-p.y; 46 return p; 47 } 48 49 recti bounds() { 50 auto r = realRect; 51 r.add(vec2i(cast(int)transform.position.x,cast(int)transform.position.y)); 52 return r; 53 } 54 55 //Updates the Projection 56 void UpdateResolution() { 57 rect.min.x = -(Core.width) / 2; 58 rect.max.x = (Core.width) / 2; 59 rect.min.y = -(Core.height) / 2; 60 rect.max.y = (Core.height) / 2; 61 62 realRect.min.x = center.x - (center.x - rect.min.x); 63 realRect.max.x = center.x - (center.x - rect.max.x); 64 realRect.min.y = center.y - (center.y - rect.min.y); 65 realRect.max.y = center.y - (center.y - rect.max.y); 66 67 realRect.min.x = cast(int)(center.x - (center.x-rect.min.x)*size); 68 realRect.max.x = cast(int)(center.x - (center.x-rect.max.x)*size); 69 realRect.min.y = cast(int)(center.y - (center.y-rect.min.y)*size); 70 realRect.max.y = cast(int)(center.y - (center.y-rect.max.y)*size); 71 72 projection = mat4().orthographic(realRect.min.x, realRect.max.x, realRect.min.y, realRect.max.y, -100000, 100000); 73 } 74 75 mat4 View() { 76 return transform.InvertedMatrix(); 77 } 78 } 79