MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / PadSingleSprite

Function PadSingleSprite

src/spritecache.cpp:272–319  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

270}
271
272static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
273{
274 uint width = sprite->width + pad_left + pad_right;
275 uint height = sprite->height + pad_top + pad_bottom;
276
277 if (width > UINT16_MAX || height > UINT16_MAX) return false;
278
279 /* Copy source data and reallocate sprite memory. */
280 size_t sprite_size = static_cast<size_t>(sprite->width) * sprite->height;
281 std::vector<SpriteLoader::CommonPixel> src_data(sprite->data, sprite->data + sprite_size);
282 sprite->AllocateData(zoom, static_cast<size_t>(width) * height);
283
284 /* Copy with padding to destination. */
285 SpriteLoader::CommonPixel *src = src_data.data();
286 SpriteLoader::CommonPixel *data = sprite->data;
287 for (uint y = 0; y < height; y++) {
288 if (y < pad_top || pad_bottom + y >= height) {
289 /* Top/bottom padding. */
290 std::fill_n(data, width, SpriteLoader::CommonPixel{});
291 data += width;
292 } else {
293 if (pad_left > 0) {
294 /* Pad left. */
295 std::fill_n(data, pad_left, SpriteLoader::CommonPixel{});
296 data += pad_left;
297 }
298
299 /* Copy pixels. */
300 std::copy_n(src, sprite->width, data);
301 src += sprite->width;
302 data += sprite->width;
303
304 if (pad_right > 0) {
305 /* Pad right. */
306 std::fill_n(data, pad_right, SpriteLoader::CommonPixel{});
307 data += pad_right;
308 }
309 }
310 }
311
312 /* Update sprite size. */
313 sprite->width = width;
314 sprite->height = height;
315 sprite->x_offs -= pad_left;
316 sprite->y_offs -= pad_top;
317
318 return true;
319}
320
321static bool PadSprites(SpriteLoader::SpriteCollection &sprite, ZoomLevels sprite_avail, SpriteEncoder *encoder)
322{

Callers 1

PadSpritesFunction · 0.85

Calls 3

fill_nFunction · 0.85
AllocateDataMethod · 0.80
dataMethod · 0.45

Tested by

no test coverage detected