Patch to allow Jigzo have all media files already uncompressed in the
puzzles directories, so that it is easier to make and test new puzzles,
and also reduces the amount of data that needs to be extracted and copied
to /tmp/

The changes are backwards compatible. All the puzzles you might have are
still supported.

Copyright (C) 2008, Miriam Ruiz <little_miry@yahoo.es>

This patch 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.

Index: jigzo-0.6.1/src/Sprite.hxx
===================================================================
--- jigzo-0.6.1.orig/src/Sprite.hxx	2008-12-12 10:41:28.000000000 +0100
+++ jigzo-0.6.1/src/Sprite.hxx	2008-12-12 10:41:34.000000000 +0100
@@ -35,7 +35,7 @@
 	{
 		return Rect(pos.x,pos.y,map->rect.w,map->rect.h);
 	}
-	Sprite()
+	Sprite() : map(NULL)
 	{
 		ownmap = false;
 	}
Index: jigzo-0.6.1/src/main.cxx
===================================================================
--- jigzo-0.6.1.orig/src/main.cxx	2008-12-12 10:41:28.000000000 +0100
+++ jigzo-0.6.1/src/main.cxx	2008-12-12 11:59:35.000000000 +0100
@@ -46,6 +46,12 @@
 int glw = 0;
 int glh = 0;
 
+#ifndef PUZZLES_FOLDER
+#define PUZZLES_FOLDER "/usr/share/jigzo/puzzles/"
+#endif
+
+static const char *puzzles_folder = PUZZLES_FOLDER;
+
 bool rotateEnabled = false;
 bool soundEnabled = false;
 bool soundFailed = false;
@@ -232,7 +238,7 @@
 class FolderSprite:public Sprite
 {
 public:
-	int id;
+	unsigned int id;
 	
 	FolderSprite(int i):id(i)
 	{
@@ -368,8 +374,8 @@
 };
 
 vector<PreviewFolder*> previewfolders;
-int currentPreviewFolder = 0;
-int nextPreviewFolder = 0;
+unsigned int currentPreviewFolder = 0;
+unsigned int nextPreviewFolder = 0;
 
 void FreeImages(void)
 {
@@ -518,7 +524,25 @@
 			f.getline(tmp, 256, '\n'); // filename
 			if (!f.eof())
 			{
-				string filename(tmpdir.Path() + tmp);
+				#ifdef WIN32
+				string pzlpath = pzl.substr( 0, pzl.rfind("\\")+1 );
+				#else
+				string pzlpath = pzl.substr( 0, pzl.rfind("/")+1 );
+				#endif
+
+				bool extract = true;
+				string filename = pzlpath + tmp;
+				struct stat stat_buffer;
+				if (stat(filename.c_str(), &stat_buffer) == 0)
+				{
+					extract = false;
+				}
+				else
+				{
+					filename = tmpdir.Path() + tmp;
+					extract = true;
+				}
+
 				if (filename.substr(filename.length()-6)=="-t.jpg")
 					thumbJpg = filename;
 				else
@@ -534,19 +558,25 @@
 						throw "ERROR PARSING PUZZLE FILE";
 				}
 
-				std::ofstream of(filename.c_str(), std::ios::binary);
-				f.getline(tmp, 256); // length
-				string s(tmp);
-				stringstream ss(s);
-				ss >> chucklen;
-				int n = chucklen;
-				while (n)
+				if (extract)
 				{
-					int m = n;
-					if (m>4096) m = 4096;
-					f.read(tmp,m);
-					of.write(tmp,m);
-					n -= m;
+					std::ofstream of(filename.c_str(), std::ios::binary);
+					f.getline(tmp, 256); // length
+					chucklen = atoi(tmp);
+					int n = chucklen;
+					if (!n)
+					{
+						string imgfile( tmpdir.Path()  );
+						printf("%s\n", imgfile.c_str());
+					}
+					while (n)
+					{
+						int m = n;
+						if (m>4096) m = 4096;
+						f.read(tmp,m);
+						of.write(tmp,m);
+						n -= m;
+					}
 				}
 			}
 		}
@@ -669,23 +699,23 @@
 	for (it = subdirs.begin(); it != subdirs.end(); it++)
 	{
 		RecursePreviewFolder(dirname + (*it) + "/");
-	}	
+	}
 }
 
 
 void ReadPreviewFolders(void)
 {
-	RecursePreviewFolder("/usr/share/jigzo/puzzles/");
+	RecursePreviewFolder(puzzles_folder);
 };
 
 void ReadPreviews(void)
 {
 	currentPreviewFolder = nextPreviewFolder;
 	ReadPreviewFolders();
-	
 	texture = new Texture(global_gl_texture);
 
-	previewfolders[currentPreviewFolder]->Read();
+	if (previewfolders.size() >= currentPreviewFolder)
+		previewfolders[currentPreviewFolder]->Read();
 
 }
 void PreviewFolder::Read(void)
@@ -1491,7 +1521,8 @@
 	printf("  -h                   show this usage info\n");
 	printf("  -f                   fullscreen mode\n");
 	printf("  -w                   window mode (default)\n");
-	printf("  -s <width>x<height>  screen size (default: " SCREEN_WIDTH "x" SCREEN_HEIGHT ")\n");
+	printf("  -s <width>x<height>  screen size (default: %dx%d)\n", SCREEN_WIDTH, SCREEN_HEIGHT);
+	printf("  -d <directory>       puzzles folder (default: \"" PUZZLES_FOLDER "\"\n");
 	printf("\n");
 	printf
 	("jigzo requires a OpenGL hardware accelaration\n\n");
@@ -1549,6 +1580,13 @@
 						usage();
 					continue;
 				}
+				if (string(argv[i]) == "-d") {
+					i++;
+					if (i >= argc)
+						usage();
+					puzzles_folder = argv[i];
+					continue;
+				}
 			}
 		}
 

