gbooleans are more than one byte. Who knew.
[rfk] / rfk.c
diff --git a/rfk.c b/rfk.c
index f9d997b..95dfec0 100644 (file)
--- a/rfk.c
+++ b/rfk.c
@@ -3,9 +3,10 @@
  *  ported to maemo by Thomas Thurman, 2009
  *  suggestions welcome
  *  Compile with:
- *  gcc -Wall -g rfk.c -o rfk `pkg-config --cflags --libs gtk+-2.0 hildon-1`
+ *  gcc -Wall -g rfk.c -o rfk `pkg-config --cflags --libs gtk+-2.0 hildon-1 dbus-glib-1 dbus-1`
  */
 
+#include <dbus/dbus-glib.h>
 #include <gtk/gtk.h>
 #include <stdlib.h>
 #include <glib.h>
 
 const int amount_of_random_stuff = 15;
 
-const char *explanation =
-  "In this game, you are robot (#). "
-  "Your job is to find kitten. This task is complicated "
-  "by the existence of various things which are not kitten. "
-  "Robot must touch items to determine if they are kitten or "
-  "not. The game ends when robotfindskitten. You may move "
-  "robot about by tapping on any side of robot, or with the "
-  "cursor keys.";
+typedef enum {
+  STATE_PROLOGUE,
+  STATE_PLAYING,
+  STATE_EPILOGUE,
+  STATE_LAST
+} StateOfPlay;
+
+StateOfPlay current_state = STATE_LAST;
+GtkWidget* state_widget[STATE_LAST];
 
 GSList *nki = NULL;
 guint nki_count = 0;
 
 GtkWidget *arena[ARENA_WIDTH][ARENA_HEIGHT];
-GtkWidget *table, *window, *robot, *kitten;
+GtkWidget *window, *robot, *kitten;
 int robot_x, robot_y;
 gboolean *used = NULL;
 
 GdkPixbuf *robot_pic, *love_pic, *kitten_pic;
-GtkWidget *animation_area;
 
 const GdkColor black = { 0, };
 
@@ -55,8 +56,8 @@ description (void)
       r = random() % nki_count;
     }
   while (used[r]);
-
   used[r] = TRUE;
+
   return g_slist_nth_data (nki, r);
 }
 
@@ -69,7 +70,7 @@ place_in_arena_at_xy (GtkWidget *item, int x, int y)
 {
   arena[x][y] = item;
 
-  gtk_table_attach_defaults (GTK_TABLE (table),
+  gtk_table_attach_defaults (GTK_TABLE (state_widget[STATE_PLAYING]),
                             item,
                             x, x+1,
                             y, y+1);
@@ -129,7 +130,8 @@ show_message (const char *message)
 {
   HildonNote* note = HILDON_NOTE
     (hildon_note_new_information (GTK_WINDOW (window),
-                                 message));
+                                 message?message:
+                                 "Some message was supposed to be here."));
   gtk_dialog_run (GTK_DIALOG (note));
   gtk_widget_destroy (GTK_WIDGET (note));
 }
@@ -158,7 +160,10 @@ ensure_messages_loaded (void)
   while (!feof (nki_file))
     {
       char newline;
-      fscanf (nki_file, "%a[^\n]%c", &line, &newline);
+      if (fscanf (nki_file, "%a[^\n]%c", &line, &newline) == EOF)
+       {
+         break;
+       }
 
       if (strcmp(line, "")==0)
        {
@@ -179,81 +184,135 @@ ensure_messages_loaded (void)
     }
 
   fclose (nki_file);
+}
 
-  used = g_malloc0 (nki_count);
+void
+load_images (void)
+{
+  robot_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-robot.png", NULL);
+  love_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-love.png", NULL);
+  kitten_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-kitten.png", NULL);
+}
+
+/****************************************************************/
+/* Stop doing that, and do something else.                      */
+/****************************************************************/
+static void
+switch_state (StateOfPlay new_state)
+{
+  if (current_state != STATE_LAST)
+    {
+      gtk_container_remove (GTK_CONTAINER (window), state_widget[current_state]);
+    }
+  gtk_container_add (GTK_CONTAINER (window), state_widget[new_state]);
+
+  gtk_widget_show_all (window);
+  gdk_window_set_events (GTK_WIDGET (window)->window,
+                        gdk_window_get_events(GTK_WIDGET (window)->window) | GDK_BUTTON_PRESS_MASK);
+
+  current_state = new_state;
 }
 
 /****************************************************************/
 /* The ending animation.                                        */
 /****************************************************************/
 
