[gnome-panel/wip/free-the-fish: 8/9] Free-the-fish: Cleanup code and rename variables



commit e40921037636a23b33156693ca894c53e8c583c8
Author: Sebastian Geiger <sbastig gmx net>
Date:   Mon Jun 1 18:46:20 2015 +0200

    Free-the-fish: Cleanup code and rename variables

 gnome-panel/free-the-fish.c |  100 ++++++++++++++++++++++++-------------------
 1 files changed, 56 insertions(+), 44 deletions(-)
---
diff --git a/gnome-panel/free-the-fish.c b/gnome-panel/free-the-fish.c
index 959f5f7..d0fecdf 100644
--- a/gnome-panel/free-the-fish.c
+++ b/gnome-panel/free-the-fish.c
@@ -21,16 +21,18 @@
 /* Some important code copied from PonG */
 typedef struct _FreeTheFish FreeTheFish;
 struct _FreeTheFish {
-        GdkWindow *win;
+        GdkWindow *window;
+        /* This indicates if the fish is about hide it self by moving out of the screen area. */
         gboolean hide_mode;
         int state;
-        int x, y, xs, ys;
+        int x, y, x_speed, y_speed;
         int handler;
         cairo_pattern_t *fish_pattern[FISH_FRAMES];
         cairo_pattern_t *fish_pattern_reverse[FISH_FRAMES];
         cairo_region_t *fish_shape;
         cairo_region_t *fish_shape_reverse;
 };
+
 static FreeTheFish fish = {NULL};
 
 static void
@@ -55,12 +57,13 @@ fish_kill (void)
                 cairo_region_destroy (fish.fish_shape_reverse);
         fish.fish_shape_reverse = NULL;
 
-        gdk_window_destroy (fish.win);
+        gdk_window_destroy (fish.window);
 
         g_source_remove ((guint) fish.handler);
 
         memset (&fish, 0, sizeof (FreeTheFish));
 
+        /* We need to remove our fish_handle_event function and restore the original gtk function. */
         gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
 }
 
@@ -76,16 +79,16 @@ fish_draw (int orient, int frame)
         region = orient ? fish.fish_shape : fish.fish_shape_reverse;
         shape_offset = orient ? -frame : frame + 1 - FISH_FRAMES;
 
-        cr = gdk_cairo_create (fish.win);
+        cr = gdk_cairo_create (fish.window);
         cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
         cairo_set_source (cr, pattern);
         cairo_paint (cr);
         cairo_destroy (cr);
 
         /* Yes. Don't ask. */
-        gdk_window_set_background_pattern (fish.win, pattern);
+        gdk_window_set_background_pattern (fish.window, pattern);
 
-        gdk_window_shape_combine_region (fish.win, region,
+        gdk_window_shape_combine_region (fish.window, region,
                                          shape_offset * FISH_WIDTH, 0);
 }
 
@@ -95,8 +98,8 @@ fish_move (gpointer data)
         int orient, frame;
         gboolean change = TRUE;
 
-        fish.x += fish.xs;
-        fish.y += fish.ys;
+        fish.x += fish.x_speed;
+        fish.y += fish.y_speed;
         if (fish.x <= -FISH_WIDTH ||
             fish.x >= gdk_screen_width ()) {
                 fish_kill ();
@@ -105,9 +108,9 @@ fish_move (gpointer data)
         if (fish.y <= 0 ||
             fish.y >= gdk_screen_height () - FISH_HEIGHT ||
             g_random_int() % (fish.hide_mode?10:50) == 0)
-                fish.ys = -fish.ys;
+                fish.y_speed = -fish.y_speed;
 
-        fish.state ++;
+        fish.state++;
         if (fish.hide_mode) {
                 fish.state ++;
                 if (fish.state >= 2* FISH_FRAMES)
@@ -120,21 +123,39 @@ fish_move (gpointer data)
         }
 
         frame = fish.state / (fish.hide_mode?2:1);
-        orient = fish.xs >= 0 ? 0 : 1;
+        orient = fish.x_speed >= 0 ? 0 : 1;
 
         if (change)
                 fish_draw (orient, frame);
 
-        gdk_window_move (fish.win, fish.x, fish.y);
-        gdk_window_raise (fish.win);
+        gdk_window_move (fish.window, fish.x, fish.y);
+        gdk_window_raise (fish.window);
 
         return TRUE;
 }
 
