SSD1306 OLED display driver  1.4.6
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
sprite_pool.cpp
1 /*
2  MIT License
3 
4  Copyright (c) 2017-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 */
24 
25 #include "sprite_pool.h"
26 #include "ssd1306.h"
27 
28 
30  : m_canvas( 8, 8, m_canvasBuf)
31  , m_canvasBuf{0}
32  , m_sprites{0}
33  , m_count( 0 )
34 {
35  m_rect.left = 0;
36  m_rect.top = 0;
37  m_rect.right = (ssd1306_displayWidth() >> 3) - 1;
38  m_rect.bottom = (ssd1306_displayHeight() >> 3) - 1;
39 };
40 
41 void SpritePool::drawBlock(uint8_t blockColumn, uint8_t blockRow)
42 {
43  m_canvas.clear();
44 };
45 
47 {
48  for (uint8_t i = 0; i < m_count; i++)
49  {
50  SPRITE * sprite = m_sprites[i];
51  if ( sprite->isNearMove( ) )
52  {
53  updateRegion(sprite->getUpdateRect());
54  }
55  else
56  {
57  updateRegion(sprite->getRect());
58  updateRegion(sprite->getLRect());
59  }
60  sprite->lx = sprite->x;
61  sprite->ly = sprite->y;
62  }
63 }
64 
66 {
67  updateRegion( (SSD1306_RECT){ (uint8_t)(m_rect.left<<3),
68  (uint8_t)(m_rect.top<<3),
69  (uint8_t)(m_rect.right<<3),
70  (uint8_t)(m_rect.bottom<<3) } );
71 }
72 
73 uint8_t SpritePool::add( SPRITE &sprite )
74 {
75  uint8_t index = m_count;
76  if (index >= MAX_SPRITES)
77  {
79  }
80  m_sprites[index] = &sprite;
81  m_count++;
82  return index;
83 };
84 
86 {
87  m_count = 0;
88 };
89 
90 void SpritePool::remove( SPRITE &sprite )
91 {
92  updateRegion( sprite.getLRect() );
93  for (uint8_t i=0; i<m_count; i++)
94  {
95  if (m_sprites[i] == &sprite)
96  {
97  m_count--;
98  for (uint8_t j=i; j<m_count; j++)
99  {
100  m_sprites[j] = m_sprites[j+1];
101  }
102  break;
103  }
104  }
105 }
106 
107 
108 void SpritePool::updateRegion(SSD1306_RECT ur)
109 {
110  ur.left >>= 3;
111  ur.top >>= 3;
112  ur.right >>= 3;
113  ur.bottom >>= 3;
114  ur.left = max(ur.left, m_rect.left);
115  ur.top = max(ur.top, m_rect.top);
116  ur.right = min(ur.right, m_rect.right);
117  ur.bottom = min(ur.bottom, m_rect.bottom);
118  for(uint8_t x = ur.left; x <= ur.right; x++)
119  {
120  for(uint8_t y = ur.top; y <= ur.bottom; y++)
121  {
122  drawBlock(x,y);
123  for (uint8_t i = 0; i < m_count; i++)
124  {
125  m_canvas.drawSpritePgm(m_sprites[i]->x - (x << 3),
126  m_sprites[i]->y - (y << 3),
127  m_sprites[i]->data );
128  }
129  m_canvas.blt( x << 3, y );
130  }
131  }
132 }
static const uint8_t SP_ERR_NO_SPACE
No free space for new sprite error.
Definition: sprite_pool.h:44
uint8_t top
top
void clear()
Definition: nano_gfx.cpp:111
void drawSpritePgm(uint8_t x, uint8_t y, const uint8_t sprite[])
Definition: nano_gfx.cpp:408
void refreshScreen()
Definition: sprite_pool.cpp:65
NanoCanvas m_canvas
Canvas used to draw sprites to avoid flickering.
Definition: sprite_pool.h:94
uint8_t ssd1306_displayHeight()
Definition: ssd1306.c:45
static const uint8_t MAX_SPRITES
Defines max sprites number supported by SpritePool.
Definition: sprite_pool.h:51
void drawSprites()
Definition: sprite_pool.cpp:46
void remove(SPRITE &sprite)
Definition: sprite_pool.cpp:90
uint8_t add(SPRITE &sprite)
Definition: sprite_pool.cpp:73
uint8_t bottom
bottom
#define max(a, b)
uint8_t y
draw position Y on the screen
#define min(a, b)
SSD1306_RECT getUpdateRect() const
uint8_t x
draw position X on the screen
SSD1306_RECT m_rect
Rectangle, which specifies part of the display, used by the sprites.
Definition: sprite_pool.h:101
uint8_t ssd1306_displayWidth()
Definition: ssd1306.c:50
void blt(uint8_t x, uint8_t y)
Definition: nano_gfx.cpp:453
SSD1306_RECT getRect() const
uint8_t lx
last draw position X on the screen
void clear()
Definition: sprite_pool.cpp:85
virtual void drawBlock(uint8_t blockColumn, uint8_t blockRow)
Definition: sprite_pool.cpp:41
bool isNearMove() const
uint8_t right
right
uint8_t ly
last draw position Y on the screen
SSD1306_RECT getLRect() const
uint8_t left
left