gnome-games r8363 - trunk/mahjongg



Author: rancell
Date: Tue Dec  9 15:33:03 2008
New Revision: 8363
URL: http://svn.gnome.org/viewvc/gnome-games?rev=8363&view=rev

Log:
Refactor new game code so all state iss correctly reset

Modified:
   trunk/mahjongg/drawing.c
   trunk/mahjongg/mahjongg.c
   trunk/mahjongg/mahjongg.h

Modified: trunk/mahjongg/drawing.c
==============================================================================
--- trunk/mahjongg/drawing.c	(original)
+++ trunk/mahjongg/drawing.c	Tue Dec  9 15:33:03 2008
@@ -76,10 +76,9 @@
 #define MINHEIGHT 200
 
 /* These two are in units of tiles and do not include a half tile border. */
-gint gridwidth;
-gint gridheight;
+static gint gridwidth = 0, gridheight = 0;
 
-gchar *warning_message = NULL;
+static gchar *warning_message = NULL;
 
 static void
 recalculate_sizes (gint width, gint height)
@@ -165,7 +164,6 @@
    * position information is for the upper left corner. */
   gridwidth = gridwidth / 2 + 2;
   gridheight = gridheight / 2 + 2;
-
 }
 
 static gint
@@ -211,6 +209,9 @@
   guint dx, dy;
   guint sx, sy;
   gint i, j;
+   
+  if (nowindow)
+    return;
 
   ox = view_geometry[tileno].x;
   oy = view_geometry[tileno].y;
@@ -248,6 +249,9 @@
   gint i;
   guint sx, sy;
   guint dx, dy;
+   
+  if (nowindow)
+    return;
 
   gdk_gc_set_clip_mask (gc, NULL);
   gdk_draw_rectangle (buffer, gc, TRUE, 0, 0, windowwidth, windowheight);

Modified: trunk/mahjongg/mahjongg.c
==============================================================================
--- trunk/mahjongg/mahjongg.c	(original)
+++ trunk/mahjongg/mahjongg.c	Tue Dec  9 15:33:03 2008
@@ -60,59 +60,49 @@
 #define DEFAULT_TILESET "default.png"
 #define DEFAULT_MAPSET "Easy"
 
-tilepos *pos = 0;
-
-tile tiles[MAX_TILES];
+static GtkWidget *window, *statusbar;
+static GtkWidget *tiles_label;
+static GtkWidget *toolbar;
+static GtkWidget *moves_label, *chrono;
+static GtkWidget *warn_cb = NULL, *confirm_cb = NULL;
+static GtkWidget *colour_well = NULL;
+static GtkWidget *pref_dialog = NULL;
+
+static GtkAction *pause_action;
+static GtkAction *resume_action;
+static GtkAction *hint_action;
+static GtkAction *redo_action;
+static GtkAction *undo_action;
+static GtkAction *restart_action;
+static GtkAction *scores_action;
+static GtkAction *show_toolbar_action;
+static GtkAction *fullscreen_action;
+static GtkAction *leavefullscreen_action;
 
-GtkWidget *window, *statusbar;
-GtkWidget *tiles_label;
-GtkWidget *toolbar;
-
-GtkAction *pause_action;
-GtkAction *resume_action;
-GtkAction *hint_action;
-GtkAction *redo_action;
-GtkAction *undo_action;
-GtkAction *restart_action;
-GtkAction *scores_action;
-GtkAction *show_toolbar_action;
-GtkAction *fullscreen_action;
-GtkAction *leavefullscreen_action;
-
-gint selected_tile, visible_tiles;
-gint sequence_number;
-
-GList *tileset_list = NULL;
+/* Available tilsets */
+static GList *tileset_list = NULL;
 
 gchar *tileset = NULL;
-static gint mapset = -1;
-static gchar *score_current_mapset = NULL;
-
-static gchar *selected_tileset = NULL;
-
-gboolean undo_state = FALSE;
-gboolean redo_state = FALSE;
 
