SSD1306 OLED display driver  1.3.5
This library is developed to control SSD1306 i2c/spi OLED display
sprite_pool.cpp
1 /*
2  Copyright (C) 2017 Alexey Dynda
3 
4  This file is part of SSD1306 library.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "sprite_pool.h"
21 #include "ssd1306.h"
22 
23 
25  : m_canvas( 8, 8, m_canvasBuf)
26  , m_canvasBuf{0}
27  , m_sprites{0}
28  , m_count( 0 )
29 {
30  m_rect.left = 0;
31  m_rect.top = 0;
32  m_rect.right = (ssd1306_displayWidth() >> 3) - 1;
33  m_rect.bottom = (ssd1306_displayHeight() >> 3) - 1;
34 };
35 
36 void SpritePool::drawBlock(uint8_t blockColumn, uint8_t blockRow)
37 {
38  m_canvas.clear();
39 };
40 
42 {
43  for (uint8_t i = 0; i < m_count; i++)
44  {
45  SPRITE * sprite = m_sprites[i];
46  if ( sprite->isNearMove( ) )
47  {
48  updateRegion(sprite->getUpdateRect());
49  }
50  else
51  {
52  updateRegion(sprite->getRect());
53  updateRegion(sprite->getLRect());
54  }
55  sprite->lx = sprite->x;
56  sprite->ly = sprite->y;
57  }
58 }
59 
61 {
62  updateRegion( (SSD1306_RECT){ (uint8_t)(m_rect.left<<3),
63  (uint8_t)(m_rect.top<<3),
64  (uint8_t)(m_rect.right<<3),
65  (uint8_t)(m_rect.bottom<<3) } );
66 }
67 
68 uint8_t SpritePool::add( SPRITE &sprite )
69 {
70  uint8_t index = m_count;
71  if (index >= MAX_SPRITES)
72  {
74  }
75  m_sprites[index] = &sprite;
76  m_count++;
77  return index;
78 };
79 
81 {
82  m_count = 0;
83 };
84 
85 void SpritePool::remove( SPRITE &sprite )
86 {
87  updateRegion( sprite.getLRect() );
88  for (uint8_t i=0; i<m_count; i++)
89  {
90  if (m_sprites[i] == &sprite)
91  {
92  m_count--;
93  for (uint8_t j=i; j<m_count; j++)
94  {
95  m_sprites[j] = m_sprites[j+1];
96  }
97  break;
98  }
99  }
100 }
101 
102 
103 void SpritePool::updateRegion(SSD1306_RECT ur)
104 {
105  ur.left >>= 3;
106  ur.top >>= 3;
107  ur.right >>= 3;
108  ur.bottom >>= 3;
109  ur.left = max(ur.left, m_rect.left);
110  ur.top = max(ur.top, m_rect.top);
111  ur.right = min(ur.right, m_rect.right);
112  ur.bottom = min(ur.bottom, m_rect.bottom);
113  for(uint8_t x = ur.left; x <= ur.right; x++)
114  {
115  for(uint8_t y = ur.top; y <= ur.bottom; y++)
116  {
117  drawBlock(x,y);
118  for (uint8_t i = 0; i < m_count; i++)
119  {
120  m_canvas.drawSpritePgm(m_sprites[i]->x - (x << 3),
121  m_sprites[i]->y - (y << 3),
122  m_sprites[i]->data );
123  }
124  m_canvas.blt( x << 3, y );
125  }
126  }
127 }
static const uint8_t SP_ERR_NO_SPACE
No free space for new sprite error.
Definition: sprite_pool.h:39
uint8_t top
top
void clear()
Definition: nano_gfx.cpp:105
void drawSpritePgm(uint8_t x, uint8_t y, const uint8_t sprite[])
Definition: nano_gfx.cpp:240
void refreshScreen()
Definition: sprite_pool.cpp:60
NanoCanvas m_canvas
Canvas used to draw sprites to avoid flickering.
Definition: sprite_pool.h:83
uint8_t ssd1306_displayHeight()
Definition: ssd1306.cpp:36
static const uint8_t MAX_SPRITES
Defines max sprites number supported by SpritePool.
Definition: sprite_pool.h:42
void drawSprites()
Definition: sprite_pool.cpp:41
void remove(SPRITE &sprite)
Definition: sprite_pool.cpp:85
uint8_t add(SPRITE &sprite)
Definition: sprite_pool.cpp:68
uint8_t bottom
bottom
uint8_t y
draw position Y on the screen
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:90
uint8_t ssd1306_displayWidth()
Definition: ssd1306.cpp:41
void blt(uint8_t x, uint8_t y)
Definition: nano_gfx.cpp:285
SSD1306_RECT getRect() const
uint8_t lx
last draw position X on the screen
void clear()
Definition: sprite_pool.cpp:80
virtual void drawBlock(uint8_t blockColumn, uint8_t blockRow)
Definition: sprite_pool.cpp:36
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