MCPcopy Create free account
hub / github.com/OneLoneCoder/olcPixelGameEngine / DrawSprite

Method DrawSprite

extensions/olcPGEX_Graphics2D.h:134–179  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

132namespace olc
133{
134 void GFX2D::DrawSprite(olc::Sprite *sprite, olc::GFX2D::Transform2D &transform)
135 {
136 if (sprite == nullptr)
137 return;
138
139 // Work out bounding rectangle of sprite
140 float ex, ey;
141 float sx, sy;
142 float px, py;
143
144 transform.Forward(0.0f, 0.0f, sx, sy);
145 px = sx; py = sy;
146 sx = std::min(sx, px); sy = std::min(sy, py);
147 ex = std::max(ex, px); ey = std::max(ey, py);
148
149 transform.Forward((float)sprite->width, (float)sprite->height, px, py);
150 sx = std::min(sx, px); sy = std::min(sy, py);
151 ex = std::max(ex, px); ey = std::max(ey, py);
152
153 transform.Forward(0.0f, (float)sprite->height, px, py);
154 sx = std::min(sx, px); sy = std::min(sy, py);
155 ex = std::max(ex, px); ey = std::max(ey, py);
156
157 transform.Forward((float)sprite->width, 0.0f, px, py);
158 sx = std::min(sx, px); sy = std::min(sy, py);
159 ex = std::max(ex, px); ey = std::max(ey, py);
160
161 // Perform inversion of transform if required
162 transform.Invert();
163
164 if (ex < sx)
165 std::swap(ex, sx);
166 if (ey < sy)
167 std::swap(ey, sy);
168
169 // Iterate through render space, and sample Sprite from suitable texel location
170 for (float i = sx; i < ex; i++)
171 {
172 for (float j = sy; j < ey; j++)
173 {
174 float ox, oy;
175 transform.Backward(i, j, ox, oy);
176 pge->Draw((int32_t)i, (int32_t)j, sprite->GetPixel((int32_t)(ox+0.5f), (int32_t)(oy+0.5f)));
177 }
178 }
179 }
180
181 olc::GFX2D::Transform2D::Transform2D()
182 {

Callers 1

DrawMethod · 0.45

Calls 5

ForwardMethod · 0.80
InvertMethod · 0.80
BackwardMethod · 0.80
GetPixelMethod · 0.80
DrawMethod · 0.45

Tested by

no test coverage detected