PETKA: refactor VideoSystem

This commit is contained in:
Andrei Prykhodko 2020-10-12 22:49:57 +03:00
parent 852d597588
commit 6ff94e6d47
6 changed files with 83 additions and 76 deletions

View File

@ -82,6 +82,38 @@ void Interface::removeTexts() {
}
}
void Interface::update(uint time) {
for (uint i = _startIndex; i < _objs.size(); ++i) {
_objs[i]->update(time);
}
for (uint i = 0; i < _objs.size(); ++i) {
_objs[i]->updateZ();
}
sort();
}
void Interface::draw() {
for (uint i = 0; i < _objs.size(); ++i) {
_objs[i]->draw();
}
}
void Interface::sort() {
for (uint i = 0; i < _objs.size() - 1; ++i) {
uint minIndex = i;
for (uint j = i + 1; j < _objs.size(); ++j) {
if (_objs[j]->_z < _objs[minIndex]->_z) {
minIndex = j;
}
}
if (i != minIndex) {
SWAP(_objs[i], _objs[minIndex]);
}
}
}
void SubInterface::start(int id) {
QSystem *sys = g_vm->getQSystem();
QObjectCursor *cursor = sys->getCursor();

View File

@ -40,6 +40,9 @@ public:
virtual void start(int id) {};
virtual void stop();
virtual void update(uint time);
void draw();
virtual void onLeftButtonDown(Common::Point p) {};
virtual void onRightButtonDown(Common::Point p) {};
virtual void onMouseMove(Common::Point p) {};
@ -53,6 +56,9 @@ public:
void initCursor(int id, bool show, bool animate);
private:
void sort();
public:
Common::Array<QVisibleObject *> _objs;
QVisibleObject *_objUnderCursor;

View File

@ -276,4 +276,22 @@ void InterfaceMain::removeTextDescription() {
removeTexts();
}
void InterfaceMain::update(uint time) {
QSystem *sys = g_vm->getQSystem();
int xOff = sys->_xOffset;
int reqOffset = sys->_reqOffset;
if (xOff != reqOffset && ((xOff != sys->_sceneWidth - 640 && xOff < reqOffset) || (xOff > 0 && xOff > reqOffset))) {
if (xOff <= reqOffset) {
xOff += 8;
xOff = MIN<int>(xOff, reqOffset);
} else {
xOff -= 8;
xOff = MAX<int>(xOff, reqOffset);
}
sys->_xOffset = CLIP(xOff, 0, sys->_sceneWidth - 640);
g_vm->videoSystem()->makeAllDirty();
}
Interface::update(time);
}
} // End of namespace Petka

View File

@ -43,6 +43,8 @@ public:
void start(int id) override;
virtual void update(uint time);
void loadRoom(int id, bool fromSave);
const BGInfo *findBGInfo(int id) const;

View File

