[gnome-2048] Fix some races.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-2048] Fix some races.
- Date: Wed, 30 Jan 2019 12:53:53 +0000 (UTC)
commit 353a35d4aa2f5f0d186c1bf5cf96142a8bdf52fb
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Wed Jan 30 13:51:13 2019 +0100
Fix some races.
The restoration of the game is racy.
Try to fix that two ways: firstly by
not resetting _game_restored when no
move happens; and secondly by adding
a boolean to ensure startup is done.
Should fix #3.
src/application.vala | 53 +++++++++++++++++++++++++++++++++++++++++++++-------
src/game.vala | 51 ++++++++++++--------------------------------------
2 files changed, 58 insertions(+), 46 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index ecd67a3..0385f46 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -41,6 +41,7 @@ public class Application : Gtk.Application
private GtkClutter.Embed _embed;
private bool _game_restored;
+ private bool _game_should_init = true;
private Game _game;
@@ -144,6 +145,7 @@ public class Application : Gtk.Application
_game_restored = _game.restore_game ();
if (!_game_restored)
new_game_cb ();
+ _game_should_init = false;
}
protected override void activate ()
@@ -419,12 +421,23 @@ public class Application : Gtk.Application
private bool key_press_event_cb (Widget widget, Gdk.EventKey event)
{
- _game_restored = false;
-
if (_hamburger_button.active || (_window.focus_visible && !_embed.is_focus))
return false;
+ if (_game.cannot_move ())
+ return false;
- return _game.key_pressed (event);
+ switch (_upper_key (event.keyval))
+ {
+ case Gdk.Key.Down: _request_move (MoveRequest.DOWN); return true;
+ case Gdk.Key.Up: _request_move (MoveRequest.UP); return true;
+ case Gdk.Key.Left: _request_move (MoveRequest.LEFT); return true;
+ case Gdk.Key.Right: _request_move (MoveRequest.RIGHT); return true;
+ default: return false;
+ }
+ }
+ private static inline uint _upper_key (uint keyval)
+ {
+ return (keyval > 255) ? keyval : ((char) keyval).toupper ();
}
private void window_size_allocate_cb ()
@@ -647,18 +660,44 @@ public class Application : Gtk.Application
if (left_or_right)
{
if (velocity_x < -10.0)
- _game.move_left ();
+ _request_move (MoveRequest.LEFT);
else if (velocity_x > 10.0)
- _game.move_right ();
+ _request_move (MoveRequest.RIGHT);
}
else if (up_or_down)
{
if (velocity_y < -10.0)
- _game.move_up ();
+ _request_move (MoveRequest.UP);
else if (velocity_y > 10.0)
- _game.move_down ();
+ _request_move (MoveRequest.DOWN);
}
else
return;
}
+
+ /*\
+ * * move requests
+ \*/
+
+ private enum MoveRequest {
+ UP,
+ RIGHT,
+ DOWN,
+ LEFT;
+ }
+
+ private void _request_move (MoveRequest request)
+ {
+ if (_game_should_init)
+ return;
+
+ _game_restored = false;
+ switch (request)
+ {
+ case MoveRequest.UP : _game.move_up (); break;
+ case MoveRequest.RIGHT : _game.move_right (); break;
+ case MoveRequest.DOWN : _game.move_down (); break;
+ case MoveRequest.LEFT : _game.move_left (); break;
+ }
+ }
}
diff --git a/src/game.vala b/src/game.vala
index 9553a78..a4994d9 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -112,9 +112,7 @@ public class Game : Object
}
}
- public uint score {
- get; set;
- }
+ internal uint score { internal get; private set; default = 0; }
public void new_game ()
{
@@ -193,20 +191,9 @@ public class Game : Object
return true;
}
- public bool key_pressed (Gdk.EventKey event)
+ internal bool cannot_move ()
{
- if (_state != GameState.IDLE)
- return true;
-
- uint keyval = _upper_key (event.keyval);
-
- if (keyval == Gdk.Key.Down) move_down ();
- else if (keyval == Gdk.Key.Up) move_up ();
- else if (keyval == Gdk.Key.Left) move_left ();
- else if (keyval == Gdk.Key.Right) move_right ();
- else
- return false;
- return true;
+ return _state != GameState.IDLE;
}
public void reload_settings ()
@@ -244,11 +231,6 @@ public class Game : Object
// return false;
}
- private uint _upper_key (uint keyval)
- {
- return (keyval > 255) ? keyval : ((char) keyval).toupper ();
- }
-
private void _on_allocation_changed (Clutter.ActorBox box, Clutter.AllocationFlags flags)
{
if (_background == null)
@@ -369,24 +351,15 @@ public class Game : Object
private void _create_tile (Tile tile)
{
- GridPosition pos;
- RoundedRectangle rect;
- TileView view;
- float x;
- float y;
- float width;
- float height;
-
- pos = tile.pos;
- rect = _background[pos.row,pos.col];
- x = rect.actor.x;
- y = rect.actor.y;
- width = rect.actor.width;
- height = rect.actor.height;
-
- assert (_foreground_nxt[pos.row,pos.col] == null);
- view = new TileView (x, y, width, height, tile.val);
- _foreground_nxt[pos.row,pos.col] = view;
+ GridPosition pos = tile.pos;
+ assert (_foreground_nxt [pos.row, pos.col] == null);
+
+ Clutter.Actor actor = _background [pos.row, pos.col].actor;
+ _foreground_nxt [pos.row, pos.col] = new TileView (actor.x,
+ actor.y,
+ actor.width,
+ actor.height,
+ tile.val);
}
internal void move_down ()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]