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     Sprite transformation matrix.
74     +/
75     float[4][4] matrix = identity();
76 
77     /++
78     A set of other sprites to help create a consistent look for the sprite.
79     +/
80     Sprite[] skelet;
81 
82 override:
83     void draw(IRenderer renderer, Vecf position)
84     {
85         mat4 mat = identity();
86 
87         if (draws !is null)
88         {
89             if (renderer.type == RenderType.opengl)
90             {
91                 mat = mulmat(renderer.currentModelMatrix, matrix);
92                 renderer.currentModelMatrix = mat;
93 
94                 if(renderer.currentShader is null)
95                     renderer.currentShader = shader;
96             }
97 
98             draws.drawEx(   renderer,
99                             this.position + position,
100                             angle,
101                             center,
102                             !width || !height ? vecfNaN : vecf(width, height),
103                             alpha,
104                             rgb(255, 255, 255));
105         }
106 
107         if (skelet.length != 0)
108         {
109             foreach (e; skelet)
110             {
111                 if (renderer.type == RenderType.opengl)
112                     renderer.currentModelMatrix = mat;
113                 e.draw(renderer, position);
114             }
115         }
116     }
117 }