SSD1306 OLED display driver  1.8.2
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
sprite.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2018, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
28 #ifndef _NANO_SPRITE_H_
29 #define _NANO_SPRITE_H_
30 
31 #include "point.h"
32 #include "rect.h"
33 #include "ssd1306_hal/io.h"
34 
45 template<typename T, T &E>
47 {
48 public:
56  NanoSprite(const NanoPoint &pos, const NanoPoint &size, const uint8_t *bitmap)
57  : m_rect{pos, pos + size}
58  , m_bitmap( bitmap )
59  {
60  }
61 
65  void draw()
66  {
67  E.canvas.drawBitmap1(m_rect.p1.x, m_rect.p1.y, m_rect.width(), m_rect.height(), m_bitmap);
68  }
69 
73  void refresh()
74  {
75  E.refreshWorld( m_rect );
76  }
77 
81  void moveTo(const NanoPoint &p)
82  {
83  refresh();
84  m_rect = { p, p + m_rect.size() };
85  refresh();
86  }
87 
91  void moveBy(const NanoPoint &p)
92  {
93  refresh();
94  m_rect += p;
95  refresh();
96  }
97 
101  const NanoPoint bottom() const
102  {
103  return { (m_rect.p1.x + m_rect.p2.x) >> 1, m_rect.p2.y };
104  }
105 
109  const NanoPoint top() const
110  {
111  return { (m_rect.p1.x + m_rect.p2.x) >> 1, m_rect.p1.y };
112  }
113 
117  const NanoPoint left() const
118  {
119  return { m_rect.p1.x, (m_rect.p1.y + m_rect.p2.y) >> 1 };
120  }
121 
125  const NanoPoint right() const
126  {
127  return { m_rect.p2.x, (m_rect.p1.y + m_rect.p2.y) >> 1 };
128  }
129 
133  const NanoPoint center() const
134  {
135  return { (m_rect.p1.x + m_rect.p2.x) >> 1, (m_rect.p1.y + m_rect.p2.y) >> 1 };
136  }
137 
141  void setBitmap( const uint8_t * bitmap )
142  {
143  m_bitmap = bitmap;
144  }
145 
149  lcdint_t x( ) const { return m_rect.p1.x; }
150 
154  lcdint_t y( ) const { return m_rect.p1.y; }
155 
156 private:
157  NanoRect m_rect;
158  const uint8_t *m_bitmap;
159 };
160 
165 template<typename T, T &E>
167 {
168 public:
176  NanoFixedSprite(const NanoPoint &pos, const NanoPoint &size, const uint8_t *bitmap)
177  : m_pos(pos)
178  , m_size(size)
179  , m_bitmap( bitmap )
180  {
181  }
182 
186  void draw()
187  {
188  E.canvas.drawBitmap1(m_pos.x, m_pos.y, m_size.x, m_size.y, m_bitmap);
189  }
190 
194  void refresh()
195  {
196  E.refreshWorld( m_pos.x, m_pos.y,
197  m_pos.x + m_size.x - 1, m_pos.y + m_size.y - 1 );
198  }
199 
203  void moveTo(const NanoPoint &p)
204  {
205  refresh();
206  m_pos = p;
207  refresh();
208  }
209 
213  void moveBy(const NanoPoint &p)
214  {
215  refresh();
216  m_pos += p;
217  refresh();
218  }
219 
223  const NanoPoint bottom() const
224  {
225  return { m_pos.x + (m_size.x >> 1), m_pos.y + m_size.y - 1 };
226  }
227 
231  const NanoPoint top() const
232  {
233  return { m_pos.x + (m_size.x >> 1), m_pos.y };
234  }
235 
239  const NanoPoint left() const
240  {
241  return { m_pos.x, m_pos.y + (m_size.y>>1) };
242  }
243 
247  const NanoPoint right() const
248  {
249  return { m_pos.x + m_size.x - 1, m_pos.y + (m_size.y>>1) };
250  }
251 
255  const NanoPoint center() const
256  {
257  return { m_pos.x + (m_size.x >> 1), m_pos.y + (m_size.y>>1) };
258  }
259 
263  void setBitmap( const uint8_t * bitmap )
264  {
265  m_bitmap = bitmap;
266  }
267 
271  const NanoPoint & getPosition() const { return m_pos; }
272 
276  lcdint_t x( ) const { return m_pos.x; }
277 
281  lcdint_t y( ) const { return m_pos.y; }
282 
286  const NanoPoint & pos() const { return m_pos; }
287 
288 protected:
293 
294 private:
295  const uint8_t *m_bitmap;
296 };
297 
302 #endif
303 
void setBitmap(const uint8_t *bitmap)
Definition: sprite.h:263
const NanoPoint & getPosition() const
Definition: sprite.h:271
const NanoPoint top() const
Definition: sprite.h:109
void moveTo(const NanoPoint &p)
Definition: sprite.h:81
void setBitmap(const uint8_t *bitmap)
Definition: sprite.h:141
Definition: rect.h:42
lcdint_t height() const
Definition: rect.h:63
const NanoPoint center() const
Definition: sprite.h:133
NanoPoint p2
Definition: rect.h:48
lcdint_t y
Definition: point.h:45
lcdint_t x() const
Definition: sprite.h:149
const NanoPoint bottom() const
Definition: sprite.h:101
lcdint_t x() const
Definition: sprite.h:276
void refresh()
Definition: sprite.h:194
const NanoPoint right() const
Definition: sprite.h:247
NanoPoint m_pos
Definition: sprite.h:292
const NanoPoint top() const
Definition: sprite.h:231
void moveBy(const NanoPoint &p)
Definition: sprite.h:91
lcdint_t y() const
Definition: sprite.h:154
void draw()
Definition: sprite.h:186
const NanoPoint right() const
Definition: sprite.h:125
NanoSprite(const NanoPoint &pos, const NanoPoint &size, const uint8_t *bitmap)
Definition: sprite.h:56
void moveBy(const NanoPoint &p)
Definition: sprite.h:213
void moveTo(const NanoPoint &p)
Definition: sprite.h:203
lcdint_t width() const
Definition: rect.h:51
lcdint_t y() const
Definition: sprite.h:281
const NanoPoint center() const
Definition: sprite.h:255
const NanoPoint size() const
Definition: rect.h:57
void refresh()
Definition: sprite.h:73
const NanoPoint bottom() const
Definition: sprite.h:223
NanoPoint p1
Definition: rect.h:45
NanoFixedSprite(const NanoPoint &pos, const NanoPoint &size, const uint8_t *bitmap)
Definition: sprite.h:176
lcdint_t x
Definition: point.h:43
const NanoPoint left() const
Definition: sprite.h:239
const NanoPoint left() const
Definition: sprite.h:117
const NanoPoint & pos() const
Definition: sprite.h:286
const NanoPoint m_size
Definition: sprite.h:290
void draw()
Definition: sprite.h:65