SSD1306 I2C Display Driver  1.1.0
This library is developed to control SSD1306 I2C 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_count( 0 )
27 {
28  m_rect = (SSD1306_RECT){ (uint8_t)0,
29  (uint8_t)0,
30  (uint8_t)((128>>3) - 1),
31  (uint8_t)((ssd1306_displayHeight()>>3) - 1) };
32 };
33 
34 void SpritePool::drawBlock(uint8_t blockColumn, uint8_t blockRow)
35 {
36  m_canvas.clear();
37 };
38 
40 {
41  for (uint8_t i = 0; i < m_count; i++)
42  {
43  SPRITE * sprite = m_sprites[i];
44  if ( sprite->isNearMove( ) )
45  {
46  updateRegion(sprite->getUpdateRect());
47  }
48  else
49  {
50  updateRegion(sprite->getRect());
51  updateRegion(sprite->getLRect());
52  }
53  sprite->lx = sprite->x;
54  sprite->ly = sprite->y;
55  }
56 }
57 
59 {
60  updateRegion( (SSD1306_RECT){ (uint8_t)(m_rect.left<<3),
61  (uint8_t)(m_rect.top<<3),
62  (uint8_t)(m_rect.right<<3),
63  (uint8_t)(m_rect.bottom<<3) } );
64 }
65 
66 uint8_t SpritePool::add( SPRITE &sprite )
67 {
68  uint8_t index = m_count;
69  if (index >= MAX_SPRITES)
70  {
72  }
73  m_sprites[index] = &sprite;
74  m_count++;
75  return index;
76 };
77 
79 {
80  m_count = 0;
81 };
82 
83 void SpritePool::remove( SPRITE &sprite )
84 {
85  updateRegion( sprite.getLRect() );
86  for (uint8_t i=0; i<m_count; i++)
87  {
88  if (m_sprites[i] == &sprite)
89  {
90  m_count--;
91  for (uint8_t j=i; j<m_count; j++)
92  {
93  m_sprites[j] = m_sprites[j+1];
94  }
95  break;
96  }
97  }
98 }
99 
100 
101 void SpritePool::updateRegion(SSD1306_RECT ur)
102 {
103  ur.left >>= 3;
104  ur.top >>= 3;
105  ur.right >>= 3;
106  ur.bottom >>= 3;
107  ur.left = max(ur.left, m_rect.left);
108  ur.top = max(ur.top, m_rect.top);
109  ur.right = min(ur.right, m_rect.right);
110  ur.bottom = min(ur.bottom, m_rect.bottom);
111  for(uint8_t x = ur.left; x <= ur.right; x++)
112  {
113  for(uint8_t y = ur.top; y <= ur.bottom; y++)
114  {
115  drawBlock(x,y);
116  for (uint8_t i = 0; i < m_count; i++)
117  {
118  m_canvas.drawSpritePgm(m_sprites[i]->x - (x << 3),
119  m_sprites[i]->y - (y << 3),
120  m_sprites[i]->data );
121  }
122  m_canvas.blt( x << 3, y );
123  }
124  }
125 }
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:155
void refreshScreen()
Definition: sprite_pool.cpp:58
NanoCanvas m_canvas
Canvas used to draw sprites to avoid flickering.
Definition: sprite_pool.h:80
uint8_t ssd1306_displayHeight()
Definition: ssd1306.cpp:33
SSD1306_RECT getLRect()
void drawSprites()
Definition: sprite_pool.cpp:39
void remove(SPRITE &sprite)
Definition: sprite_pool.cpp:83
SSD1306_RECT getRect()
uint8_t add(SPRITE &sprite)
Definition: sprite_pool.cpp:66
uint8_t bottom
bottom
SSD1306_RECT getUpdateRect()
uint8_t y
draw position Y on the screen
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:87
void blt(uint8_t x, uint8_t y)
Definition: nano_gfx.cpp:200
uint8_t lx
last draw position X on the screen
void clear()
Definition: sprite_pool.cpp:78
virtual void drawBlock(uint8_t blockColumn, uint8_t blockRow)
Definition: sprite_pool.cpp:34
uint8_t right
right
bool isNearMove()
uint8_t ly
last draw position Y on the screen
uint8_t left
left