Permalink
Browse files

add states to Game::mainLoop

  • Loading branch information...
1 parent 1994c00 commit fad03884dfc9359648c1ea4fba78250755927c7b @cyxx committed Jul 7, 2015
Showing with 122 additions and 74 deletions.
  1. +6 −13 bag.cpp
  2. +13 −13 dialogue.cpp
  3. +77 −36 game.cpp
  4. +15 −1 game.h
  5. +8 −10 main.cpp
  6. +3 −1 systemstub_sdl.cpp
View
@@ -7,13 +7,9 @@
#include "systemstub.h"
void Game::handleBagMenu() {
- if (!(_varsTable[0] < 10 && _loadDataState == 2 && _sceneNumber != -1000))
- return;
-
- int xPosWnd = (kGameScreenWidth - (_bagBackgroundImage.w + 1)) / 2;
- int yPosWnd = (kGameScreenHeight - (_bagBackgroundImage.h + 1)) / 4;
- while (!_stub->_quit) {
+ const int xPosWnd = (kGameScreenWidth - (_bagBackgroundImage.w + 1)) / 2;
+ const int yPosWnd = (kGameScreenHeight - (_bagBackgroundImage.h + 1)) / 4;
if (_stub->_pi.leftMouseButton) {
_stub->_pi.leftMouseButton = false;
@@ -62,11 +58,13 @@ void Game::handleBagMenu() {
if (_stub->_pi.tab) {
_stub->_pi.tab = false;
- break;
+ _nextState = kStateGame;
+ return;
}
if (_stub->_pi.enter) {
_stub->_pi.enter = false;
- break;
+ _nextState = kStateGame;
+ return;
}
if (_stub->_pi.dirMask & PlayerInput::DIR_LEFT) {
@@ -130,11 +128,6 @@ void Game::handleBagMenu() {
_bagWeaponAreaBlinkCounter = 0;
}
}
-
- _stub->updateScreen();
- _stub->processEvents();
- _stub->sleep(50);
- }
}
void Game::drawBagMenu(int xPosWnd, int yPosWnd) {
View
@@ -101,8 +101,7 @@ void Game::redrawDialogueTexts() {
free(textBuffer);
}
-void Game::handleDialogue() {
- debug(DBG_DIALOGUE, "Game::handleDialogue()");
+void Game::initDialogue() {
playMusic("..\\midi\\sadialog.mid");
for (int spr = 0; spr < 3; ++spr) {
for (int i = 0; i < 105; ++i) {
@@ -119,8 +118,11 @@ void Game::handleDialogue() {
_stub->_pi.dirMask = 0;
_stub->_pi.escape = false;
_stub->_pi.enter = false;
+}
+
+void Game::handleDialogue() {
+ debug(DBG_DIALOGUE, "Game::handleDialogue()");
- while (!_stub->_quit) {
if (_stub->_pi.dirMask & PlayerInput::DIR_DOWN) {
_stub->_pi.dirMask &= ~PlayerInput::DIR_DOWN;
if (_dialogueChoiceSelected == 0 && _dialogueSpeechIndex < _dialogueChoiceCounter - 1) {
@@ -135,7 +137,8 @@ void Game::handleDialogue() {
}
if (_stub->_pi.escape) {
_stub->_pi.escape = false;
- break;
+ _nextState = kStateGame;
+ return;
}
if (_stub->_pi.enter) {
_stub->_pi.enter = false;
@@ -152,7 +155,8 @@ void Game::handleDialogue() {
if (_dialogueChoiceGotoFlag[_dialogueSpeechIndex]) {
setupDialog(_dialogueChoiceNextId[_dialogueSpeechIndex]);
if (_dialogueChoiceCounter == 0) {
- break;
+ _nextState = kStateGame;
+ return;
}
} else {
int n = atoi(_tempTextBuffer);
@@ -161,7 +165,7 @@ void Game::handleDialogue() {
} else {
_dialogueEndedFlag = 1;
_lastDialogueEndedId = atoi(_tempTextBuffer);
- break;
+ return;
}
}
}
@@ -171,18 +175,14 @@ void Game::handleDialogue() {
if (_dialogueSpriteIndex == 2) {
if (_dialogueSpriteCurrentFrameTable[2] == 0) {
_dialogueEndedFlag = 1;
- break;
+ return;
}
} else {
redrawDialogueTexts();
}
+}
- _stub->updateScreen();
- _stub->processEvents();
-
- _stub->sleep(50);
- }
-
+void Game::finiDialogue() {
unloadDialogueData();
if (_dialogueFrameSpriteData) {
free(_dialogueFrameSpriteData);
View
@@ -16,6 +16,7 @@ static const bool kCheatNoHit = false;
Game::Game(SystemStub *stub, const char *dataPath, const char *savePath, const char *musicPath)
: _fs(dataPath), _stub(stub), _dataPath(dataPath), _savePath(savePath), _musicPath(musicPath) {
+ _state = _nextState = -1;
_mixer = _stub->getMixer();
_stateSlot = 1;
detectVersion();
@@ -106,20 +107,54 @@ void Game::restart() {
strcpy(_tempTextBuffer, _startupScene);
}
-void Game::mainLoop() {
+void Game::init() {
_stub->init(kGameWindowTitle, kGameScreenWidth, kGameScreenHeight);
allocateTables();
loadCommonSprites();
restart();
if (_isDemo) {
- playBitmapSequenceDemo();
+ _bitmapSequence = 0;
+ _nextState = kStateBitmapSequence;
} else {
playVideo("DATA/LOGO.AVI");
playVideo("DATA/INTRO.AVI");
}
_lastFrameTimeStamp = _stub->getTimeStamp();
- while (!_stub->_quit) {
- if (_switchScene) {
+}
+
+void Game::fini() {
+ clearSceneData(-1);
+ deallocateTables();
+ unloadCommonSprites();
+ _stub->destroy();
+}
+
+void Game::mainLoop() {
+ if (_nextState != _state) {
+ // fini
+ switch (_state) {
+ case kStateGame:
+ break;
+ case kStateDialogue:
+ finiDialogue();
+ break;
+ }
+ _state = _nextState;
+ // init
+ switch (_state) {
+ case kStateGame:
+ break;
+ case kStateDialogue:
+ initDialogue();
+ break;
+ case kStateBitmapSequence:
+ drawBitmapSequenceDemo(_bitmapSequence);
+ break;
+ }
+ }
+ switch (_state) {
+ case kStateGame:
+ while (_switchScene) {
_switchScene = false;
if (stringEndsWith(_tempTextBuffer, "SCN")) {
win16_sndPlaySound(6);
@@ -166,24 +201,39 @@ void Game::mainLoop() {
updateKeysPressedTable();
updateMouseButtonsPressed();
runObjectsScript();
- if (!_switchScene) {
- _stub->updateScreen();
- uint32_t end = _lastFrameTimeStamp + kCycleDelay;
- do {
- _stub->sleep(10);
- _stub->processEvents();
- } while (!_stub->_pi.fastMode && _stub->getTimeStamp() < end);
- _lastFrameTimeStamp = _stub->getTimeStamp();
- }
if (_startDialogue) {
_startDialogue = false;
- handleDialogue();
+ _nextState = kStateDialogue;
}
- }
- clearSceneData(-1);
- deallocateTables();
- unloadCommonSprites();
- _stub->destroy();
+ break;
+ case kStateBag:
+ handleBagMenu();
+ break;
+ case kStateDialogue:
+ handleDialogue();
+ if (_dialogueEndedFlag) {
+ _nextState = kStateGame;
+ }
+ break;
+ case kStateBitmapSequence:
+ if (_stub->_pi.enter) {
+ _stub->_pi.enter = false;
+ ++_bitmapSequence;
+ if (_bitmapSequence == 3) {
+ _nextState = kStateGame;
+ } else {
+ drawBitmapSequenceDemo(_bitmapSequence);
+ }
+ }
+ break;
+ }
+ _stub->updateScreen();
+ const uint32_t end = _lastFrameTimeStamp + kCycleDelay;
+ do {
+ _stub->sleep(10);
+ _stub->processEvents();
+ } while (!_stub->_pi.fastMode && _stub->getTimeStamp() < end);
+ _lastFrameTimeStamp = _stub->getTimeStamp();
}
void Game::updateMouseButtonsPressed() {
@@ -209,7 +259,9 @@ void Game::updateKeysPressedTable() {
_keysPressed[40] = (_stub->_pi.dirMask & PlayerInput::DIR_DOWN) ? 1 : 0;
if (_stub->_pi.tab) {
_stub->_pi.tab = false;
- handleBagMenu();
+ if (_varsTable[0] < 10 && _loadDataState == 2 && _sceneNumber != -1000) {
+ _nextState = kStateBag;
+ }
}
if (_stub->_pi.ctrl) {
_stub->_pi.ctrl = false;
@@ -893,22 +945,11 @@ void Game::playVideo(const char *name) {
}
}
-void Game::playBitmapSequenceDemo() {
- const char *bitmapList[] = { "..\\wgp\\title.bmp", "..\\wgp\\title1.bmp", "..\\wgp\\title2.bmp" };
- for (int i = 0; i < 3; ++i) {
- loadWGP(bitmapList[i]);
- _stub->setPalette(_bitmapBuffer0 + kOffsetBitmapPalette, 256);
- _stub->copyRect(0, 0, kGameScreenWidth, kGameScreenHeight, _bitmapBuffer1.bits, _bitmapBuffer1.pitch);
- _stub->updateScreen();
- do {
- _stub->sleep(10);
- _stub->processEvents();
- if (_stub->_quit) {
- return;
- }
- } while (!_stub->_pi.enter);
- _stub->_pi.enter = false;
- }
+void Game::drawBitmapSequenceDemo(int num) {
+ static const char *bitmapList[] = { "..\\wgp\\title.bmp", "..\\wgp\\title1.bmp", "..\\wgp\\title2.bmp" };
+ loadWGP(bitmapList[num]);
+ _stub->setPalette(_bitmapBuffer0 + kOffsetBitmapPalette, 256);
+ _stub->copyRect(0, 0, kGameScreenWidth, kGameScreenHeight, _bitmapBuffer1.bits, _bitmapBuffer1.pitch);
}
void Game::stopMusic() {
View
@@ -188,6 +188,14 @@ enum {
};
enum {
+ kStateInit,
+ kStateGame,
+ kStateBag,
+ kStateDialogue,
+ kStateBitmapSequence,
+};
+
+enum {
kCycleDelay = 50,
kGameScreenWidth = 640,
kGameScreenHeight = 480,
@@ -277,11 +285,15 @@ struct Game {
void redrawDialogueSprite(int num);
void redrawDialogueBackground();
void redrawDialogueTexts();
+ void initDialogue();
+ void finiDialogue();
void handleDialogue();
// game.cpp
void detectVersion();
void restart();
+ void init();
+ void fini();
void mainLoop();
void updateMouseButtonsPressed();
void updateKeysPressedTable();
@@ -302,7 +314,7 @@ struct Game {
void redrawObjectBoxes(int previousObject, int currentObject);
void redrawObjects();
void playVideo(const char *name);
- void playBitmapSequenceDemo();
+ void drawBitmapSequenceDemo(int num);
void stopMusic();
void playMusic(const char *name);
void changeObjectMotionFrame(int object, int object2, int useObject2, int count1, int count2, int useDx, int dx, int useDy, int dy);
@@ -424,6 +436,7 @@ struct Game {
int win16_sndPlaySound(int op, void *data = 0);
void win16_stretchBits(SceneBitmap *bits, int srcHeight, int srcWidth, int srcY, int srcX, int dstHeight, int dstWidth, int dstY, int dstX);
+ int _nextState, _state;
bool _isDemo;
const char *_startupScene;
FileSystem _fs;
@@ -437,6 +450,7 @@ struct Game {
int _mixerSoundId;
int _mixerMusicId;
uint32_t _lastFrameTimeStamp;
+ int _bitmapSequence;
uint8_t *_bitmapBuffer0;
SceneBitmap _bitmapBuffer1;
View
@@ -26,14 +26,6 @@ static bool parseOption(const char *arg, const char *longCmd, const char **opt)
return handled;
}
-static void exitMain() {
- if (g_stub) {
- g_stub->destroy();
- delete g_stub;
- g_stub = 0;
- }
-}
-
#undef main
int main(int argc, char *argv[]) {
const char *dataPath = "DATA";
@@ -53,9 +45,15 @@ int main(int argc, char *argv[]) {
}
g_debugMask = DBG_INFO; // | DBG_GAME | DBG_OPCODES | DBG_DIALOGUE;
g_stub = SystemStub_SDL_create();
- atexit(exitMain);
Game *g = new Game(g_stub, dataPath, savePath, musicPath);
- g->mainLoop();
+ g->init();
+ while (!g_stub->_quit) {
+ g->mainLoop();
+ }
+ g->fini();
delete g;
+ g_stub->destroy();
+ delete g_stub;
+ g_stub = 0;
return 0;
}
View
@@ -43,7 +43,9 @@ struct SystemStub_SDL : SystemStub {
_gameBuffer(0), _videoBuffer(0) {
_mixer = Mixer_SDL_create(this);
}
- virtual ~SystemStub_SDL() {}
+ virtual ~SystemStub_SDL() {
+ delete _mixer;
+ }
virtual void init(const char *title, int w, int h);
virtual void destroy();

0 comments on commit fad0388

Please sign in to comment.