1 /* robotfindskitten for maemo
2 * original by Leonard Richardson, 1997
3 * ported to maemo by Thomas Thurman, 2009
6 * gcc -Wall -g rfk.c -o rfk `pkg-config --cflags --libs gtk+-2.0 hildon-1`
12 #include <hildon/hildon.h>
17 #define ARENA_WIDTH 25
18 #define ARENA_HEIGHT 12
20 const int amount_of_random_stuff = 15;
25 GtkWidget *arena[ARENA_WIDTH][ARENA_HEIGHT];
26 GtkWidget *intro, *table, *window, *robot, *kitten;
28 gboolean *used = NULL;
30 GdkPixbuf *robot_pic, *love_pic, *kitten_pic;
31 GtkWidget *animation_area;
33 const GdkColor black = { 0, };
35 /****************************************************************/
36 /* Random object descriptions. */
37 /****************************************************************/
46 r = random() % nki_count;
51 return g_slist_nth_data (nki, r);
54 /****************************************************************/
55 /* Placing objects. */
56 /****************************************************************/
59 place_in_arena_at_xy (GtkWidget *item, int x, int y)
63 gtk_table_attach_defaults (GTK_TABLE (table),
76 place_in_arena_randomly (GtkWidget *item)
82 x = random() % ARENA_WIDTH;
83 y = random() % ARENA_HEIGHT;
87 place_in_arena_at_xy (item, x, y);
90 /****************************************************************/
91 /* Labels representing things the robot might find. */
92 /****************************************************************/
95 random_character (gchar *description)
97 gchar character[2] = { random() % ('~'-'!') + '!', 0 };
98 gchar *escaped_character = g_markup_escape_text (character, -1);
99 gchar *markup = g_strdup_printf ("<span color=\"#%02x%02x%02x\">%s</span>",
100 (int) (random() % 0x7F)+0x80,
101 (int) (random() % 0x7F)+0x80,
102 (int) (random() % 0x7F)+0x80,
104 GtkWidget *result = gtk_label_new (NULL);
105 gtk_label_set_markup (GTK_LABEL (result), markup);
107 g_free (escaped_character);
109 g_object_set_data (G_OBJECT (result), "examine", description);
114 /****************************************************************/
115 /* Talking back to the user. */
116 /****************************************************************/
119 show_message (const char *message)
121 HildonNote* note = HILDON_NOTE
122 (hildon_note_new_information (GTK_WINDOW (window),
124 gtk_dialog_run (GTK_DIALOG (note));
125 gtk_widget_destroy (GTK_WIDGET (note));
128 /****************************************************************/
129 /* Loading the non-kitten objects. */
130 /****************************************************************/
132 ensure_messages_loaded (void)
134 FILE *nki_file = NULL;
136 gboolean headers = TRUE;
141 nki_file = fopen ("/usr/share/rfk/non-kitten-items.rfk", "r");
145 show_message ("Could not open list of non-kitten items! Must quit.");
149 while (!feof (nki_file))
152 if (fscanf (nki_file, "%a[^\n]%c", &line, &newline) == EOF)
157 if (strcmp(line, "")==0)
160 fscanf (nki_file, "%c", &newline);
165 /* we ignore all the headers for now */
170 nki = g_slist_prepend (nki, line);
177 used = g_malloc0 (nki_count);
183 robot_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-robot.png", NULL);
184 love_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-love.png", NULL);
185 kitten_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-kitten.png", NULL);
188 /****************************************************************/
189 /* The ending animation. */
190 /****************************************************************/
193 ending_animation_quit (gpointer data)
200 ending_animation_draw (GtkWidget *widget, GdkEventExpose *event, gpointer data)
202 /* We only run through once, so just make it static. */
203 static int cycle_count = 0;
205 static int robot_x = 0;
206 static int robot_stop = 0;
207 static int kitten_x = 0;
208 static int all_y = 0;
210 const int stepsize = 3;
214 all_y = (event->area.height - gdk_pixbuf_get_height (love_pic)) / 2;
216 robot_stop = gdk_pixbuf_get_width (robot_pic) + gdk_pixbuf_get_width (love_pic);
217 kitten_x = event->area.width - (cycle_count*stepsize + gdk_pixbuf_get_width (kitten_pic));
220 gdk_gc_set_foreground (widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
223 gdk_draw_rectangle (GDK_DRAWABLE(widget->window),
224 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
226 0, 0, event->area.width, event->area.height);
228 gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
229 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
233 GDK_RGB_DITHER_NONE, 0, 0);
235 gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
236 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
240 GDK_RGB_DITHER_NONE, 0, 0);
244 kitten_x -= stepsize;
246 if (robot_x+robot_stop >= kitten_x)
248 gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
249 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
251 robot_x + gdk_pixbuf_get_width (robot_pic), all_y,
253 GDK_RGB_DITHER_NONE, 0, 0);
255 g_object_unref (love_pic);
258 g_timeout_add (2000, ending_animation_quit, NULL);
265 ending_animation_step (gpointer data)
269 gdk_window_invalidate_rect (animation_area->window,
281 animation_area = gtk_drawing_area_new ();
283 gtk_container_remove (GTK_CONTAINER (window), GTK_WIDGET (table));
284 gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (animation_area));
285 gtk_widget_show_all (window);
287 g_signal_connect (G_OBJECT (animation_area),
288 "expose_event", G_CALLBACK (ending_animation_draw), NULL);
289 g_timeout_add (10, ending_animation_step, NULL);
292 /****************************************************************/
293 /* Moving the robot. Way to go, robot! */
294 /****************************************************************/
298 gchar vi_key; /* or nethack equivalent */
303 direction directions[] = {
304 { GDK_Home, 'y', -1, -1 },
305 { GDK_Left, 'h', -1, 0 },
306 { GDK_End, 'b', -1, 1 },
307 { GDK_Down, 'j', 0, 1 },
308 { GDK_Page_Down, 'n', 1, 1 },
309 { GDK_Right, 'l', 1, 0 },
310 { GDK_Page_Up, 'u', 1, -1 },
311 { GDK_Up, 'k', 0, -1 }
315 move_robot (guint8 whichway)
317 GtkWidget *new_space;
318 gint8 dx = directions[whichway].move_x;
319 gint8 dy = directions[whichway].move_y;
325 robot_x+dx>=ARENA_WIDTH ||
326 robot_y+dy>=ARENA_HEIGHT)
329 new_space = arena[robot_x+dx][robot_y+dy];
330 found = g_object_get_data (G_OBJECT (new_space), "examine");
334 show_message (found);
336 if (new_space == kitten)
345 /* just an ordinary move into an empty space */
347 g_object_ref (new_space);
349 gtk_container_remove (GTK_CONTAINER (table), robot);
350 gtk_container_remove (GTK_CONTAINER (table), new_space);
352 place_in_arena_at_xy (new_space, robot_x, robot_y);
353 place_in_arena_at_xy (robot, robot_x+dx, robot_y+dy);
355 g_object_unref (new_space);
361 /****************************************************************/
362 /* Event handlers. */
363 /****************************************************************/
366 on_window_clicked (GtkWidget *widget,
367 GdkEventButton *event,
370 /** Centre point of robot's representation on screen */
374 rx = (robot->allocation.x+robot->allocation.width/2);
375 ry = (robot->allocation.y+robot->allocation.height/2);
377 angle = atan2(event->x - rx,
381 move_robot (((int) (angle / (M_PI/4))) % 8);
387 on_key_pressed (GtkWidget *widget,
392 guint keyval = event->keyval;
394 if (keyval>='A' && keyval<='Z')
399 for (i=0; i<G_N_ELEMENTS(directions); i++)
401 if (keyval==directions[i].gdk_key ||
402 keyval==directions[i].vi_key)
404 if (event->state & GDK_SHIFT_MASK)
406 while (!move_robot (i))
408 /* keep going, robot! */
425 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
426 gtk_window_set_title (GTK_WINDOW (window), "robotfindskitten");
427 gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black);
435 g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK (on_window_clicked), NULL);
436 g_signal_connect (G_OBJECT (window), "key-press-event", G_CALLBACK (on_key_pressed), NULL);
437 g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
438 gdk_window_set_events (GTK_WIDGET (window)->window,
439 gdk_window_get_events(GTK_WIDGET (window)->window) | GDK_BUTTON_PRESS_MASK);
441 table = gtk_table_new (ARENA_HEIGHT, ARENA_WIDTH, TRUE);
442 gtk_container_remove (GTK_CONTAINER (window), GTK_WIDGET (intro));
443 gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (table));
445 robot = gtk_label_new ("#");
446 g_object_ref (robot);
447 kitten = random_character ("You found kitten! Way to go, robot!");
449 place_in_arena_randomly (robot);
450 place_in_arena_randomly (kitten);
452 if (nki_count < amount_of_random_stuff)
454 gtk_widget_show_all (window);
455 show_message ("There are too few non-kitten items to play a meaningful game.");
459 for (x=0; x < amount_of_random_stuff; x++)
460 place_in_arena_randomly (random_character (description ()));
462 for (x=0; x < ARENA_WIDTH; x++)
463 for (y=0; y < ARENA_HEIGHT; y++)
465 place_in_arena_at_xy (gtk_label_new (NULL), x, y);
467 gtk_widget_show_all (window);
471 get_help (gpointer button, gpointer data)
473 show_message ("Not yet implemented.");
477 play_game (gpointer button, gpointer data)
485 GtkWidget *middle = gtk_hbox_new (FALSE, 0);
486 GtkWidget *buttons = gtk_hbox_new (TRUE, 0);
487 GtkWidget *explain = NULL, *help_button, *play_button;
488 const char *explanation =
489 "In this game, you are robot (#). "
490 "Your job is to find kitten. This task is complicated "
491 "by the existence of various things which are not kitten. "
492 "Robot must touch items to determine if they are kitten or "
493 "not. The game ends when robotfindskitten. You may move "
494 "robot about by tapping on any side of robot, or with the "
496 GKeyFile *desktop = g_key_file_new ();
499 if (g_key_file_load_from_file (desktop,
500 "/usr/share/applications/hildon/rfk.desktop",
504 version = g_strdup_printf("v%s.%d",
505 g_key_file_get_value (desktop, "Desktop Entry", "Version", NULL),
507 g_key_file_free (desktop);
511 version = g_strdup("");
514 help_button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_THUMB_HEIGHT,
515 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
517 g_signal_connect (help_button, "clicked", G_CALLBACK (get_help), NULL);
519 play_button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_THUMB_HEIGHT,
520 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
522 g_signal_connect (play_button, "clicked", G_CALLBACK (play_game), NULL);
524 gtk_box_pack_end (buttons, play_button, TRUE, TRUE, 0);
525 gtk_box_pack_end (buttons, help_button, TRUE, TRUE, 0);
527 explain = gtk_label_new (explanation);
528 gtk_label_set_line_wrap (explain, TRUE);
530 gtk_box_pack_end (middle, explain, TRUE, TRUE, 0);
531 gtk_box_pack_end (middle, gtk_image_new_from_pixbuf (robot_pic), FALSE, FALSE, 0);
533 intro = gtk_vbox_new (FALSE, 0);
534 gtk_box_pack_end (GTK_BOX (intro), buttons, FALSE, FALSE, 0);
535 gtk_box_pack_end (GTK_BOX (intro), middle, TRUE, TRUE, 0);
536 gtk_box_pack_end (GTK_BOX (intro), gtk_label_new (version), FALSE, FALSE, 0);
540 gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (intro));
542 gtk_widget_show_all (window);
545 /****************************************************************/
546 /* Let's kick the whole thing off... */
547 /****************************************************************/
553 gtk_init (&argc, &argv);
554 g_set_application_name ("robotfindskitten");
557 ensure_messages_loaded ();