[xiph-commits] r9649 - trunk/planarity
xiphmont at svn.xiph.org
xiphmont at svn.xiph.org
Sat Jul 30 01:36:14 PDT 2005
Author: xiphmont
Date: 2005-07-30 01:36:08 -0700 (Sat, 30 Jul 2005)
New Revision: 9649
Added:
trunk/planarity/pause.c
trunk/planarity/pause.h
Modified:
trunk/planarity/Makefile
trunk/planarity/box.c
trunk/planarity/box.h
trunk/planarity/buttonbar.c
trunk/planarity/buttons.c
trunk/planarity/gameboard.c
trunk/planarity/gameboard.h
trunk/planarity/gamestate.c
trunk/planarity/gamestate.h
trunk/planarity/version.h
Log:
Pause implemeneted.
Modified: trunk/planarity/Makefile
===================================================================
--- trunk/planarity/Makefile 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/Makefile 2005-07-30 08:36:08 UTC (rev 9649)
@@ -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
+ buttons.c buttonbar.c box.c pause.c
OBJ = graph.o arrange.o gameboard.o generate.o gamestate.o button_base.o\
- buttons.o buttonbar.o box.o
+ buttons.o buttonbar.o box.o pause.o
GCF = `pkg-config --static --cflags "gtk+-2.0 >= 2.7.2"`
# uncomment below for a normal dynamic build
Modified: trunk/planarity/box.c
===================================================================
--- trunk/planarity/box.c 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/box.c 2005-07-30 08:36:08 UTC (rev 9649)
@@ -163,3 +163,89 @@
}
+void centerbox (cairo_t *c, int x, int y, double w, double h){
+
+ double x0 = B_BORDER+B_RADIUS;
+ double y0 = h-B_BORDER;
+
+ double x1 = B_BORDER;
+ double y1 = h-B_BORDER-B_RADIUS;
+
+ double x2 = B_BORDER;
+ double y2 = B_BORDER+B_RADIUS;
+
+ double x3 = B_BORDER+B_RADIUS;
+ double y3 = B_BORDER;
+
+ double x8 = w - B_BORDER -B_RADIUS;
+ double y8 = B_BORDER;
+
+ double x9 = w - B_BORDER;
+ double y9 = B_BORDER+B_RADIUS;
+
+ double x10 = w - B_BORDER;
+ double y10 = h- B_BORDER -B_RADIUS;
+
+ double x11 = w - B_BORDER-B_RADIUS;
+ double y11 = h-B_BORDER;
+
+ cairo_save(c);
+ cairo_translate(c,x,y);
+ cairo_set_line_width(c,B_LINE);
+
+ cairo_move_to (c, x0, y0);
+ cairo_curve_to (c, x1,y0, x1,y0, x1,y1);
+ cairo_line_to (c, x2,y2);
+ cairo_curve_to (c, x2,y3, x2,y3, x3,y3);
+ cairo_line_to (c, x8,y8);
+ cairo_curve_to (c, x9,y8, x9,y8, x9,y9);
+ cairo_line_to (c, x10,y10);
+ cairo_curve_to (c, x10,y11, x10,y11, x11,y11);
+ cairo_close_path (c);
+
+ cairo_set_source_rgba (c, B_COLOR);
+ cairo_fill_preserve (c);
+ cairo_set_source_rgba (c, B_LINE_COLOR);
+ cairo_stroke (c);
+
+ cairo_restore(c);
+}
+
+void borderbox_path (cairo_t *c, int x, int y, double w, double h){
+
+ double x0 = x+ B_RADIUS;
+ double y0 = y+ h;
+
+ double x1 = x;
+ double y1 = y+ h-B_RADIUS;
+
+ double x2 = x;
+ double y2 = y+ B_RADIUS;
+
+ double x3 = x+ B_RADIUS;
+ double y3 = y;
+
+ double x8 = x+ w -B_RADIUS;
+ double y8 = y;
+
+ double x9 = x+ w;
+ double y9 = y+ B_RADIUS;
+
+ double x10 = x+ w;
+ double y10 = y+ h -B_RADIUS;
+
+ double x11 = x+ w -B_RADIUS;
+ double y11 = y+h;
+
+ cairo_move_to (c, x0, y0);
+ cairo_curve_to (c, x1,y0, x1,y0, x1,y1);
+ cairo_line_to (c, x2,y2);
+ cairo_curve_to (c, x2,y3, x2,y3, x3,y3);
+ cairo_line_to (c, x8,y8);
+ cairo_curve_to (c, x9,y8, x9,y8, x9,y9);
+ cairo_line_to (c, x10,y10);
+ cairo_curve_to (c, x10,y11, x10,y11, x11,y11);
+ cairo_close_path (c);
+
+}
+
Modified: trunk/planarity/box.h
===================================================================
--- trunk/planarity/box.h 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/box.h 2005-07-30 08:36:08 UTC (rev 9649)
@@ -12,3 +12,5 @@
extern void topbox (cairo_t *c, double w, double h);
extern void bottombox (cairo_t *c, double w, double h);
+extern void centerbox (cairo_t *c, int x, int y, double w, double h);
+extern void borderbox_path (cairo_t *c, int x, int y, double w, double h);
Modified: trunk/planarity/buttonbar.c
===================================================================
--- trunk/planarity/buttonbar.c 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/buttonbar.c 2005-07-30 08:36:08 UTC (rev 9649)
@@ -8,6 +8,7 @@
#include "gamestate.h"
#include "button_base.h"
#include "buttonbar.h"
+#include "pause.h"
/************************ the lower button bar *********************/
@@ -103,6 +104,7 @@
states[0].callback = quit;
states[2].callback = reset_board;
+ states[3].callback = pause_game;
states[5].callback = expand;
states[6].callback = shrink;
states[7].callback = hide_show_lines;
@@ -148,15 +150,17 @@
b->x_active=b->target_x_active=count;
b->x_inactive=b->target_x_inactive=count+BUTTON_EXPOSE;
b->y = h - BUTTONBAR_Y_FROM_BOTTOM;
- if(i!=9) // special-case the checkbutton
+ if(i!=9 || checkbutton_deployed) // special-case the checkbutton
count -= BUTTONBAR_SPACING;
}
}
// special-case the checkbutton
- states[9].target_x_inactive=states[9].target_x_active+BUTTONBAR_SPACING;
- states[9].x_inactive=states[9].target_x_inactive;
- states[9].target_x=states[9].target_x_inactive;
+ if(!checkbutton_deployed){
+ states[9].target_x_inactive=states[9].target_x_active+BUTTONBAR_SPACING;
+ states[9].x_inactive=states[9].target_x_inactive;
+ states[9].target_x=states[9].target_x_inactive;
+ }
for(i=0;i<NUMBUTTONS;i++)
if(states[i].position)
@@ -167,10 +171,13 @@
/* effects animated 'rollout' of buttons when level begins */
void deploy_buttonbar(Gameboard *g){
if(!buttons_ready ){
+ if(get_num_intersections() <= get_objective())
+ checkbutton_deployed=1;
+ else
+ checkbutton_deployed=0;
setup_buttonbar(g);
timer = g_timeout_add(BUTTON_ANIM_INTERVAL, buttonbar_animate_buttons, (gpointer)g);
buttons_ready=1;
- checkbutton_deployed=0;
}
}
Modified: trunk/planarity/buttons.c
===================================================================
--- trunk/planarity/buttons.c 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/buttons.c 2005-07-30 08:36:08 UTC (rev 9649)
@@ -261,9 +261,9 @@
cairo_arc(c,0,0,14,0,2*M_PI);
- cairo_move_to(c,-10,-8);
- cairo_line_to(c,0,10);
- cairo_line_to(c,-10,8);
+ cairo_move_to(c,-8,-8);
+ cairo_line_to(c,10,0);
+ cairo_line_to(c,-8,8);
cairo_close_path(c);
cairo_fill_preserve(c);
Modified: trunk/planarity/gameboard.c
===================================================================
--- trunk/planarity/gameboard.c 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/gameboard.c 2005-07-30 08:36:08 UTC (rev 9649)
@@ -551,10 +551,12 @@
GdkEventMotion *event){
Gameboard *g = GAMEBOARD (widget);
- if(g->button_grabbed){
+ if(g->button_grabbed || paused_p()){
g->button_grabbed =
button_motion_event(g,event,
(!g->lit_vertex && !g->grabbed_vertex && !g->selection_grab));
+
+ if(paused_p())return TRUE;
}
if(!g->button_grabbed){
@@ -627,6 +629,13 @@
vertex *v = find_vertex((int)event->x,(int)event->y);
g->button_grabbed=0;
+ if(paused_p()){
+ // only buttongrabs
+ if(button_button_press(g,event,1))
+ g->button_grabbed=1;
+ return TRUE;
+ }
+
if(!g->group_drag && event->state&GDK_SHIFT_MASK){
if(v){
if(v->selected){
@@ -715,6 +724,7 @@
Gameboard *g = GAMEBOARD (widget);
button_button_release(g,event,1);
+ if(paused_p())return TRUE;
if(g->grabbed_vertex){
ungrab_vertex(g->grabbed_vertex);
@@ -806,6 +816,55 @@
}
}
+void pop_background(Gameboard *g){
+ if(g->pushed_background){
+ g->pushed_background=0;
+ update_full(g);
+ }
+}
+
+void push_background(Gameboard *g){
+ cairo_t *c = cairo_create(g->background);
+ int w = g->w.allocation.width;
+ int h = g->w.allocation.height;
+
+ if(g->pushed_background)
+ pop_background(g);
+
+ foreground_draw(g,c,0,0,w,h);
+
+ // copy in the score and button surfaces
+ cairo_set_source_surface(c,g->forescore,0,0);
+ cairo_rectangle(c, 0,0,w,
+ min(SCOREHEIGHT,h));
+ cairo_fill(c);
+
+ cairo_set_source_surface(c,g->forebutton,0,g->w.allocation.height-SCOREHEIGHT);
+ 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);
+ }
+
+ cairo_destroy(c);
+
+ g->pushed_background=1;
+ {
+ 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){
@@ -819,31 +878,34 @@
cairo_set_source_surface(c,g->background,0,0);
cairo_rectangle(c,x,y,w,h);
cairo_fill(c);
- foreground_draw(g,c,x,y,w,h);
- // copy in any of the score or button surfaces?
- if(y<SCOREHEIGHT){
- cairo_set_source_surface(c,g->forescore,0,0);
- cairo_rectangle(c, x,y,w,
- min(SCOREHEIGHT-y,h));
- cairo_fill(c);
+ if(!g->pushed_background){
+ foreground_draw(g,c,x,y,w,h);
+
+ // copy in any of the score or button surfaces?
+ if(y<SCOREHEIGHT){
+ cairo_set_source_surface(c,g->forescore,0,0);
+ cairo_rectangle(c, x,y,w,
+ min(SCOREHEIGHT-y,h));
+ cairo_fill(c);
+ }
+ if(y+h>g->w.allocation.height-SCOREHEIGHT){
+ cairo_set_source_surface(c,g->forebutton,0,g->w.allocation.height-SCOREHEIGHT);
+ cairo_rectangle(c, x,y,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,x,y,w,h);
+ }
}
- if(y+h>g->w.allocation.height-SCOREHEIGHT){
- cairo_set_source_surface(c,g->forebutton,0,g->w.allocation.height-SCOREHEIGHT);
- cairo_rectangle(c, x,y,w,h);
- cairo_fill(c);
- }
+
expose_buttons(g,c,x,y,w,h);
- 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);
- }
-
cairo_destroy(c);
// blit to window
Modified: trunk/planarity/gameboard.h
===================================================================
--- trunk/planarity/gameboard.h 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/gameboard.h 2005-07-30 08:36:08 UTC (rev 9649)
@@ -35,6 +35,8 @@
struct _Gameboard{
GtkWidget w;
+ int pushed_background;
+
cairo_t *wc;
cairo_surface_t *vertex;
cairo_surface_t *vertex_lit;
@@ -100,3 +102,5 @@
extern void update_full(Gameboard *g);
extern double get_curtain(Gameboard *g);
extern void set_curtain(Gameboard *g, double alpha);
+extern void pop_background(Gameboard *g);
+extern void push_background(Gameboard *g);
Modified: trunk/planarity/gamestate.c
===================================================================
--- trunk/planarity/gamestate.c 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/gamestate.c 2005-07-30 08:36:08 UTC (rev 9649)
@@ -19,7 +19,24 @@
static int initial_intersections;
static int objective=0;
static int objective_lessthan=0;
+static int paused=0;
+static time_t begin_time_add=0;
+static time_t begin_time;
+time_t get_elapsed(){
+ if(paused)
+ return begin_time_add;
+ else{
+ time_t ret = time(NULL);
+ return ret - begin_time + begin_time_add;
+ }
+}
+
+void set_timer(time_t off){
+ begin_time_add = off;
+ begin_time = time(NULL);
+}
+
static gboolean key_press(GtkWidget *w,GdkEventKey *event,gpointer in){
if(event->keyval == GDK_q && event->state&GDK_CONTROL_MASK)
@@ -50,6 +67,8 @@
//gdk_flush();
deploy_buttonbar(gameboard);
+ set_timer(0);
+ unpause();
}
#define RESET_DELTA 2;
@@ -110,9 +129,26 @@
undeploy_check(gameboard);
}
update_full(gameboard);
+
// reset timer
+ set_timer(0);
+ unpause();
}
+void pause(){
+ begin_time_add = get_elapsed();
+ paused=1;
+}
+
+void unpause(){
+ paused=0;
+ set_timer(begin_time_add);
+}
+
+int paused_p(){
+ return paused;
+}
+
void scale(double scale){
int x=get_board_width()/2;
int y=get_board_height()/2;
@@ -182,6 +218,7 @@
void finish_board(){
if(get_num_intersections()<=initial_intersections){
+ pause();
score+=initial_intersections;
level++;
undeploy_buttonbar(gameboard,setup_board);
Modified: trunk/planarity/gamestate.h
===================================================================
--- trunk/planarity/gamestate.h 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/gamestate.h 2005-07-30 08:36:08 UTC (rev 9649)
@@ -1,3 +1,4 @@
+#include <time.h>
extern void resize_board(int x, int y);
extern int get_board_width();
@@ -15,3 +16,9 @@
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 void set_timer(time_t off);
Added: trunk/planarity/pause.c
===================================================================
--- trunk/planarity/pause.c 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/pause.c 2005-07-30 08:36:08 UTC (rev 9649)
@@ -0,0 +1,207 @@
+#include <math.h>
+#include <string.h>
+#include <gtk/gtk.h>
+#include <gdk/gdk.h>
+
+#include "graph.h"
+#include "gameboard.h"
+#include "gamestate.h"
+#include "button_base.h"
+#include "buttonbar.h"
+#include "pause.h"
+#include "box.h"
+
+static gint timer;
+static void (*callback)(Gameboard *);
+
+/* perform a single frame of animation for all pause dialog buttons/rollovers */
+static gboolean pause_animate_buttons(gpointer ptr){
+ Gameboard *g=(Gameboard *)ptr;
+ int ret=0;
+
+ ret=animate_button_frame(g);
+
+ if(!ret && timer!=0){
+ g_source_remove(timer);
+ timer=0;
+ }
+
+ if(!ret && callback)
+ // undeploy finished... call the undeploy callback
+ callback(g);
+
+ return ret;
+}
+
+static void unpause_post (Gameboard *g){
+ // back to buttonbar activity!
+ pop_background(g);
+ deploy_buttonbar(g);
+ unpause();
+}
+
+static void unpause_quit (Gameboard *g){
+ quit();
+}
+
+static void undeploy_buttons(Gameboard *g){
+ // undeploy pause buttons
+ button_clear_state(g);
+ buttons_ready=0;
+
+ {
+ buttonstate *b=states;
+ b->target_x-=BUTTON_EXPOSE;
+ }
+ {
+ buttonstate *b=states+10;
+ b->target_x+=BUTTON_EXPOSE;
+ }
+}
+
+static void local_unpause (Gameboard *g){
+ undeploy_buttons(g);
+ callback = unpause_post;
+ timer = g_timeout_add(BUTTON_ANIM_INTERVAL, pause_animate_buttons, (gpointer)g);
+}
+
+static void local_quit (Gameboard *g){
+ undeploy_buttons(g);
+ callback = unpause_quit;
+ timer = g_timeout_add(BUTTON_ANIM_INTERVAL, pause_animate_buttons, (gpointer)g);
+}
+
+/* initialize the rather weird little animation engine */
+static void setup_pause_buttons(Gameboard *g){
+ int i;
+ int w=get_board_width();
+ int h=get_board_height();
+
+ states[0].rollovertext="exit gPlanarity";
+ states[10].rollovertext="resume game!";
+
+ states[0].callback = local_quit;
+ states[10].callback = local_unpause;
+
+ for(i=0;i<NUMBUTTONS;i++)
+ states[i].position=0;
+
+ states[0].position = 2; //center;
+ states[10].position = 2; //center;
+
+ {
+ buttonstate *b=states;
+ b->target_x_active=
+ b->x_active=
+ b->target_x_active=
+ b->target_x=
+ w/2 - PAUSEBOX_WIDTH/2 + PAUSE_BUTTON_BORDER;
+ b->x=b->target_x_inactive=b->x_inactive=b->target_x - BUTTON_EXPOSE;
+ b->y = h/2 + PAUSEBOX_HEIGHT/2 - PAUSE_BUTTON_Y;
+ }
+
+ {
+ buttonstate *b=states+10;
+ b->target_x_active=
+ b->x_active=
+ b->target_x_active=
+ b->target_x=
+ w/2 + PAUSEBOX_WIDTH/2 - PAUSE_BUTTON_BORDER;
+ b->x=b->target_x_inactive=b->x_inactive=b->target_x + BUTTON_EXPOSE;
+ b->y = h/2 + PAUSEBOX_HEIGHT/2 - PAUSE_BUTTON_Y;
+ }
+
+ for(i=0;i<NUMBUTTONS;i++)
+ if(states[i].position)
+ rollover_extents(g,states+i);
+}
+
+static void render_text_centered(cairo_t *c, char *s, int x, int y){
+ cairo_text_extents_t ex;
+
+ cairo_text_extents (c, s, &ex);
+ cairo_move_to (c, x-(ex.width/2)-ex.x_bearing, y-(ex.height/2)-ex.y_bearing);
+ cairo_show_text (c, s);
+}
+
+static void draw_pausebox(Gameboard *g){
+ int w= get_board_width();
+ int h= get_board_height();
+
+ push_background(g);
+
+ cairo_t *c = cairo_create(g->background);
+ borderbox_path(c,
+ w/2 - PAUSEBOX_WIDTH/2,
+ h/2 - PAUSEBOX_HEIGHT/2,
+ PAUSEBOX_WIDTH,
+ PAUSEBOX_HEIGHT);
+ cairo_set_source_rgb(c,1,1,1);
+ cairo_fill(c);
+
+ centerbox(c,
+ w/2 - PAUSEBOX_WIDTH/2,
+ h/2 - PAUSEBOX_HEIGHT/2,
+ PAUSEBOX_WIDTH,
+ SCOREHEIGHT);
+
+ centerbox(c,
+ w/2 - PAUSEBOX_WIDTH/2 ,
+ h/2 + PAUSEBOX_HEIGHT/2 - SCOREHEIGHT,
+ PAUSEBOX_WIDTH,
+ SCOREHEIGHT);
+
+ {
+ cairo_matrix_t ma;
+ char time[160];
+ int ho = get_elapsed() / 3600;
+ int mi = get_elapsed() / 60 - ho*60;
+ int se = get_elapsed() - ho*3600 - mi*60;
+
+ if(ho){
+ snprintf(time,160,"%d:%02d:%02d",ho,mi,se);
+ }else if (mi){
+ snprintf(time,160,"%d:%02d",mi,se);
+ }else{
+ snprintf(time,160,"%d seconds",se);
+ }
+
+ cairo_select_font_face (c, "Arial",
+ CAIRO_FONT_SLANT_NORMAL,
+ CAIRO_FONT_WEIGHT_BOLD);
+
+ cairo_matrix_init_scale (&ma, 18.,18.);
+ cairo_set_font_matrix (c,&ma);
+ cairo_set_source_rgba (c, TEXT_COLOR);
+
+ render_text_centered(c,"Game Paused", w/2,h/2-PAUSEBOX_HEIGHT/2+SCOREHEIGHT/2);
+ cairo_select_font_face (c, "Arial",
+ CAIRO_FONT_SLANT_NORMAL,
+ CAIRO_FONT_WEIGHT_NORMAL);
+ render_text_centered(c,"Time Elapsed:", w/2,h/2-30);
+ render_text_centered(c,time, w/2,h/2);
+ }
+
+ cairo_destroy(c);
+}
+
+static void pause_game_post_undeploy(Gameboard *g){
+ // set up new buttons
+ setup_pause_buttons(g);
+
+ // draw pausebox
+ draw_pausebox(g);
+
+ // deploy new buttons
+ callback=0;
+ timer = g_timeout_add(BUTTON_ANIM_INTERVAL, pause_animate_buttons, (gpointer)g);
+ buttons_ready=1;
+}
+
+void pause_game(Gameboard *g){
+ // grab timer state
+ pause();
+
+ // undeploy buttonbar
+ undeploy_buttonbar(g,pause_game_post_undeploy);
+}
Added: trunk/planarity/pause.h
===================================================================
--- trunk/planarity/pause.h 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/pause.h 2005-07-30 08:36:08 UTC (rev 9649)
@@ -0,0 +1,7 @@
+#define PAUSE_BUTTON_BORDER 35
+#define PAUSE_BUTTON_Y 25
+#define PAUSEBOX_WIDTH 180
+#define PAUSEBOX_HEIGHT 250
+
+extern void pause_game(Gameboard *g);
+
Modified: trunk/planarity/version.h
===================================================================
--- trunk/planarity/version.h 2005-07-30 06:16:21 UTC (rev 9648)
+++ trunk/planarity/version.h 2005-07-30 08:36:08 UTC (rev 9649)
@@ -1,2 +1,2 @@
/* DO NOT EDIT: Automated versioning hack [Thu Jul 21 05:46:15 EDT 2005] */
-/* DO NOT EDIT: Automated versioning hack [Sat Jul 30 02:12:52 EDT 2005] */
+/* DO NOT EDIT: Automated versioning hack [Sat Jul 30 04:32:47 EDT 2005] */
More information about the commits
mailing list