-gint hint_tiles[2];
-guint timer;
-guint timeout_counter = HINT_BLINK_NUM + 1;
-
-GtkWidget *moves_label;
-gint moves_left = 0;
-GtkWidget *chrono;
-gint paused = 0;
-
-/* for the preferences */
-GtkWidget *warn_cb = NULL, *confirm_cb = NULL;
-GtkWidget *colour_well = NULL;
-GtkWidget *pref_dialog = NULL;
+tile tiles[MAX_TILES];
+static gint visible_tiles; /* Count of how many tiles are active */
 
-/* Has the map been changed ? */
-gboolean new_map = TRUE;
+tilepos *pos = 0;
+static gint mapset = -1, active_mapset = -1;
+static gchar *selected_tileset = NULL;
+static gint selected_tile;
+static gint sequence_number;
+static gboolean undo_state = FALSE;
+static gboolean redo_state = FALSE;
+static gint moves_left = 0; /* The number of moves that can be made */
+gboolean paused = FALSE; /* true if the game is paused */
+
+/* Hint animation */
+static gint hint_tiles[2];
+static guint timer = 0;
+static guint timeout_counter = HINT_BLINK_NUM + 1;
 
-GamesScores *highscores;
-
-static gint get_mapset_index (void);
+static GamesScores *highscores;
 
 static const GamesScoresDescription scoredesc = { NULL,
   "Easy",
@@ -128,16 +118,15 @@
   GAME_DEAD
 } game_over = GAME_WAITING;
 
+static gint get_mapset_index (void);
 static void clear_undo_queue (void);
 void you_won (void);
 void check_free (void);
 void undo_tile_callback (void);
 void properties_callback (void);
 void shuffle_tiles_callback (void);
-void ensure_pause_off (void);
 void pause_callback (void);
-void new_game (void);
-void restart_game (void);
+void new_game (gboolean shuffle);
 
 void
 mahjongg_theme_warning (gchar * message)
@@ -310,11 +299,9 @@
 
     mapset = get_mapset_index ();
 
-    new_map = TRUE;
-
     /* Skip the dialog if a game isn't in play. */
     if (game_over || !games_clock_get_seconds (GAMES_CLOCK (chrono))) {
-      new_game ();
+      new_game (TRUE);
       return;
     }
 
@@ -332,7 +319,7 @@
     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
     response = gtk_dialog_run (GTK_DIALOG (dialog));
     if (response == GTK_RESPONSE_ACCEPT)
-      new_game ();
+      new_game (TRUE);
     gtk_widget_destroy (dialog);
   }
 }
@@ -980,15 +967,6 @@
     message ("");
   }
   noloops = FALSE;
-
-
-}
-
-void
-ensure_pause_off (void)
-{
-  if (paused)
-    pause_callback ();
 }
 
 static void
@@ -1009,42 +987,16 @@
 }
 
 static void
-init_game (void)
-{
-  gchar *newtitle;
-
-  /* Translators: This is the window title for Mahjongg which contains the map name, e.g. 'Mahjongg - Red Dragon' */
-  newtitle = g_strdup_printf (_("Mahjongg - %s"), Q_ (maps[mapset].name));
-  gtk_window_set_title (GTK_WINDOW (window), newtitle);
-  g_free (newtitle);
-
-  score_current_mapset = maps[mapset].score_name;
-
-  gtk_label_set_text (GTK_LABEL (tiles_label), MAX_TILES_STR);
-  update_moves_left ();
-  game_over = GAME_WAITING;
-  sequence_number = 1;
-  visible_tiles = MAX_TILES;
-  selected_tile = MAX_TILES + 1;
-  set_undoredo_state (FALSE, FALSE);
-
-  games_clock_stop (GAMES_CLOCK (chrono));
-  games_clock_set_seconds (GAMES_CLOCK (chrono), 0);
-}
-
-static void
 new_game_cb (GtkAction * action, gpointer data)
 {
-  stop_hints ();
-  ensure_pause_off ();
-  new_game ();
+  new_game (TRUE);
 }
 
 static void
 restart_game_cb (GtkAction * action, gpointer data)
 {
   stop_hints ();
-  restart_game ();
+  new_game (FALSE);
 }
 
 static void
@@ -1053,21 +1005,6 @@
   gtk_main_quit ();
 }
 
