[Scummvm-cvs-logs] SF.net SVN: scummvm:[33957] residual/trunk

aquadran at users.sourceforge.net aquadran at users.sourceforge.net
Sun Aug 17 09:08:50 CEST 2008


Revision: 33957
          http://scummvm.svn.sourceforge.net/scummvm/?rev=33957&view=rev
Author:   aquadran
Date:     2008-08-17 07:08:50 +0000 (Sun, 17 Aug 2008)

Log Message:
-----------
moved few things in common utils code

Modified Paths:
--------------
    residual/trunk/common/debug.cpp
    residual/trunk/common/debug.h
    residual/trunk/common/module.mk
    residual/trunk/common/util.h
    residual/trunk/engine/imuse/imuse_sndmgr.cpp

Added Paths:
-----------
    residual/trunk/common/util.cpp

Modified: residual/trunk/common/debug.cpp
===================================================================
--- residual/trunk/common/debug.cpp	2008-08-17 05:36:41 UTC (rev 33956)
+++ residual/trunk/common/debug.cpp	2008-08-17 07:08:50 UTC (rev 33957)
@@ -29,66 +29,34 @@
 
 #include "engine/backend/platform/driver.h"
 
-Common::String tag2string(uint32 tag) {
-	char str[5];
-	str[0] = (char)(tag >> 24);
-	str[1] = (char)(tag >> 16);
-	str[2] = (char)(tag >> 8);
-	str[3] = (char)tag;
-	str[4] = '\0';
-	return Common::String(str);
-}
+#ifdef __PLAYSTATION2__
+	// for those replaced fopen/fread/etc functions
+	typedef unsigned long	uint64;
+	typedef signed long	int64;
+	#include "engine/backend/platform/ps2/fileio.h"
 
-void hexdump(const byte *data, int len, int bytesPerLine) {
-	assert(1 <= bytesPerLine && bytesPerLine <= 32);
-	int i;
-	byte c;
-	int offset = 0;
+	#define fprintf				ps2_fprintf
+	#define fflush(a)			ps2_fflush(a)
+#endif
 
-	while (len >= bytesPerLine) {
-		printf("%06x: ", offset);
-		for (i = 0; i < bytesPerLine; i++) {
-			printf("%02x ", data[i]);
-			if (i % 4 == 3)
-				printf(" ");
-		}
-		printf(" |");
-		for (i = 0; i < bytesPerLine; i++) {
-			c = data[i];
-			if (c < 32 || c >= 127)
-				c = '.';
-			printf("%c", c);
-		}
-		printf("|\n");
-		data += bytesPerLine;
-		len -= bytesPerLine;
-		offset += bytesPerLine;
-	}
+#ifdef __DS__
+	#include "engine/backend/fs/ds/ds-fs.h"
 
-	if (len <= 0)
-		return;
+	#undef stderr
+	#undef stdout
+	#undef stdin
 
-	printf("%06x: ", offset);
-	for (i = 0; i < bytesPerLine; i++) {
-		if (i < len)
-			printf("%02x ", data[i]);
-		else
-			printf("   ");
-		if (i % 4 == 3)
-			printf(" ");
-	}
-	printf(" |");
-	for (i = 0; i < len; i++) {
-		c = data[i];
-		if (c < 32 || c >= 127)
-			c = '.';
-		printf("%c", c);
-	}
-	for (; i < bytesPerLine; i++)
-		printf(" ");
-	printf("|\n");
-}
+	#define stdout ((DS::fileHandle*) -1)
+	#define stderr ((DS::fileHandle*) -2)
+	#define stdin ((DS::fileHandle*) -3)
 
