C4FontLoader: Stop manually managing memory

Instead of implementing a dynamically growable array with new and delete
and memcpy like cavemen, we'll use a vector of smart pointers.
liquid_container
Nicolas Hake 2016-02-08 21:26:49 +01:00
parent 47d6f2d75a
commit a204439f80
2 changed files with 6 additions and 20 deletions

View File

@ -284,9 +284,7 @@ CStdFont::CStdFont()
{
#ifndef USE_CONSOLE
// set default values
psfcFontData = NULL;
sfcCurrent = NULL;
iNumFontSfcs = 0;
iSfcSizes = 64;
dwDefFontHeight=iLineHgt=10;
iFontZoom=1; // default: no internal font zooming - likely no antialiasing either...
@ -305,21 +303,16 @@ CStdFont::CStdFont()
#ifndef USE_CONSOLE
bool CStdFont::AddSurface()
{
// add new surface as render target; copy old ones
C4Surface **pNewSfcs = new C4Surface *[iNumFontSfcs+1];
if (iNumFontSfcs) memcpy(pNewSfcs, psfcFontData, iNumFontSfcs * sizeof (C4Surface *));
delete [] psfcFontData;
psfcFontData = pNewSfcs;
C4Surface *sfcNew = psfcFontData[iNumFontSfcs] = new C4Surface();
++iNumFontSfcs;
if (iSfcSizes) if (!sfcNew->Create(iSfcSizes, iSfcSizes)) return false;
// add new surface as render target
auto sfcNew = std::make_unique<C4Surface>(iSfcSizes, iSfcSizes);
// If old surface was locked, unlock it and lock the new one in its stead
if (sfcCurrent && sfcCurrent->IsLocked())
{
sfcCurrent->Unlock();
sfcNew->Lock();
}
sfcCurrent = sfcNew;
sfcCurrent = sfcNew.get();
psfcFontData.push_back(std::move(sfcNew));
iCurrentSfcX = iCurrentSfcY = 0;
return true;
}
@ -496,14 +489,8 @@ void CStdFont::Clear()
pVectorFont = NULL;
// clear font sfcs
if (psfcFontData)
{
while (iNumFontSfcs--) delete psfcFontData[iNumFontSfcs];
delete [] psfcFontData;
psfcFontData = NULL;
}
sfcCurrent = NULL;
iNumFontSfcs = 0;
psfcFontData.clear();
for (int c=' '; c<256; ++c) fctAsciiTexCoords[c-' '].Default();
fctUnicodeMap.clear();
// set default values

View File

@ -98,8 +98,7 @@ protected:
DWORD dwDefFontHeight; // configured font size (in points)
char szFontName[80+1]; // used font name (or surface file name)
C4Surface **psfcFontData; // font recource surfaces - additional surfaces created as needed
int iNumFontSfcs; // number of created font surfaces
std::vector<std::unique_ptr<C4Surface>> psfcFontData; // font resource surfaces - additional surfaces created as needed
int iSfcSizes; // size for font surfaces
int iFontZoom; // zoom of font in texture