[Scummvm-cvs-logs] CVS: scummvm/gob game.cpp,1.15,1.16 util.cpp,1.9,1.10

Eugene Sandulenko sev at users.sourceforge.net
Wed Apr 13 13:54:54 CEST 2005


Update of /cvsroot/scummvm/scummvm/gob
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20680

Modified Files:
	game.cpp util.cpp 
Log Message:
Patch from wjp. Fix keyboard input.


Index: game.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gob/game.cpp,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- game.cpp	12 Apr 2005 23:45:14 -0000	1.15
+++ game.cpp	13 Apr 2005 20:53:36 -0000	1.16
@@ -463,6 +463,7 @@
 
 	if (*pButtons == 3)
 		*pButtons = 0;
+
 	return util_checkKey();
 }
 
@@ -520,18 +521,28 @@
 				draw_blitInvalidated();
 		}
 
-		key = game_checkKeys(&inter_mouseX, &inter_mouseY, &game_mouseButtons, handleMouse);
+		// NOTE: the original asm does the below game_checkKeys call
+		// _before_ this check. However, that can cause keypresses to get lost
+		// since there's a return statement in this check.
+		// Additionally, I added a 'deltaTime == -1' check there, since
+		// when this function is called with deltaTime == -1 in game_inputArea,
+		// and the return value is then discarded.
 		if (deltaTime < 0) {
-			if (util_getTimeKey() + deltaTime > timeKey) {
+			uint32 curtime = util_getTimeKey();
+			if (deltaTime == -1 || curtime + deltaTime > timeKey) {
 				if (pResId != 0)
 					*pResId = 0;
-
+				
 				if (pResIndex != 0)
 					*pResIndex = 0;
+				
 				return 0;
 			}
 		}
 
+		key = game_checkKeys(&inter_mouseX, &inter_mouseY,
+							 &game_mouseButtons, handleMouse);
+
 		if (handleMouse == 0 && game_mouseButtons != 0) {
 			util_waitMouseRelease(0);
 			key = 3;
@@ -892,11 +903,15 @@
 					util_cutFromStr(str, strlen(str) - 1,
 					    1);
 
+				if (key >= 'a' && key <= 'z')
+					key += ('A' - 'a');
+
 				pos++;
 				game_tempStr[0] = key;
 				game_tempStr[1] = 0;
 
 				util_insertStr(game_tempStr, str, pos - 1);
+
 				//strupr(str);
 			}
 		}

Index: util.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gob/util.cpp,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- util.cpp	9 Apr 2005 19:32:29 -0000	1.9
+++ util.cpp	13 Apr 2005 20:53:36 -0000	1.10
@@ -28,17 +28,44 @@
 
 namespace Gob {
 
-static int16 _mouseX, _mouseY, _keyPressed, _mouseButtons;
+static const int kKeyBufSize = 16;
+
+static int16 _mouseX, _mouseY, _mouseButtons;
+static int16 _keyBuffer[kKeyBufSize], _keyBufferHead, _keyBufferTail;
+
+static void addKeyToBuffer(int16 key) {
+	if ((_keyBufferHead + 1) % kKeyBufSize == _keyBufferTail) {
+		warning("key buffer overflow!");
+		return;
+	}
+
+	_keyBuffer[_keyBufferHead] = key;
+	_keyBufferHead = (_keyBufferHead + 1) % kKeyBufSize;
+}
+
+static bool keyBufferEmpty() {
+	return (_keyBufferHead == _keyBufferTail);
+}
+
+static bool getKeyFromBuffer(int16& key) {
+	if (_keyBufferHead == _keyBufferTail) return false;
+
+	key = _keyBuffer[_keyBufferTail];
+	_keyBufferTail = (_keyBufferTail + 1) % kKeyBufSize;
+
+	return true;
+}
+
 
 void util_initInput(void) {
-	_mouseX = _mouseY = _keyPressed = _mouseButtons = 0;
+	_mouseX = _mouseY = _mouseButtons = 0;
+	_keyBufferHead = _keyBufferTail = 0;
 }
 
 void util_waitKey(void) {
-	while (_keyPressed) {
-		util_processInput();
-		g_system->delayMillis(10);
-	}
+	// FIXME: wrong function name? This functions clears the keyboard buffer.
+	util_processInput();
+	_keyBufferHead = _keyBufferTail = 0;
 }
 
 int16 util_translateKey(int16 key) {
@@ -71,26 +98,29 @@
 		if (key == keys[i].from)
 			return keys[i].to;
 
+	if (key < 32 || key >= 128)
+		return 0;
+
 	return key;
 }
 
 int16 util_getKey(void) {
-	while (!_keyPressed) {
-		util_processInput();
+	int16 key;
 
-		if (_keyPressed)
-			break;
+	while (!getKeyFromBuffer(key)) {
+		util_processInput();
 
-		g_system->delayMillis(10);
+		if (keyBufferEmpty())
+			g_system->delayMillis(10);
 	}
-	return util_translateKey(_keyPressed);
+	return util_translateKey(key);
 }
 
 int16 util_checkKey(void) {
-	int key = _keyPressed;
+	int16 key;
 
-	if (_keyPressed)
-		_keyPressed = 0;
+	if (!getKeyFromBuffer(key))
+		key = 0;
 
 	return util_translateKey(key);
 }
@@ -120,10 +150,9 @@
 			_mouseButtons &= ~2;
 			break;
 		case OSystem::EVENT_KEYDOWN:
-			_keyPressed = event.kbd.keycode;
+			addKeyToBuffer(event.kbd.keycode);
 			break;
 		case OSystem::EVENT_KEYUP:
-			_keyPressed = 0;
 			break;
 		case OSystem::EVENT_QUIT:
 			g_system->quit();





More information about the Scummvm-git-logs mailing list