[xiph-commits] r9778 - trunk/planarity
xiphmont at svn.xiph.org
xiphmont at svn.xiph.org
Fri Aug 19 19:21:19 PDT 2005
Author: xiphmont
Date: 2005-08-19 19:21:15 -0700 (Fri, 19 Aug 2005)
New Revision: 9778
Added:
trunk/planarity/levelstate.h
trunk/planarity/main.c
Modified:
trunk/planarity/Makefile
trunk/planarity/finish.c
trunk/planarity/gameboard.c
trunk/planarity/gamestate.c
trunk/planarity/gamestate.h
trunk/planarity/levelstate.c
trunk/planarity/pause.c
trunk/planarity/version.h
Log:
DOES NOT BUILD, DOES NOT RUN
grab the version from yesterday for a mostly working build
This impelments more cross-level state.
Modified: trunk/planarity/Makefile
===================================================================
--- trunk/planarity/Makefile 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/Makefile 2005-08-20 02:21:15 UTC (rev 9778)
@@ -17,9 +17,9 @@
# bleeding-edge GTK 2.7/2.8 libs installed.
SRC = graph.c arrange.c gameboard.c generate.c gamestate.c button_base.c\
- buttons.c buttonbar.c box.c pause.c finish.c
+ buttons.c buttonbar.c box.c pause.c finish.c levelstate.c main.c
OBJ = graph.o arrange.o gameboard.o generate.o gamestate.o button_base.o\
- buttons.o buttonbar.o box.o pause.o finish.o
+ buttons.o buttonbar.o box.o pause.o finish.o levelstate.o main.o
GCF = `pkg-config --static --cflags "gtk+-2.0 >= 2.7.2"`
# uncomment below for a normal dynamic build
Modified: trunk/planarity/finish.c
===================================================================
--- trunk/planarity/finish.c 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/finish.c 2005-08-20 02:21:15 UTC (rev 9778)
@@ -38,7 +38,9 @@
// back to buttonbar activity!
ui_next=0;
pop_background(g);
- setup_board(g);
+ levelstate_next();
+ levelstate_go();
+ gamestate_go();
}
static void finish_quit (Gameboard *g){
Modified: trunk/planarity/gameboard.c
===================================================================
--- trunk/planarity/gameboard.c 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/gameboard.c 2005-08-20 02:21:15 UTC (rev 9778)
@@ -11,6 +11,7 @@
#include "graph.h"
#include "gameboard.h"
#include "gamestate.h"
+#include "levelstate.h"
#include "button_base.h"
#include "buttonbar.h"
#include "box.h"
@@ -453,7 +454,7 @@
cairo_set_font_matrix (c,&m);
cairo_set_source_rgba (c, TEXT_COLOR);
- snprintf(level_string,160,"Level %d: \"%s\"",get_level()+1,get_level_string());
+ snprintf(level_string,160,"Level %d: \"%s\"",get_level_num()+1,get_level_name());
snprintf(score_string,160,"Score: %d",get_score());
snprintf(int_string,160,"Intersections: %d",g->g->active_intersections);
snprintf(obj_string,160,"Objective: %s",get_objective_string());
Modified: trunk/planarity/gamestate.c
===================================================================
--- trunk/planarity/gamestate.c 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/gamestate.c 2005-08-20 02:21:15 UTC (rev 9778)
@@ -4,10 +4,7 @@
#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"
#include "gameboard.h"
#include "generate.h"
@@ -15,37 +12,29 @@
#include "buttons.h"
#include "buttonbar.h"
#include "finish.h"
-#include "version.h"
#include "pause.h"
-#define boardstate "/.gPlanarity/boards/"
-#define mainstate "/.gPlanarity/"
-Gameboard *gameboard;
-GtkWidget *toplevel_window;
-graph maingraph;
+extern GtkWidget *toplevel_window;
+extern Gameboard *gameboard;
+extern graph maingraph;
static int width=800;
static int height=640;
static int orig_width=800;
static int orig_height=640;
-static int level=0;
-static int score=0;
-
static int initial_intersections;
static float intersection_mult=1.;
static int objective=0;
static int objective_lessthan=0;
+static char objective_string[80];
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(){
+time_t get_timer(){
if(paused)
return begin_time_add;
else{
@@ -59,14 +48,6 @@
begin_time = time(NULL);
}
-static gboolean key_press(GtkWidget *w,GdkEventKey *event,gpointer in){
-
- if(event->keyval == GDK_q && event->state&GDK_CONTROL_MASK)
- gtk_main_quit();
-
- return FALSE;
-}
-
void resize_board(int x, int y){
width=x;
height=y;
@@ -90,16 +71,17 @@
return orig_height;
}
-void setup_board(){
- generate_mesh_1(&maingraph,level);
- impress_location(&maingraph);
- initial_intersections = maingraph.original_intersections;
- gameboard_reset(gameboard);
+void gamestate_generate(int level){
+ generate_mesh_1(&maingraph,level);
+ impress_location(&maingraph);
+ initial_intersections = maingraph.original_intersections;
+}
- //gdk_flush();
- deploy_buttonbar(gameboard);
- set_timer(0);
- unpause();
+void gamestate_go(){
+ gameboard_reset(gameboard);
+ set_timer(0);
+ deploy_buttonbar(gameboard);
+ unpause();
}
#define RESET_DELTA 2;
@@ -255,10 +237,7 @@
void finish_board(){
if(maingraph.active_intersections<=initial_intersections){
pause();
- score+=initial_intersections;
- if(get_elapsed()<initial_intersections)
- score+=initial_intersections-get_elapsed();
- level++;
+ levelstate_finish();
undeploy_buttonbar(gameboard,finish_level_dialog);
}
}
@@ -269,11 +248,29 @@
}
int get_score(){
- return score + initial_intersections-maingraph.active_intersections;
+ float intersection_score = (initial_intersections- maingraph.active_intersections)*
+ intersection_mult;
+
+ float obj_multiplier = 1;
+
+ if(objective_lessthan)
+ if(objective > maingraph.active_intersections)
+ obj_multiplier += (objective-maingraph.active_intersections)*objective_mult;
+
+ return rint( intersection_score * obj_multiplier );
}
-int get_raw_score(){
- return score;
+int get_bonus(){
+ float obj_multiplier = 1;
+
+ if(objective_lessthan)
+ if(objective > maingraph.active_intersections)
+ obj_multiplier += (objective-maingraph.active_intersections)*objective_mult;
+
+ if(get_elapsed()<initial_intersections)
+ return rint ((initial_intersections-get_elapsed()) * obj_multiplier);
+
+ return 0;
}
int get_initial_intersections(){
@@ -285,37 +282,23 @@
}
char *get_objective_string(){
- return "zero intersections";
-}
-
-int get_level(){
- return level;
-}
-
-char *get_level_string(){
- return "original-style";
-}
-
-char *get_version_string(){
- 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;
+ if(objective == 0)
+ return "zero intersections";
+ if(objective == 1){
+ if(objective_lessthan){
+ return "1 intersection or fewer";
+ }else{
+ return "1 intersection";
}
+ }else{
+ snprintf(objective_string,80,"%d intersections%s",
+ objective,(objective_lessthan?
+ " or fewer":""));
+ return objective_string;
}
- return 0;
-}
+}
-int write_board(char *basename){
+int write_board(char *boarddir, char *basename){
char *name;
FILE *f;
@@ -340,15 +323,6 @@
fprintf(f,"board %d %d %d %d\n",
width,height,orig_width,orig_height);
- if(about_dialog_active())
- fprintf(f,"about 1\n");
- if(pause_dialog_active())
- fprintf(f,"pause 1\n");
- if(finish_dialog_active())
- fprintf(f,"finish 1\n");
- //if(level_dialog_active())
- //fprintf(f,"level 1\n");
-
gameboard_write(f, gameboard);
fclose(f);
@@ -366,16 +340,12 @@
return 0;
}
-int read_board(char *basename){
+int read_board(char *boarddir,char *basename){
FILE *f;
char *name;
char *line=NULL;
size_t n=0;
- int aboutflag=0;
- int pauseflag=0;
- int finishflag=0;
-
name=alloca(strlen(boarddir)+strlen(basename)+3);
name[0]=0;
strcat(name,boarddir);
@@ -412,18 +382,6 @@
}else{
sscanf(line,"board %d %d %d %d",&width,&height,&orig_width,&orig_height);
- if(sscanf(line,"about %d",&o)==1)
- if(o==1)
- aboutflag=1;
-
- if(sscanf(line,"pause %d",&o)==1)
- if(o==1)
- pauseflag=1;
-
- if(sscanf(line,"finish %d",&o)==1)
- if(o==1)
- finishflag=1;
-
}
}
@@ -437,85 +395,6 @@
gtk_window_resize(GTK_WINDOW(toplevel_window),width,height);
gameboard_reset(gameboard);
- if(pauseflag){
- pause_game(gameboard);
-
- }else if (aboutflag){
- about_game(gameboard);
-
-
- }else if (finishflag){
- finish_level_dialog(gameboard);
-
- }else{
- deploy_buttonbar(gameboard);
- unpause();
- }
-
return 0;
}
-
-int main(int argc, char *argv[]){
- 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,' ');
- if(versionend)versionend=strchr(versionend+1,' ');
- if(versionend)versionend=strchr(versionend+1,' ');
- if(versionend)versionend=strchr(versionend+1,' ');
- if(versionend){
- int len=versionend-version-9;
- version=strdup(version+10);
- version[len-1]=0;
- }
- }else{
- version="";
- }
-
- gtk_init (&argc, &argv);
-
- toplevel_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- g_signal_connect (G_OBJECT (toplevel_window), "delete-event",
- G_CALLBACK (gtk_main_quit), NULL);
- g_signal_connect (G_OBJECT (toplevel_window), "key-press-event",
- G_CALLBACK (key_press), toplevel_window);
-
- gameboard = gameboard_new (&maingraph);
-
- gtk_container_add (GTK_CONTAINER (toplevel_window), GTK_WIDGET(gameboard));
- gtk_widget_show_all (toplevel_window);
- memset(&maingraph,0,sizeof(maingraph));
-
- if(read_board("test"))
- setup_board(gameboard);
-
- gtk_main ();
-
- write_board("test");
- return 0;
-}
Modified: trunk/planarity/gamestate.h
===================================================================
--- trunk/planarity/gamestate.h 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/gamestate.h 2005-08-20 02:21:15 UTC (rev 9778)
@@ -5,7 +5,8 @@
extern int get_board_height();
extern int get_orig_width();
extern int get_orig_height();
-extern void setup_board();
+extern void gamestate_generate(int level);
+extern void gamestate_go();
extern void finish_board();
extern void hide_show_lines();
extern void mark_intersections();
@@ -13,17 +14,17 @@
extern void expand();
extern void shrink();
extern int get_score();
+extern int get_bonus();
extern int get_objective();
extern char *get_objective_string();
-extern int get_level();
-extern char *get_level_string();
extern void quit();
extern void pause();
extern void unpause();
extern int paused_p();
-extern time_t get_elapsed();
+extern time_t get_timer();
extern void set_timer(time_t off);
extern int get_initial_intersections();
-extern int get_raw_score();
-extern char *get_version_string();
+
+extern int read_board(char *boarddir,char *basename);
+extern int write_board(char *boarddir,char *basename);
Modified: trunk/planarity/levelstate.c
===================================================================
--- trunk/planarity/levelstate.c 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/levelstate.c 2005-08-20 02:21:15 UTC (rev 9778)
@@ -1,3 +1,6 @@
+#include <stdlib.h>
+#include "levelstate.h"
+
#define CHUNK 64
#define SAVENAME "levelstate"
@@ -16,10 +19,10 @@
levelstate *curr;
levelstate *pool;
-levelstate *new_level(){
+static levelstate *new_level(){
levelstate *ret;
- if(level_pool==0){
+ if(pool==0){
int i;
pool = calloc(CHUNK,sizeof(*pool));
for(i=0;i<CHUNK-1;i++) /* last addition's next points to nothing */
@@ -48,7 +51,7 @@
}
-levelstate *find_level(char *name){
+static levelstate *find_level(char *name){
int level=board_name_to_level(name);
if(level<0)return 0;
@@ -79,7 +82,10 @@
name[0]=0;
strcat(name,boarddir);
strcat(name,levelstate);
-
+
+ if(curr->in_progress)
+ write_board(curr->name);
+
f = fopen(name,"wb");
if(f==NULL){
fprintf(stderr,"ERROR: Could not save game state file \"%s\":\n\t%s\n",
@@ -99,14 +105,27 @@
}
}
+ if(about_dialog_active())
+ fprintf(f,"about 1\n");
+ if(pause_dialog_active())
+ fprintf(f,"pause 1\n");
+ if(finish_dialog_active())
+ fprintf(f,"finish 1\n");
+ //if(level_dialog_active())
+ //fprintf(f,"select 1\n");
+
return 0;
}
-int levelstate_read(char *statedir, char *boarddir){
+int levelstate_read(char *statedir){
char *cur_name;
int count;
char *line=NULL;
size_t n=0;
+
+ int aboutflag=0;
+ int pauseflag=0;
+ int finishflag=0;
// first get all levels we've seen.
while(getline(&line,&n,f)>0){
@@ -149,11 +168,35 @@
}
}
}
+
+ if(sscanf(line,"about %d",&i)==1)
+ if(i==1)
+ aboutflag=1;
+
+ if(sscanf(line,"pause %d",&i)==1)
+ if(i==1)
+ pauseflag=1;
+
+ if(sscanf(line,"finish %d",&i)==1)
+ if(i==1)
+ finishflag=1;
+
}
if(!head)new_level();
if(!curr)curr=head;
+ levelstate_go();
+ if(pauseflag){
+ pause_game();
+ }else if (aboutflag){
+ about_game();
+ }else if (finishflag){
+ finish_level_dialog();
+ }else{
+ gamestate_go();
+ }
+
return 0;
}
@@ -173,25 +216,39 @@
return curr->highscore;
}
-void levelstate_set_hiscore(long score){
- if(curr)
- curr->highscore=score;
-}
-
void levelstate_next(){
-
-
-
-
+ if(curr->next)
+ curr=curr->next;
}
void levelstate_prev(){
+ if(curr->prev)
+ curr=curr->prev;
+}
+int get_level_num(){
+ return curr->num;
+}
+char *get_level_name(){
+ return curr->name;
+}
+void levelstate_finish(){
+ curr->in_progress=0;
+ if(get_score() > curr->highscore)
+ curr->highscore = get_score();
}
+/* commit to the currently selected level and set the game state to
+ readiness using it */
void levelstate_go(){
+ if(!curr->in_progress || read_board(curr->name)){
+ /* not on disk or couldn't load it. Get a fresh version */
+ gamestate_generate(curr->level)
+ }
+ curr->in_progress=1;
+
}
Added: trunk/planarity/levelstate.h
===================================================================
--- trunk/planarity/levelstate.h 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/levelstate.h 2005-08-20 02:21:15 UTC (rev 9778)
@@ -0,0 +1,10 @@
+extern int levelstate_write(char *statedir);
+extern int levelstate_read(char *statedir);
+extern long levelstate_total_hiscore();
+extern long levelstate_get_hiscore();
+extern void levelstate_next();
+extern void levelstate_prev();
+extern int get_level_num();
+extern char *get_level_name();
+extern void levelstate_finish();
+extern void levelstate_go();
Added: trunk/planarity/main.c
===================================================================
--- trunk/planarity/main.c 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/main.c 2005-08-20 02:21:15 UTC (rev 9778)
@@ -0,0 +1,100 @@
+#define _GNU_SOURCE
+#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 "version.h"
+#include "graph.h"
+#include "gameboard.h"
+#include "levelstate.h"
+
+#define boardstate "/.gPlanarity/boards/"
+#define mainstate "/.gPlanarity/"
+
+static char *boarddir;
+static char *statedir;
+
+Gameboard *gameboard;
+GtkWidget *toplevel_window;
+graph maingraph;
+
+char *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 main(int argc, char *argv[]){
+ 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,' ');
+ if(versionend)versionend=strchr(versionend+1,' ');
+ if(versionend)versionend=strchr(versionend+1,' ');
+ if(versionend)versionend=strchr(versionend+1,' ');
+ if(versionend){
+ int len=versionend-version-9;
+ version=strdup(version+10);
+ version[len-1]=0;
+ }
+ }else{
+ version="";
+ }
+
+ gtk_init (&argc, &argv);
+
+ toplevel_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ g_signal_connect (G_OBJECT (toplevel_window), "delete-event",
+ G_CALLBACK (gtk_main_quit), NULL);
+
+ gameboard = gameboard_new (&maingraph);
+
+ gtk_container_add (GTK_CONTAINER (toplevel_window), GTK_WIDGET(gameboard));
+ gtk_widget_show_all (toplevel_window);
+ memset(&maingraph,0,sizeof(maingraph));
+
+ levelstate_read(statedir);
+
+ gtk_main ();
+
+ levelstate_write(statedir);
+ return 0;
+}
Modified: trunk/planarity/pause.c
===================================================================
--- trunk/planarity/pause.c 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/pause.c 2005-08-20 02:21:15 UTC (rev 9778)
@@ -16,6 +16,8 @@
static gint timer;
static void (*callback)(Gameboard *);
+extern char *version;
+
/* perform a single frame of animation for all pause dialog buttons/rollovers */
static gboolean pause_animate_buttons(gpointer ptr){
Gameboard *g=(Gameboard *)ptr;
@@ -289,7 +291,7 @@
cairo_matrix_init_scale (&ma, 10.,11.);
cairo_set_font_matrix (c,&ma);
- render_text_centered(c,get_version_string(), w/2,y);
+ render_text_centered(c,version, w/2,y);
}
Modified: trunk/planarity/version.h
===================================================================
--- trunk/planarity/version.h 2005-08-20 00:39:09 UTC (rev 9777)
+++ trunk/planarity/version.h 2005-08-20 02:21:15 UTC (rev 9778)
@@ -1,2 +1,2 @@
#define VERSION "$Id$ "
-/* DO NOT EDIT: Automated versioning hack [Sat Aug 13 01:31:34 EDT 2005] */
+/* DO NOT EDIT: Automated versioning hack [Fri Aug 19 22:20:08 EDT 2005] */
More information about the commits
mailing list