]> git.cworth.org Git - kub/blobdiff - kub.c
Draw tiles owned by player in a way which differentiates them from tiles on board
[kub] / kub.c
diff --git a/kub.c b/kub.c
index a029c137b845d6228a4cf9d61ce531ef0b2ba85b..91ca7fb1036fa0be0cc067204b119be8ddaa6047 100644 (file)
--- a/kub.c
+++ b/kub.c
@@ -54,6 +54,7 @@ typedef struct tile {
     int x;
     int y;
     int selected;
+    int owned;
 } tile_t;
 
 #define DECK_MAX_TILES 104
@@ -97,6 +98,7 @@ typedef struct game {
     selection_box_t selection_box;
     RsvgHandle *blanktile;
     RsvgHandle *selectedtile;
+    RsvgHandle *ownedtile;
 
     int current_tile;
     int select_mode;
@@ -128,6 +130,7 @@ static void tile_init (tile_t *tile, color_t color, int number)
     tile->x = 0;
     tile->y = 0;
     tile->selected = 0;
+    tile->owned = 0;
 }
 
 static void tile_set_x_y (tile_t *tile, int x, int y)
@@ -163,6 +166,8 @@ static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region
 
     if (tile->selected)
        rsvg_handle_render_cairo (game->selectedtile, cr);
+    if (tile->owned)
+       rsvg_handle_render_cairo (game->ownedtile, cr); 
     else
        rsvg_handle_render_cairo (game->blanktile, cr);
 
@@ -364,6 +369,7 @@ static void deck_deal(game_t *game, deck_t *deck)
            temp = deck->tiles[rand_tile];
            deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
            game->players[i].hand.tiles[j] = temp;
+           game->players[i].hand.tiles[j].owned = 1;
            deck->num_tiles -= 1;
            game->players[i].hand.num_tiles += 1;
        }
@@ -484,11 +490,15 @@ static void game_init(game_t *game)
     deck_init(&game->deck);
     deck_shuffle(&game->deck);
 
-    game->selectedtile = rsvg_handle_new_from_file ("tiles/selectedtile.svg", &error);
+    game->selectedtile = rsvg_handle_new_from_file ("tiles/selected_tile.svg", &error);
     if (error)
        FATAL_ERROR (error->message);
 
-    game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
+    game->ownedtile = rsvg_handle_new_from_file ("tiles/owned_tile.svg", &error);
+    if (error)
+       FATAL_ERROR (error->message);
+
+    game->blanktile = rsvg_handle_new_from_file ("tiles/blank_tile.svg", &error);
     if (error)
        FATAL_ERROR (error->message);
 
@@ -530,6 +540,13 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event,
 
     for (i = 0; i < game->deck.num_tiles; i++)
     {
+       curr_tile = &game->deck.tiles[i];
+       if (curr_tile->selected)
+       {
+           curr_tile->selected = 0;
+           gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+       }
+
        tile_x = game->deck.tiles[i].x;
        tile_y = game->deck.tiles[i].y;
        if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
@@ -537,10 +554,6 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event,
         {
            game->select_mode = 0;
  
-           curr_tile = &game->deck.tiles[game->current_tile];
-           game->deck.tiles[game->current_tile].selected = 0;
-           gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
-
            game->current_tile = i;
            curr_tile = &game->deck.tiles[game->current_tile];
            if (!curr_tile->selected)
@@ -647,26 +660,69 @@ static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event
        box = &game->selection_box;
        box->visible = 1;
 
-       gtk_widget_queue_draw_area ( widget,  MIN(box->x1, box->x2), MIN(box->y1,box->y2), abs(box->x2 - box->x1), abs(box->y2 - box->y1) );
+       int x_min = MIN(box->x1, box->x2);
+       int x_max = MAX(box->x1, box->x2);
+       int y_min = MIN(box->y1, box->y2);
+       int y_max = MAX(box->y1, box->y2);
+       int width = abs(box->x2 - box->x1);
+       int height = abs(box->y2 - box->y1);
+
+       gtk_widget_queue_draw_area ( widget,  x_min, y_min, width, height );
 
        box->x2 = event->x;
        box->y2 = event->y;
 
        gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2), MIN(box->y1, box->y2), abs(box->x2 - box->x1), abs(box->y2 - box->y1) );
 
-       int i;
+       int i, tile_x, tile_y, tile_x2, tile_y2;
        tile_t *curr_tile;
        for (i = 0; i < game->deck.num_tiles; i++)
-       {
-           if ((event->x >= game->deck.tiles[i].x &&
-                event->x <= game->deck.tiles[i].x + TILE_WIDTH) &&
-               (event->y >= game->deck.tiles[i].y &&
-                event->y <= game->deck.tiles[i].y + TILE_HEIGHT))
+        {
+           curr_tile = &game->deck.tiles[i];
+           if (curr_tile->selected)
+           {
+               curr_tile->selected = 0;
+               gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+           }
+
+           tile_x = game->deck.tiles[i].x;
+           tile_y = game->deck.tiles[i].y;
+           tile_x2 = tile_x + TILE_WIDTH;
+           tile_y2 = tile_y + TILE_HEIGHT;
+           if (/*If top-left corner*/
+               (tile_x >= x_min && tile_x <= x_max &&
+                tile_y >= y_min && tile_y <= y_max) ||
+               /*or bottom-right corner*/
+               (tile_x2 >= x_min && tile_x2 <= x_max &&
+                tile_y2 >= y_min && tile_y2 <= y_max) ||
+               /*or bottom-left corner*/
+               (tile_x >= x_min && tile_x <= x_max &&
+                tile_y2 >= y_min && tile_y2 <= y_max) ||
+               /*or top-right corner of tile selected*/
+               (tile_x2 >= x_min && tile_x2 <= x_max &&
+                tile_y >= y_min && tile_y <= y_max) ||
+               /*or left edge*/
+               (y_min >= tile_y && y_min <= tile_y2 &&
+                x_min <= tile_x && x_max >= tile_x) ||
+               /*or top edge*/
+               (x_min >= tile_x && x_min <= tile_x2 &&
+                y_min <= tile_y && y_max >= tile_y) ||
+               /*or right edge*/
+               (y_min >= tile_y && y_min <= tile_y2 &&
+                x_min >= tile_x && x_min <= tile_x2) ||
+               /*or bottom edge*/
+               (x_min >= tile_x && x_min <= tile_x2 &&
+                y_min >= tile_y && y_min <= tile_y) )
            {
-               curr_tile = &game->deck.tiles[i];
                curr_tile->selected = 1;
                gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
            }
+/*
+           else
+           {
+               curr_tile->selected = 0;
+               gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+               }*/
        }
     }
     else