Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
STARK: Fix the busy-wait for clicking sound
Browse files Browse the repository at this point in the history
  • Loading branch information
Dougaak committed May 18, 2018
1 parent 7a6a8fd commit f69be9f
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 24 deletions.
30 changes: 18 additions & 12 deletions engines/stark/services/userinterface.cpp
Expand Up @@ -55,7 +55,8 @@ UserInterface::UserInterface(Gfx::Driver *gfx) :
_interactionAttemptDenied(false),
_currentScreen(nullptr),
_gameWindowThumbnail(nullptr),
_preScreenNameStack() {
_prevScreenNameStack(),
_nextScreenName(Screen::kScreenMainMenu) {
}

UserInterface::~UserInterface() {
Expand Down Expand Up @@ -91,7 +92,10 @@ void UserInterface::update() {
}

void UserInterface::handleMouseMove(const Common::Point &pos) {
_cursor->setMousePosition(pos);
// Cursor should not move when there is request of changing the displayed screen
if (!hasChangeScreenRequest()) {
_cursor->setMousePosition(pos);
}
}

void UserInterface::handleClick() {
Expand Down Expand Up @@ -134,24 +138,26 @@ void UserInterface::requestFMVPlayback(const Common::String &name) {
}

void UserInterface::onFMVStopped() {
backPreScreen();
backPrevScreen();
}

void UserInterface::changeScreen(Screen::Name screenName) {
if (screenName == _currentScreen->getName()) {
return;
}
_nextScreenName = screenName;
}

_preScreenNameStack.push(_currentScreen->getName());
_currentScreen->close();
_currentScreen = getScreenByName(screenName);
_currentScreen->open();
void UserInterface::handleChangeScreen() {
if (hasChangeScreenRequest() && !(_currentScreen->hasRemainingSound())) {
_prevScreenNameStack.push(_currentScreen->getName());
_currentScreen->close();
_currentScreen = getScreenByName(_nextScreenName);
_currentScreen->open();
}
}

void UserInterface::backPreScreen() {
void UserInterface::backPrevScreen() {
// No need to check the stack since at least there will be a MainMenuScreen in it
// and MainMenuScreen will not request to go back
changeScreen(_preScreenNameStack.pop());
changeScreen(_prevScreenNameStack.pop());
}

Screen *UserInterface::getScreenByName(Screen::Name screenName) const {
Expand Down
15 changes: 12 additions & 3 deletions engines/stark/services/userinterface.h
Expand Up @@ -82,11 +82,19 @@ class UserInterface {
*/
bool skipFMV();

/** Set the currently displayed screen */
/** Request to change the currently displayed screen */
void changeScreen(Screen::Name screenName);

/** Check whether there is request of changing screen */
bool hasChangeScreenRequest() {
return _nextScreenName != _currentScreen->getName();
}

/** Handle the request of changing the currently displayed screen */
void handleChangeScreen();

/** Back to the previous displayed screen */
void backPreScreen();
void backPrevScreen();

/** Is the game screen currently displayed? */
bool isInGameScreen() const;
Expand Down Expand Up @@ -146,7 +154,8 @@ class UserInterface {
DiaryIndexScreen *_diaryIndexScreen;
MainMenuScreen *_mainMenuScreen;
Screen *_currentScreen;
Common::Stack<Screen::Name> _preScreenNameStack;
Common::Stack<Screen::Name> _prevScreenNameStack;
Screen::Name _nextScreenName;

Gfx::Driver *_gfx;
Cursor *_cursor;
Expand Down
2 changes: 2 additions & 0 deletions engines/stark/stark.cpp
Expand Up @@ -200,6 +200,8 @@ void StarkEngine::mainLoop() {
break;
}

_userInterface->handleChangeScreen();

if (_resourceProvider->hasLocationChangeRequest()) {
_global->setNormalSpeed();
_resourceProvider->performLocationChange();
Expand Down
2 changes: 1 addition & 1 deletion engines/stark/ui/menu/diaryindex.cpp
Expand Up @@ -110,7 +110,7 @@ void DiaryIndexScreen::widgetTextColorHandler(StaticLocationWidget &widget, cons
}

void DiaryIndexScreen::backHandler() {
StarkUserInterface->backPreScreen();
StarkUserInterface->backPrevScreen();
}

void DiaryIndexScreen::quitHandler() {
Expand Down
8 changes: 6 additions & 2 deletions engines/stark/ui/menu/locationscreen.cpp
Expand Up @@ -108,6 +108,12 @@ void StaticLocationScreen::onClick(const Common::Point &pos) {
for (uint i = 1; i < _widgets.size(); i++) {
StaticLocationWidget *widget = _widgets[i];
if (widget->isVisible() && widget->isMouseInside(pos)) {
// The click sound needs to be tracked to ensure being played completely
Resources::Sound *clickSound = widget->getSoundMouseClick();
if (clickSound) {
_prevTrackedSound = clickSound;
}

widget->onClick();
break;
}
Expand Down Expand Up @@ -178,8 +184,6 @@ void StaticLocationWidget::onClick() {

if (_soundMouseClick) {
_soundMouseClick->play();
// Ensure the click sound is played completely
while (_soundMouseClick->isPlaying()) {};
}

if (_onClick) {
Expand Down
3 changes: 3 additions & 0 deletions engines/stark/ui/menu/locationscreen.h
Expand Up @@ -88,6 +88,9 @@ class StaticLocationWidget {
/** Lookup sounds in the static location for use when hovering / clicking the widget */
void setupSounds(int16 enterSound, int16 clickSound);

/** Get the mouse click sound */
Resources::Sound *getSoundMouseClick() { return _soundMouseClick; }

/**
* Override the text color
*
Expand Down
24 changes: 19 additions & 5 deletions engines/stark/ui/menu/mainmenu.cpp
Expand Up @@ -24,10 +24,13 @@
#include "engines/stark/services/userinterface.h"
#include "engines/stark/services/resourceprovider.h"

#define SET_CLOSE_ACTION(method) new Common::Functor0Mem<void, MainMenuScreen>(this, &MainMenuScreen::method);

namespace Stark {

MainMenuScreen::MainMenuScreen(Gfx::Driver *gfx, Cursor *cursor) :
StaticLocationScreen(gfx, cursor, "MainMenuLocation", Screen::kScreenMainMenu) {
StaticLocationScreen(gfx, cursor, "MainMenuLocation", Screen::kScreenMainMenu),
_closeAction(nullptr) {
}

MainMenuScreen::~MainMenuScreen() {
Expand Down Expand Up @@ -125,6 +128,14 @@ void MainMenuScreen::open() {
nullptr));
}

void MainMenuScreen::close() {
StaticLocationScreen::close();

if (_closeAction) {
(*_closeAction)();
}
}

template<uint N>
void MainMenuScreen::helpTextHandler(StaticLocationWidget &widget, const Common::Point &mousePos) {
if (widget.isVisible()) {
Expand All @@ -140,16 +151,19 @@ void MainMenuScreen::creditsHandler() {

void MainMenuScreen::newGameHandler() {
StarkUserInterface->changeScreen(kScreenGame);
_closeAction = SET_CLOSE_ACTION(newGameHandlerHelper);
}

void MainMenuScreen::quitHandler() {
StarkUserInterface->notifyShouldExit();
}

void MainMenuScreen::newGameHandlerHelper() {
if (isDemo()) {
StarkResourceProvider->requestLocationChange(0x4f, 0x00);
} else {
StarkResourceProvider->requestLocationChange(0x45, 0x00);
}
}

void MainMenuScreen::quitHandler() {
StarkUserInterface->notifyShouldExit();
}

} // End of namespace Stark
7 changes: 7 additions & 0 deletions engines/stark/ui/menu/mainmenu.h
Expand Up @@ -39,15 +39,22 @@ class MainMenuScreen : public StaticLocationScreen {

// StaticLocationScreen API
void open() override;
void close() override;

private:
typedef Common::Functor0Mem<void, MainMenuScreen> CloseActionFunctor;

CloseActionFunctor *_closeAction;

template<uint N>
void helpTextHandler(StaticLocationWidget &widget, const Common::Point &mousePos);

void newGameHandler();
void creditsHandler();
void quitHandler();

void newGameHandlerHelper();

bool isDemo() {
return StarkGameDescription->flags & ADGF_DEMO;
}
Expand Down
9 changes: 8 additions & 1 deletion engines/stark/ui/screen.h
Expand Up @@ -27,6 +27,7 @@
#include "common/rect.h"

#include "engines/stark/ui/window.h"
#include "engines/stark/resources/sound.h"

namespace Stark {

Expand All @@ -42,7 +43,7 @@ class Screen {
kScreenDiaryIndex
};

explicit Screen(Name name) : _name(name) {};
explicit Screen(Name name) : _name(name), _prevTrackedSound(nullptr) {};
virtual ~Screen() {};

/** Obtain the name of the screen */
Expand All @@ -57,11 +58,17 @@ class Screen {
/** Draw the screen */
virtual void render() = 0;

/** Check whether the tracked sound has done playing */
bool hasRemainingSound() { return _prevTrackedSound && _prevTrackedSound->isPlaying(); }

virtual void handleMouseMove() = 0;
virtual void handleClick() = 0;
virtual void handleRightClick() = 0;
virtual void handleDoubleClick() = 0;

protected:
Resources::Sound *_prevTrackedSound;

private:
Name _name;
};
Expand Down

0 comments on commit f69be9f

Please sign in to comment.