mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::unique_ptr over manual memory management
parent
8942ac1e3e
commit
8f7468483a
|
@ -20,11 +20,6 @@
|
||||||
/** Instantiation of the 32bpp with animation blitter factory. */
|
/** Instantiation of the 32bpp with animation blitter factory. */
|
||||||
static FBlitter_32bppAnim iFBlitter_32bppAnim;
|
static FBlitter_32bppAnim iFBlitter_32bppAnim;
|
||||||
|
|
||||||
Blitter_32bppAnim::~Blitter_32bppAnim()
|
|
||||||
{
|
|
||||||
free(this->anim_alloc);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <BlitterMode mode>
|
template <BlitterMode mode>
|
||||||
inline void Blitter_32bppAnim::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
|
inline void Blitter_32bppAnim::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
|
||||||
{
|
{
|
||||||
|
@ -545,13 +540,12 @@ void Blitter_32bppAnim::PostResize()
|
||||||
if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height ||
|
if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height ||
|
||||||
_screen.pitch != this->anim_buf_pitch) {
|
_screen.pitch != this->anim_buf_pitch) {
|
||||||
/* The size of the screen changed; we can assume we can wipe all data from our buffer */
|
/* The size of the screen changed; we can assume we can wipe all data from our buffer */
|
||||||
free(this->anim_alloc);
|
|
||||||
this->anim_buf_width = _screen.width;
|
this->anim_buf_width = _screen.width;
|
||||||
this->anim_buf_height = _screen.height;
|
this->anim_buf_height = _screen.height;
|
||||||
this->anim_buf_pitch = (_screen.width + 7) & ~7;
|
this->anim_buf_pitch = (_screen.width + 7) & ~7;
|
||||||
this->anim_alloc = CallocT<uint16_t>(this->anim_buf_pitch * this->anim_buf_height + 8);
|
this->anim_alloc = std::make_unique<uint16_t[]>(this->anim_buf_pitch * this->anim_buf_height + 8);
|
||||||
|
|
||||||
/* align buffer to next 16 byte boundary */
|
/* align buffer to next 16 byte boundary */
|
||||||
this->anim_buf = reinterpret_cast<uint16_t *>((reinterpret_cast<uintptr_t>(this->anim_alloc) + 0xF) & (~0xF));
|
this->anim_buf = reinterpret_cast<uint16_t *>((reinterpret_cast<uintptr_t>(this->anim_alloc.get()) + 0xF) & (~0xF));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
class Blitter_32bppAnim : public Blitter_32bppOptimized {
|
class Blitter_32bppAnim : public Blitter_32bppOptimized {
|
||||||
protected:
|
protected:
|
||||||
uint16_t *anim_buf; ///< In this buffer we keep track of the 8bpp indexes so we can do palette animation
|
uint16_t *anim_buf; ///< In this buffer we keep track of the 8bpp indexes so we can do palette animation
|
||||||
void *anim_alloc; ///< The raw allocated buffer, not necessarily aligned correctly
|
std::unique_ptr<uint16_t[]> anim_alloc; ///< The raw allocated buffer, not necessarily aligned correctly
|
||||||
int anim_buf_width; ///< The width of the animation buffer.
|
int anim_buf_width; ///< The width of the animation buffer.
|
||||||
int anim_buf_height; ///< The height of the animation buffer.
|
int anim_buf_height; ///< The height of the animation buffer.
|
||||||
int anim_buf_pitch; ///< The pitch of the animation buffer (width rounded up to 16 byte boundary).
|
int anim_buf_pitch; ///< The pitch of the animation buffer (width rounded up to 16 byte boundary).
|
||||||
|
@ -25,7 +25,6 @@ protected:
|
||||||
public:
|
public:
|
||||||
Blitter_32bppAnim() :
|
Blitter_32bppAnim() :
|
||||||
anim_buf(nullptr),
|
anim_buf(nullptr),
|
||||||
anim_alloc(nullptr),
|
|
||||||
anim_buf_width(0),
|
anim_buf_width(0),
|
||||||
anim_buf_height(0),
|
anim_buf_height(0),
|
||||||
anim_buf_pitch(0)
|
anim_buf_pitch(0)
|
||||||
|
@ -33,8 +32,6 @@ public:
|
||||||
this->palette = _cur_palette;
|
this->palette = _cur_palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
~Blitter_32bppAnim();
|
|
||||||
|
|
||||||
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override;
|
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override;
|
||||||
void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal) override;
|
void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal) override;
|
||||||
void SetPixel(void *video, int x, int y, uint8_t colour) override;
|
void SetPixel(void *video, int x, int y, uint8_t colour) override;
|
||||||
|
|
|
@ -290,7 +290,7 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
|
||||||
/* streams of pixels (a, r, g, b channels)
|
/* streams of pixels (a, r, g, b channels)
|
||||||
*
|
*
|
||||||
* stored in separated stream so data are always aligned on 4B boundary */
|
* stored in separated stream so data are always aligned on 4B boundary */
|
||||||
Colour *dst_px_orig[ZOOM_LVL_END];
|
std::array<std::unique_ptr<Colour[]>, ZOOM_LVL_END> dst_px_orig;
|
||||||
|
|
||||||
/* interleaved stream of 'm' channel and 'n' channel
|
/* interleaved stream of 'm' channel and 'n' channel
|
||||||
* 'n' is number of following pixels with the same alpha channel class
|
* 'n' is number of following pixels with the same alpha channel class
|
||||||
|
@ -298,7 +298,7 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
|
||||||
*
|
*
|
||||||
* it has to be stored in one stream so fewer registers are used -
|
* it has to be stored in one stream so fewer registers are used -
|
||||||
* x86 has problems with register allocation even with this solution */
|
* x86 has problems with register allocation even with this solution */
|
||||||
uint16_t *dst_n_orig[ZOOM_LVL_END];
|
std::array<std::unique_ptr<uint16_t[]>, ZOOM_LVL_END> dst_n_orig;
|
||||||
|
|
||||||
/* lengths of streams */
|
/* lengths of streams */
|
||||||
uint32_t lengths[ZOOM_LVL_END][2];
|
uint32_t lengths[ZOOM_LVL_END][2];
|
||||||
|
@ -320,11 +320,11 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
|
||||||
|
|
||||||
uint size = src_orig->height * src_orig->width;
|
uint size = src_orig->height * src_orig->width;
|
||||||
|
|
||||||
dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
|
dst_px_orig[z] = std::make_unique<Colour[]>(size + src_orig->height * 2);
|
||||||
dst_n_orig[z] = CallocT<uint16_t>(size * 2 + src_orig->height * 4 * 2);
|
dst_n_orig[z] = std::make_unique<uint16_t[]>(size * 2 + src_orig->height * 4 * 2);
|
||||||
|
|
||||||
uint32_t *dst_px_ln = (uint32_t *)dst_px_orig[z];
|
uint32_t *dst_px_ln = reinterpret_cast<uint32_t *>(dst_px_orig[z].get());
|
||||||
uint32_t *dst_n_ln = (uint32_t *)dst_n_orig[z];
|
uint32_t *dst_n_ln = reinterpret_cast<uint32_t *>(dst_n_orig[z].get());
|
||||||
|
|
||||||
const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
|
const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
|
||||||
|
|
||||||
|
@ -405,8 +405,8 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
|
||||||
dst_n_ln = (uint32_t *)dst_n;
|
dst_n_ln = (uint32_t *)dst_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
lengths[z][0] = (uint8_t *)dst_px_ln - (uint8_t *)dst_px_orig[z]; // all are aligned to 4B boundary
|
lengths[z][0] = reinterpret_cast<uint8_t *>(dst_px_ln) - reinterpret_cast<uint8_t *>(dst_px_orig[z].get()); // all are aligned to 4B boundary
|
||||||
lengths[z][1] = (uint8_t *)dst_n_ln - (uint8_t *)dst_n_orig[z];
|
lengths[z][1] = reinterpret_cast<uint8_t *>(dst_n_ln) - reinterpret_cast<uint8_t *>(dst_n_orig[z].get());
|
||||||
}
|
}
|
||||||
|
|
||||||
uint len = 0; // total length of data
|
uint len = 0; // total length of data
|
||||||
|
@ -428,11 +428,8 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
|
||||||
dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
|
dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
|
||||||
dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
|
dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
|
||||||
|
|
||||||
memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
|
memcpy(dst->data + dst->offset[z][0], dst_px_orig[z].get(), lengths[z][0]);
|
||||||
memcpy(dst->data + dst->offset[z][1], dst_n_orig[z], lengths[z][1]);
|
memcpy(dst->data + dst->offset[z][1], dst_n_orig[z].get(), lengths[z][1]);
|
||||||
|
|
||||||
free(dst_px_orig[z]);
|
|
||||||
free(dst_n_orig[z]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return dest_sprite;
|
return dest_sprite;
|
||||||
|
|
Loading…
Reference in New Issue