[xiph-commits] r9738 - trunk/planarity
xiphmont at svn.xiph.org
xiphmont at svn.xiph.org
Thu Aug 11 20:19:13 PDT 2005
Author: xiphmont
Date: 2005-08-11 20:19:08 -0700 (Thu, 11 Aug 2005)
New Revision: 9738
Added:
trunk/planarity/boardgen.c
trunk/planarity/levelstate.c
Modified:
trunk/planarity/arrange.c
trunk/planarity/arrange.h
trunk/planarity/button_base.c
trunk/planarity/button_base.h
trunk/planarity/buttonbar.c
trunk/planarity/buttons.h
trunk/planarity/finish.c
trunk/planarity/finish.h
trunk/planarity/gameboard.c
trunk/planarity/gameboard.h
trunk/planarity/gamestate.c
trunk/planarity/gamestate.h
trunk/planarity/generate.c
trunk/planarity/generate.h
trunk/planarity/graph.c
trunk/planarity/graph.h
trunk/planarity/pause.c
trunk/planarity/pause.h
trunk/planarity/version.h
Log:
get more multilevel code in place
Modified: trunk/planarity/arrange.c
===================================================================
--- trunk/planarity/arrange.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/arrange.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -2,25 +2,27 @@
#include "graph.h"
#include "arrange.h"
+#include "gamestate.h"
-void arrange_verticies_circle(int n){
- vertex *v = get_verticies();
- int bw=get_board_width();
- int bh=get_board_height();
+void arrange_verticies_circle(graph *g){
+ vertex *v = g->verticies;
+ int n = g->vertex_num;
+ int bw=get_orig_width();
+ int bh=get_orig_height();
int radius=min(bw,bh)*.45;
int i;
for(i=0;i<n;i++){
int x = rint( radius * cos( i*M_PI*2./n));
int y = rint( radius * sin( i*M_PI*2./n));
- move_vertex(v,x+(bw>>1),y+(bh>>1));
+ move_vertex(g,v,x+(bw>>1),y+(bh>>1));
v=v->next;
}
}
-void arrange_verticies_mesh(int width, int height){
- vertex *v = get_verticies();
- int bw=get_board_width();
- int bh=get_board_height();
+void arrange_verticies_mesh(graph *g, int width, int height){
+ vertex *v = g->verticies;
+ int bw=get_orig_width();
+ int bh=get_orig_height();
int spacing=min((float)bw/width,(float)bh/height)*.9;
int x,y;
@@ -29,7 +31,7 @@
for(y=0;y<height;y++){
for(x=0;x<width;x++){
- move_vertex(v,x*spacing+xpad,y*spacing+ypad);
+ move_vertex(g,v,x*spacing+xpad,y*spacing+ypad);
v=v->next;
}
}
Modified: trunk/planarity/arrange.h
===================================================================
--- trunk/planarity/arrange.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/arrange.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -1,2 +1,2 @@
-extern void arrange_verticies_circle(int n);
-extern void arrange_verticies_mesh(int width, int height);
+extern void arrange_verticies_circle(graph *g);
+extern void arrange_verticies_mesh(graph *g, int width, int height);
Added: trunk/planarity/boardgen.c
===================================================================
--- trunk/planarity/boardgen.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/boardgen.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -0,0 +1,9 @@
+char *board_level_to_name(int level){
+
+
+}
+
+int board_name_to_level(int level){
+
+
+}
Modified: trunk/planarity/button_base.c
===================================================================
--- trunk/planarity/button_base.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/button_base.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -15,14 +15,14 @@
buttonstate states[NUMBUTTONS];
int buttons_ready=0;
-static int width=0;
-static int height=0;
static int allclear=1; // actually just a shirt-circuit
static buttonstate *grabbed=0;
/* determine the x/y/w/h box around the rollover text */
static GdkRectangle rollover_box(buttonstate *b){
GdkRectangle r;
+ int width = get_board_width();
+ int height = get_board_height();
int x = b->x - b->ex.width/2 + b->ex.x_bearing -2;
int y = b->y - BUTTON_RADIUS - BUTTON_TEXT_BORDER + b->ex.y_bearing -2;
@@ -389,8 +389,6 @@
states[10].lit = cache_button(g, path_button_play,
BUTTON_CHECK_LIT_PATH,
BUTTON_CHECK_LIT_FILL);
- width = get_board_width();
- height = get_board_height();
}
/* cache the text extents of a rollover */
@@ -480,10 +478,10 @@
}
/* resize the button bar; called from master resize in gameboard */
-void resize_buttons(int w,int h){
+void resize_buttons(int oldw,int oldh,int w,int h){
int i;
- int dx=w/2-width/2;
- int dy=h/2-height/2;
+ int dx=w/2-oldw/2;
+ int dy=h/2-oldh/2;
for(i=0;i<NUMBUTTONS;i++){
if(states[i].position == 2){
@@ -497,8 +495,8 @@
}
}
- dx=w-width;
- dy=h-height;
+ dx=w-oldw;
+ dy=h-oldh;
for(i=0;i<NUMBUTTONS;i++){
if(states[i].position == 1){
@@ -517,9 +515,6 @@
states[i].y+=dy;
}
}
-
- width=w;
- height=h;
}
/* clear all buttons to unpressed/unlit */
Modified: trunk/planarity/button_base.h
===================================================================
--- trunk/planarity/button_base.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/button_base.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -37,7 +37,7 @@
extern void rollover_extents(Gameboard *g, buttonstate *b);
extern gboolean animate_button_frame(gpointer ptr);
extern void expose_buttons(Gameboard *g,cairo_t *c,int x, int y, int w, int h);
-extern void resize_buttons(int w,int h);
+extern void resize_buttons(int oldw,int oldh,int w,int h);
extern int button_motion_event(Gameboard *g, GdkEventMotion *event, int focusable);
extern int button_button_press(Gameboard *g, GdkEventButton *event, int focusable);
extern int button_button_release(Gameboard *g, GdkEventButton *event, int focusable);
Modified: trunk/planarity/buttonbar.c
===================================================================
--- trunk/planarity/buttonbar.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/buttonbar.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -172,7 +172,7 @@
/* effects animated 'rollout' of buttons when level begins */
void deploy_buttonbar(Gameboard *g){
if(!buttons_ready ){
- if(get_num_intersections() <= get_objective())
+ if(g->g->active_intersections <= get_objective())
checkbutton_deployed=1;
else
checkbutton_deployed=0;
Modified: trunk/planarity/buttons.h
===================================================================
--- trunk/planarity/buttons.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/buttons.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -1,18 +1,3 @@
-extern void go_buttons(Gameboard *g);
-extern void init_buttons(Gameboard *g);
-extern void expose_buttons(Gameboard *g,cairo_t *c,int x, int y, int w, int h);
-extern void deploy_buttons(Gameboard *g);
-extern void undeploy_buttons(Gameboard *g, void (*callback)());
-extern void resize_buttons(int w,int h);
-extern void deploy_check(Gameboard *g);
-extern void undeploy_check(Gameboard *g);
-
-extern int button_motion_event(Gameboard *g, GdkEventMotion *event, int focusable);
-extern int button_button_press(Gameboard *g, GdkEventButton *event, int focusable);
-extern int button_button_release(Gameboard *g, GdkEventButton *event, int focusable);
-
-
-
extern void path_button_help(cairo_t *c, double x, double y);
extern void path_button_back(cairo_t *c, double x, double y);
extern void path_button_reset(cairo_t *c, double x, double y);
Modified: trunk/planarity/finish.c
===================================================================
--- trunk/planarity/finish.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/finish.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -11,6 +11,7 @@
#include "finish.h"
#include "box.h"
+static int ui_next=0;
static gint timer;
static void (*callback)(Gameboard *);
@@ -35,6 +36,7 @@
static void finish_post (Gameboard *g){
// back to buttonbar activity!
+ ui_next=0;
pop_background(g);
setup_board(g);
}
@@ -226,7 +228,7 @@
setup_finish_buttons(g,FINISHBOX_WIDTH, FINISHBOX_HEIGHT);
// draw pausebox
- push_background(g,draw_finishbox);
+ push_curtain(g,draw_finishbox);
// deploy new buttons
callback=0;
@@ -236,6 +238,11 @@
void finish_level_dialog(Gameboard *g){
// undeploy buttonbar
+ ui_next=1;
+ push_background(g,0);
undeploy_buttonbar(g,finish_post_undeploy);
}
+int finish_dialog_active(){
+ return ui_next;
+}
Modified: trunk/planarity/finish.h
===================================================================
--- trunk/planarity/finish.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/finish.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -4,4 +4,4 @@
#define FINISHBOX_HEIGHT 300
extern void finish_level_dialog(Gameboard *g);
-
+extern int finish_dialog_active();
Modified: trunk/planarity/gameboard.c
===================================================================
--- trunk/planarity/gameboard.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/gameboard.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
#include <gtk/gtk.h>
#include <gtk/gtkmain.h>
#include <gdk/gdk.h>
@@ -10,7 +11,8 @@
#include "graph.h"
#include "gameboard.h"
#include "gamestate.h"
-#include "buttons.h"
+#include "button_base.h"
+#include "buttonbar.h"
#include "box.h"
static GtkWidgetClass *parent_class = NULL;
@@ -82,7 +84,7 @@
// invalidate the selection box plus enough area to catch any verticies
static void invalidate_region_verticies_selection(GtkWidget *widget){
Gameboard *g = GAMEBOARD (widget);
- vertex *v=get_verticies();
+ vertex *v=g->g->verticies;
while(v){
if(v->selected)
invalidate_region_vertex_off(widget,v,g->dragx,g->dragy);
@@ -265,7 +267,7 @@
/* verticies are drawn in the foreground */
{
- vertex *v = get_verticies();
+ vertex *v = g->g->verticies;
int clipx = x-V_RADIUS;
int clipw = x+w+V_RADIUS;
int clipy = y-V_RADIUS;
@@ -297,7 +299,7 @@
static void background_draw(Gameboard *g,cairo_t *c){
int width=get_board_width();
int height=get_board_height();
- edge *e=get_edges();
+ edge *e=g->g->edges;
cairo_set_source_rgb(c,1,1,1);
cairo_paint(c);
@@ -341,7 +343,7 @@
/* if a group drag is in progress, draw the group ghosted in the foreground */
if(g->group_drag){
- vertex *v = get_verticies();
+ vertex *v = g->g->verticies;
while(v){
if( v->selected ){
@@ -405,7 +407,7 @@
static void check_lit(GtkWidget *widget,int x, int y){
Gameboard *g = GAMEBOARD (widget);
- vertex *v = find_vertex(x,y);
+ vertex *v = find_vertex(g->g,x,y);
if(v!=g->lit_vertex){
invalidate_region_vertex(g,v);
invalidate_region_vertex(g,g->lit_vertex);
@@ -427,7 +429,7 @@
cairo_t *c = cairo_create(g->forescore);
- if(get_num_intersections() <= get_objective()){
+ if(g->g->active_intersections <= get_objective()){
deploy_check(g);
}else{
undeploy_check(g);
@@ -453,7 +455,7 @@
snprintf(level_string,160,"Level %d: \"%s\"",get_level()+1,get_level_string());
snprintf(score_string,160,"Score: %d",get_score());
- snprintf(int_string,160,"Intersections: %d",get_num_intersections());
+ snprintf(int_string,160,"Intersections: %d",g->g->active_intersections);
snprintf(obj_string,160,"Objective: %s",get_objective_string());
cairo_text_extents (c, level_string, &extentsL);
@@ -568,7 +570,7 @@
invalidate_region_vertex(g,g->grabbed_vertex);
invalidate_region_edges(widget,g->grabbed_vertex);
- move_vertex(g->grabbed_vertex,x+g->graboffx,y+g->graboffy);
+ move_vertex(g->g,g->grabbed_vertex,x+g->graboffx,y+g->graboffy);
invalidate_region_vertex(g,g->grabbed_vertex);
invalidate_region_edges(widget,g->grabbed_vertex);
}else if (g->selection_grab){
@@ -578,7 +580,8 @@
g->selectionw = labs(g->grabx - event->x);
g->selectiony = min(g->graby, event->y);
g->selectionh = labs(g->graby - event->y);
- select_verticies(g->selectionx,g->selectiony,
+ select_verticies(g->g,
+ g->selectionx,g->selectiony,
g->selectionx+g->selectionw-1,
g->selectiony+g->selectionh-1);
@@ -591,7 +594,7 @@
// if this puts any of the dragged offscreen adjust the drag back
// onscreen. It will avoid confusing/concerning users
{
- vertex *v=get_verticies();
+ vertex *v=g->g->verticies;
int w=get_board_width();
int h=get_board_height();
@@ -626,7 +629,7 @@
GdkEventButton *event){
Gameboard *g = GAMEBOARD (widget);
- vertex *v = find_vertex((int)event->x,(int)event->y);
+ vertex *v = find_vertex(g->g,(int)event->x,(int)event->y);
g->button_grabbed=0;
if(paused_p()){
@@ -659,7 +662,7 @@
}else{
if(g->selection_active){
- vertex *v = find_vertex((int)event->x,(int)event->y);
+ vertex *v = find_vertex(g->g,(int)event->x,(int)event->y);
if(v && v->selected){
// group drag
g->group_drag=1;
@@ -674,7 +677,7 @@
if(button_button_press(g,event,1)){
g->button_grabbed=1;
}else{
- deselect_verticies();
+ deselect_verticies(g->g);
update_background(widget);
g->selection_active=0;
g->group_drag=0;
@@ -691,7 +694,7 @@
g->graboffx = g->grabbed_vertex->x-g->grabx;
g->graboffy = g->grabbed_vertex->y-g->graby;
- grab_vertex(g->grabbed_vertex);
+ grab_vertex(g->g,g->grabbed_vertex);
invalidate_region_attached(widget,g->grabbed_vertex);
invalidate_region_edges(widget,g->grabbed_vertex);
@@ -727,7 +730,7 @@
if(paused_p())return TRUE;
if(g->grabbed_vertex){
- ungrab_vertex(g->grabbed_vertex);
+ ungrab_vertex(g->g,g->grabbed_vertex);
update_background_addv(widget,g->grabbed_vertex);
score_update(g);
g->grabbed_vertex = 0;
@@ -737,17 +740,17 @@
invalidate_region_selection(widget);
g->selection_grab=0;
// are there selected verticies? Avoid accidentally misgrabbing one.
- if(num_selected_verticies()<=1){
+ if(num_selected_verticies(g->g)<=1){
g->selection_active=0;
- deselect_verticies(); // could have grabbed just one
+ deselect_verticies(g->g); // could have grabbed just one
}else{
commit_volatile_selection();
- g->selection_active=num_selected_verticies();
+ g->selection_active=num_selected_verticies(g->g);
}
}
if(g->group_drag){
- move_selected_verticies(g->dragx,g->dragy);
+ move_selected_verticies(g->g,g->dragx,g->dragy);
update_background(widget); // cheating
score_update(g);
g->group_drag=0;
@@ -761,8 +764,8 @@
static void size_request (GtkWidget *widget,GtkRequisition *requisition){
- requisition->width = get_board_width();
- requisition->height = get_board_height();
+ requisition->width = get_orig_width();
+ requisition->height = get_orig_height();
}
@@ -798,7 +801,7 @@
double y2=h+y+(INTERSECTION_LINE_WIDTH + INTERSECTION_RADIUS*2);
// walk the intersection list of all edges; draw if paired is a higher pointer
- edge *e=get_edges();
+ edge *e=g->g->edges;
while(e){
intersection *i = e->i.next;
while(i){
@@ -819,6 +822,7 @@
void pop_background(Gameboard *g){
if(g->pushed_background){
g->pushed_background=0;
+ g->pushed_curtain=0;
update_full(g);
}
}
@@ -845,13 +849,7 @@
cairo_rectangle(c, 0,0,w,h);
cairo_fill(c);
- if(g->curtain_alpha>0.){
- cairo_set_source (c, g->curtainp);
- cairo_rectangle (c, 0, 0, get_board_width(), get_board_height());
- cairo_fill (c);
- }else{
- expose_intersections(g,c,0,0,w,h);
- }
+ expose_intersections(g,c,0,0,w,h);
cairo_destroy(c);
if(redraw_callback)redraw_callback(g);
@@ -868,6 +866,36 @@
}
}
+void push_curtain(Gameboard *g,void(*redraw_callback)(Gameboard *g)){
+ if(g->pushed_background){
+ if(!g->pushed_curtain){
+ cairo_t *c = cairo_create(g->background);
+ int w = g->w.allocation.width;
+ int h = g->w.allocation.height;
+ g->pushed_curtain=1;
+
+ g->redraw_callback=redraw_callback;
+ cairo_set_source (c, g->curtainp);
+ cairo_rectangle (c, 0, 0, get_board_width(), get_board_height());
+ cairo_fill (c);
+ cairo_destroy(c);
+
+ if(redraw_callback)redraw_callback(g);
+
+ {
+ GdkRectangle r;
+ r.x=0;
+ r.y=0;
+ r.width=w;
+ r.height=h;
+
+ gdk_window_invalidate_rect (g->w.window, &r, FALSE);
+ }
+
+ }
+ }
+}
+
void run_immediate_expose(Gameboard *g,
int x, int y, int w, int h){
@@ -897,13 +925,15 @@
cairo_rectangle(c, x,y,w,h);
cairo_fill(c);
}
-
+
+ expose_intersections(g,c,x,y,w,h);
+ }
+
+ if(!g->pushed_curtain){
if(g->curtain_alpha>0.){
cairo_set_source (c, g->curtainp);
cairo_rectangle (c, 0, 0, get_board_width(), get_board_height());
cairo_fill (c);
- }else{
- expose_intersections(g,c,x,y,w,h);
}
}
@@ -1063,7 +1093,7 @@
// recenter all the verticies; doesn't require recomputation
{
- vertex *v=get_verticies();
+ vertex *v=g->g->verticies;
int xd=(widget->allocation.width-oldw)*.5;
int yd=(widget->allocation.height-oldh)*.5;
@@ -1084,7 +1114,7 @@
update_background(widget);
paint_bottom_box(g);
score_update(g);
- resize_buttons(widget->allocation.width,widget->allocation.height);
+ resize_buttons(oldw,oldh,widget->allocation.width,widget->allocation.height);
if(flag)push_background(g,g->redraw_callback);
@@ -1140,9 +1170,10 @@
return gameboard_type;
}
-Gameboard *gameboard_new (void) {
+Gameboard *gameboard_new (graph *gr) {
GtkWidget *g = GTK_WIDGET (g_object_new (GAMEBOARD_TYPE, NULL));
Gameboard *gb = GAMEBOARD (g);
+ gb->g=gr;
return gb;
}
@@ -1154,7 +1185,7 @@
g->group_drag=0;
g->button_grabbed=0;
g->selection_grab=0;
- g->selection_active=0;
+ g->selection_active=num_selected_verticies(g->g);
update_background(&g->w);
score_update(g);
@@ -1193,3 +1224,39 @@
int selected(Gameboard *g){
return g->selection_active;
}
+
+/***************** save/load gameboard the widget state we want to be persistent **************/
+
+// there are only a few things; lines, intersections
+int gameboard_write(FILE *f, Gameboard *g){
+
+ if(g->hide_lines)
+ fprintf(f,"hide_lines 1\n");
+ if(g->show_intersections)
+ fprintf(f,"show_intersections 1\n");
+
+ return 0;
+}
+
+int gameboard_read(FILE *f, Gameboard *g){
+ char *line=NULL;
+ size_t n=0;
+ int i;
+
+ g->hide_lines = 0;
+ g->show_intersections = 0;
+
+ while(getline(&line,&n,f)>0){
+ if (sscanf(line,"hide_lines %d",&i)==1)
+ if(i)
+ g->hide_lines = 1;
+
+ if (sscanf(line,"show_intersections %d",&i)==1)
+ if(i)
+ g->show_intersections = 1;
+ }
+
+ free(line);
+
+ return 0;
+}
Modified: trunk/planarity/gameboard.h
===================================================================
--- trunk/planarity/gameboard.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/gameboard.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -34,8 +34,10 @@
struct _Gameboard{
GtkWidget w;
+ graph *g;
int pushed_background;
+ int pushed_curtain;
void (*redraw_callback)(Gameboard *g);
cairo_t *wc;
@@ -86,7 +88,7 @@
};
GType gameboard_get_type (void);
-Gameboard* gameboard_new (void);
+Gameboard* gameboard_new (graph *g);
G_END_DECLS
@@ -105,3 +107,6 @@
extern void set_curtain(Gameboard *g, double alpha);
extern void pop_background(Gameboard *g);
extern void push_background(Gameboard *g, void (*callback)(Gameboard *g));
+extern void push_curtain(Gameboard *g, void (*callback)(Gameboard *g));
+extern int gameboard_write(FILE *f, Gameboard *g);
+extern int gameboard_read(FILE *f, Gameboard *g);
Modified: trunk/planarity/gamestate.c
===================================================================
--- trunk/planarity/gamestate.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/gamestate.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -16,13 +16,18 @@
#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;
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;
@@ -76,10 +81,19 @@
return height;
}
+// graphs are originally generated to a 'nominal' minimum sized board.
+int get_orig_width(){
+ return orig_width;
+}
+
+int get_orig_height(){
+ return orig_height;
+}
+
void setup_board(){
- generate_mesh_1(level);
- impress_location();
- initial_intersections = get_num_intersections();
+ generate_mesh_1(&maingraph,level);
+ impress_location(&maingraph);
+ initial_intersections = maingraph.original_intersections;
gameboard_reset(gameboard);
//gdk_flush();
@@ -95,34 +109,39 @@
int hide_state = get_hide_lines(gameboard);
hide_lines(gameboard);
- vertex *v=get_verticies();
+ vertex *v=maingraph.verticies;
while(v){
- deactivate_vertex(v);
+ deactivate_vertex(&maingraph,v);
v=v->next;
}
while(flag){
flag=0;
- v=get_verticies();
+ v=maingraph.verticies;
while(v){
- if(v->x != v->orig_x || v->y != v->orig_y){
+ int bxd = (width - orig_width) >>1;
+ int byd = (height - orig_height) >>1;
+ int vox = v->orig_x + bxd;
+ int voy = v->orig_y + byd;
+
+ if(v->x != vox || v->y != voy){
flag=1;
invalidate_region_vertex(gameboard,v);
- if(v->x<v->orig_x){
+ if(v->x<vox){
v->x+=RESET_DELTA;
- if(v->x>v->orig_x)v->x=v->orig_x;
+ if(v->x>vox)v->x=vox;
}else{
v->x-=RESET_DELTA;
- if(v->x<v->orig_x)v->x=v->orig_x;
+ if(v->x<vox)v->x=vox;
}
- if(v->y<v->orig_y){
+ if(v->y<voy){
v->y+=RESET_DELTA;
- if(v->y>v->orig_y)v->y=v->orig_y;
+ if(v->y>voy)v->y=voy;
}else{
v->y-=RESET_DELTA;
- if(v->y<v->orig_y)v->y=v->orig_y;
+ if(v->y<voy)v->y=voy;
}
invalidate_region_vertex(gameboard,v);
}
@@ -133,14 +152,14 @@
}
- v=get_verticies();
+ v=maingraph.verticies;
while(v){
- activate_vertex(v);
+ activate_vertex(&maingraph,v);
v=v->next;
}
if(!hide_state)show_lines(gameboard);
- if(get_num_intersections() <= get_objective()){
+ if(maingraph.active_intersections <= get_objective()){
deploy_check(gameboard);
}else{
undeploy_check(gameboard);
@@ -172,7 +191,7 @@
int sel = selected(gameboard);
// if selected, expand from center of selected mass
if(sel){
- vertex *v=get_verticies();
+ vertex *v=maingraph.verticies;
double xac=0;
double yac=0;
while(v){
@@ -186,7 +205,7 @@
y = yac / selected(gameboard);
}
- vertex *v=get_verticies();
+ vertex *v=maingraph.verticies;
while(v){
if(!sel || v->selected){
@@ -198,16 +217,16 @@
if(ny<0)ny=0;
if(ny>=height)ny=height-1;
- deactivate_vertex(v);
+ deactivate_vertex(&maingraph,v);
v->x = nx;
v->y = ny;
}
v=v->next;
}
- v=get_verticies();
+ v=maingraph.verticies;
while(v){
- activate_vertex(v);
+ activate_vertex(&maingraph,v);
v=v->next;
}
@@ -234,7 +253,7 @@
}
void finish_board(){
- if(get_num_intersections()<=initial_intersections){
+ if(maingraph.active_intersections<=initial_intersections){
pause();
score+=initial_intersections;
if(get_elapsed()<initial_intersections)
@@ -245,11 +264,12 @@
}
void quit(){
+ pause();
undeploy_buttonbar(gameboard,gtk_main_quit);
}
int get_score(){
- return score + initial_intersections-get_num_intersections();
+ return score + initial_intersections-maingraph.active_intersections;
}
int get_raw_score(){
@@ -296,8 +316,6 @@
}
int write_board(char *basename){
- vertex *v=get_verticies();
- edge *e=get_edges();
char *name;
FILE *f;
@@ -313,25 +331,25 @@
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;
- }
+ graph_write(&maingraph,f);
- 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);
+ 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_write(f, gameboard);
fclose(f);
@@ -345,7 +363,6 @@
//gameboard_write_icon(f);
fclose(f);
-
return 0;
}
@@ -355,6 +372,10 @@
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);
@@ -369,80 +390,73 @@
return errno;
}
- // successful open
- // read the board
- new_board(0);
+ graph_read(&maingraph,f);
- {
- int i,x,y,ox,oy,sel,count=0,A,B;
- vertex **flat,*v;
+ // get local game state
+ while(getline(&line,&n,f)>0){
+ int o;
+ char c;
+ long l;
+
+ if (sscanf(line,"objective %c %d",&c,&o)==2){
+
+ objective_lessthan = (c=='*');
+ objective = o;
- // get all verticies first
- while(getline(&line,&n,f)>0){
+ }else if (sscanf(line,"scoring %d %f %f %ld",
+ &initial_intersections,&intersection_mult,
+ &objective_mult,&l)==4){
+ paused=1;
+ begin_time_add = l;
- {
- 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;
- }
+ }else{
+ sscanf(line,"board %d %d %d %d",&width,&height,&orig_width,&orig_height);
- // get edges and other next
- while(getline(&line,&n,f)>0){
- int o;
- char c;
- long l;
+ if(sscanf(line,"about %d",&o)==1)
+ if(o==1)
+ aboutflag=1;
- 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);
+ if(sscanf(line,"pause %d",&o)==1)
+ if(o==1)
+ pauseflag=1;
+
+ if(sscanf(line,"finish %d",&o)==1)
+ if(o==1)
+ finishflag=1;
+
}
}
rewind(f);
- //read_gameboard(f);
+ gameboard_read(f,gameboard);
fclose (f);
+ free(line);
+
+ 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[]){
- GtkWidget *window;
char *homedir = getenv("home");
if(!homedir)
homedir = getenv("HOME");
@@ -485,19 +499,23 @@
gtk_init (&argc, &argv);
- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- g_signal_connect (G_OBJECT (window), "delete-event",
+ 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 (window), "key-press-event",
- G_CALLBACK (key_press), window);
+ g_signal_connect (G_OBJECT (toplevel_window), "key-press-event",
+ G_CALLBACK (key_press), toplevel_window);
- gameboard = gameboard_new ();
+ gameboard = gameboard_new (&maingraph);
- gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET(gameboard));
- gtk_widget_show_all (window);
+ gtk_container_add (GTK_CONTAINER (toplevel_window), GTK_WIDGET(gameboard));
+ gtk_widget_show_all (toplevel_window);
+ memset(&maingraph,0,sizeof(maingraph));
- setup_board(gameboard);
+ 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-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/gamestate.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -3,6 +3,8 @@
extern void resize_board(int x, int y);
extern int get_board_width();
extern int get_board_height();
+extern int get_orig_width();
+extern int get_orig_height();
extern void setup_board();
extern void finish_board();
extern void hide_show_lines();
Modified: trunk/planarity/generate.c
===================================================================
--- trunk/planarity/generate.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/generate.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -126,7 +126,7 @@
nl->num=count;
}
-static void random_populate(int current, mesh *m, int min_connect, float prob){
+static void random_populate(graph *g, int current, mesh *m, int min_connect, float prob){
int num_edges=0,i;
neighbors_grid ng;
neighbors_list nl;
@@ -144,7 +144,7 @@
while(num_edges<min_connect && nl.num){
int choice = random() % nl.num;
- add_edge(m->v[current], m->v[nl.vnum[choice]]);
+ add_edge(g,m->v[current], m->v[nl.vnum[choice]]);
num_edges++;
filter_intersections(&ng);
filter_edges(&ng,&nl);
@@ -153,11 +153,11 @@
for(i=0;i<nl.num;i++)
if(random()<RAND_MAX*prob){
num_edges++;
- add_edge(m->v[current], m->v[nl.vnum[i]]);
+ add_edge(g,m->v[current], m->v[nl.vnum[i]]);
}
}
-static void span_depth_first(int current, mesh *m){
+static void span_depth_first(graph *g,int current, mesh *m){
neighbors_grid ng;
neighbors_list nl;
@@ -170,14 +170,14 @@
{
int choice = random() % nl.num;
- add_edge(m->v[current], m->v[nl.vnum[choice]]);
+ add_edge(g,m->v[current], m->v[nl.vnum[choice]]);
- span_depth_first(nl.vnum[choice], m);
+ span_depth_first(g,nl.vnum[choice], m);
}
}
}
-void generate_mesh_1(int order){
+void generate_mesh_1(graph *g, int order){
int flag=0;
mesh m;
m.width=3;
@@ -196,7 +196,7 @@
}
}
- vlist=new_board(m.width * m.height);
+ vlist=new_board(g, m.width * m.height);
/* a flat vector is easier to address while building the mesh */
{
@@ -210,17 +210,17 @@
}
/* first walk a random spanning tree */
- span_depth_first(0, &m);
+ span_depth_first(g, 0, &m);
/* now iterate the whole mesh adding random edges */
{
int i;
for(i=0;i<m.width*m.height;i++)
- random_populate(i, &m, 2, .25);
+ random_populate(g, i, &m, 2, .25);
}
- randomize_verticies(vlist);
- arrange_verticies_circle(m.width*m.height);
+ randomize_verticies(g);
+ arrange_verticies_circle(g);
//arrange_verticies_mesh(m.width,m.height);
}
Modified: trunk/planarity/generate.h
===================================================================
--- trunk/planarity/generate.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/generate.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -1 +1 @@
-extern void generate_mesh_1(int order);
+extern void generate_mesh_1(graph *g, int order);
Modified: trunk/planarity/graph.c
===================================================================
--- trunk/planarity/graph.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/graph.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -1,3 +1,5 @@
+#define _GNU_SOURCE
+#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
@@ -3,23 +5,15 @@
#include "graph.h"
+#include "gamestate.h"
#include "gameboard.h"
#define CHUNK 64
-/* 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;
static edge_list *edge_list_pool=0;
static intersection *intersection_pool=0;
-static int active_intersections=0;
-static int reported_intersections=0;
+/* mesh/board state */
-static int num_edges=0;
-static int num_edges_active=0;
-
-
/************************ edge list maint operations ********************/
static void add_edge_to_list(vertex *v, edge *e){
@@ -135,7 +129,7 @@
/************************ edge maint operations ******************************/
/* also adds to the edge list */
-edge *add_edge(vertex *A, vertex *B){
+edge *add_edge(graph *g, vertex *A, vertex *B){
edge *ret;
if(edge_pool==0){
@@ -152,19 +146,19 @@
ret->B=B;
ret->active=0;
ret->i.next=0;
- ret->next=edges;
- edges=ret;
-
+ ret->next=g->edges;
+ g->edges=ret;
+
add_edge_to_list(A,ret);
add_edge_to_list(B,ret);
- num_edges++;
+ g->num_edges++;
return ret;
}
-static void release_edges(){
- edge *e = edges;
+static void release_edges(graph *g){
+ edge *e = g->edges;
while(e){
edge *next=e->next;
@@ -173,9 +167,9 @@
edge_pool=e;
e=next;
}
- edges=0;
- num_edges=0;
- num_edges_active=0;
+ g->edges=0;
+ g->num_edges=0;
+ g->num_edges_active=0;
}
static int intersects(vertex *L1, vertex *L2, vertex *M1, vertex *M2, double *xo, double *yo){
@@ -305,34 +299,34 @@
return 1;
}
-static void activate_edge(edge *e){
+static void activate_edge(graph *g, edge *e){
/* computes all intersections */
if(!e->active && e->A->active && e->B->active){
- edge *test=edges;
+ edge *test=g->edges;
while(test){
if(test != e && test->active){
double x,y;
if(intersects(e->A,e->B,test->A,test->B,&x,&y)){
add_paired_intersection(e,test,x,y);
- active_intersections++;
+ g->active_intersections++;
}
}
test=test->next;
}
e->active=1;
- num_edges_active++;
- if(num_edges_active == num_edges)
- reported_intersections = active_intersections;
+ g->num_edges_active++;
+ if(g->num_edges_active == g->num_edges && g->original_intersections==0)
+ g->original_intersections = g->active_intersections;
}
}
-static void deactivate_edge(edge *e){
+static void deactivate_edge(graph *g, edge *e){
/* releases all associated intersections */
if(e->active){
- active_intersections -=
+ g->active_intersections -=
release_paired_intersection_list(e);
- num_edges_active--;
+ g->num_edges_active--;
e->active=0;
}
}
@@ -348,7 +342,7 @@
}
/*********************** vertex maint operations *************************/
-vertex *get_vertex(){
+vertex *get_vertex(graph *g){
vertex *ret;
if(vertex_pool==0){
@@ -369,10 +363,10 @@
ret->grabbed=0;
ret->attached_to_grabbed=0;
ret->edges=0;
- ret->num=vertex_num++;
+ ret->num=g->vertex_num++;
- ret->next=verticies;
- verticies=ret;
+ ret->next=g->verticies;
+ g->verticies=ret;
return ret;
}
@@ -383,45 +377,54 @@
vertex_pool=v;
}
-static void set_num_verticies(int num){
+static void set_num_verticies(graph *g, int num){
/* do it the simple way; release all, link anew */
- vertex *v=verticies;
+ vertex *v=g->verticies;
while(v){
vertex *next=v->next;
release_vertex(v);
v=next;
}
- verticies=0;
- vertex_num=0;
- release_edges();
+ g->verticies=0;
+ g->vertex_num=0;
+ release_edges(g);
while(num--)
- get_vertex();
+ get_vertex(g);
+ g->original_intersections = 0;
}
-void activate_vertex(vertex *v){
+void activate_vertex(graph *g,vertex *v){
edge_list *el=v->edges;
v->active=1;
while(el){
- activate_edge(el->edge);
+ activate_edge(g,el->edge);
el=el->next;
}
}
-void deactivate_vertex(vertex *v){
+void deactivate_vertex(graph *g, vertex *v){
edge_list *el=v->edges;
while(el){
edge_list *next=el->next;
- deactivate_edge(el->edge);
+ deactivate_edge(g,el->edge);
el=next;
}
v->active=0;
}
-void grab_vertex(vertex *v){
+void activate_verticies(graph *g){
+ vertex *v=g->verticies;
+ while(v){
+ activate_vertex(g,v);
+ v=v->next;
+ }
+}
+
+void grab_vertex(graph *g, vertex *v){
edge_list *el=v->edges;
- deactivate_vertex(v);
+ deactivate_vertex(g,v);
while(el){
edge_list *next=el->next;
edge *e=el->edge;
@@ -432,9 +435,9 @@
v->grabbed=1;
}
-void ungrab_vertex(vertex *v){
+void ungrab_vertex(graph *g,vertex *v){
edge_list *el=v->edges;
- activate_vertex(v);
+ activate_vertex(g,v);
while(el){
edge_list *next=el->next;
edge *e=el->edge;
@@ -445,8 +448,8 @@
v->grabbed=0;
}
-vertex *find_vertex(int x, int y){
- vertex *v = verticies;
+vertex *find_vertex(graph *g, int x, int y){
+ vertex *v = g->verticies;
vertex *match = 0;
while(v){
@@ -460,7 +463,7 @@
return match;
}
-static void check_vertex_helper(vertex *v, int reactivate){
+static void check_vertex_helper(graph *g, vertex *v, int reactivate){
int flag=0;
if(v->x>=get_board_width()){
@@ -481,43 +484,43 @@
}
if(flag){
if(v->edges){
- deactivate_vertex(v);
- if(reactivate)activate_vertex(v);
+ deactivate_vertex(g,v);
+ if(reactivate)activate_vertex(g,v);
}
}
}
-static void check_vertex(vertex *v){
- check_vertex_helper(v,1);
+static void check_vertex(graph *g, vertex *v){
+ check_vertex_helper(g,v,1);
}
-void check_verticies(){
- vertex *v=verticies;
+void check_verticies(graph *g){
+ vertex *v=g->verticies;
while(v){
vertex *next=v->next;
- check_vertex_helper(v,0);
+ check_vertex_helper(g,v,0);
v=next;
}
- v=verticies;
+ v=g->verticies;
while(v){
vertex *next=v->next;
- activate_vertex(v);
+ activate_vertex(g,v);
v=next;
}
}
-void move_vertex(vertex *v, int x, int y){
- if(!v->grabbed) deactivate_vertex(v);
+void move_vertex(graph *g, vertex *v, int x, int y){
+ if(!v->grabbed) deactivate_vertex(g,v);
v->x=x;
v->y=y;
- check_vertex_helper(v,0);
- if(!v->grabbed) activate_vertex(v);
+ check_vertex_helper(g,v,0);
+ if(!v->grabbed) activate_vertex(g,v);
}
// tenative selection; must be confirmed if next call should not clear
-void select_verticies(int x1, int y1, int x2, int y2){
- vertex *v = verticies;
+void select_verticies(graph *g,int x1, int y1, int x2, int y2){
+ vertex *v = g->verticies;
if(x1>x2){
int temp=x1;
@@ -536,7 +539,7 @@
y2+=V_RADIUS;
while(v){
- vertex *next=v->next;
+
if(v->selected_volatile)v->selected=0;
if(!v->selected){
@@ -546,12 +549,12 @@
}
}
- v=next;
+ v=v->next;
}
}
-int num_selected_verticies(){
- vertex *v = verticies;
+int num_selected_verticies(graph *g){
+ vertex *v = g->verticies;
int count=0;
while(v){
if(v->selected)count++;
@@ -560,8 +563,8 @@
return count;
}
-void deselect_verticies(){
- vertex *v = verticies;
+void deselect_verticies(graph *g){
+ vertex *v = g->verticies;
while(v){
v->selected=0;
@@ -571,8 +574,8 @@
}
-void commit_volatile_selection(){
- vertex *v = verticies;
+void commit_volatile_selection(graph *g){
+ vertex *v = g->verticies;
while(v){
v->selected_volatile=0;
@@ -580,49 +583,49 @@
}
}
-void move_selected_verticies(int dx, int dy){
- vertex *v = verticies;
+void move_selected_verticies(graph *g,int dx, int dy){
+ vertex *v = g->verticies;
/* deactivate selected verticies */
while(v){
vertex *next=v->next;
if(v->selected)
- deactivate_vertex(v);
+ deactivate_vertex(g,v);
v=next;
}
/* move selected verticies and reactivate */
- v=verticies;
+ v=g->verticies;
while(v){
vertex *next=v->next;
if(v->selected){
v->x+=dx;
v->y+=dy;
- check_vertex(v);
- activate_vertex(v);
+ check_vertex(g,v);
+ activate_vertex(g,v);
}
v=next;
}
}
-void scale_verticies(float amount){
- vertex *v=verticies;
+void scale_verticies(graph *g,float amount){
+ vertex *v=g->verticies;
int x=get_board_width()/2;
int y=get_board_height()/2;
while(v){
vertex *next=v->next;
- deactivate_vertex(v);
+ deactivate_vertex(g,v);
v->x=rint((v->x-x)*amount)+x;
v->y=rint((v->y-y)*amount)+y;
v=next;
}
- v=verticies;
+ v=g->verticies;
while(v){
vertex *next=v->next;
- check_vertex(v);
- activate_vertex(v);
+ check_vertex(g,v);
+ activate_vertex(g,v);
v=next;
}
}
@@ -675,32 +678,105 @@
return v;
}
-void randomize_verticies(){
- verticies=randomize_helper(verticies);
+void randomize_verticies(graph *g){
+ g->verticies=randomize_helper(g->verticies);
}
-int get_num_intersections(){
- return reported_intersections;
+vertex *new_board(graph *g, int num_v){
+ set_num_verticies(g,num_v);
+ return g->verticies;
}
-vertex *get_verticies(){
- return verticies;
-}
+// take the mesh, which is always generated for a 'normal sized' board
+// (right now, 800x600), set the original vertex coordinates to the
+// 'nominal' board, and move the 'live' vertex positions to be
+// centered on what the current board size is.
-edge *get_edges(){
- return edges;
+void impress_location(graph *g){
+ int xd = (get_board_width()-get_orig_width())>>1;
+ int yd = (get_board_height()-get_orig_height())>>1;
+ vertex *v=g->verticies;
+ while(v){
+ v->orig_x=v->x;
+ v->orig_y=v->y;
+ v->x+=xd;
+ v->y+=yd;
+ v=v->next;
+ }
}
-vertex *new_board(int num_v){
- set_num_verticies(num_v);
- return verticies;
+/******** read/write board **********/
+
+int graph_write(graph *g, FILE *f){
+ int i;
+ vertex *v=g->verticies;
+ edge *e=g->edges;
+ vertex **flat = alloca(g->vertex_num*sizeof(*flat));
+
+ while(v){
+ flat[v->num]=v;
+ v=v->next;
+ }
+
+ for(i=0;i<g->vertex_num;i++){
+ v = flat[i];
+ fprintf(f,"vertex %d %d %d %d %d\n",
+ v->orig_x,v->orig_y,v->x,v->y,v->selected);
+ }
+
+ while(e){
+ fprintf(f,"edge %d %d\n",e->A->num,e->B->num);
+ e=e->next;
+ }
+
+ return 0;
}
-void impress_location(){
- vertex *v=verticies;
+int graph_read(graph *g,FILE *f){
+ char *line=NULL;
+ int i,x,y,ox,oy,sel,A,B,n=0;
+ vertex **flat,*v;
+
+ new_board(g,0);
+
+ // get all verticies first
+ while(getline(&line,&n,f)>0){
+
+ if(sscanf(line,"vertex %d %d %d %d %d",
+ &ox,&oy,&x,&y,&sel)==5){
+
+ v=get_vertex(g);
+ v->orig_x=ox;
+ v->orig_y=oy;
+ v->x=x;
+ v->y=y;
+ v->selected=sel;
+ }
+ }
+
+ rewind(f);
+ flat=alloca(g->vertex_num*sizeof(*flat));
+ v=g->verticies;
+ i=0;
while(v){
- v->orig_x=v->x;
- v->orig_y=v->y;
+ flat[v->num]=v;
v=v->next;
}
+
+ // get edges next
+ while(getline(&line,&n,f)>0){
+
+ if(sscanf(line,"edge %d %d",&A,&B)==2){
+ if(A>=0 && A<g->vertex_num && B>=0 && B<g->vertex_num)
+ add_edge(g,flat[A],flat[B]);
+ else
+ fprintf(stderr,"WARNING: edge references out of range vertex in save file\n");
+ }
+ }
+
+ rewind(f);
+ free(line);
+ activate_verticies(g);
+
+ return 0;
}
Modified: trunk/planarity/graph.h
===================================================================
--- trunk/planarity/graph.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/graph.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -4,7 +4,6 @@
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
-
typedef struct vertex {
int num;
int x;
@@ -44,29 +43,44 @@
struct edge_list *next;
} edge_list;
-extern void resize_board(int x, int y);
-extern vertex *new_board(int num_v);
-extern vertex *find_vertex(int x, int y);
-extern void move_vertex(vertex *v, int x, int y);
-extern void grab_vertex(vertex *v);
-extern void ungrab_vertex(vertex *v);
-extern void activate_vertex(vertex *v);
-extern void deactivate_vertex(vertex *v);
-extern void select_verticies(int x1, int y1, int x2, int y2);
-extern void deselect_verticies();
-extern void move_selected_verticies(int dx, int dy);
-extern void scale_verticies(float amount);
-extern void randomize_verticies();
-extern edge *add_edge(vertex *A, vertex *B);
+typedef struct graph {
+ vertex *verticies;
+ int vertex_num;
+ edge *edges;
+ int active_intersections;
+ int original_intersections;
+
+ int num_edges;
+ int num_edges_active;
+} graph;
+
+#include <stdio.h>
+
+extern vertex *new_board(graph *g, int num_v);
+extern vertex *find_vertex(graph *g, int x, int y);
+extern void move_vertex(graph *g, vertex *v, int x, int y);
+extern void grab_vertex(graph *g, vertex *v);
+extern void ungrab_vertex(graph *g,vertex *v);
+extern void activate_vertex(graph *g, vertex *v);
+extern void deactivate_vertex(graph *g, vertex *v);
+extern void select_verticies(graph *g, int x1, int y1, int x2, int y2);
+extern void deselect_verticies(graph *g);
+extern void move_selected_verticies(graph *g, int dx, int dy);
+extern void scale_verticies(graph *g, float amount);
+extern void randomize_verticies(graph *g);
+extern edge *add_edge(graph *g,vertex *A, vertex *B);
extern int exists_edge(vertex *a, vertex *b);
extern int get_board_width();
extern int get_board_height();
extern vertex *get_verticies();
extern edge *get_edges();
-extern int num_selected_verticies();
+extern int num_selected_verticies(graph *g);
extern int get_num_intersections();
extern int get_max_intersections();
extern void check_verticies();
extern void impress_location();
extern void commit_volatile_selection();
extern vertex *get_vertex();
+extern void activate_verticies();
+extern int graph_write(graph *g,FILE *f);
+extern int graph_read(graph *g,FILE *f);
Added: trunk/planarity/levelstate.c
===================================================================
--- trunk/planarity/levelstate.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/levelstate.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -0,0 +1,159 @@
+#define CHUNK 64
+#define SAVENAME "levelstate"
+
+typedef struct levelstate{
+ struct levelstate *prev;
+ struct levelstate *next;
+
+ int num;
+ char *name;
+ long highscore;
+ int in_progress;
+} levelstate;
+
+levelstate *head;
+levelstate *tail;
+levelstate *curr;
+levelstate *pool;
+
+levelstate *new_level(){
+ levelstate *ret;
+
+ if(level_pool==0){
+ int i;
+ pool = calloc(CHUNK,sizeof(*pool));
+ for(i=0;i<CHUNK-1;i++) /* last addition's next points to nothing */
+ pool[i].next=pool+i+1;
+ }
+
+ ret=pool;
+ pool=ret->next;
+
+ ret->next=0;
+ ret->prev=tail;
+
+ if(tail){
+ ret->prev->next=ret;
+ ret->num=ret->prev->num+1;
+ }else{
+ head=ret;
+ ret->num=0;
+ }
+
+ ret->name=board_level_to_name(ret->num);
+ ret->highscore=0;
+ ret->in_progress=0;
+
+ return ret;
+}
+
+
+levelstate *find_level(char *name){
+ int level=board_name_to_level(name);
+
+ if(level<0)return 0;
+
+ if(!tail)new_level();
+
+ if(level=<tail->num){
+ // find it in the existing levels
+ levelstate *l=tail;
+ while(l){
+ if(level == l->num) return l;
+ l=l->prev;
+ }
+ return 0;
+ }else{
+ // make new levels to fill
+
+ while(tail->num<level)
+ new_level();
+
+ return tail;
+ }
+}
+
+int levelstate_write(char *statedir){
+ FILE *f;
+ char *name=alloca(strlen(statedir)+strlen(levelstate)+1);
+ name[0]=0;
+ strcat(name,boarddir);
+ strcat(name,levelstate);
+
+ f = fopen(name,"wb");
+ if(f==NULL){
+ fprintf(stderr,"ERROR: Could not save game state file \"%s\":\n\t%s\n",
+ name,strerror(errno));
+ return errno;
+ }
+
+ fprintf(f,"current %d : %s\n",strlen(curr->name),curr->name);
+
+ {
+ levelstate *l=head;
+ while(l){
+ fprintf(f,"level %ld %d %d %s\n",
+ l->highscore,l->in_progress,
+ strlen(l->name),l->name);
+ l=l->next;
+ }
+ }
+
+ return 0;
+}
+
+int levelstate_read(char *statedir, char *boarddir){
+ char *cur_name;
+ int count;
+ char *line=NULL;
+ size_t n=0;
+
+ // first get all levels we've seen.
+ while(getline(&line,&n,f)>0){
+ long l;
+ int i,j;
+ if (sscanf(line,"level %ld %d %d : ",&l,&i,&j)==3){
+ char *name=strchr(line,':');
+ // guard against bad edits
+ if(name &&
+ (strlen(line) - (name - line + 2) >= j)){
+ levelstate *l;
+ name += 2;
+ name[j]=0;
+ l = find_level(name);
+ if(l){
+ l->highscore=l;
+ l->in_progress=i;
+ }
+ }
+ }
+ }
+
+ rewind(f);
+
+ // get current
+ while(getline(&line,&n,f)>0){
+ int i;
+ if (sscanf(line,"current %d : ",&i)==1){
+ char *name=strchr(line,':');
+ // guard against bad edits
+ if(name &&
+ (strlen(line) - (name - line + 2) >= j)){
+ levelstate *l;
+ name += 2;
+ name[j]=0;
+ l = find_level(name);
+ if(l){
+ curr=l;
+ break;
+ }
+ }
+ }
+ }
+
+ if(!head)new_level();
+ if(!curr)curr=head;
+
+ return 0;
+}
+
Modified: trunk/planarity/pause.c
===================================================================
--- trunk/planarity/pause.c 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/pause.c 2005-08-12 03:19:08 UTC (rev 9738)
@@ -11,6 +11,8 @@
#include "pause.h"
#include "box.h"
+static int ui_paused=0;
+static int ui_about=0;
static gint timer;
static void (*callback)(Gameboard *);
@@ -38,6 +40,8 @@
pop_background(g);
deploy_buttonbar(g);
unpause();
+ ui_paused=0;
+ ui_about=0;
}
static void unpause_quit (Gameboard *g){
@@ -188,7 +192,7 @@
setup_pause_buttons(g,PAUSEBOX_WIDTH, PAUSEBOX_HEIGHT);
// draw pausebox
- push_background(g,draw_pausebox);
+ push_curtain(g,draw_pausebox);
// deploy new buttons
callback=0;
@@ -198,8 +202,10 @@
void pause_game(Gameboard *g){
// grab timer state
+ ui_paused=1;
pause();
+ push_background(g,0);
// undeploy buttonbar
undeploy_buttonbar(g,pause_game_post_undeploy);
}
@@ -235,7 +241,7 @@
{
cairo_matrix_t ma;
int y = h/2-ABOUTBOX_HEIGHT/2+SCOREHEIGHT/2;
- cairo_select_font_face (c, "Arial",
+ cairo_select_font_face (c, "Sans",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
@@ -244,14 +250,14 @@
cairo_set_source_rgba (c, TEXT_COLOR);
render_text_centered(c,"gPlanarity", w/2,y);
- cairo_select_font_face (c, "Arial",
+ cairo_select_font_face (c, "Sans",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
y+=45;
render_text_centered(c,"Untangle the mess!", w/2,y);
y+=30;
- cairo_matrix_init_scale (&ma, 14.,14.);
+ cairo_matrix_init_scale (&ma, 13.,13.);
cairo_set_font_matrix (c,&ma);
render_text_centered(c,"Drag verticies to eliminate crossed lines.", w/2,y); y+=16;
render_text_centered(c,"The objective may be a complete solution or", w/2,y); y+=16;
@@ -265,7 +271,7 @@
cairo_stroke(c);
y+=32;
- cairo_matrix_init_scale (&ma, 13.,14.);
+ cairo_matrix_init_scale (&ma, 12.,13.);
cairo_set_font_matrix (c,&ma);
render_text_centered(c,"gPlanarity written by Monty <monty at xiph.org>",w/2,y);y+=17;
render_text_centered(c,"as a demonstration of Gtk+/Cairo",w/2,y);y+=32;
@@ -277,7 +283,7 @@
y = h/2+ABOUTBOX_HEIGHT/2-SCOREHEIGHT/2;
- cairo_select_font_face (c, "Arial",
+ cairo_select_font_face (c, "Sans",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
@@ -290,14 +296,12 @@
cairo_destroy(c);
}
-
-
static void about_game_post_undeploy(Gameboard *g){
// set up new buttons
setup_pause_buttons(g,ABOUTBOX_WIDTH,ABOUTBOX_HEIGHT);
// draw about box
- push_background(g,draw_aboutbox);
+ push_curtain(g,draw_aboutbox);
// deploy new buttons
callback=0;
@@ -307,8 +311,18 @@
void about_game(Gameboard *g){
// grab timer state
+ ui_about=1;
pause();
+ push_background(g,0);
// undeploy buttonbar
undeploy_buttonbar(g,about_game_post_undeploy);
}
+
+int pause_dialog_active(){
+ return ui_paused;
+}
+
+int about_dialog_active(){
+ return ui_about;
+}
Modified: trunk/planarity/pause.h
===================================================================
--- trunk/planarity/pause.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/pause.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -3,9 +3,11 @@
#define PAUSEBOX_WIDTH 180
#define PAUSEBOX_HEIGHT 250
-#define ABOUTBOX_WIDTH 300
+#define ABOUTBOX_WIDTH 320
#define ABOUTBOX_HEIGHT 400
extern void pause_game(Gameboard *g);
extern void about_game(Gameboard *g);
+extern int pause_dialog_active();
+extern int about_dialog_active();
Modified: trunk/planarity/version.h
===================================================================
--- trunk/planarity/version.h 2005-08-12 03:08:33 UTC (rev 9737)
+++ trunk/planarity/version.h 2005-08-12 03:19:08 UTC (rev 9738)
@@ -1,2 +1,2 @@
#define VERSION "$Id$ "
-/* DO NOT EDIT: Automated versioning hack [Thu Aug 11 00:13:45 EDT 2005] */
+/* DO NOT EDIT: Automated versioning hack [Thu Aug 11 20:51:22 EDT 2005] */
More information about the commits
mailing list