-void
-restart_game (void)
-{
-  gint i;
-
-  ensure_pause_off ();
-  for (i = 0; i < MAX_TILES; i++) {
-    tiles[i].visible = 1;
-    tiles[i].selected = 0;
-    tiles[i].sequence = 0;
-  }
-  draw_all_tiles ();
-  init_game ();
-}
-
 static void
 redo_tile_callback (GtkAction * action, gpointer data)
 {
@@ -1167,25 +1104,6 @@
 }
 
 static void
-load_map (void)
-{
-  new_map = FALSE;
-  pos = maps[mapset].map;
-  generate_dependencies ();
-  calculate_view_geometry ();
-  configure_pixmaps ();
-}
-
-static void
-do_game (void)
-{
-  games_scores_set_category (highscores, maps[mapset].score_name);
-  if (new_map)
-    load_map ();
-  generate_game (g_random_int ());	/* puts in the positions of the tiles */
-}
-
-static void
 load_preferences (void)
 {
   gchar *buf;
@@ -1202,13 +1120,66 @@
 }
 
 void
-new_game (void)
+new_game (gboolean shuffle)
 {
-  do_game ();
+  gint i;
+  gchar *title;
 
-  draw_all_tiles ();
+  /* Reset state */
+  game_over = GAME_WAITING;
+  sequence_number = 1;
+  visible_tiles = MAX_TILES;
+  selected_tile = MAX_TILES + 1;
+  undo_state = FALSE;
+  redo_state = FALSE;
+  hint_tiles[0] = MAX_TILES + 1;
+  hint_tiles[1] = MAX_TILES + 1;   
+  if (timer)
+    g_source_remove (timer);
+  timer = 0;
+  timeout_counter = HINT_BLINK_NUM + 1;
+  moves_left = 0;
+  paused = FALSE;
+  for (i = 0; i < MAX_TILES; i++) {
+    tiles[i].visible = 1;
+    tiles[i].selected = 0;
+    tiles[i].sequence = 0;
+  }
+
+  /* Load map */
+  if (active_mapset != mapset) {
+    active_mapset = mapset;
+    pos = maps[mapset].map;
+    generate_dependencies ();
+    calculate_view_geometry ();
+    configure_pixmaps ();
+    games_scores_set_category (highscores, maps[mapset].score_name);
+  }
+
+  /* Generate layout */
+  if (shuffle) {
+     generate_game (g_random_int ());
+  }
+   
+  /* Analyse layout */
+  update_moves_left ();
+  
+  /* Update menus */
+  update_menu_sensitivities ();
+
+  /* Set window title */
+  /* Translators: This is the window title for Mahjongg which contains the map name, e.g. 'Mahjongg - Red Dragon' */
+  title = g_strdup_printf (_("Mahjongg - %s"), Q_ (maps[mapset].name));
+  gtk_window_set_title (GTK_WINDOW (window), title);
+  g_free (title);
+  gtk_label_set_text (GTK_LABEL (tiles_label), MAX_TILES_STR);
 
-  init_game ();
+  /* Prepare clock */
+  games_clock_stop (GAMES_CLOCK (chrono));
+  games_clock_set_seconds (GAMES_CLOCK (chrono), 0);
+
+  /* Redraw */
+  draw_all_tiles ();
 }
 
 void
@@ -1532,9 +1503,8 @@
 
   /* FIXME: get these in the best place (as per the comment below. */
   init_config ();
-
-  do_game ();
-  init_game ();
+   
+  new_game (TRUE);
 
   /* Don't leave the keyboard focus on the toolbar */
   gtk_widget_grab_focus (board);

Modified: trunk/mahjongg/mahjongg.h
==============================================================================
--- trunk/mahjongg/mahjongg.h	(original)
+++ trunk/mahjongg/mahjongg.h	Tue Dec  9 15:33:03 2008
@@ -38,7 +38,7 @@
 void mahjongg_theme_warning (gchar * message_format);
 
 extern tile tiles[MAX_TILES];
-extern gint paused;
+extern gboolean paused;
 extern gchar *tileset;
 
 #endif /* MAHJONGG_H */



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