Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoVerticalSpriteSheet.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoVerticalSpriteSheet
3 
4 Written by Michael C. Miller.
5 
6 I invest time and resources providing this open source code,
7 please support me by dontating (see https://github.com/Makuna)
8 
9 -------------------------------------------------------------------------
10 This file is part of the LUMITRONIX_iFlex_Workshop library.
11 
12 LumitronixIFlexBus is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as
14 published by the Free Software Foundation, either version 3 of
15 the License, or (at your option) any later version.
16 
17 LumitronixIFlexBus is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Lesser General Public License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public
23 License along with LumitronixIFlex. If not, see
24 <http://www.gnu.org/licenses/>.
25 -------------------------------------------------------------------------*/
26 #pragma once
27 
28 // T_BUFFER_METHOD - one of
29 // NeoBufferMethod
30 // NeoBufferProgmemMethod
31 //
32 template<typename T_BUFFER_METHOD> class NeoVerticalSpriteSheet
33 {
34 public:
35  NeoVerticalSpriteSheet(uint16_t width,
36  uint16_t height,
37  uint16_t spriteHeight,
38  PGM_VOID_P pixels) :
39  _method(width, height, pixels),
40  _spriteHeight(spriteHeight),
41  _spriteCount(height / spriteHeight)
42  {
43  }
44 
46  {
47  return _method;
48  }
49 
50  uint16_t SpriteWidth() const
51  {
52  return _method.Width();
53  };
54 
55  uint16_t SpriteHeight() const
56  {
57  return _spriteHeight;
58  };
59 
60  uint16_t SpriteCount() const
61  {
62  return _spriteCount;
63  }
64 
65  void SetPixelColor(uint16_t indexSprite,
66  int16_t x,
67  int16_t y,
68  typename T_BUFFER_METHOD::ColorObject color)
69  {
70  _method.SetPixelColor(pixelIndex(indexSprite, x, y), color);
71  };
72 
73  typename T_BUFFER_METHOD::ColorObject GetPixelColor(uint16_t indexSprite,
74  int16_t x,
75  int16_t y) const
76  {
77  return _method.GetPixelColor(pixelIndex(indexSprite, x, y));
78  };
79 
80  void ClearTo(typename T_BUFFER_METHOD::ColorObject color)
81  {
82  _method.ClearTo(color);
83  };
84 
86  uint16_t indexPixel,
87  uint16_t indexSprite)
88  {
89  uint16_t destPixelCount = destBuffer.PixelCount();
90  // validate indexPixel
91  if (indexPixel >= destPixelCount)
92  {
93  return;
94  }
95 
96  // validate indexSprite
97  if (indexSprite >= _spriteCount)
98  {
99  return;
100  }
101  // calc how many we can copy
102  uint16_t copyCount = destPixelCount - indexPixel;
103 
104  if (copyCount > SpriteWidth())
105  {
106  copyCount = SpriteWidth();
107  }
108 
109  uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexPixel);
110  const uint8_t* pSrc = T_BUFFER_METHOD::ColorFeature::getPixelAddress(_method.Pixels(), pixelIndex(indexSprite, 0, 0));
111  _method.CopyPixels(pDest, pSrc, copyCount);
112  }
113 
115  int16_t x,
116  int16_t y,
117  uint16_t indexSprite,
118  LayoutMapCallback layoutMap)
119  {
120  if (indexSprite >= _spriteCount)
121  {
122  return;
123  }
124  uint16_t destPixelCount = destBuffer.PixelCount();
125 
126  for (int16_t srcY = 0; srcY < SpriteHeight(); srcY++)
127  {
128  for (int16_t srcX = 0; srcX < SpriteWidth(); srcX++)
129  {
130  uint16_t indexDest = layoutMap(srcX + x, srcY + y);
131 
132  if (indexDest < destPixelCount)
133  {
134  const uint8_t* pSrc = T_BUFFER_METHOD::ColorFeature::getPixelAddress(_method.Pixels(), pixelIndex(indexSprite, srcX, srcY));
135  uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexDest);
136 
137  _method.CopyPixels(pDest, pSrc, 1);
138  }
139  }
140  }
141 
142  }
143 
144 private:
145  T_BUFFER_METHOD _method;
146 
147  const uint16_t _spriteHeight;
148  const uint16_t _spriteCount;
149 
150  uint16_t pixelIndex(uint16_t indexSprite,
151  int16_t x,
152  int16_t y) const
153  {
154  uint16_t result = PixelIndex_OutOfBounds;
155 
156  if (indexSprite < _spriteCount &&
157  x >= 0 &&
158  static_cast<uint16_t>(x) < SpriteWidth() &&
159  y >= 0 &&
160  static_cast<uint16_t>(y) < SpriteHeight())
161  {
162  result = x + y * SpriteWidth() + indexSprite * _spriteHeight * SpriteWidth();
163  }
164  return result;
165  }
166 };
std::function< uint16_t(int16_t x, int16_t y)> LayoutMapCallback
Definition: LayoutMapCallback.h:39
const uint16_t PixelIndex_OutOfBounds
Definition: LumitronixIFlexBus.h:33
#define PGM_VOID_P
Definition: NeoUtil.h:42
Definition: NeoVerticalSpriteSheet.h:33
void Blt(NeoBufferContext< typename T_BUFFER_METHOD::ColorFeature > destBuffer, uint16_t indexPixel, uint16_t indexSprite)
Definition: NeoVerticalSpriteSheet.h:85
uint16_t SpriteCount() const
Definition: NeoVerticalSpriteSheet.h:60
NeoVerticalSpriteSheet(uint16_t width, uint16_t height, uint16_t spriteHeight, PGM_VOID_P pixels)
Definition: NeoVerticalSpriteSheet.h:35
uint16_t SpriteHeight() const
Definition: NeoVerticalSpriteSheet.h:55
uint16_t SpriteWidth() const
Definition: NeoVerticalSpriteSheet.h:50
void ClearTo(typename T_BUFFER_METHOD::ColorObject color)
Definition: NeoVerticalSpriteSheet.h:80
void Blt(NeoBufferContext< typename T_BUFFER_METHOD::ColorFeature > destBuffer, int16_t x, int16_t y, uint16_t indexSprite, LayoutMapCallback layoutMap)
Definition: NeoVerticalSpriteSheet.h:114
void SetPixelColor(uint16_t indexSprite, int16_t x, int16_t y, typename T_BUFFER_METHOD::ColorObject color)
Definition: NeoVerticalSpriteSheet.h:65
T_BUFFER_METHOD::ColorObject GetPixelColor(uint16_t indexSprite, int16_t x, int16_t y) const
Definition: NeoVerticalSpriteSheet.h:73
Definition: NeoBufferContext.h:32
uint8_t * Pixels
Definition: NeoBufferContext.h:43
uint16_t PixelCount() const
Definition: NeoBufferContext.h:40