+gboolean animation_running = FALSE;
+
+/*
+static gboolean
+love_animation_draw
+*/
 static gboolean
 ending_animation_quit (gpointer data)
 {
-  gtk_main_quit ();
+  switch_state (STATE_PROLOGUE);
   return FALSE;
 }
 
 static gboolean
 ending_animation_draw (GtkWidget *widget, GdkEventExpose *event, gpointer data)
 {
-  /* We only run through once, so just make it static. */
   static int cycle_count = 0;
 
   static int robot_x = 0;
   static int robot_stop = 0;
   static int kitten_x = 0;
   static int all_y = 0;
+  static GdkGC *gc = NULL;
 
   const int stepsize = 3;
+  static int love_size = 40;
 
   if (!kitten_x)
     {
+      gc = gdk_gc_new (GDK_DRAWABLE (widget->window));
+
       all_y = (event->area.height - gdk_pixbuf_get_height (love_pic)) / 2;
 
       robot_stop = gdk_pixbuf_get_width (robot_pic) + gdk_pixbuf_get_width (love_pic);
       kitten_x = event->area.width - (cycle_count*stepsize + gdk_pixbuf_get_width (kitten_pic));
     }
 
-  gdk_gc_set_foreground (widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
-                        &black);
+  gdk_gc_set_foreground (gc, &black);
 
-  gdk_draw_rectangle (GDK_DRAWABLE(widget->window),
-                     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
+  gdk_draw_rectangle (GDK_DRAWABLE (widget->window),
+                     gc,
                      TRUE,
                      0, 0, event->area.width, event->area.height);
 
-  gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
-                  widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
+  gdk_draw_pixbuf (GDK_DRAWABLE (widget->window),
+                  gc,
                   robot_pic, 0, 0,
                   robot_x, all_y,
                   -1, -1,
                   GDK_RGB_DITHER_NONE, 0, 0);
 
-  gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
-                  widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
+  gdk_draw_pixbuf (GDK_DRAWABLE (widget->window),
+                  gc,
                   kitten_pic, 0, 0,
                   kitten_x, all_y,
                   -1, -1,
                   GDK_RGB_DITHER_NONE, 0, 0);
 
-  cycle_count++;
-  robot_x += stepsize;
-  kitten_x -= stepsize;
-
-  if (robot_x+robot_stop >= kitten_x)
+  if (robot_x+robot_stop < kitten_x)
+    {
+      cycle_count++;
+      robot_x += stepsize;
+      kitten_x -= stepsize;
+    }
+  else
     {
+      GdkPixbuf *scaled_love_pic =
+       gdk_pixbuf_scale_simple (love_pic,
+                                love_size,
+                                love_size,
+                                GDK_INTERP_BILINEAR);
+
       gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
-                      widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
-                      love_pic, 0, 0,
+                      gc,
+                      scaled_love_pic, 0, 0,
                       robot_x + gdk_pixbuf_get_width (robot_pic), all_y,
                       -1, -1,
                       GDK_RGB_DITHER_NONE, 0, 0);
 
-      g_object_unref (love_pic);
-      love_pic = NULL;
+      love_size ++;
 
-      g_timeout_add (2000, ending_animation_quit, NULL);
+      if (love_size >= gdk_pixbuf_get_width (love_pic))
+       {
+         animation_running = FALSE;
+
+         g_timeout_add (2000, ending_animation_quit, NULL);
+
+         gdk_gc_unref (gc);
+         love_size = 40;
+         cycle_count = 0;
+         robot_x = 0;
+         robot_stop = 0;
+         kitten_x = 0;
+         all_y = 0;
+         gc = NULL;
+       }
     }
 
   return TRUE;
@@ -262,9 +321,9 @@ ending_animation_draw (GtkWidget *widget, GdkEventExpose *event, gpointer data)
 static gboolean
 ending_animation_step (gpointer data)
 {
-  if (love_pic)
+  if (animation_running)
     {
-      gdk_window_invalidate_rect (animation_area->window,
+      gdk_window_invalidate_rect (state_widget[STATE_EPILOGUE]->window,
                                  NULL, TRUE);
 
       return TRUE;
@@ -276,18 +335,11 @@ ending_animation_step (gpointer data)
 static void
 ending_animation ()
 {
-  robot_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-robot.png", NULL);
-  love_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-love.png", NULL);
-  kitten_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-kitten.png", NULL);
-  animation_area =  gtk_drawing_area_new ();
-
-  gtk_container_remove (GTK_CONTAINER (window), GTK_WIDGET (table));
-  gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (animation_area));
-  gtk_widget_show_all (window);
-
-  g_signal_connect (G_OBJECT (animation_area),
-                   "expose_event", G_CALLBACK (ending_animation_draw), NULL);
-  g_timeout_add (10, ending_animation_step, NULL);
+  if (current_state!=STATE_EPILOGUE)
+    {
+      animation_running = TRUE;
+      g_timeout_add (10, ending_animation_step, NULL);
+    }
 }
 
 /****************************************************************/
@@ -336,7 +388,7 @@ move_robot (guint8 whichway)
 
       if (new_space == kitten)
        {
-         ending_animation ();
+         switch_state (STATE_EPILOGUE);
        }
 
       return TRUE;
@@ -347,8 +399,8 @@ move_robot (guint8 whichway)
 
       g_object_ref (new_space);
 
-      gtk_container_remove (GTK_CONTAINER (table), robot);
-      gtk_container_remove (GTK_CONTAINER (table), new_space);
+      gtk_container_remove (GTK_CONTAINER (state_widget[STATE_PLAYING]), robot);
+      gtk_container_remove (GTK_CONTAINER (state_widget[STATE_PLAYING]), new_space);
 
       place_in_arena_at_xy (new_space, robot_x, robot_y);
       place_in_arena_at_xy (robot, robot_x+dx, robot_y+dy);
@@ -372,15 +424,19 @@ on_window_clicked (GtkWidget      *widget,
   int rx, ry;
   double angle;
 
+  if (current_state!=STATE_PLAYING)
+    {
+      return TRUE;
+    }
+
   rx = (robot->allocation.x+robot->allocation.width/2);
   ry = (robot->allocation.y+robot->allocation.height/2);
 
   angle = atan2(event->x - rx,
                event->y - ry) +
-    M_PI +
-    M_PI/8;
+    M_PI * (9/8);
 
-  move_robot (((int) (angle / (M_PI/4)))-1);
+  move_robot (((int) (angle / (M_PI/4))) % 8);
 
   return TRUE;
 }
@@ -393,6 +449,11 @@ on_key_pressed (GtkWidget      *widget,
   gint i;
   guint keyval = event->keyval;
 
+  if (current_state!=STATE_PLAYING)
+    {
+      return FALSE;
+    }
+
   if (keyval>='A' && keyval<='Z')
     {
       keyval += ('a'-'A');
@@ -422,20 +483,121 @@ on_key_pressed (GtkWidget      *widget,
 }
 
 /****************************************************************/
-/* Let's kick the whole thing off...                            */
+/* Online help.                                                 */
 /****************************************************************/
+gboolean
+get_help (gpointer button, gpointer data)
+{
+  DBusGConnection *connection;
+  GError *error = NULL;
 
-int
-main (gint argc,
-      gchar **argv)
+  DBusGProxy *proxy;
+
+  connection = dbus_g_bus_get (DBUS_BUS_SESSION,
+                               &error);
+  if (connection == NULL)
+    {
+      show_message (error->message);
+      g_error_free (error);
+      return FALSE;
+    }
+
+  proxy = dbus_g_proxy_new_for_name (connection,
+                                     "com.nokia.osso_browser",
+                                     "/com/nokia/osso_browser/request",
+                                     "com.nokia.osso_browser");
+
+  error = NULL;
+  if (!dbus_g_proxy_call (proxy, "load_url", &error,
+                         G_TYPE_STRING, "/usr/share/rfk/help.html",
+                         G_TYPE_INVALID,
+                         G_TYPE_INVALID))
+    {
+      show_message (error->message);
+      g_error_free (error);
+      return FALSE;
+    }
+  return FALSE;
+}
+
+void
+play_game (gpointer button, gpointer data)
 {
-  int x, y;
+  switch_state (STATE_PLAYING);
+}
 
-  gtk_init (&argc, &argv);
-  g_set_application_name ("robotfindskitten");
-  srandom (time(0));
+static void
+set_up_board (void)
+{
+  guint x, y;
 
-  ensure_messages_loaded ();
+  if (current_state==STATE_PLAYING)
+    {
+      /* end of the game; clean up */
+
+      for (x=0; x < ARENA_WIDTH; x++)
+       for (y=0; y < ARENA_HEIGHT; y++)
+         if (arena[x][y])
+           {
+             gtk_container_remove (GTK_CONTAINER (state_widget[STATE_PLAYING]),
+                                   arena[x][y]);
+             arena[x][y] = NULL;
+           }
+
+      g_object_unref (robot);
+      g_object_unref (kitten);
+    }
+  else
+    {
+      /* make everything new */
+  
+      g_free (used);
+      used = g_malloc0 (nki_count * sizeof(gboolean));
+
+      robot = gtk_label_new ("#");
+      g_object_ref (robot);
+      kitten = random_character ("You found kitten!  Way to go, robot!");
+      g_object_ref (kitten);
+
+      place_in_arena_randomly (robot);
+      place_in_arena_randomly (kitten);
+
+      if (nki_count < amount_of_random_stuff)
+       {
+         /* sanity check failed */
+         show_message ("There are too few non-kitten items to play a meaningful game.");
+         exit (EXIT_FAILURE);
+       }
+
+      for (x=0; x < amount_of_random_stuff; x++)
+       place_in_arena_randomly (random_character (description ()));
+
+      for (x=0; x < ARENA_WIDTH; x++)
+       for (y=0; y < ARENA_HEIGHT; y++)
+         if (!arena[x][y])
+           place_in_arena_at_xy (gtk_label_new (NULL), x, y);
+    }
+}
+
+static void
+set_up_widgets (void)
+{
+  GtkWidget *middle = gtk_hbox_new (FALSE, 0);
+  GtkWidget *buttons = gtk_hbox_new (TRUE, 0);
+  GtkWidget *explain = NULL, *help_button, *play_button, *intro;
+  const char *explanation =
+    "In this game, you are robot (#). "
+    "Your job is to find kitten. This task is complicated "
+    "by the existence of various things which are not kitten. "
+    "Robot must touch items to determine if they are kitten or "
+    "not. The game ends when robotfindskitten. You may move "
+    "robot about by tapping on any side of robot, or with the "
+    "arrow keys.";
+  GKeyFile *desktop = g_key_file_new ();
+  gchar *version;
+  guint x, y;
+  
+  /* The window */
 
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title (GTK_WINDOW (window), "robotfindskitten");
@@ -443,33 +605,91 @@ main (gint argc,
   g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK (on_window_clicked), NULL);
   g_signal_connect (G_OBJECT (window), "key-press-event", G_CALLBACK (on_key_pressed), NULL);
   g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
-       
-  table = gtk_table_new (ARENA_HEIGHT, ARENA_WIDTH, TRUE);
-  gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (table));
 
-  robot = gtk_label_new ("#");
-  g_object_ref (robot);
-  kitten = random_character ("You found kitten!  Way to go, robot!");
+  /* The prologue */
 
-  place_in_arena_randomly (robot);
-  place_in_arena_randomly (kitten);
+  if (g_key_file_load_from_file (desktop,
+                                "/usr/share/applications/hildon/rfk.desktop",
+                                G_KEY_FILE_NONE,
+                                NULL))
+    {
+      version = g_strdup_printf("v%s.%d",
+                               g_key_file_get_value (desktop, "Desktop Entry", "Version", NULL),
+                               nki_count);
+      g_key_file_free (desktop);
+    }
+  else
+    {
+      version = g_strdup("");
+    }
+
+  help_button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_THUMB_HEIGHT,
+                                            HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
+                                            "Help", NULL);
+  g_signal_connect (help_button, "clicked", G_CALLBACK (get_help), NULL);
+
+  play_button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_THUMB_HEIGHT,
+                                            HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
+                                            "Play", NULL);
+  g_signal_connect (play_button, "clicked", G_CALLBACK (play_game), NULL);
+
+  gtk_box_pack_end (GTK_BOX (buttons), play_button, TRUE, TRUE, 0);
+  gtk_box_pack_end (GTK_BOX (buttons), help_button, TRUE, TRUE, 0);
+
+  explain = gtk_label_new (explanation);
+  gtk_label_set_line_wrap (GTK_LABEL (explain), TRUE);
 
-  for (x=0; x < amount_of_random_stuff; x++)
-    place_in_arena_randomly (random_character (description ()));
+  gtk_box_pack_end (GTK_BOX (middle), explain, TRUE, TRUE, 0);
+  gtk_box_pack_end (GTK_BOX (middle), gtk_image_new_from_pixbuf (robot_pic), FALSE, FALSE, 0);
+
+  intro = gtk_vbox_new (FALSE, 0);
+  gtk_box_pack_end (GTK_BOX (intro), buttons, FALSE, FALSE, 0);
+  gtk_box_pack_end (GTK_BOX (intro), middle, TRUE, TRUE, 0);
+  gtk_box_pack_end (GTK_BOX (intro), gtk_label_new (version), FALSE, FALSE, 0);
+  g_free (version);
+
+  state_widget[STATE_PROLOGUE] = intro;
+
+  /* The game itself */
+
+  state_widget[STATE_PLAYING] = gtk_table_new (ARENA_HEIGHT, ARENA_WIDTH, TRUE);
+  g_signal_connect (state_widget[STATE_PLAYING], "parent-set", G_CALLBACK (set_up_board), NULL);
 
   for (x=0; x < ARENA_WIDTH; x++)
     for (y=0; y < ARENA_HEIGHT; y++)
-      if (!arena[x][y])
-       place_in_arena_at_xy (gtk_label_new (NULL), x, y);
+      arena[x][y] = NULL;
 
-  gtk_widget_show_all (window);
+  /* The epilogue */
+  state_widget[STATE_EPILOGUE] =  gtk_drawing_area_new ();
+  g_signal_connect (state_widget[STATE_EPILOGUE], "parent-set", G_CALLBACK (ending_animation), NULL);
+  g_signal_connect (G_OBJECT (state_widget[STATE_EPILOGUE]),
+                   "expose_event", G_CALLBACK (ending_animation_draw), NULL);
 
-  gdk_window_set_events (GTK_WIDGET (window)->window,
-                        gdk_window_get_events(GTK_WIDGET (window)->window) | GDK_BUTTON_PRESS_MASK);
+  for (x=0; x<STATE_LAST; x++)
+    {
+      /* so we don't lose them when we take them offscreen */
+      g_object_ref (state_widget[x]);
+    }
+}
+
+/****************************************************************/
+/* Let's kick the whole thing off...                            */
+/****************************************************************/
+
+int
+main (gint argc,
+      gchar **argv)
+{
+  gtk_init (&argc, &argv);
+  g_set_application_name ("robotfindskitten");
+  srandom (time(0));
 
-       
-  show_message (explanation);
+  ensure_messages_loaded ();
+  load_images ();
 
+  set_up_widgets ();
+  switch_state (STATE_PROLOGUE);
+  
   gtk_main ();
 
   return EXIT_SUCCESS;