+/* Starts Wandas hide mode. Wanda will move fast and it will move towards the
+ * side of the screen that is closer to it.*/
+static void fish_start_hide_mode() {
+        g_source_remove ((guint) fish.handler);
+        fish.handler = g_timeout_add (FISH_HIDE_TIMEOUT,
+                                      fish_move, NULL);
+        fish.x_speed *= FISH_XS_HIDE_FACTOR;
+        fish.y_speed *= FISH_YS_HIDE_FACTOR;
+        fish.hide_mode = TRUE;
+        if (fish.x_speed > 0) {
+                if (fish.x < (gdk_screen_width () / 2))
+                        fish.x_speed *= -1;
+        } else {
+                if (fish.x > (gdk_screen_width () / 2))
+                        fish.x_speed *= -1;
+        }
+}
+
 static void
 fish_handle_event (GdkEvent *event)
 {
-        if (event->any.window != fish.win)
+        if (event->any.window != fish.window)
                 goto out;
 
         if (fish.hide_mode)
@@ -144,21 +165,10 @@ fish_handle_event (GdkEvent *event)
                 case GDK_SCROLL:
                 case GDK_BUTTON_PRESS:
                 case GDK_2BUTTON_PRESS:
-                case GDK_3BUTTON_PRESS:
-                        g_source_remove ((guint) fish.handler);
-                        fish.handler = g_timeout_add (FISH_HIDE_TIMEOUT,
-                                                      fish_move, NULL);
-                        fish.xs *= FISH_XS_HIDE_FACTOR;
-                        fish.ys *= FISH_YS_HIDE_FACTOR;
-                        fish.hide_mode = TRUE;
-                        if (fish.xs > 0) {
-                                if (fish.x < (gdk_screen_width () / 2))
-                                        fish.xs *= -1;
-                        } else {
-                                if (fish.x > (gdk_screen_width () / 2))
-                                        fish.xs *= -1;
-                        }
+                case GDK_3BUTTON_PRESS: {
+                        fish_start_hide_mode();
                         break;
+                };
                 default:
                         break;
         }
@@ -170,6 +180,8 @@ out:
 static GdkPixbuf *
 get_fish_pixbuf (void)
 {
+        /* See panel.gresource.xml
+         * This uses ../applets/fish/wanda.png */
         return gdk_pixbuf_new_from_resource ("/org/gnome/panel/anim/wanda.png", NULL);
 }
 
@@ -179,18 +191,18 @@ fish_unsea (cairo_surface_t *surface)
         guchar *pixels = cairo_image_surface_get_data (surface);
         int rs = cairo_image_surface_get_stride (surface);
         int w = cairo_image_surface_get_width (surface);
-        int h = cairo_image_surface_get_height (surface);
-        int x, y;
+        int height = cairo_image_surface_get_height (surface);
+        int x, row;
         guint32 *p;
         guchar a, r, g, b;
 
-        for (y = 0; y < h; y++, pixels += rs) {
+        for (row = 0; row < height; row++, pixels += rs) {
                 p = (guint32 *) pixels;
                 for (x = 0; x < w; x++, p++) {
-                        a = ((*p)>>24);
-                        r = ((*p)>>16)&0xff;
-                        g = ((*p)>> 8)&0xff;
-                        b = ((*p)>> 0)&0xff;
+                        a = (guchar) ((*p)>>24);
+                        r = (guchar) (((*p)>>16)&0xff);
+                        g = (guchar) (((*p)>> 8)&0xff);
+                        b = (guchar) (((*p)>> 0)&0xff);
                         if (FISH_PIXEL_STORE_MOVE(a, r, g, b))
                                 *p = 0;
                 }
@@ -282,7 +294,7 @@ get_fish_shape_reverse (cairo_surface_t *surface)
 
 /* this checks the screen */
 static void
-check_screen (void)
+fish_init (void)
 {
         GdkWindowAttr attributes;
         GdkPixbuf *fish_pixbuf;
@@ -292,7 +304,7 @@ check_screen (void)
         int orient;
         int i;
 
-        if (fish.win != NULL)
+        if (fish.window != NULL)
                 return;
 
         fish_pixbuf = get_fish_pixbuf ();
@@ -331,8 +343,8 @@ check_screen (void)
         fish.hide_mode = FALSE;
         fish.x = orient ? -FISH_WIDTH : gdk_screen_width ();
         fish.y = (g_random_int() % (gdk_screen_height() - FISH_HEIGHT - 2)) + 1;
-        fish.xs = orient ? FISH_XS : -FISH_XS;
-        fish.ys = FISH_YS;
+        fish.x_speed = orient ? FISH_XS : -FISH_XS;
+        fish.y_speed = FISH_YS;
 
         attributes.window_type = GDK_WINDOW_TEMP;
         attributes.x = fish.x;
@@ -342,12 +354,12 @@ check_screen (void)
         attributes.wclass = GDK_INPUT_OUTPUT;
         attributes.event_mask = GDK_BUTTON_PRESS_MASK;
 
-        fish.win = gdk_window_new (NULL, &attributes,
+        fish.window = gdk_window_new (NULL, &attributes,
                                    GDK_WA_X | GDK_WA_Y);
 
         for (i = 0; i < FISH_FRAMES; i++) {
-                fish.fish_pattern[i] = get_fish_frame (fish.win, surface, i);
-                fish.fish_pattern_reverse[i] = get_fish_frame_reverse (fish.win, surface, i);
+                fish.fish_pattern[i] = get_fish_frame (fish.window, surface, i);
+                fish.fish_pattern_reverse[i] = get_fish_frame_reverse (fish.window, surface, i);
         }
 
         fish.fish_shape = get_fish_shape (surface);
@@ -356,7 +368,7 @@ check_screen (void)
         cairo_surface_destroy (surface);
 
         fish_draw (0, 0);
-        gdk_window_show (fish.win);
+        gdk_window_show (fish.window);
 
         gdk_event_handler_set ((GdkEventFunc)fish_handle_event, NULL, NULL);
 
@@ -370,7 +382,7 @@ check_screen_timeout (gpointer data)
 {
         screen_check_id = 0;
 
-        check_screen ();
+        fish_init();
 
         screen_check_id = g_timeout_add (FISH_CHECK_TIMEOUT,
                                          check_screen_timeout, NULL);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]