- Declared all stack functions as inline

- Sleep some ms after 500 opcodes to reduce CPU load
- Fixed odd bug in LGoP2 where text disappeared quickly without waiting for user input by returning 0x38 in sfShowPage

svn-id: r34746
This commit is contained in:
Benjamin Haisch 2008-10-04 21:40:14 +00:00
parent b41cd58cee
commit 99f8add65c
2 changed files with 28 additions and 13 deletions

View file

@ -30,6 +30,7 @@
#include "made/script.h" #include "made/script.h"
#include "made/database.h" #include "made/database.h"
#include "made/scriptfuncs.h" #include "made/scriptfuncs.h"
#include "made/screen.h"
namespace Made { namespace Made {
@ -44,47 +45,47 @@ ScriptStack::ScriptStack() {
ScriptStack::~ScriptStack() { ScriptStack::~ScriptStack() {
} }
int16 ScriptStack::top() { inline int16 ScriptStack::top() {
return _stack[_stackPos]; return _stack[_stackPos];
} }
int16 ScriptStack::pop() { inline int16 ScriptStack::pop() {
if (_stackPos == kScriptStackSize) if (_stackPos == kScriptStackSize)
error("ScriptStack::pop() Stack underflow"); error("ScriptStack::pop() Stack underflow");
return _stack[_stackPos++]; return _stack[_stackPos++];
} }
void ScriptStack::push(int16 value) { inline void ScriptStack::push(int16 value) {
if (_stackPos == 0) if (_stackPos == 0)
error("ScriptStack::push() Stack overflow"); error("ScriptStack::push() Stack overflow");
_stack[--_stackPos] = value; _stack[--_stackPos] = value;
} }
void ScriptStack::setTop(int16 value) { inline void ScriptStack::setTop(int16 value) {
_stack[_stackPos] = value; _stack[_stackPos] = value;
} }
int16 ScriptStack::peek(int16 index) { inline int16 ScriptStack::peek(int16 index) {
return _stack[index]; return _stack[index];
} }
void ScriptStack::poke(int16 index, int16 value) { inline void ScriptStack::poke(int16 index, int16 value) {
_stack[index] = value; _stack[index] = value;
} }
void ScriptStack::alloc(int16 count) { inline void ScriptStack::alloc(int16 count) {
_stackPos -= count; _stackPos -= count;
} }
void ScriptStack::free(int16 count) { inline void ScriptStack::free(int16 count) {
_stackPos += count; _stackPos += count;
} }
void ScriptStack::setStackPos(int16 stackPtr) { inline void ScriptStack::setStackPos(int16 stackPtr) {
_stackPos = stackPtr; _stackPos = stackPtr;
} }
int16 *ScriptStack::getStackPtr() { inline int16 *ScriptStack::getStackPtr() {
return &_stack[_stackPos]; return &_stack[_stackPos];
} }
@ -187,6 +188,9 @@ ScriptInterpreter::~ScriptInterpreter() {
} }
void ScriptInterpreter::runScript(int16 scriptObjectIndex) { void ScriptInterpreter::runScript(int16 scriptObjectIndex) {
uint32 opcodeSleepCounter = 0;
_vm->_quit = false; _vm->_quit = false;
_runningScriptObjectIndex = scriptObjectIndex; _runningScriptObjectIndex = scriptObjectIndex;
@ -194,19 +198,27 @@ void ScriptInterpreter::runScript(int16 scriptObjectIndex) {
_codeBase = _vm->_dat->getObject(_runningScriptObjectIndex)->getData(); _codeBase = _vm->_dat->getObject(_runningScriptObjectIndex)->getData();
_codeIp = _codeBase; _codeIp = _codeBase;
while (!_vm->_quit) { while (!_vm->_quit) {
_vm->handleEvents(); _vm->handleEvents();
byte opcode = readByte(); byte opcode = readByte();
if (opcode >= 1 && opcode <= _commandsMax) { if (opcode >= 1 && opcode <= _commandsMax) {
debug(4, "[%04X:%04X] %s", _runningScriptObjectIndex, (uint) (_codeIp - _codeBase), _commands[opcode - 1].desc); debug(4, "[%04X:%04X] %s", _runningScriptObjectIndex, (uint) (_codeIp - _codeBase), _commands[opcode - 1].desc);
(this->*_commands[opcode - 1].proc)(); (this->*_commands[opcode - 1].proc)();
} else { } else {
warning("ScriptInterpreter::runScript(%d) Unknown opcode %02X", _runningScriptObjectIndex, opcode); warning("ScriptInterpreter::runScript(%d) Unknown opcode %02X", _runningScriptObjectIndex, opcode);
} }
/* We sleep a little after 500 opcodes to reduce the CPU load.
*/
if (++opcodeSleepCounter > 500) {
_vm->_screen->updateScreenAndWait(5);
opcodeSleepCounter = 0;
}
} }
} }

View file

@ -195,7 +195,10 @@ int16 ScriptFunctions::sfClearScreen(int16 argc, int16 *argv) {
int16 ScriptFunctions::sfShowPage(int16 argc, int16 *argv) { int16 ScriptFunctions::sfShowPage(int16 argc, int16 *argv) {
_vm->_screen->show(); _vm->_screen->show();
return 0; // NOTE: We need to return something != 0 here or some game scripts won't
// work correctly. The actual meaning of this value is unknown to me.
// 0x38 was found out by analyzing debug output of the original engine.
return 0x38;
} }
int16 ScriptFunctions::sfPollEvent(int16 argc, int16 *argv) { int16 ScriptFunctions::sfPollEvent(int16 argc, int16 *argv) {