Skip to content

Commit

Permalink
ZVISION: Implement PanTrack.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marisa-Chan committed Jan 17, 2014
1 parent 61224e7 commit 4141500
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 5 deletions.
24 changes: 24 additions & 0 deletions engines/zvision/actions.cpp
Expand Up @@ -313,6 +313,30 @@ bool ActionMusic::execute() {
return true;
}

//////////////////////////////////////////////////////////////////////////////
// ActionPanTrack
//////////////////////////////////////////////////////////////////////////////

ActionPanTrack::ActionPanTrack(ZVision *engine, int32 slotkey, const Common::String &line) :
ResultAction(engine, slotkey),
_pos(0),
_mus_slot(0) {

sscanf(line.c_str(), "%u %d", &_mus_slot, &_pos);
}

ActionPanTrack::~ActionPanTrack() {
_engine->getScriptManager()->killSideFx(_slotkey);
}

bool ActionPanTrack::execute() {
if (_engine->getScriptManager()->getSideFX(_slotkey))
return true;

_engine->getScriptManager()->addSideFX(new PanTrackNode(_engine, _slotkey, _mus_slot, _pos));

return true;
}

//////////////////////////////////////////////////////////////////////////////
// ActionPreloadAnimation
Expand Down
11 changes: 11 additions & 0 deletions engines/zvision/actions.h
Expand Up @@ -247,6 +247,17 @@ class ActionMusic : public ResultAction {
bool _universe;
};

class ActionPanTrack : public ResultAction {
public:
ActionPanTrack(ZVision *engine, int32 slotkey, const Common::String &line);
~ActionPanTrack();
bool execute();

private:
int32 _pos;
uint32 _mus_slot;
};

class ActionPlayAnimation : public ResultAction {
public:
ActionPlayAnimation(ZVision *engine, int32 slotkey, const Common::String &line);
Expand Down
75 changes: 74 additions & 1 deletion engines/zvision/music_node.cpp
Expand Up @@ -26,6 +26,7 @@

#include "zvision/zvision.h"
#include "zvision/script_manager.h"
#include "zvision/render_manager.h"
#include "zvision/zork_raw.h"

#include "common/stream.h"
Expand All @@ -40,7 +41,7 @@ MusicNode::MusicNode(ZVision *engine, uint32 key, Common::String &filename, bool
_loop = loop;
_volume = volume;
_crossfade = false;
_crossfade_delta = 0;
_crossfade_target = 0;
_crossfade_time = 0;
_attenuate = 0;
_pantrack = false;
Expand All @@ -57,6 +58,7 @@ MusicNode::MusicNode(ZVision *engine, uint32 key, Common::String &filename, bool
audioStream = makeRawZorkStream(filename, _engine);
}

_stereo = audioStream->isStereo();

if (_loop) {
Audio::LoopingAudioStream *loopingAudioStream = new Audio::LoopingAudioStream(audioStream, 0, DisposeAfterUse::YES);
Expand All @@ -76,10 +78,81 @@ MusicNode::~MusicNode() {
debug(1, "MusicNode: %d destroyed\n", _key);
}

void MusicNode::setPanTrack(int16 pos) {
if (!_stereo) {
_pantrack = true;
_pantrack_X = pos;
setVolume(_volume);
}
}

void MusicNode::unsetPanTrack() {
_pantrack = false;
setVolume(_volume);
}

bool MusicNode::process(uint32 deltaTimeInMillis) {
if (! _engine->_mixer->isSoundHandleActive(_handle))
return stop();
else {
uint8 _newvol = _volume;

if (_pantrack || _volume != _newvol)
setVolume(_newvol);
}
return false;
}

void MusicNode::setVolume(uint8 new_volume) {
if (_pantrack) {
int cur_x = _engine->getScriptManager()->getStateValue(StateKey_ViewPos);
cur_x -= _pantrack_X;
int32 _width = _engine->getRenderManager()->getBkgSize().x;
if (cur_x < (-_width) / 2)
cur_x += _width;
else if (cur_x >= _width / 2)
cur_x -= _width;

float norm = (float)cur_x / ((float)_width / 2.0);
float lvl = fabs(norm);
if (lvl > 0.5)
lvl = (lvl - 0.5) * 1.7;
else
lvl = 1.0;

float bal = sin(-norm * 3.1415926) * 127.0;

if (_engine->_mixer->isSoundHandleActive(_handle)) {
_engine->_mixer->setChannelBalance(_handle, bal);
_engine->_mixer->setChannelVolume(_handle, new_volume * lvl);
}
} else {
if (_engine->_mixer->isSoundHandleActive(_handle)) {
_engine->_mixer->setChannelBalance(_handle, 0);
_engine->_mixer->setChannelVolume(_handle, new_volume);
}
}

_volume = new_volume;
}

PanTrackNode::PanTrackNode(ZVision *engine, uint32 key, uint32 slot, int16 pos)
: SideFX(engine, key, SIDEFX_PANTRACK) {
_slot = slot;

SideFX *fx = _engine->getScriptManager()->getSideFX(slot);
if (fx && fx->getType() == SIDEFX_AUDIO) {
MusicNode *mus = (MusicNode *)fx;
mus->setPanTrack(pos);
}
}

PanTrackNode::~PanTrackNode() {
SideFX *fx = _engine->getScriptManager()->getSideFX(_slot);
if (fx && fx->getType() == SIDEFX_AUDIO) {
MusicNode *mus = (MusicNode *)fx;
mus->unsetPanTrack();
}
}

} // End of namespace ZVision
19 changes: 16 additions & 3 deletions engines/zvision/music_node.h
Expand Up @@ -45,20 +45,33 @@ class MusicNode : public SideFX {
*/
bool process(uint32 deltaTimeInMillis);

void setVolume(uint8 volume);

void setPanTrack(int16 pos);
void unsetPanTrack();
private:
int32 _timeLeft;
bool _pantrack;
int32 _pantrack_X;
int32 _attenuate;
int8 _volume;
int32 _id;
uint8 _volume;
bool _loop;
bool _crossfade;
int32 _crossfade_delta;
uint8 _crossfade_target;
int32 _crossfade_time;
bool _stereo;
Audio::SoundHandle _handle;
};

class PanTrackNode : public SideFX {
public:
PanTrackNode(ZVision *engine, uint32 key, uint32 slot, int16 pos);
~PanTrackNode();

private:
uint32 _slot;
};

} // End of namespace ZVision

#endif
2 changes: 1 addition & 1 deletion engines/zvision/scr_file_handling.cpp
Expand Up @@ -248,7 +248,7 @@ void ScriptManager::parseResults(Common::SeekableReadStream &stream, Common::Lis
} else if (act.matchString("music", true)) {
actionList.push_back(new ActionMusic(_engine, slot, args, false));
} else if (act.matchString("pan_track", true)) {
// TODO: Implement ActionPanTrack
actionList.push_back(new ActionPanTrack(_engine, slot, args));
} else if (act.matchString("playpreload", true)) {
actionList.push_back(new ActionPlayPreloadAnimation(_engine, slot, args));
} else if (act.matchString("preferences", true)) {
Expand Down

0 comments on commit 4141500

Please sign in to comment.