Ticket #1399: sword2-animation2.diff

File sword2-animation2.diff, 3.6 KB (added by eriktorbjorn, 20 years ago)

Slightly cleaned up version of the previous patch

  • scummvm/sword2/driver/animation.cpp

    diff -ur --exclude=CVS ScummVM/scummvm/sword2/driver/animation.cpp ScummVM+hack/scummvm/sword2/driver/animation.cpp
    old new  
    128128
    129129        info = mpeg2_info(decoder);
    130130        framenum = 0;
    131         ticks = _vm->_system->get_msecs();
    132131
    133132        // Play audio
    134133
     
    140139        if (sndfile->open(tempFile)) {
    141140                bgSoundStream = makeVorbisStream(sndfile, sndfile->size());
    142141                _vm->_mixer->playInputStream(&bgSound, bgSoundStream, false, 255, 0, -1);
     142               
    143143        }
    144 
    145144#endif
    146145
     146        startTime = _vm->_system->get_msecs();
     147
    147148        return true;
    148149#else /* USE_MPEG2 */
    149150        return false;
     
    337338                case STATE_SLICE:
    338339                case STATE_END:
    339340                        if (info->display_fbuf) {
    340                                 /* simple audio video sync code:
    341                                  * we calculate the actual frame by taking the delivered audio samples
    342                                  * we add 2 frames as the number of samples delivered is higher than the
    343                                  * number actually played due to buffering
     341                                /* Audio/video sync is extremely simple. We
     342                                 * just keep track of how long it's been since
     343                                 * the audio track was started to calculate an
     344                                 * approximate frame number. We try not to lag
     345                                 * more than one frame behind this.
    344346                                 *
    345                                  * we then try to stay inside +- 1 frame of this calculated frame number by
    346                                  * dropping frames if we run behind and delaying if we are too fast
     347                                 * We do it this way since there is no way of
     348                                 * asking the mixer how many samples have
     349                                 * actually been played.
    347350                                 */
    348351
    349352#ifdef BACKEND_8BIT
    350                                 if (checkPaletteSwitch() || (bgSoundStream == NULL) ||
    351                                         (bgSoundStream->getSamplesPlayed() * 12 / bgSoundStream->getRate()) < (framenum + 3)){
    352353
     354                                if (checkPaletteSwitch() || _vm->_system->get_msecs() < startTime + (1000 * (framenum + 1)) / 12) {
    353355                                        _vm->_graphics->plotYUV(lut, sequence_i->width, sequence_i->height, info->display_fbuf->buf);
    354 
    355                                         if (bgSoundStream) {
    356                                                 while ((bgSoundStream->getSamplesPlayed() * 12 / bgSoundStream->getRate()) < framenum + 1)
    357                                                         _vm->_system->delay_msecs(10);
    358                                         } else {
    359                                                 ticks += 83;
    360                                                 _vm->sleepUntil(ticks);
    361                                         }
    362 
     356                                        _vm->sleepUntil(startTime + (1000 * framenum) / 12);
    363357                                        _vm->_graphics->setNeedFullRedraw();
    364 
    365358                                } else
    366359                                        warning("dropped frame %i", framenum);
    367360
     
    369362
    370363#else
    371364
    372                                 if ((bgSoundStream == NULL) ||
    373                                         (bgSoundStream->getSamplesPlayed()*12/bgSoundStream->getRate()) < (framenum+3)){
    374 
     365                                if (_vm->_system->get_msecs() < startTime + (1000 * (framenum + 1)) / 12) {
    375366                                        plotYUV(lookup2, sequence_i->width, sequence_i->height, info->display_fbuf->buf);
    376 
    377                                         if (bgSoundStream) {
    378                                                 while ((bgSoundStream->getSamplesPlayed()*12/bgSoundStream->getRate()) < framenum + 1)
    379                                                         _vm->_system->delay_msecs(10);
    380                                         } else {
    381                                                 ticks += 83;
    382                                                 _vm->sleepUntil(ticks);
    383                                         }
    384 
     367                                        _vm->sleepUntil(startTime + (1000 * framenum) / 12);
    385368                                } else
    386369                                        warning("dropped frame %i", framenum);
    387370
     
    389372
    390373                                framenum++;
    391374                                return true;
    392 
    393375                        }
    394376                        break;
    395377
  • scummvm/sword2/driver/animation.h

    diff -ur --exclude=CVS ScummVM/scummvm/sword2/driver/animation.h ScummVM+hack/scummvm/sword2/driver/animation.h
    old new  
    6969private:
    7070        Sword2Engine *_vm;
    7171
    72         int framenum;
    73         int ticks;
     72        uint framenum;
     73        uint32 startTime;
    7474
    7575#ifdef USE_MPEG2
    7676        mpeg2dec_t *decoder;
     
    100100
    101101        struct {
    102102                int cnt;
    103                 int end;
     103                uint end;
    104104                byte pal[4 * 256];
    105105        } palettes[50];
    106106#else