[Scummvm-cvs-logs] CVS: scummvm/sword2 function.cpp,1.65,1.66

Torbjörn Andersson eriktorbjorn at users.sourceforge.net
Sat Sep 4 14:18:02 CEST 2004


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

Modified Files:
	function.cpp 
Log Message:
Rewrote the code that loads credits.clu into memory. The new code is
perhaps less clever than the old one I wrote, but should be much easier to
read. Besides, the old code had a small memory leak in it.


Index: function.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sword2/function.cpp,v
retrieving revision 1.65
retrieving revision 1.66
diff -u -d -r1.65 -r1.66
--- function.cpp	4 Sep 2004 09:27:16 -0000	1.65
+++ function.cpp	4 Sep 2004 21:17:27 -0000	1.66
@@ -469,8 +469,6 @@
 		creditsLines[i].sprite = NULL;
 	}
 
-	char textLine[80];
-
 	if (!f.open("credits.clu")) {
 		warning("Can't find credits.clu");
 		return IR_CONT;
@@ -478,87 +476,106 @@
 
 	int lineTop = 400;
 	int lineCount = 0;
-	int pos = 0;
-
-	textLine[0] = 0;
-
 	int paragraphStart = 0;
 	bool hasCenterMark = false;
 
 	while (1) {
+		char buffer[80];
+		char *line = buffer;
+		char *center_mark = NULL;
+
 		if (lineCount >= ARRAYSIZE(creditsLines)) {
 			warning("Too many credits lines");
 			break;
 		}
 
-		byte b = f.readByte();
+		int pos = 0;
 
-		if (f.ioFailed())
-			break;
+		while (1) {
+			byte b = f.readByte();
 
-		// Remember that the current paragraph has at least once center
-		// mark. If a paragraph has no center marks, it should be
-		// centered.
+			if (f.ioFailed()) {
+				if (pos != 0)
+					line[pos] = 0;
+				else
+					line = NULL;
+				break;
+			}
 
-		if (b == '^')
-			hasCenterMark = true;
+			if (b == 0x0d) {
+				f.readByte();
+				line[pos] = 0;
+				pos = 0;
+				break;
+			}
 
-		if (b == '^' && pos != 0) {
-			textLine[pos] = 0;
+			if (pos < ARRAYSIZE(buffer)) {
+				if (b == '^')
+					center_mark = line + pos;
+				line[pos++] = b;
+			}
+		}
 
-			creditsLines[lineCount].top = lineTop;
-			creditsLines[lineCount].height = CREDITS_FONT_HEIGHT;
-			creditsLines[lineCount].type = LINE_LEFT;
-			creditsLines[lineCount].str = strdup(textLine);
+		if (!line || strlen(line) == 0) {
+			if (!hasCenterMark) {
+				for (i = paragraphStart; i < lineCount; i++)
+					creditsLines[i].type = LINE_CENTER;
+			}
+			paragraphStart = lineCount;
+			hasCenterMark = false;
+			if (!line)
+				break;
+			if (paragraphStart == lineCount)
+				lineTop += CREDITS_LINE_SPACING;
+			continue;
+		}
 
-			lineCount++;
-			textLine[0] = '^';
-			pos = 1;
-		} else if (b == 0x0a) {
-			creditsLines[lineCount].top = lineTop;
+		if (center_mark) {
+			// The current paragraph has at least one center mark.
+			hasCenterMark = true;
 
-			if (textLine[0] == '^') {
-				creditsLines[lineCount].str = strdup(textLine + 1);
-				creditsLines[lineCount].type = LINE_RIGHT;
-			} else {
-				creditsLines[lineCount].str = strdup(textLine);
-				creditsLines[lineCount].type = LINE_LEFT;
-			}
+			if (center_mark != line) {
+				// The center mark is somewhere inside the
+				// line. Split it into left and right side.
+				*center_mark = 0;
 
-			if (strcmp(textLine, "@") == 0) {
-				creditsLines[lineCount].height = logoHeight;
-				lineTop += logoHeight;
-			} else {
+				creditsLines[lineCount].top = lineTop;
 				creditsLines[lineCount].height = CREDITS_FONT_HEIGHT;
-				lineTop += CREDITS_LINE_SPACING;
-			}
+				creditsLines[lineCount].type = LINE_LEFT;
+				creditsLines[lineCount].str = strdup(line);
 
-			if (strlen(textLine) > 0)
+				*center_mark = '^';
+				line = center_mark;
 				lineCount++;
-			else {
-				if (!hasCenterMark)
-					for (int j = paragraphStart; j < lineCount; j++)
-						creditsLines[j].type = LINE_CENTER;
 
-				paragraphStart = lineCount;
-				hasCenterMark = false;
+				if (lineCount >= ARRAYSIZE(creditsLines)) {
+					warning("Too many credits lines");
+					break;
+				}
 			}
+		}
 
-			pos = 0;
-		} else if (b == 0x0d) {
-			textLine[pos++] = 0;
+		creditsLines[lineCount].top = lineTop;
+
+		if (*line == '^') {
+			creditsLines[lineCount].type = LINE_RIGHT;
+			line++;
 		} else
-			textLine[pos++] = b;
-	}
+			creditsLines[lineCount].type = LINE_LEFT;
 
-	f.close();
+		if (strcmp(line, "@") == 0) {
+			creditsLines[lineCount].height = logoHeight;
+			lineTop += logoHeight;
+		} else {
+			creditsLines[lineCount].height = CREDITS_FONT_HEIGHT;
+			lineTop += CREDITS_LINE_SPACING;
+		}
 
-	// The paragraph detection above won't find the last paragraph, so we
-	// have to deal with it separately.
+		creditsLines[lineCount].str = strdup(line);
+		lineCount++;
+	}
 
-	if (!hasCenterMark)
-		for (int j = paragraphStart; j < lineCount; j++)
-			creditsLines[j].type = LINE_CENTER;
+	f.close();
 
 	// We could easily add some ScummVM stuff to the credits, if we wanted
 	// to. On the other hand, anyone with the attention span to actually





More information about the Scummvm-git-logs mailing list