[xiph-commits] r9727 - trunk/planarity

xiphmont at svn.xiph.org xiphmont at svn.xiph.org
Wed Aug 10 21:21:37 PDT 2005


Author: xiphmont
Date: 2005-08-10 21:21:34 -0700 (Wed, 10 Aug 2005)
New Revision: 9727

Modified:
   trunk/planarity/gamestate.c
   trunk/planarity/graph.c
   trunk/planarity/graph.h
   trunk/planarity/version.h
Log:
Begin addition of load/save code


Modified: trunk/planarity/gamestate.c
===================================================================
--- trunk/planarity/gamestate.c	2005-08-10 21:17:36 UTC (rev 9726)
+++ trunk/planarity/gamestate.c	2005-08-11 04:21:34 UTC (rev 9727)
@@ -1,5 +1,11 @@
+#define _GNU_SOURCE
 #include <math.h>
 #include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 #include "graph.h"
@@ -11,6 +17,8 @@
 #include "finish.h"
 #include "version.h"
 
+#define boardstate "/.gPlanarity/boards/"
+#define mainstate "/.gPlanarity/"
 Gameboard *gameboard;
 
 static int width=800;
@@ -20,13 +28,18 @@
 static int score=0;
 
 static int initial_intersections;
+static float intersection_mult=1.;
 static int objective=0;
 static int objective_lessthan=0;
+static float objective_mult=1.;
 static int paused=0;
 static time_t begin_time_add=0;
 static time_t begin_time;
 static char *version = "";
 
+static char *boarddir;
+static char *statedir;
+
 time_t get_elapsed(){
   if(paused)
     return begin_time_add;
@@ -267,9 +280,194 @@
   return version;
 }
 
