[cairo-commit] cairo-demo/gtkcairo_slide ChangeLog, 1.8,
1.9 gtkcairo_slide.c, 1.3, 1.4
Oeyvind Kolaas
commit at pdx.freedesktop.org
Fri Apr 29 11:24:12 PDT 2005
Committed by: pippin
Update of /cvs/cairo/cairo-demo/gtkcairo_slide
In directory gabe:/tmp/cvs-serv30295
Modified Files:
ChangeLog gtkcairo_slide.c
Log Message:
added greeting scroll
Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/gtkcairo_slide/ChangeLog,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- ChangeLog 20 Jan 2005 16:33:29 -0000 1.8
+++ ChangeLog 29 Apr 2005 18:24:10 -0000 1.9
@@ -1,3 +1,8 @@
+2005-04-29 Ãyvind KolÃ¥s <pippin at freedesktop.org>
+
+ * gtkcairo_slide.c: reordered functions, made game easier and added
+ welcome scroll/splash.
+
2005-01-20 Carl Worth <cworth at cworth.org>
* puzzle.c: Add include of stdio.h now that cairo.h doesn't
Index: gtkcairo_slide.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/gtkcairo_slide/gtkcairo_slide.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- gtkcairo_slide.c 11 Nov 2004 17:18:47 -0000 1.3
+++ gtkcairo_slide.c 29 Apr 2005 18:24:10 -0000 1.4
@@ -1,73 +1,209 @@
+/* GtkCairo slide, a sample game built around the puzzle widget */
+
+/* since the puzzle widget leaves the drawing behavior in a sane state, we can hook up the menu / score
+ * display on top of it, this might be considered an evil hack, but might make some programming easier
+ */
+
#include <gtkcairo.h>
#include "puzzle.h"
-static void
-puzzle_solved (GtkWidget *widget,
- gpointer data)
+#define INITIAL_WIDTH 200
+#define INITIAL_HEIGHT 210
+
+GtkWidget *puzzle_window;
+GtkWidget *puzzle;
+
+GList *messages = NULL;
+double y_pos = 0.0;
+double y_inc = 0.1;
+
+double fade = 0.0;
+
+int rows = 3;
+int cols = 3;
+int shuffles = 15;
+
+gboolean update_messages (gpointer data)
{
- GtkWidget *dialog;
- GtkWidget *label;
- GtkWidget *parent;
+ if (messages)
+ {
+ y_pos += y_inc;
+ if (y_pos > 1.0)
+ {
+ char *msg = messages->data;
+ messages = g_list_remove (messages, msg);
+ g_free (msg);
+ y_pos = 0.0;
+ }
+ gtk_widget_queue_draw (puzzle);
+ }
+ return TRUE;
+}
- parent = gtk_widget_get_toplevel (widget);
+void add_message (const char *message)
+{
+ messages = g_list_append (messages, g_strdup(message));
+}
- dialog = gtk_dialog_new_with_buttons ("GtkCairoSlide - solved",
- GTK_WINDOW (parent),
- GTK_DIALOG_MODAL,
- GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
- NULL);
+static GtkWidget *
+puzzle_window_new (void);
- label = gtk_label_new ("You solved the puzzle!");
- gtk_misc_set_padding (GTK_MISC (label), 20, 20);
- gtk_widget_show (label);
+gint
+main (gint argc,
+ gchar *argv[])
+{
- gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
- gtk_widget_show (dialog);
+ gtk_init (&argc, &argv);
- gtk_dialog_run (GTK_DIALOG (dialog));
- gtk_main_quit ();
+ puzzle_window = puzzle_window_new ();
+ gtk_widget_show (puzzle_window);
+ add_message ("");
+ add_message ("GtkCairo slide");
+ add_message ("");
+ add_message ("order the boxes");
+ g_timeout_add (100, update_messages ,puzzle);
+
+ gtk_main ();
+
+ return 0;
}
+static void paint_status (GtkCairo *gtkcairo,
+ cairo_t *cr,
+ gpointer user_data);
+
static void
-show_game (void) {
- GtkWidget *win;
+puzzle_solved (GtkWidget *widget,
+ gpointer data);
+
+static GtkWidget *
+puzzle_window_new (void) {
+ GtkWidget *self;
GtkWidget *vbox;
- GtkWidget *puzzle;
- win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- gtk_window_set_title (GTK_WINDOW (win), "Sliding GtkCairo Puzzle");
- g_signal_connect (G_OBJECT (win), "delete-event",
+ self = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_title (GTK_WINDOW (self), "Sliding GtkCairo Puzzle");
+ g_signal_connect (G_OBJECT (self), "delete-event",
G_CALLBACK (gtk_main_quit), NULL);
- vbox = gtk_vbox_new (FALSE, 6);
- gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
+ vbox = gtk_vbox_new (FALSE, 0);
+ gtk_container_set_border_width (GTK_CONTAINER (vbox), 0);
puzzle = puzzle_new ();
- g_object_set (G_OBJECT (puzzle), "rows", 4, NULL);
- g_object_set (G_OBJECT (puzzle), "cols", 4, NULL);
- g_object_set (G_OBJECT (puzzle), "shuffles", 2048, NULL);
+ g_signal_connect (G_OBJECT (puzzle), "paint",
+ G_CALLBACK (paint_status), NULL);
- gtk_widget_set_usize (GTK_WIDGET (puzzle), 192, 192);
+ g_object_set (G_OBJECT (puzzle), "rows", rows, NULL);
+ g_object_set (G_OBJECT (puzzle), "cols", cols, NULL);
+ g_object_set (G_OBJECT (puzzle), "shuffles", shuffles, NULL);
+
+ gtk_widget_set_usize (GTK_WIDGET (puzzle), INITIAL_WIDTH, INITIAL_HEIGHT);
g_signal_connect (G_OBJECT (puzzle), "puzzle_solved",
G_CALLBACK (puzzle_solved), puzzle);
gtk_container_add (GTK_CONTAINER (vbox), puzzle);
- gtk_container_add (GTK_CONTAINER (win), vbox);
+ gtk_container_add (GTK_CONTAINER (self), vbox);
gtk_widget_show_all (vbox);
- gtk_widget_show (win);
+
+ return self;
}
-gint
-main (gint argc,
- gchar *argv[])
+
+static void paint_status (GtkCairo *gtkcairo,
+ cairo_t *cr,
+ gpointer user_data)
{
- gtk_init (&argc, &argv);
- show_game ();
- gtk_main ();
+ GtkWidget *widget = GTK_WIDGET (gtkcairo);
- return 0;
+ double w = widget->allocation.width;
+ double h = widget->allocation.height;
+
+ if (!messages) /* bail out early, if nothing to paint */
+ return;
+
+ cairo_save (cr);
+ {
+ if (fade>0.01)
+ {
+ cairo_set_source_rgba (cr, 0,0,0, fade);
+ cairo_rectangle (cr, 0, 0, w*1.0, h*1.0);
+ cairo_fill (cr);
+ }
+
+#define FONT_HEIGHT 0.1
+#define LINE_HEIGHT 1.3
+
+ cairo_set_font_size (cr, h*FONT_HEIGHT);
+ cairo_select_font (cr, "Sans", 0, 0);
+
+ {
+ GList *item = messages;
+ double x,y;
+
+ /*
+ char utf8[42];
+ snprintf (utf8, 42, "%iÃ%i @%i", cols, rows, shuffles);
+ */
+
+ y = FONT_HEIGHT * LINE_HEIGHT - y_pos * FONT_HEIGHT * LINE_HEIGHT;
+ while (item)
+ {
+ cairo_text_extents_t extents;
+ const char *utf8;
+
+ utf8 = item->data;
+ cairo_text_extents (cr, utf8, &extents);
+ x = 0.5 - (extents.width / 2 + extents.x_bearing) / w;
+ cairo_move_to (cr, w*x, h*y);
+ cairo_text_path (cr, utf8);
+
+ cairo_save (cr);
+ {
+ cairo_set_source_rgba (cr, 0,0,0,0.2);
+ cairo_set_line_width (cr, h*0.02);
+ cairo_stroke (cr);
+ }
+ cairo_restore (cr);
+ cairo_set_source_rgba (cr, 1,1,1,1.0);
+ cairo_fill (cr);
+
+ y+= FONT_HEIGHT * LINE_HEIGHT;
+ item = g_list_next (item);
+ }
+
+
+ }
+ }
+ cairo_restore (cr);
+
}
+static void
+puzzle_solved (GtkWidget *widget,
+ gpointer data)
+{
+ GtkWidget *dialog;
+ GtkWidget *label;
+ GtkWidget *parent;
+
+ parent = gtk_widget_get_toplevel (widget);
+
+ dialog = gtk_dialog_new_with_buttons ("GtkCairoSlide - solved",
+ GTK_WINDOW (parent),
+ GTK_DIALOG_MODAL,
+ GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
+ NULL);
+
+ label = gtk_label_new ("You solved the puzzle!");
+ gtk_misc_set_padding (GTK_MISC (label), 20, 20);
+ gtk_widget_show (label);
+
+ gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
+ gtk_widget_show (dialog);
+
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_main_quit ();
+}
More information about the cairo-commit
mailing list