Ticket #8598: nut-allocate.diff

File nut-allocate.diff, 3.0 KB (added by eriktorbjorn, 14 years ago)

Patch against current SVN

  • engines/scumm/nut_renderer.cpp

     
    3030NutRenderer::NutRenderer(ScummEngine *vm) :
    3131        _vm(vm),
    3232        _loaded(false),
    33         _numChars(0) {
     33        _numChars(0),
     34        _decodedData(0) {
    3435        memset(_chars, 0, sizeof(_chars));
    3536}
    3637
    3738NutRenderer::~NutRenderer() {
    38         for (int i = 0; i < _numChars; i++) {
    39                 delete []_chars[i].src;
    40         }
     39        delete [] _decodedData;
    4140}
    4241
    4342void smush_decode_codec1(byte *dst, const byte *src, int left, int top, int width, int height, int pitch);
     
    8988        }
    9089
    9190        uint32 length = file.readUint32BE();
    92         byte *dataSrc = (byte *)malloc(length);
     91        byte *dataSrc = new byte[length];
    9392        file.read(dataSrc, length);
    9493        file.close();
    9594
     
    9998                return false;
    10099        }
    101100
     101        // We pre-decode the font, which may seem wasteful at first. Actually,
     102        // the memory needed for just the decoded glyphs is smaller than the
     103        // whole of the undecoded font file.
     104
    102105        _numChars = READ_LE_UINT16(dataSrc + 10);
    103106        assert(_numChars <= ARRAYSIZE(_chars));
     107
    104108        uint32 offset = 0;
     109        uint32 decodedLength = 0;
    105110        for (int l = 0; l < _numChars; l++) {
     111                offset += READ_BE_UINT32(dataSrc + offset + 4) + 16;
     112                int width = READ_LE_UINT16(dataSrc + offset + 14);
     113                int height = READ_LE_UINT16(dataSrc + offset + 16);
     114                decodedLength += (width * height);
     115        }
     116
     117        // If characters have transparency, then bytes just get skipped and
     118        // so there may appear some garbage. That's why we have to fill it
     119        // with zeroes first.
     120
     121        _decodedData = new byte[decodedLength];
     122        memset(_decodedData, 0, decodedLength);
     123
     124        byte *decodedPtr = _decodedData;
     125
     126        offset = 0;
     127        for (int l = 0; l < _numChars; l++) {
    106128                offset += READ_BE_UINT32(dataSrc + offset + 4) + 8;
    107129                if (READ_BE_UINT32(dataSrc + offset) != 'FRME') {
    108130                        error("NutRenderer::loadFont(%s) there is no FRME chunk %d (offset %x)", filename, l, offset);
     
    118140                _chars[l].yoffs = READ_LE_UINT16(dataSrc + offset + 12);
    119141                _chars[l].width = READ_LE_UINT16(dataSrc + offset + 14);
    120142                _chars[l].height = READ_LE_UINT16(dataSrc + offset + 16);
    121                 const int srcSize = _chars[l].width * _chars[l].height;
    122                 _chars[l].src = new byte[srcSize];
    123                 // If characters have transparency, then bytes just get skipped and
    124                 // so there may appear some garbage. That's why we have to fill it
    125                 // with zeroes first.
    126                 memset(_chars[l].src, 0, srcSize);
     143                _chars[l].src = decodedPtr;
    127144
     145                decodedPtr += (_chars[l].width * _chars[l].height);
     146
    128147                const uint8 *fobjptr = dataSrc + offset + 22;
    129148                switch (codec) {
    130149                case 1:
     
    139158                }
    140159        }
    141160
    142         free(dataSrc);
     161        delete [] dataSrc;
    143162        _loaded = true;
    144163        return true;
    145164}
  • engines/scumm/nut_renderer.h

     
    3434        ScummEngine *_vm;
    3535        bool _loaded;
    3636        int _numChars;
     37        byte *_decodedData;
    3738        struct {
    3839                int xoffs;
    3940                int yoffs;