+	void	std_fprintf(FILE* handle, const char* fmt, ...);
+	void	std_fflush(FILE* handle);
+
+	#define fprintf(file, fmt, ...)				{ char str[128]; sprintf(str, fmt, ##__VA_ARGS__); DS::std_fwrite(str, strlen(str), 1, file); }
+	#define fflush(file)						DS::std_fflush(file)
+#endif
+
 static void debugHelper(const char *in_buf, bool caret = true) {
 	char buf[STRINGBUFLEN];
 

Modified: residual/trunk/common/debug.h
===================================================================
--- residual/trunk/common/debug.h	2008-08-17 05:36:41 UTC (rev 33956)
+++ residual/trunk/common/debug.h	2008-08-17 07:08:50 UTC (rev 33957)
@@ -58,8 +58,4 @@
 void CDECL debug(int level, const char *s, ...);
 void CDECL debug(const char *s, ...);
 
-void hexdump(const byte * data, int len, int bytesPerLine);
-
-Common::String tag2string(uint32 tag);
-
 #endif

Modified: residual/trunk/common/module.mk
===================================================================
--- residual/trunk/common/module.mk	2008-08-17 05:36:41 UTC (rev 33956)
+++ residual/trunk/common/module.mk	2008-08-17 07:08:50 UTC (rev 33957)
@@ -10,9 +10,10 @@
 	matrix3.o \
 	matrix4.o \
 	memorypool.o \
+	mutex.o
 	str.o \
 	stream.o \
-	mutex.o
+	utio.o
 
 # Include common rules
 include $(srcdir)/rules.mk

Added: residual/trunk/common/util.cpp
===================================================================
--- residual/trunk/common/util.cpp	                        (rev 0)
+++ residual/trunk/common/util.cpp	2008-08-17 07:08:50 UTC (rev 33957)
@@ -0,0 +1,158 @@
+/* Residual - Virtual machine to run LucasArts' 3D adventure games
+ *
+ * Residual is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the AUTHORS
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ */
+
+#include "common/util.h"
+#include "engine/backend/platform/driver.h"
+
+#ifdef _WIN32_WCE
+// This is required for the debugger attachment
+extern bool isSmartphone(void);
+#endif
+
+#ifdef __PLAYSTATION2__
+	// for those replaced fopen/fread/etc functions
+	typedef unsigned long	uint64;
+	typedef signed long	int64;
+	#include "backends/platform/ps2/fileio.h"
+
+	#define fprintf				ps2_fprintf
+	#define fflush(a)			ps2_fflush(a)
+#endif
+
+#ifdef __DS__
+	#include "backends/fs/ds/ds-fs.h"
+
+	#undef stderr
+	#undef stdout
+	#undef stdin
+
+	#define stdout ((DS::fileHandle*) -1)
+	#define stderr ((DS::fileHandle*) -2)
+	#define stdin ((DS::fileHandle*) -3)
+
+	void	std_fprintf(FILE* handle, const char* fmt, ...);
+	void	std_fflush(FILE* handle);
+
+	#define fprintf(file, fmt, ...)				{ char str[128]; sprintf(str, fmt, ##__VA_ARGS__); DS::std_fwrite(str, strlen(str), 1, file); }
+	#define fflush(file)						DS::std_fflush(file)
+#endif
+
+
+namespace Common {
+
+//
+// Print hexdump of the data passed in
+//
+void hexdump(const byte * data, int len, int bytesPerLine) {
+	assert(1 <= bytesPerLine && bytesPerLine <= 32);
+	int i;
+	byte c;
+	int offset = 0;
+	while (len >= bytesPerLine) {
+		printf("%06x: ", offset);
+		for (i = 0; i < bytesPerLine; i++) {
+			printf("%02x ", data[i]);
+			if (i % 4 == 3)
+				printf(" ");
+		}
+		printf(" |");
+		for (i = 0; i < bytesPerLine; i++) {
+			c = data[i];
+			if (c < 32 || c >= 127)
+				c = '.';
+			printf("%c", c);
+		}
+		printf("|\n");
+		data += bytesPerLine;
+		len -= bytesPerLine;
+		offset += bytesPerLine;
+	}
+
+	if (len <= 0)
+		return;
+
+	printf("%06x: ", offset);
+	for (i = 0; i < bytesPerLine; i++) {
+		if (i < len)
+			printf("%02x ", data[i]);
+		else
+			printf("   ");
+		if (i % 4 == 3)
+			printf(" ");
+	}
+	printf(" |");
+	for (i = 0; i < len; i++) {
+		c = data[i];
+		if (c < 32 || c >= 127)
+			c = '.';
+		printf("%c", c);
+	}
+	for (; i < bytesPerLine; i++)
+		printf(" ");
+	printf("|\n");
+}
+
+#pragma mark -
+
+
+RandomSource::RandomSource() {
+	// Use system time as RNG seed. Normally not a good idea, if you are using
+	// a RNG for security purposes, but good enough for our purposes.
+	assert(g_driver);
+	uint32 seed = g_driver->getMillis();
+	setSeed(seed);
+}
+
+void RandomSource::setSeed(uint32 seed) {
+	_randSeed = seed;
+}
+
+uint RandomSource::getRandomNumber(uint max) {
+	_randSeed = 0xDEADBF03 * (_randSeed + 1);
+	_randSeed = (_randSeed >> 13) | (_randSeed << 19);
+	return _randSeed % (max + 1);
+}
+
+uint RandomSource::getRandomBit(void) {
+	_randSeed = 0xDEADBF03 * (_randSeed + 1);
+	_randSeed = (_randSeed >> 13) | (_randSeed << 19);
+	return _randSeed & 1;
+}
+
+uint RandomSource::getRandomNumberRng(uint min, uint max) {
+	return getRandomNumber(max - min) + min;
+}
+
+Common::String tag2string(uint32 tag) {
+	char str[5];
+	str[0] = (char)(tag >> 24);
+	str[1] = (char)(tag >> 16);
+	str[2] = (char)(tag >> 8);
+	str[3] = (char)tag;
+	str[4] = '\0';
+	return Common::String(str);
+}
+
+}	// End of namespace Common
+


Property changes on: residual/trunk/common/util.cpp
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author URL Id
Added: svn:eol-style
   + native

Modified: residual/trunk/common/util.h
===================================================================
--- residual/trunk/common/util.h	2008-08-17 05:36:41 UTC (rev 33956)
+++ residual/trunk/common/util.h	2008-08-17 07:08:50 UTC (rev 33957)
@@ -61,4 +61,56 @@
 #define round(x) ((x > 0.0) ? floor((x) + 0.5) : ceil((x) - 0.5))
 #endif
 
+namespace Common {
+
+/**
+ * Print a hexdump of the data passed in. The number of bytes per line is
+ * customizable.
+ * @param data	the data to be dumped
+ * @param len	the lenght of that data
+ * @param bytesPerLine	number of bytes to print per line (default: 16)
+ */
+extern void hexdump(const byte *data, int len, int bytesPerLine = 16);
+
+/**
+ * Simple random number generator. Although it is definitely not suitable for
+ * cryptographic purposes, it serves our purposes just fine.
+ */
+class RandomSource {
+private:
+	uint32 _randSeed;
+
+public:
+	RandomSource();
+	void setSeed(uint32 seed);
+
+	uint32 getSeed() {
+		return _randSeed;
+	}
+
+	/**
+	 * Generates a random unsigned integer in the interval [0, max].
+	 * @param max	the upper bound
+	 * @return	a random number in the interval [0, max].
+	 */
+	uint getRandomNumber(uint max);
+	/**
+	 * Generates a random unsigned integer in the interval [0, 1].
+	 * Identical to getRandomNumber(1), but faster, hopefully.
+	 * @return	a random number in the interval [0, max].
+	 */
+	uint getRandomBit(void);
+	/**
+	 * Generates a random unsigned integer in the interval [min, max].
+	 * @param min	the lower bound
+	 * @param max	the upper bound
+	 * @return	a random number in the interval [min, max].
+	 */
+	uint getRandomNumberRng(uint min, uint max);
+};
+
+Common::String tag2string(uint32 tag);
+
+}	// End of namespace Common
+
 #endif

Modified: residual/trunk/engine/imuse/imuse_sndmgr.cpp
===================================================================
--- residual/trunk/engine/imuse/imuse_sndmgr.cpp	2008-08-17 05:36:41 UTC (rev 33956)
+++ residual/trunk/engine/imuse/imuse_sndmgr.cpp	2008-08-17 07:08:50 UTC (rev 33957)
@@ -71,7 +71,7 @@
 			size = READ_BE_UINT32(ptr); ptr += size + 4;
 			break;
 		default:
-			error("ImuseSndMgr::countElements() Unknown MAP tag '%s'", tag2string(tag).c_str());
+			error("ImuseSndMgr::countElements() Unknown MAP tag '%s'", Common::tag2string(tag).c_str());
 		}
 	} while (tag != MKID_BE('DATA'));
 }
@@ -134,7 +134,7 @@
 				ptr += 4;
 				break;
 			default:
-				error("ImuseSndMgr::prepareSound(%s) Unknown MAP tag '%s'", sound->name, tag2string(tag).c_str());
+				error("ImuseSndMgr::prepareSound(%s) Unknown MAP tag '%s'", sound->name, Common::tag2string(tag).c_str());
 			}
 		} while (tag != MKID_BE('DATA'));
 		headerSize = ptr - s_ptr;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list