Browse Source

Sound patch from Ubuntux #1

Michał W. Urbańczyk 16 years ago
parent
commit
d625066449
6 changed files with 185 additions and 62 deletions
  1. 114 0
      aclocal/m4/ax_boost_iostreams.m4
  2. 1 1
      client/Makefile.am
  3. 1 0
      configure.ac
  4. 2 2
      hch/CMusicHandler.cpp
  5. 56 51
      hch/CSndHandler.cpp
  6. 11 8
      hch/CSndHandler.h

+ 114 - 0
aclocal/m4/ax_boost_iostreams.m4

@@ -0,0 +1,114 @@
+# ===========================================================================
+#           http://autoconf-archive.cryp.to/ax_boost_iostreams.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_BOOST_IOSTREAMS
+#
+# DESCRIPTION
+#
+#   Test for IOStreams library from the Boost C++ libraries. The macro
+#   requires a preceding call to AX_BOOST_BASE. Further documentation is
+#   available at <http://randspringer.de/boost/index.html>.
+#
+#   This macro calls:
+#
+#     AC_SUBST(BOOST_IOSTREAMS_LIB)
+#
+#   And sets:
+#
+#     HAVE_BOOST_IOSTREAMS
+#
+# LAST MODIFICATION
+#
+#   2008-04-12
+#
+# COPYLEFT
+#
+#   Copyright (c) 2008 Thomas Porschberg <[email protected]>
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved.
+
+AC_DEFUN([AX_BOOST_IOSTREAMS],
+[
+	AC_ARG_WITH([boost-iostreams],
+	AS_HELP_STRING([--with-boost-iostreams@<:@=special-lib@:>@],
+                   [use the IOStreams library from boost - it is possible to specify a certain library for the linker
+                        e.g. --with-boost-iostreams=boost_iostreams-gcc-mt-d-1_33_1 ]),
+        [
+        if test "$withval" = "no"; then
+			want_boost="no"
+        elif test "$withval" = "yes"; then
+            want_boost="yes"
+            ax_boost_user_iostreams_lib=""
+        else
+		    want_boost="yes"
+        	ax_boost_user_iostreams_lib="$withval"
+		fi
+        ],
+        [want_boost="yes"]
+	)
+
+	if test "x$want_boost" = "xyes"; then
+        AC_REQUIRE([AC_PROG_CC])
+		CPPFLAGS_SAVED="$CPPFLAGS"
+		CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
+		export CPPFLAGS
+
+		LDFLAGS_SAVED="$LDFLAGS"
+		LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
+		export LDFLAGS
+
+        AC_CACHE_CHECK(whether the Boost::IOStreams library is available,
+					   ax_cv_boost_iostreams,
+        [AC_LANG_PUSH([C++])
+		 AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[@%:@include <boost/iostreams/filtering_stream.hpp>
+											 @%:@include <boost/range/iterator_range.hpp>
+											]],
+                                  [[std::string  input = "Hello World!";
+       								 namespace io = boost::iostreams;
+									 io::filtering_istream  in(boost::make_iterator_range(input));
+									 return 0;
+                                   ]]),
+                             ax_cv_boost_iostreams=yes, ax_cv_boost_iostreams=no)
+         AC_LANG_POP([C++])
+		])
+		if test "x$ax_cv_boost_iostreams" = "xyes"; then
+			AC_DEFINE(HAVE_BOOST_IOSTREAMS,,[define if the Boost::IOStreams library is available])
+            BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
+            if test "x$ax_boost_user_iostreams_lib" = "x"; then
+                for libextension in `ls $BOOSTLIBDIR/libboost_iostreams*.{so,a}* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_iostreams.*\)\.so.*$;\1;' -e 's;^lib\(boost_iostreams.*\)\.a*$;\1;'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_IOSTREAMS_LIB="-l$ax_lib"; AC_SUBST(BOOST_IOSTREAMS_LIB) link_iostreams="yes"; break],
+                                 [link_iostreams="no"])
+  				done
+                if test "x$link_iostreams" != "xyes"; then
+                for libextension in `ls $BOOSTLIBDIR/boost_iostreams*.{dll,a}* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_iostreams.*\)\.dll.*$;\1;' -e 's;^\(boost_iostreams.*\)\.a*$;\1;'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_IOSTREAMS_LIB="-l$ax_lib"; AC_SUBST(BOOST_IOSTREAMS_LIB) link_iostreams="yes"; break],
+                                 [link_iostreams="no"])
+  				done
+                fi
+
+            else
+               for ax_lib in $ax_boost_user_iostreams_lib boost_iostreams-$ax_boost_user_iostreams_lib; do
+				      AC_CHECK_LIB($ax_lib, main,
+                                   [BOOST_IOSTREAMS_LIB="-l$ax_lib"; AC_SUBST(BOOST_IOSTREAMS_LIB) link_iostreams="yes"; break],
+                                   [link_iostreams="no"])
+                  done
+
+            fi
+			if test "x$link_iostreams" != "xyes"; then
+				AC_MSG_ERROR(Could not link against $ax_lib !)
+			fi
+		fi
+
+		CPPFLAGS="$CPPFLAGS_SAVED"
+    	LDFLAGS="$LDFLAGS_SAVED"
+	fi
+])

