1 /*
2   Copyright 2019-2020 David Robillard <d@drobilla.net>
3 
4   Permission to use, copy, modify, and/or distribute this software for any
5   purpose with or without fee is hereby granted, provided that the above
6   copyright notice and this permission notice appear in all copies.
7 
8   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 
17 #ifndef EXAMPLES_RECTS_H
18 #define EXAMPLES_RECTS_H
19 
20 #include <math.h>
21 #include <stddef.h>
22 
23 typedef float vec2[2];
24 
25 typedef struct {
26   float pos[2];
27   float size[2];
28   float fillColor[4];
29 } Rect;
30 
31 static const vec2 rectVertices[] = {
32   {0.0f, 0.0f}, // TL
33   {1.0f, 0.0f}, // TR
34   {0.0f, 1.0f}, // BL
35   {1.0f, 1.0f}  // BR
36 };
37 
38 static const unsigned rectIndices[4] = {0, 1, 2, 3};
39 
40 /// Make a new rectangle with the given index (each is slightly different)
41 static inline Rect
makeRect(const size_t index,const float frameWidth)42 makeRect(const size_t index, const float frameWidth)
43 {
44   static const float alpha   = 0.3f;
45   const float        minSize = frameWidth / 64.0f;
46   const float        maxSize = frameWidth / 6.0f;
47   const float        s       = (sinf((float)index) / 2.0f + 0.5f);
48   const float        c       = (cosf((float)index) / 2.0f + 0.5f);
49 
50   const Rect rect = {
51     {0.0f, 0.0f}, // Position is set later during expose
52     {minSize + s * maxSize, minSize + c * maxSize},
53     {0.0f, s / 2.0f + 0.25f, c / 2.0f + 0.25f, alpha},
54   };
55 
56   return rect;
57 }
58 
59 /// Move `rect` with the given index around in an arbitrary way that looks cool
60 static inline void
moveRect(Rect * const rect,const size_t index,const size_t numRects,const float frameWidth,const float frameHeight,const double time)61 moveRect(Rect* const  rect,
62          const size_t index,
63          const size_t numRects,
64          const float  frameWidth,
65          const float  frameHeight,
66          const double time)
67 {
68   const float normal    = (float)index / (float)numRects;
69   const float offset[2] = {normal * 128.0f, normal * 128.0f};
70 
71   rect->pos[0] = (frameWidth - rect->size[0] + offset[0]) *
72                  (sinf((float)time * rect->size[0] / 64.0f + normal) + 1.0f) /
73                  2.0f;
74   rect->pos[1] = (frameHeight - rect->size[1] + offset[1]) *
75                  (cosf((float)time * rect->size[1] / 64.0f + normal) + 1.0f) /
76                  2.0f;
77 }
78 
79 #endif // EXAMPLES_RECTS_H
80