@ -22,78 +22,49 @@
#include "common/system.h"
#include "petka/sound.h"
#include "petka/flc.h"
#include "petka/petka.h"
#include "petka/q_system.h"
#include "petka/interfaces/main.h"
#include "petka/interfaces/dialog_interface.h"
#include "petka/objects/object.h"
#include "petka/interfaces/interface.h"
#include "petka/video.h"
namespace Petka {
// COMPLETED
const uint kShakeTime = 30;
const int kShakeOffset = 3;
VideoSystem::VideoSystem(PetkaEngine &vm) :
_vm(vm), _shake(false), _shift(false), _shakeTime(0), _time(0) {
makeAllDirty();
VideoSystem::VideoSystem(PetkaEngine &vm)
: _vm(vm) {
_shakeTime = 0;
_time = g_system->getMillis();
_allowAddingRects = true;
_shake = false;
_shift = false;
_allowAddingRects = false;
}
void VideoSystem::update() {
QSystem *sys = _vm.getQSystem();
Interface *interface = sys->_currInterface;
uint32 time = g_system->getMillis();
if (interface) {
if (sys->_currInterface == sys->_mainInterface.get()) {
int xOff = sys->_xOffset;
int reqOffset = sys->_reqOffset;
if (xOff != reqOffset && ((xOff != sys->_sceneWidth - 640 && xOff < reqOffset) || (xOff > 0 && xOff > reqOffset))) {
if (xOff <= reqOffset) {
xOff += 8;
xOff = MIN<int>(xOff, reqOffset);
} else {
xOff -= 8;
xOff = MAX<int>(xOff, reqOffset);
}
sys->_xOffset = CLIP(xOff, 0, sys->_sceneWidth - 640);
makeAllDirty();
}
}
assert(sys);
assert(interface);
for (uint i = interface->_startIndex; i < interface->_objs.size(); ++i) {
interface->_objs[i]->update(time - _time);
}
interface->update(time - _time);
for (uint i = 0; i < interface->_objs.size(); ++i) {
interface->_objs[i]->updateZ();
}
mergeDirtyRects();
sort();
mergeDirtyRects();
_allowAddingRects = false;
interface->draw();
_allowAddingRects = true;
_allowAddingRects = false;
for (uint i = 0; i < interface->_objs.size(); ++i) {
interface->_objs[i]->draw();
}
_allowAddingRects = true;
for (Common::List<Common::Rect>::iterator i = _dirtyRects.begin(); i != _dirtyRects.end(); ++i) {
const Common::Rect &r = *i;
const byte *srcP = (const byte *)getBasePtr(r.left, r.top);
g_system->copyRectToScreen(srcP, pitch, r.left, r.top,
r.width(), r.height());
}
_dirtyRects.clear();
for (Common::Rect &r : _dirtyRects) {
const byte *srcP = (const byte *)getBasePtr(r.left, r.top);
g_system->copyRectToScreen(srcP, pitch, r.left, r.top, r.width(), r.height());
}
_dirtyRects.clear();
_time = time;
if (_shake) {
@ -119,14 +90,13 @@ void VideoSystem::addDirtyRect(Common::Point pos, Common::Rect rect) {
}
void VideoSystem::addDirtyRect(Common::Point pos, FlicDecoder &flc) {
pos.x = pos.x - g_vm->getQSystem()->_xOffset;
pos.x = pos.x - _vm.getQSystem()->_xOffset;
addDirtyRect(pos, flc.getBounds());
}
void VideoSystem::addDirtyMskRects(Common::Point pos, FlicDecoder &flc) {
const Common::Array<Common::Rect> &rects = flc.getMskRects();
for (uint i = 0; i < rects.size(); ++i) {
addDirtyRect(pos, rects[i]);
for (auto rect : flc.getMskRects()) {
addDirtyRect(pos, rect);
}
}
@ -147,22 +117,4 @@ void VideoSystem::setShake(bool shake) {
g_system->setShakePos(0, 0);
}
void VideoSystem::sort() {
Common::Array<QVisibleObject *> &objs = _vm.getQSystem()->_currInterface->_objs;
for (uint i = 0; i < objs.size() - 1; ++i) {
uint minIndex = i;
for (uint j = i + 1; j < objs.size(); ++j) {
if (objs[j]->_z < objs[minIndex]->_z) {
minIndex = j;
}
}
if (i != minIndex) {
QVisibleObject *tmp = objs[i];
objs[i] = objs[minIndex];
objs[minIndex] = tmp;
}
}
}
}
} // End of namespace Petka

View File

@ -33,7 +33,7 @@ class PetkaEngine;
class VideoSystem : public Graphics::Screen {
public:
VideoSystem(PetkaEngine &vm);
explicit VideoSystem(PetkaEngine &vm);
void updateTime();
void update() override;
@ -49,9 +49,6 @@ public:
const Common::List<Common::Rect> &rects() const;
private:
void sort();
private:
PetkaEngine &_vm;
uint32 _shakeTime;
@ -61,6 +58,6 @@ private:
bool _allowAddingRects;
};
}
} // End of namespace Petka
#endif