+ 1 - 1
client/Makefile.am

@@ -1,6 +1,6 @@
 bin_PROGRAMS = vcmiclient
 
-vcmiclient_LDADD = $(top_builddir)/lib/libvcmi.la $(BOOST_SYSTEM_LIB) $(BOOST_FILESYSTEM_LIB) $(BOOST_THREAD_LIB)
+vcmiclient_LDADD = $(top_builddir)/lib/libvcmi.la $(BOOST_SYSTEM_LIB) $(BOOST_FILESYSTEM_LIB) $(BOOST_THREAD_LIB) $(BOOST_IOSTREAMS_LIB)
 vcmiclient_LDFLAGS = -L$(top_builddir)/lib
 vcmiclient_SOURCES =  \
 	../AI_Base.h \

+ 1 - 0
configure.ac

@@ -13,6 +13,7 @@ AX_BOOST_BASE([1.37])
 AX_BOOST_SYSTEM
 AX_BOOST_FILESYSTEM
 AX_BOOST_THREAD
+AX_BOOST_IOSTREAMS
 AC_PROG_CPP
 AC_ENABLE_SHARED
 AC_DISABLE_STATIC

+ 2 - 2
hch/CMusicHandler.cpp

@@ -89,12 +89,12 @@ void CMusicHandler::playLodSnd(std::string sndname)
 {
 	if(!sndh) return;
 	int size;
-	unsigned char *data;
+	const unsigned char *data;
 	SDL_RWops *ops;
 	Mix_Chunk *chunk;
 	int channel;
 
-	if ((data = sndh->extract(sndname, size)) == NULL)
+	if ((data = reinterpret_cast<const unsigned char*>(sndh->extract(sndname, size))) == NULL)
 		return;
 
 	ops = SDL_RWFromConstMem(data, size);

+ 56 - 51
hch/CSndHandler.cpp

@@ -15,74 +15,75 @@ CSndHandler::~CSndHandler()
 {
 	entries.clear();
 	fimap.clear();
-	file.close();
+	mfile.close();
 }
-CSndHandler::CSndHandler(std::string fname):CHUNK(65535)
+
+// Analyze the sound file. Half of this could go away if we were using
+// a simple structure. However, some post treatment would be necessary: file
+// size and offsets are little endian, and filename have a NUL in
+// them. */
+CSndHandler::CSndHandler(std::string fname)
 {
-	file.open(fname.c_str(),std::ios::binary);
-	if (!file.is_open())
+	mfile.open(fname);
+	if (!mfile.is_open())
 	{
 		tlog1 << "Cannot open " << fname << std::endl;
 		throw std::string("Cannot open ")+fname;
 	}
-	int nr = readNormalNr(0,4);
-	char tempc;
-	for (int i=0;i<nr;i++)
+
+	const unsigned char *data = (const unsigned char *)mfile.data();
+
+	unsigned int numFiles = readNormalNr(&data[0]);
+
+	for (unsigned int i=0; i<numFiles; i++)
 	{
 		Entry entry;
-		while(true)
-		{
-			file.read(&tempc,1);
-			if (tempc)
-				entry.name+=tempc;
-			else break;
+		const unsigned char *p;
+
+		// Read file name and extension
+		p = &data[4+48*i];
+
+		while(*p) {
+			entry.name += *p;
+			p++;
 		}
+
 		entry.name+='.';
-		while(true)
+		p++;
+
+		while(*p)
 		{
-			file.read(&tempc,1);
-			if (tempc)
-				entry.name+=tempc;
-			else break;
+			entry.name += *p;
+			p++;
 		}
-		file.seekg(40-entry.name.length()-1,std::ios_base::cur);
-		entry.offset = readNormalNr(-1,4);
-		entry.size = readNormalNr(-1,4);
+
+		// Read offset and size
+		p = &data[4+48*i+40];
+		entry.offset = readNormalNr(p);
+
+		p += 4;
+		entry.size = readNormalNr(p);
+
 		entries.push_back(entry);
 		fimap[entry.name] = i;
 	}
 }
-int CSndHandler::readNormalNr (int pos, int bytCon)
+
+// Reads a 4 byte integer. Format on file is little endian.
+unsigned int CSndHandler::readNormalNr (const unsigned char *p)
 {
-	if (pos>=0)
-		file.seekg(pos,std::ios_base::beg);
-	int ret=0;
-	int amp=1;
-	unsigned char zcz=0;
-	for (int i=0; i<bytCon; i++)
-	{
-		file.read((char*)(&zcz),1);
-		ret+=zcz*amp;
-		amp*=256;
-	}
-	return ret;
+	return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
 }
+
 void CSndHandler::extract(int index, std::string dstfile) //saves selected file
 {
 	std::ofstream out(dstfile.c_str(),std::ios_base::binary);
-	file.seekg(entries[index].offset,std::ios_base::beg);
-	int toRead=entries[index].size;
-	char * buffer = new char[std::min(CHUNK,entries[index].size)];
-	while (toRead>CHUNK)
-	{
-		file.read(buffer,CHUNK);
-		out.write(buffer,CHUNK);
-		toRead-=CHUNK;
-	}
-	file.read(buffer,toRead);
-	out.write(buffer,toRead);
+	const char *data = mfile.data();
+	
+	out.write(&data[entries[index].offset], entries[index].size);
 	out.close();
 }
+
 void CSndHandler::extract(std::string srcfile, std::string dstfile, bool caseSens) //saves selected file
 {
 	if (caseSens)
@@ -108,6 +109,9 @@ void CSndHandler::extract(std::string srcfile, std::string dstfile, bool caseSen
 		}
 	}
 }
+
+#if 0
+// unused and not sure what it's supposed to do
 MemberFile CSndHandler::getFile(std::string name)
 {
 	MemberFile ret;
@@ -129,16 +133,17 @@ MemberFile CSndHandler::getFile(std::string name)
 	}
 	return ret;
 }
