FabGL
ESP32 VGA Controller and Graphics Library
fabutils.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 
24 #pragma once
25 
26 
35 #include "freertos/FreeRTOS.h"
36 
37 
38 namespace fabgl {
39 
40 
41 
43 
44 
45 // Integer square root by Halleck's method, with Legalize's speedup
46 int isqrt (int x);
47 
48 
49 template <typename T>
50 const T & tmax(const T & a, const T & b)
51 {
52  return (a < b) ? b : a;
53 }
54 
55 
56 constexpr auto imax = tmax<int>;
57 
58 
59 template <typename T>
60 const T & tmin(const T & a, const T & b)
61 {
62  return !(b < a) ? a : b;
63 }
64 
65 
66 constexpr auto imin = tmin<int>;
67 
68 
69 
70 template <typename T>
71 const T & tclamp(const T & v, const T & lo, const T & hi)
72 {
73  return (v < lo ? lo : (v > hi ? hi : v));
74 }
75 
76 
77 constexpr auto iclamp = tclamp<int>;
78 
79 
80 template <typename T>
81 const T & twrap(const T & v, const T & lo, const T & hi)
82 {
83  return (v < lo ? hi : (v > hi ? lo : v));
84 }
85 
86 
87 template <typename T>
88 void tswap(T & v1, T & v2)
89 {
90  T t = v1;
91  v1 = v2;
92  v2 = t;
93 }
94 
95 
96 constexpr auto iswap = tswap<int>;
97 
98 
99 template <typename T>
100 T moveItems(T dest, T src, size_t n)
101 {
102  T pd = dest;
103  T ps = src;
104  if (pd != ps) {
105  if (ps < pd)
106  for (pd += n, ps += n; n--;)
107  *--pd = *--ps;
108  else
109  while (n--)
110  *pd++ = *ps++;
111  }
112  return dest;
113 }
114 
115 
116 
118 
119 
125 struct Point {
126  int16_t X;
127  int16_t Y;
129  Point() : X(0), Y(0) { }
130  Point(int X_, int Y_) : X(X_), Y(Y_) { }
131 
132  Point add(Point const & p) const { return Point(X + p.X, Y + p.Y); }
133  Point sub(Point const & p) const { return Point(X - p.X, Y - p.Y); }
134  Point neg() const { return Point(-X, -Y); }
135  bool operator==(Point const & r) { return X == r.X && Y == r.Y; }
136  bool operator!=(Point const & r) { return X != r.X || Y != r.Y; }
137 };
138 
139 
143 struct Size {
144  int16_t width;
145  int16_t height;
147  Size() : width(0), height(0) { }
148  Size(int width_, int height_) : width(width_), height(height_) { }
149 };
150 
151 
152 
158 struct Rect {
159  int16_t X1;
160  int16_t Y1;
161  int16_t X2;
162  int16_t Y2;
164  Rect() : X1(0), Y1(0), X2(0), Y2(0) { }
165  Rect(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) { }
166  Rect(Rect const & r) { X1 = r.X1; Y1 = r.Y1; X2 = r.X2; Y2 = r.Y2; }
167 
168  bool operator==(Rect const & r) { return X1 == r.X1 && Y1 == r.Y1 && X2 == r.X2 && Y2 == r.Y2; }
169  bool operator!=(Rect const & r) { return X1 != r.X1 || Y1 != r.Y1 || X2 != r.X2 || Y2 != r.Y2; }
170  Point pos() const { return Point(X1, Y1); }
171  Size size() const { return Size(X2 - X1 + 1, Y2 - Y1 + 1); }
172  int width() const { return X2 - X1 + 1; }
173  int height() const { return Y2 - Y1 + 1; }
174  Rect translate(int offsetX, int offsetY) const { return Rect(X1 + offsetX, Y1 + offsetY, X2 + offsetX, Y2 + offsetY); }
175  Rect translate(Point const & offset) const { return Rect(X1 + offset.X, Y1 + offset.Y, X2 + offset.X, Y2 + offset.Y); }
176  Rect move(Point const & position) const { return Rect(position.X, position.Y, position.X + width() - 1, position.Y + height() - 1); }
177  Rect move(int x, int y) const { return Rect(x, y, x + width() - 1, y + height() - 1); }
178  Rect shrink(int value) const { return Rect(X1 + value, Y1 + value, X2 - value, Y2 - value); }
179  Rect hShrink(int value) const { return Rect(X1 + value, Y1, X2 - value, Y2); }
180  Rect vShrink(int value) const { return Rect(X1, Y1 + value, X2, Y2 - value); }
181  Rect resize(int width, int height) const { return Rect(X1, Y1, X1 + width - 1, Y1 + height - 1); }
182  Rect resize(Size size) const { return Rect(X1, Y1, X1 + size.width - 1, Y1 + size.height - 1); }
183  Rect intersection(Rect const & rect) const { return Rect(tmax(X1, rect.X1), tmax(Y1, rect.Y1), tmin(X2, rect.X2), tmin(Y2, rect.Y2)); }
184  bool intersects(Rect const & rect) const { return X1 <= rect.X2 && X2 >= rect.X1 && Y1 <= rect.Y2 && Y2 >= rect.Y1; }
185  bool contains(Rect const & rect) const { return (rect.X1 >= X1) && (rect.Y1 >= Y1) && (rect.X2 <= X2) && (rect.Y2 <= Y2); }
186  bool contains(Point const & point) const { return point.X >= X1 && point.Y >= Y1 && point.X <= X2 && point.Y <= Y2; }
187  bool contains(int x, int y) const { return x >= X1 && y >= Y1 && x <= X2 && y <= Y2; }
188 };
189 
190 
191 
195 struct MouseButtons {
196  uint8_t left : 1;
197  uint8_t middle : 1;
198  uint8_t right : 1;
199 };
200 
201 
202 
206 struct MouseStatus {
207  int16_t X;
208  int16_t Y;
209  int8_t wheelDelta;
211 };
212 
213 
214 
216 // TimeOut
217 
218 
219 struct TimeOut {
220  TimeOut();
221 
222  // -1 means "infinite", never times out
223  bool expired(int valueMS);
224 
225 private:
226  int64_t m_start;
227 };
228 
229 
230 
232 // Stack
233 
234 
235 template <typename T>
236 struct StackItem {
237  StackItem * next;
238  T item;
239  StackItem(StackItem * next_, T const & item_) : next(next_), item(item_) { }
240 };
241 
242 template <typename T>
243 class Stack {
244 public:
245  Stack() : m_items(nullptr) { }
246  bool isEmpty() { return m_items == nullptr; }
247  void push(T const & value) {
248  m_items = new StackItem<T>(m_items, value);
249  }
250  T pop() {
251  if (m_items) {
252  StackItem<T> * iptr = m_items;
253  m_items = iptr->next;
254  T r = iptr->item;
255  delete iptr;
256  return r;
257  } else
258  return T();
259  }
260  int count() {
261  int r = 0;
262  for (auto i = m_items; i; i = i->next)
263  ++r;
264  return r;
265  }
266 private:
267  StackItem<T> * m_items;
268 };
269 
270 
271 
273 // Delegate
274 
275 template <typename ...Params>
276 struct Delegate {
277 
278  template <typename Func>
279  void operator=(Func f) {
280  m_closure = [] (void * func, const Params & ...params) -> void { (*(Func *)func)(params...); };
281  m_func = heap_caps_malloc(sizeof(Func), MALLOC_CAP_32BIT);
282  moveItems<uint32_t*>((uint32_t*)m_func, (uint32_t*)&f, sizeof(Func) / sizeof(uint32_t));
283  }
284 
285  ~Delegate() {
286  heap_caps_free(m_func);
287  }
288 
289  void operator()(const Params & ...params) {
290  if (m_func)
291  m_closure(m_func, params...);
292  }
293 
294 private:
295  void (*m_closure)(void * func, const Params & ...params);
296  void * m_func = nullptr;
297 };
298 
299 
300 
302 // StringList
303 
304 class StringList {
305 
306 public:
307  StringList();
308  ~StringList();
309  int append(char const * str);
310  void insert(int index, char const * str);
311  void set(int index, char const * str);
312  void remove(int index);
313  int count() { return m_count; }
314  char const * get(int index) { return m_items[index]; }
315  void clear();
316  void takeStrings();
317  void select(int index, bool value);
318  void deselectAll();
319  bool selected(int index);
320  void copyFrom(StringList const & src);
321 
322 private:
323  void checkAllocatedSpace(int requiredItems);
324 
325  char const * * m_items;
326 
327  // each 32 bit word can select up to 32 items, one bit per item
328  uint32_t * m_selMap;
329 
330  // If true (default is false) all strings added (append/insert/set) are copied.
331  // Strings will be freed when no more used (destructor, clear(), etc...).
332  // This flag is permanently switched to True by takeStrings() call.
333  bool m_ownStrings;
334 
335  uint16_t m_count; // actual items
336  uint16_t m_allocated; // allocated items
337 
338 };
339 
340 
341 
342 
344 
345 
346 
347 bool clipLine(int & x1, int & y1, int & x2, int & y2, Rect const & clipRect, bool checkOnly);
348 
349 
350 void removeRectangle(Stack<Rect> & rects, Rect const & mainRect, Rect const & rectToRemove);
351 
352 
353 bool calcParity(uint8_t v);
354 
355 // why these? this is like heap_caps_malloc with MALLOC_CAP_32BIT. Unfortunately
356 // heap_caps_malloc crashes, so we need this workaround.
357 void * realloc32(void * ptr, size_t size);
358 void free32(void * ptr);
359 
360 
362 
552  VK_LAST, // marks the last virtual key
553 };
554 
555 
556 } // end of namespace
557 
558 
559 
Definition: fabutils.h:456
Definition: fabutils.h:500
Definition: fabutils.h:436
Definition: fabutils.h:536
Definition: fabutils.h:513
int16_t width
Definition: fabutils.h:144
Definition: fabutils.h:512
Definition: fabutils.h:385
uint8_t middle
Definition: fabutils.h:197
Definition: fabutils.h:519
Definition: fabutils.h:465
Definition: fabutils.h:481
uint8_t left
Definition: fabutils.h:196
Definition: fabutils.h:527
Definition: fabutils.h:461
Definition: fabutils.h:440
Definition: fabutils.h:405
Definition: fabutils.h:434
Definition: fabutils.h:477
int16_t X
Definition: fabutils.h:126
Definition: fabutils.h:443
Definition: fabutils.h:387
Definition: fabutils.h:450
Definition: fabutils.h:510
Definition: fabutils.h:516
Definition: fabutils.h:532
Definition: fabutils.h:515
Definition: fabutils.h:415
Definition: fabutils.h:432
Definition: fabutils.h:542
Definition: fabutils.h:442
Definition: fabutils.h:370
Definition: fabutils.h:410
Definition: fabutils.h:444
Definition: fabutils.h:540
Definition: fabutils.h:549
Definition: fabutils.h:367
Definition: fabutils.h:474
Definition: fabutils.h:504
Definition: fabutils.h:492
Definition: fabutils.h:398
Definition: fabutils.h:476
Definition: fabutils.h:389
Definition: fabutils.h:384
Definition: fabutils.h:371
Definition: fabutils.h:464
Definition: fabutils.h:414
Definition: fabutils.h:547
Definition: fabutils.h:514
int16_t height
Definition: fabutils.h:145
MouseButtons buttons
Definition: fabutils.h:210
Definition: fabutils.h:541
Definition: fabutils.h:423
Definition: fabutils.h:412
Definition: fabutils.h:483
Definition: fabutils.h:454
Definition: fabutils.h:381
Definition: fabutils.h:482
Definition: fabutils.h:471
Definition: fabutils.h:520
Definition: fabutils.h:380
Definition: fabutils.h:525
Definition: fabutils.h:530
int16_t Y2
Definition: fabutils.h:162
Definition: fabutils.h:494
Definition: fabutils.h:396
Definition: fabutils.h:546
Definition: fabutils.h:517
Definition: fabutils.h:451
Definition: fabutils.h:501
Definition: fabutils.h:495
Definition: fabutils.h:425
Definition: fabutils.h:386
Definition: fabutils.h:446
Definition: fabutils.h:473
Definition: fabutils.h:475
Definition: fabutils.h:449
Definition: fabutils.h:457
Definition: fabutils.h:526
Definition: fabutils.h:486
Definition: fabutils.h:378
Definition: fabutils.h:497
Describes mouse absolute position, scroll wheel delta and buttons status.
Definition: fabutils.h:206
Definition: fabutils.h:424
Definition: fabutils.h:453
Definition: fabutils.h:416
Definition: fabutils.h:499
int16_t Y
Definition: fabutils.h:127
Definition: fabutils.h:394
Definition: fabutils.h:429
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: fabutils.h:366
Definition: fabutils.h:420
Definition: fabutils.h:409
Definition: fabutils.h:535
Definition: fabutils.h:505
Represents the coordinate of a point.
Definition: fabutils.h:125
Definition: fabutils.h:472
Definition: fabutils.h:550
Definition: canvas.cpp:47
Definition: fabutils.h:401
Definition: fabutils.h:534
Definition: fabutils.h:426
Definition: fabutils.h:470
Definition: fabutils.h:511
Definition: fabutils.h:406
Definition: fabutils.h:390
Definition: fabutils.h:391
Definition: fabutils.h:392
Definition: fabutils.h:404
Definition: fabutils.h:529
Definition: fabutils.h:377
Definition: fabutils.h:490
Represents a rectangle.
Definition: fabutils.h:158
Definition: fabutils.h:399
Describes mouse buttons status.
Definition: fabutils.h:195
Definition: fabutils.h:418
Definition: fabutils.h:417
Definition: fabutils.h:496
Definition: fabutils.h:506
Definition: fabutils.h:395
Definition: fabutils.h:388
Definition: fabutils.h:433
Definition: fabutils.h:373
Definition: fabutils.h:400
Definition: fabutils.h:374
Definition: fabutils.h:545
Definition: fabutils.h:431
Definition: fabutils.h:382
Definition: fabutils.h:468
Definition: fabutils.h:376
Definition: fabutils.h:548
Definition: fabutils.h:478
Definition: fabutils.h:403
Definition: fabutils.h:435
int16_t X
Definition: fabutils.h:207
Definition: fabutils.h:479
Definition: fabutils.h:448
Represents a bidimensional size.
Definition: fabutils.h:143
Definition: fabutils.h:407
Definition: fabutils.h:491
Definition: fabutils.h:375
Definition: fabutils.h:421
Definition: fabutils.h:503
Definition: fabutils.h:372
Definition: fabutils.h:466
Definition: fabutils.h:538
Definition: fabutils.h:524
Definition: fabutils.h:537
Definition: fabutils.h:422
Definition: fabutils.h:522
Definition: fabutils.h:508
Definition: fabutils.h:539
Definition: fabutils.h:487
Definition: fabutils.h:393
Definition: fabutils.h:485
Definition: fabutils.h:528
Definition: fabutils.h:458
uint8_t right
Definition: fabutils.h:198
Definition: fabutils.h:441
Definition: fabutils.h:437
int16_t Y1
Definition: fabutils.h:160
Definition: fabutils.h:509
Definition: fabutils.h:469
Definition: fabutils.h:531
Definition: fabutils.h:489
Definition: fabutils.h:413
Definition: fabutils.h:544
Definition: fabutils.h:383
Definition: fabutils.h:447
Definition: fabutils.h:411
Definition: fabutils.h:445
Definition: fabutils.h:484
Definition: fabutils.h:439
Definition: fabutils.h:488
Definition: fabutils.h:498
Definition: fabutils.h:379
Definition: fabutils.h:419
int16_t X1
Definition: fabutils.h:159
Definition: fabutils.h:521
Definition: fabutils.h:518
Definition: fabutils.h:459
Definition: fabutils.h:463
Definition: fabutils.h:430
int16_t Y
Definition: fabutils.h:208
Definition: fabutils.h:397
Definition: fabutils.h:402
Definition: fabutils.h:480
Definition: fabutils.h:533
Definition: fabutils.h:369
Definition: fabutils.h:467
Definition: fabutils.h:455
Definition: fabutils.h:452
Definition: fabutils.h:408
Definition: fabutils.h:507
Definition: fabutils.h:493
Definition: fabutils.h:462
Definition: fabutils.h:428
Definition: fabutils.h:523
int8_t wheelDelta
Definition: fabutils.h:209
Definition: fabutils.h:502
Definition: fabutils.h:438
int16_t X2
Definition: fabutils.h:161
Definition: fabutils.h:460
Definition: fabutils.h:543
Definition: fabutils.h:427