+static int dir_create(char *name){
+  if(mkdir(name,0700)){
+    switch(errno){
+    case EEXIST:
+      // this is ok
+      return 0;
+    default:
+      fprintf(stderr,"ERROR:  Could not create directory (%s) to save game state:\n\t%s\n",
+	      name,strerror(errno));
+      return errno;
+    }
+  }
+  return 0;
+}     
+
+int write_board(char *basename){
+  vertex *v=get_verticies();
+  edge *e=get_edges();
+  char *name;
+  FILE *f;
+
+  name=alloca(strlen(boarddir)+strlen(basename)+3);
+  name[0]=0;
+  strcat(name,boarddir);
+  strcat(name,basename);
+  
+  f = fopen(name,"wb");
+  if(f==NULL){
+    fprintf(stderr,"ERROR:  Could not save board state for \"%s\":\n\t%s\n",
+	    get_level_string(),strerror(errno));
+    return errno;
+  }
+
+  while(v){
+    fprintf(f,"vertex %d %d %d %d %d\n",
+	    v->orig_x,v->orig_y,v->x,v->y,v->selected);
+    v=v->next;
+  }
+
+  while(e){
+    fprintf(f,"edge %d %d\n",e->A->num,e->B->num); 
+    e=e->next;
+  }
+
+  fprintf(f,"objective %c %d\n",
+	  (objective_lessthan?'*':'='),objective);
+  fprintf(f,"scoring %d %f %f %ld\n",
+	  initial_intersections,intersection_mult,objective_mult,(long)get_elapsed());
+  fprintf(f,"board %d %d\n",
+	  width,height);
+	  
+  //gameboard_write(f);
+
+  fclose(f);
+
+  strcat(name,".1");
+  f = fopen(name,"wb");
+  if(f==NULL){
+    fprintf(stderr,"ERROR:  Could not save board state for \"%s\":\n\t%s\n",
+	    get_level_string(),strerror(errno));
+    return errno;
+  }
+  //gameboard_write_icon(f);
+  
+  fclose(f);
+
+  return 0;
+}
+
+int read_board(char *basename){
+  FILE *f;
+  char *name;
+  char *line=NULL;
+  size_t n=0;
+
+  name=alloca(strlen(boarddir)+strlen(basename)+3);
+  name[0]=0;
+  strcat(name,boarddir);
+  strcat(name,basename);
+  
+  f = fopen(name,"rb");
+  if(f==NULL){
+    if(errno != ENOENT){
+      fprintf(stderr,"ERROR:  Could not read saved board state for \"%s\":\n\t%s\n",
+	      get_level_string(),strerror(errno));
+    }
+    return errno;
+  }
+  
+  // successful open 
+  // read the board
+  new_board(0);
+
+  {
+    int i,x,y,ox,oy,sel,count=0,A,B;
+    vertex **flat,*v;
+
+    // get all verticies first
+    while(getline(&line,&n,f)>0){
+      
+      {
+	if(sscanf(line,"vertex %d %d %d %d %d\n",
+		  &ox,&oy,&x,&y,&sel)==5){
+	  
+	  v=get_vertex();
+	  v->orig_x=ox;
+	  v->orig_y=oy;
+	  v->x=x;
+	  v->y=y;
+	  v->selected=sel;
+	  v->num=count++;
+	}
+      }
+    }
+    
+    rewind(f);
+    flat=alloca(count*sizeof(*flat));
+    v=get_verticies();
+    i=0;
+    while(v){
+      flat[i++]=v;
+      v=v->next;
+    }
+
+    // get edges and other next
+    while(getline(&line,&n,f)>0){
+      int o;
+      char c;
+      long l;
+
+      if(sscanf(line,"edge %d %d\n",&A,&B)==2){
+	if(A>=0 && A<count && B>=0 && B<count)
+	  add_edge(flat[A],flat[B]);
+	else
+	  fprintf(stderr,"WARNING: edge references out of range vertex in save file\n");
+	
+      }else if (sscanf(line,"objective %c %d\n",&c,&o)==2){
+	
+	objective_lessthan = (c=='*');
+	objective = o;
+      
+      }else if (sscanf(line,"scoring %d %f %f %ld\n",
+		       &initial_intersections,&intersection_mult,
+		       &objective_mult,&l)==4){
+	paused=1;
+	begin_time_add = l;
+	
+      }else 
+	sscanf(line,"board %d %d\n",&width,&height);	
+    }
+  }
+  
+
+  rewind(f);
+    
+  //read_gameboard(f);
+  fclose (f);
+  return 0;
+}
+
+
 int main(int argc, char *argv[]){
   GtkWidget *window;
+  char *homedir = getenv("home");
+  if(!homedir)
+    homedir = getenv("HOME");
+  if(!homedir)
+    homedir = getenv("homedir");
+  if(!homedir)
+    homedir = getenv("HOMEDIR");
+  if(!homedir){
+    fprintf(stderr,"No homedir environment variable set!  gPlanarity will be\n"
+	    "unable to permanently save any progress or board state.\n");
+    boarddir=NULL;
+    statedir=NULL;
+  }else{
+    boarddir=calloc(strlen(homedir)+strlen(boardstate)+1,1);
+    strcat(boarddir,homedir);
+    strcat(boarddir,boardstate);
 
+    statedir=calloc(strlen(homedir)+strlen(mainstate)+1,1);
+    strcat(statedir,homedir);
+    strcat(statedir,mainstate);
+
+    dir_create(statedir);
+    dir_create(boarddir);
+  }
+
   version=strstr(VERSION,"version.h");
   if(version){
     char *versionend=strchr(version,' ');

Modified: trunk/planarity/graph.c
===================================================================
--- trunk/planarity/graph.c	2005-08-10 21:17:36 UTC (rev 9726)
+++ trunk/planarity/graph.c	2005-08-11 04:21:34 UTC (rev 9727)
@@ -8,6 +8,7 @@
 
 /* mesh/board state */
 static vertex *verticies=0;
+static int     vertex_num=0;
 static vertex *vertex_pool=0;
 static edge *edges=0;
 static edge *edge_pool=0;
@@ -150,7 +151,6 @@
   ret->A=A;
   ret->B=B;
   ret->active=0;
-  ret->foreground=0;
   ret->i.next=0;
   ret->next=edges;
   edges=ret;
@@ -348,7 +348,7 @@
 }
 
 /*********************** vertex maint operations *************************/
-static vertex *get_vertex(){
+vertex *get_vertex(){
   vertex *ret;
   
   if(vertex_pool==0){
@@ -369,7 +369,11 @@
   ret->grabbed=0;
   ret->attached_to_grabbed=0;
   ret->edges=0;
-  ret->next=0;
+  ret->num=vertex_num++;
+
+  ret->next=verticies;
+  verticies=ret;
+
   return ret;
 }
 
@@ -389,13 +393,11 @@
     v=next;
   }
   verticies=0;
+  vertex_num=0;
   release_edges();
 
-  while(num--){
-    v=get_vertex();
-    v->next=verticies;
-    verticies=v;
-  }
+  while(num--)
+    get_vertex();
 }
 
 void activate_vertex(vertex *v){

Modified: trunk/planarity/graph.h
===================================================================
--- trunk/planarity/graph.h	2005-08-10 21:17:36 UTC (rev 9726)
+++ trunk/planarity/graph.h	2005-08-11 04:21:34 UTC (rev 9727)
@@ -6,6 +6,7 @@
 
 
 typedef struct vertex {
+  int num;
   int x;
   int y;
   int orig_x;
@@ -33,7 +34,6 @@
   vertex *B;
 
   int active;
-  int foreground;
 
   intersection i; // correct, not a pointer
   struct edge *next;
@@ -69,3 +69,4 @@
 extern void check_verticies();
 extern void impress_location();
 extern void commit_volatile_selection();
+extern vertex *get_vertex();

Modified: trunk/planarity/version.h
===================================================================
--- trunk/planarity/version.h	2005-08-10 21:17:36 UTC (rev 9726)
+++ trunk/planarity/version.h	2005-08-11 04:21:34 UTC (rev 9727)
@@ -1,2 +1,2 @@
 #define VERSION "$Id$ "
-/* DO NOT EDIT: Automated versioning hack [Sun Jul 31 07:12:39 EDT 2005] */
+/* DO NOT EDIT: Automated versioning hack [Thu Aug 11 00:13:45 EDT 2005] */



More information about the commits mailing list