-unsigned char * CSndHandler::extract (int index, int & size)
+#endif
+
+const char * CSndHandler::extract (int index, int & size)
 {
 	size = entries[index].size;
-	unsigned char * ret = new unsigned char[size];
-	file.seekg(entries[index].offset,std::ios_base::beg);
-	file.read((char*)ret,entries[index].size);
-	return ret;
+	const char *data = mfile.data();
+
+	return &data[entries[index].offset];
 }
 
-unsigned char * CSndHandler::extract (std::string srcName, int &size)
+const char * CSndHandler::extract (std::string srcName, int &size)
 {
 	int index;
 	std::map<std::string, int>::iterator fit;

+ 11 - 8
hch/CSndHandler.h

@@ -5,6 +5,8 @@
 #include <fstream>
 #include <map>
 
+#include <boost/iostreams/device/mapped_file.hpp>
+
 /*
  * CSndHandler.h, part of VCMI engine
  *
@@ -22,26 +24,27 @@ struct MemberFile
 };
 class CSndHandler
 {
-protected:
-	const int CHUNK;
+private:
 	struct Entry
 	{
 		std::string name;
-		int size, offset;
+		unsigned int size;
+		unsigned int offset;
 	};
-	std::ifstream file;
-	int readNormalNr (int pos, int bytCon);
-	bool opened;
+
+	inline unsigned int readNormalNr (const unsigned char *p);
+	boost::iostreams::mapped_file_source mfile;
+
 public:
 	std::vector<Entry> entries;
 	std::map<std::string, int> fimap; // map of wav file and index
 	~CSndHandler();
 	CSndHandler(std::string fname);
 	void extract(std::string srcfile, std::string dstfile, bool caseSens=true); //saves selected file
-	unsigned char * extract (std::string srcfile, int & size); //return selecte file data, NULL if file doesn't exist
+	const char * extract (std::string srcfile, int & size); //return selecte file data, NULL if file doesn't exist
 	void extract(int index, std::string dstfile); //saves selected file
 	MemberFile getFile(std::string name);//nie testowane - sprawdzic
-	unsigned char * extract (int index, int & size); //return selecte file - NIE TESTOWANE
+	const char * extract (int index, int & size); //return selecte file - NIE TESTOWANE
 };
 class CVidHandler
 {