]> git.cworth.org Git - kub/blobdiff - kub.c
Created deck_spread which gives x, y values to tiles in deck, for drawing 8 rows...
[kub] / kub.c
diff --git a/kub.c b/kub.c
index 912daded8e862b97c1bbb94a34e6755871a720a3..df363f54f40e182e467c6f6c096d61f5d4cc371c 100644 (file)
--- a/kub.c
+++ b/kub.c
@@ -5,6 +5,42 @@
 #include <librsvg/rsvg.h>
 #include <librsvg/rsvg-cairo.h>
 
+/*
+ *   TILE_NUMBER_X_OFFSET = 3
+ * //
+ * || TILE_NUMBER_WIDTH = 34
+ * ||/   /
+ * ||   |  TILE_WIDTH = 40
+ * ||   | /
+ * |     | 
+ * +-----+ - TILE_NUMBER_Y_OFFSET = 3  -
+ * |+---+| -                           |
+ * ||   ||                             |
+ * ||   ||   TILE_NUMBER_HEIGHT = 24   |
+ * |+---+| -                           |
+ * |  _  | _                           |-TILE_HEIGHT = 50
+ * | / \ |                             |
+ * ||   ||   TILE_SUN_HEIGHT = 20      |
+ * | \_/ | _                           |
+ * |     |   TILE_SUN_Y_OFFSET = 3     |       
+ * +-----+ -                           -
+ * ||   | TILE_SUN_WIDTH = 20
+ * ||
+ * || TILE_SUN_X_OFFSET = 10
+ */
+#define TILE_WIDTH             40
+#define TILE_HEIGHT            50
+
+#define TILE_NUMBER_X_OFFSET   3
+#define TILE_NUMBER_Y_OFFSET   3
+#define TILE_NUMBER_WIDTH      34
+#define TILE_NUMBER_HEIGHT     24
+
+#define TILE_SUN_X_OFFSET      10
+#define TILE_SUN_Y_OFFSET      3
+#define TILE_SUN_WIDTH         20
+#define TILE_SUN_HEIGHT                20
+
 #define FATAL_ERROR(msg) \
     do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0)
 
@@ -54,6 +90,8 @@ typedef struct game {
     board_t board;
     deck_t deck;
     RsvgHandle *blanktile;
+
+    int current_tile;
 } game_t;
 
 static void
@@ -321,17 +359,26 @@ static void deck_print(deck_t *deck)
     printf ("There are %d tiles in the deck\n" , deck->num_tiles);
 }
 
-static void deck_draw(game_t *game, cairo_t *cr)
+static void deck_spread(deck_t *deck)
 {
     int i, j;
-    
-    for (i = 0; i < 8; ++i)
+    for (i = 0; i < 8; i++)
     {
-       for (j = 0; j < 13; ++j)
+       for (j = 0; j < 13; j++)
        {
-           tile_draw(game, &game->deck.tiles[j + (i * 13)], cr);
+           deck->tiles[j + (i * 13)].x = j * 50;
+           deck->tiles[j + (i * 13)].y = i * 60;
        }
     }
+}
+
+static void deck_draw(game_t *game, cairo_t *cr)
+{
+    int i;
+    for (i = 0; i < game->deck.num_tiles; i++)
+    {
+           tile_draw(game, &game->deck.tiles[i], cr);
+    }
 }   
 
 static void hand_print(game_t *game)
@@ -363,6 +410,8 @@ static void game_init(game_t *game)
     game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
     if (error)
        FATAL_ERROR (error->message);
+
+    game->current_tile = game->deck.num_tiles - 1;
 }
 
 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
@@ -387,7 +436,25 @@ static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_
 
 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
 {
-    printf ("You pressed button %d\n", event->button);
+    tile_t *tile;
+
+    tile = &game->deck.tiles[game->current_tile];
+
+    printf ("Placing tile #%d\n", game->current_tile);
+
+    /* First, invalidate the region where the tile currently is. */
+    gtk_widget_queue_draw_area (widget, tile->x, tile->y, TILE_WIDTH, TILE_HEIGHT);
+                       
+    /* Then, move the tile */
+    tile->x = event->x;
+    tile->y = event->y;
+
+    /* Finally, invalidate the region where the tile is now. */
+    gtk_widget_queue_draw_area (widget, tile->x, tile->y, TILE_WIDTH, TILE_HEIGHT);
+
+    game->current_tile--;
+    if (game->current_tile < 0)
+       game->current_tile = game->deck.num_tiles - 1;
 
     return TRUE;
 }
@@ -410,9 +477,10 @@ int main(int argc, char *argv[])
     
     game_init(&game);
     deck_print(&game.deck);
+    deck_spread(&game.deck);
     //deck_deal(&game, &game.deck);
-    hand_print(&game);
-    deck_print(&game.deck);
+    //hand_print(&game);
+    //deck_print(&game.deck);
 
     /* Create a new window */
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);