1 /++ 2 Sprite unit description module. 3 4 Macros: 5 LREF = <a href="#$1">$1</a> 6 HREF = <a href="$1">$2</a> 7 8 Authors: $(HREF https://github.com/TodNaz,TodNaz) 9 Copyright: Copyright (c) 2020 - 2021, TodNaz. 10 License: $(HREF https://github.com/TodNaz/Tida/blob/master/LICENSE,MIT) 11 +/ 12 module tida.sprite; 13 14 import tida.drawable; 15 16 /++ 17 A sprite is any object that can be drawn with the rendering parameters specified 18 by the programmer, such as position, size, rotation angle, etc. It does not have 19 to be a set of pictures, but some object, for example, that sets the renderer to 20 draw lines to represent an object. This object is `IDrawableEx`, where you can 21 describe the rendering parameters. 22 +/ 23 class Sprite : IDrawable 24 { 25 import tida.vector; 26 import tida.shader; 27 import tida.render; 28 import tida.color; 29 import tida.matrix; 30 31 public: 32 /++ 33 An object that represents a picture. 34 +/ 35 IDrawableEx draws; 36 37 /++ 38 Sprite position. 39 +/ 40 Vecf position = vecf(0, 0); 41 42 /++ 43 Sprite width. (If it is equal to zero, then the size is standard). 44 +/ 45 uint width = 0; 46 47 /++ 48 Sprite height. (If it is equal to zero, then the size is standard). 49 +/ 50 uint height = 0; 51 52 /++ 53 The rotation angle of the sprite. 54 +/ 55 float angle = 0.0f; 56 57 /++ 58 The center rotation angle of the sprite. 59 +/ 60 Vecf center = vecfNaN; 61 62 /++ 63 The transparency value of the sprite. 64 +/ 65 ubyte alpha = ubyte.max; 66 67 /++ 68 The shader used for rendering. 69 +/ 70 Shader!Program shader; 71 72 /++ 73 The color used for rendering. 74 +/ 75 Color!ubyte color = Color!ubyte(255, 255, 255, 255); 76 77 /++ 78 Sprite transformation matrix. 79 +/ 80 float[4][4] matrix = identity(); 81 82 /++ 83 A set of other sprites to help create a consistent look for the sprite. 84 +/ 85 Sprite[] skelet; 86 87 override: 88 void draw(IRenderer renderer, Vecf position) 89 { 90 mat4 mat = identity(); 91 92 if (draws !is null) 93 { 94 if (renderer.type == RenderType.opengl) 95 { 96 mat = mulmat(renderer.currentModelMatrix, matrix); 97 renderer.currentModelMatrix = mat; 98 99 if(renderer.currentShader is null) 100 renderer.currentShader = shader; 101 } 102 103 draws.drawEx( renderer, 104 this.position + position, 105 angle, 106 center, 107 !width || !height ? vecfNaN : vecf(width, height), 108 alpha, 109 color); 110 } 111 112 if (skelet.length != 0) 113 { 114 foreach (e; skelet) 115 { 116 if (renderer.type == RenderType.opengl) 117 renderer.currentModelMatrix = mat; 118 e.draw(renderer, position); 119 } 120 } 121 } 122 }