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

Function PadSprites

src/spritecache.cpp:321–362  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

319}
320
321static bool PadSprites(SpriteLoader::SpriteCollection &sprite, ZoomLevels sprite_avail, SpriteEncoder *encoder)
322{
323 /* Get minimum top left corner coordinates. */
324 int min_xoffs = INT32_MAX;
325 int min_yoffs = INT32_MAX;
326 for (ZoomLevel zoom : sprite_avail) {
327 min_xoffs = std::min(min_xoffs, ScaleByZoom(sprite[zoom].x_offs, zoom));
328 min_yoffs = std::min(min_yoffs, ScaleByZoom(sprite[zoom].y_offs, zoom));
329 }
330
331 /* Get maximum dimensions taking necessary padding at the top left into account. */
332 int max_width = INT32_MIN;
333 int max_height = INT32_MIN;
334 for (ZoomLevel zoom : sprite_avail) {
335 max_width = std::max(max_width, ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom), zoom));
336 max_height = std::max(max_height, ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom), zoom));
337 }
338
339 /* Align height and width if required to match the needs of the sprite encoder. */
340 uint align = encoder->GetSpriteAlignment();
341 if (align != 0) {
342 max_width = Align(max_width, align);
343 max_height = Align(max_height, align);
344 }
345
346 /* Pad sprites where needed. */
347 for (ZoomLevel zoom : sprite_avail) {
348 auto &cur_sprite = sprite[zoom];
349 /* Scaling the sprite dimensions in the blitter is done with rounding up,
350 * so a negative padding here is not an error. */
351 int pad_left = std::max(0, cur_sprite.x_offs - UnScaleByZoom(min_xoffs, zoom));
352 int pad_top = std::max(0, cur_sprite.y_offs - UnScaleByZoom(min_yoffs, zoom));
353 int pad_right = std::max(0, UnScaleByZoom(max_width, zoom) - cur_sprite.width - pad_left);
354 int pad_bottom = std::max(0, UnScaleByZoom(max_height, zoom) - cur_sprite.height - pad_top);
355
356 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
357 if (!PadSingleSprite(&cur_sprite, zoom, pad_left, pad_top, pad_right, pad_bottom)) return false;
358 }
359 }
360
361 return true;
362}
363
364static bool ResizeSprites(SpriteLoader::SpriteCollection &sprite, ZoomLevels sprite_avail, SpriteEncoder *encoder)
365{

Callers 1

ResizeSpritesFunction · 0.85

Calls 5

ScaleByZoomFunction · 0.85
UnScaleByZoomFunction · 0.85
AlignFunction · 0.85
PadSingleSpriteFunction · 0.85
GetSpriteAlignmentMethod · 0.45

Tested by

no test coverage detected