]> git.notmuchmail.org Git - kub/blobdiff - kub.c
Clean up whitespace
[kub] / kub.c
diff --git a/kub.c b/kub.c
old mode 100644 (file)
new mode 100755 (executable)
index 558a280..223e4ea
--- a/kub.c
+++ b/kub.c
@@ -90,18 +90,28 @@ typedef struct selection_box {
 #define GAME_WINDOW_DEFAULT_WIDTH  800
 #define GAME_WINDOW_DEFAULT_HEIGHT 600
 
+typedef struct state {
+    player_t players[GAME_MAX_PLAYERS];
+    board_t board;
+    deck_t deck;
+} state_t;
+
 typedef struct game {
     player_t players[GAME_MAX_PLAYERS];
     int num_players;
     board_t board;
     deck_t deck;
     selection_box_t selection_box;
+    state_t state;
     RsvgHandle *blanktile;
     RsvgHandle *selectedtile;
     RsvgHandle *ownedtile;
 
-    int current_tile;
+    int current_player;
+    tile_t *current_tile;
+//    int current_tile;
     int select_mode;
+    int drag_group_mode;
     int diff_x, diff_y;
     int click_x, click_y;
     int release_x, release_y;    /*Currently unused*/
@@ -221,6 +231,18 @@ static int tile_compare(const void *one, const void *two)
     return tile_one->number - tile_two->number;
 }
 
+static int tile_compare_1(tile_t *tile_one, tile_t *tile_two)
+{
+    return tile_one->number - tile_two->number;
+}
+
+/*
+static int tile_in_box(game_t *game, tile_t *tile)
+{
+
+}
+*/
+
 static int tile_group_is_run_one(tile_group_t *tile_group)
 {
     int i;
@@ -455,16 +477,18 @@ static void hand_print(game_t *game, int player)
     }
 }
 
-static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
+static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region, GtkWidget *widget)
 {
     int i;
-    int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
-    int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
+    int window_width = widget->allocation.width;
+//    int window_width = GAME_WINDOW_DEFAULT_WIDTH;
+    int window_height = widget->allocation.height;
+//    int window_height = GAME_WINDOW_DEFAULT_HEIGHT;
     for (i = 0; i < game->players[player].hand.num_tiles; i++)
     {
        tile_set_x_y(&game->players[player].hand.tiles[i],
-                    ((gwdw / game->players[player].hand.num_tiles)) * i,
-                    (gwdh - TILE_HEIGHT - 6) );
+                    ((window_width / game->players[player].hand.num_tiles)) * i,
+                    (window_height - TILE_HEIGHT - 6) );
     }
     for (i = 0; i < game->players[player].hand.num_tiles; i++)
     {
@@ -472,6 +496,21 @@ static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
     }
 }
 
+static void save_state(game_t *game)
+{
+    //DO STUFF HERE
+    game->state.board = game->board;
+    game->state.deck = game->deck;
+    //game->state.players = game->players;
+}
+
+static void restore_state(game_t *game)
+{
+    game->board = game->state.board;
+    game->deck = game->state.deck;
+    //game->players = game->state.players;
+}
+
 static void game_init(game_t *game)
 {
     int i;
@@ -484,6 +523,7 @@ static void game_init(game_t *game)
        player_init(&game->players[i]);
        game->num_players += 1;
     }
+    game->current_player = 0;
     
     selection_box_init(&game->selection_box);
     board_init(&game->board);
@@ -502,8 +542,10 @@ static void game_init(game_t *game)
     if (error)
        FATAL_ERROR (error->message);
 
-    game->current_tile = game->deck.num_tiles - 1;
+//    game->current_tile = game->deck.num_tiles - 1;
+    game->current_tile = &game->deck.tiles[0];
     game->select_mode = 1;
+    game->drag_group_mode = 0;
 
     game->diff_x = game->diff_y = 0;
 }
@@ -519,7 +561,8 @@ static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_
     if (game->selection_box.visible)
        selection_box_draw(&game->selection_box, cr);
 
-    hand_draw(game, 0, cr, event->region);
+    hand_draw(game, game->current_player, cr, event->region, widget);
+//    hand_draw(game, 0, cr, event->region);
 
     cairo_destroy (cr);
 
@@ -528,16 +571,80 @@ static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_
 
 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
 {
+    cairo_t *cr;
+
+    cr = gdk_cairo_create (widget->window);
+
     printf ("You pressed key %d\n", event->keyval);
 
+    if (event->keyval == 65293)                //HIT ENTER
+    {
+       save_state(game);
+       printf ("\tEnd of player %d's turn\n", game->current_player + 1);
+       if (game->current_player == game->num_players - 1)
+           game->current_player = 0;
+       else
+           game->current_player += 1;
+       gtk_widget_queue_draw(widget);
+    }
+
+    if (event->keyval == 65307)                //HIT ESCAPE
+    {
+       restore_state(game);
+       printf ("\tChanges Reverted\n");
+       gtk_widget_queue_draw(widget);
+    }
+
+    if (event->keyval == 112)          //HIT "P"
+    {
+       deck_print(&game->deck);
+       //hand_draw(game, game->current_player, cr, event->region, widget);
+       //on_expose_event(widget, event, game);
+    }
+
+
+    if (event->keyval == 65505 || event->keyval == 65506)
+       game->drag_group_mode = 1;
+
     return TRUE;
 }
 
 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
 {
     int i, tile_x, tile_y;
-    tile_t *curr_tile = &game->deck.tiles[game->current_tile];    
+    tile_t *curr_tile;
+    player_t *curr_player = &game->players[0];
 
+    /*Handle tiles in player's hand */
+    for (i = 0; i < curr_player->hand.num_tiles; i++)
+    {
+       curr_tile = &curr_player->hand.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 = curr_player->hand.tiles[i].x;
+       tile_y = curr_player->hand.tiles[i].y;
+       if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
+           event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
+        {
+           game->select_mode = 0;
+
+           game->current_tile = curr_tile;
+
+           if (!curr_tile->selected)
+               curr_tile->selected = 1;
+           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);
+
+           game->diff_x = event->x - tile_x;
+           game->diff_y = event->y - tile_y;
+        }
+    }
+
+    /*Handle tiles in deck */
     for (i = 0; i < game->deck.num_tiles; i++)
     {
        curr_tile = &game->deck.tiles[i];
@@ -554,8 +661,10 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event,
         {
            game->select_mode = 0;
  
-           game->current_tile = i;
-           curr_tile = &game->deck.tiles[game->current_tile];
+//         game->current_tile = i;
+           game->current_tile = curr_tile;
+
+//delete_this?     curr_tile = &game->deck.tiles[game->current_tile];
            if (!curr_tile->selected)
                curr_tile->selected = 1;
            else
@@ -568,14 +677,15 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event,
     }
     if (game->select_mode)
     {
-       game->deck.tiles[game->current_tile].selected = 0;
+//     game->deck.tiles[game->current_tile].selected = 0;
+       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->selection_box.visible = 1;
-       /*These next two lines are likely to be replaced by...*/
+       /*These next two lines appear to be dead
        game->click_x = event->x;
-       game->click_y = event->y;
-       /*...these two lines*/
+       game->click_y = event->y;*/
+
        game->selection_box.x1 = event->x;
        game->selection_box.x2 = event->x;
        game->selection_box.y1 = event->y;
@@ -624,7 +734,7 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even
                /*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*/
+               /*or top-right corner*/
                (tile_x2 >= x_min && tile_x2 <= x_max &&
                 tile_y >= y_min && tile_y <= y_max) ||
                /*or left edge*/
@@ -636,14 +746,14 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even
                /*or right edge*/
                (y_min >= tile_y && y_min <= tile_y2 &&
                 x_min >= tile_x && x_min <= tile_x2) ||
-               /*or bottom edge*/
+               /*or bottom edge of tile selected*/
                (x_min >= tile_x && x_min <= tile_x2 &&
                 y_min >= tile_y && y_min <= tile_y) )
             {          
 //             group.tiles[group.num_tiles] = game->deck.tiles[i];
 //             group.num_tiles++;
 
-               group[num_tiles] = &game->deck.tiles[i];;
+               group[num_tiles] = &game->deck.tiles[i];
                num_tiles++;
             }
         }
@@ -653,6 +763,7 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even
 //     int matching_y = y_min;
        
 //     for (i = 0; i < group.num_tiles; i++)
+
        for (i = 0; i < num_tiles; i++)
        {
 //         tile_print(group.tiles[i]);
@@ -712,11 +823,6 @@ static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *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;
@@ -731,7 +837,7 @@ static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event
                /*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*/
+               /*or top-right corner*/
                (tile_x2 >= x_min && tile_x2 <= x_max &&
                 tile_y >= y_min && tile_y <= y_max) ||
                /*or left edge*/
@@ -743,25 +849,34 @@ static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event
                /*or right edge*/
                (y_min >= tile_y && y_min <= tile_y2 &&
                 x_min >= tile_x && x_min <= tile_x2) ||
-               /*or bottom edge*/
+               /*or bottom edge of tile selected*/
                (x_min >= tile_x && x_min <= tile_x2 &&
                 y_min >= tile_y && y_min <= tile_y) )
            {
                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);
-               }*/
+               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);
+               }
+           }
        }
     }
     else
     {
+       if (game->drag_group_mode)
+       {
+
+       }
+
        tile_t *tile;
-       tile = &game->deck.tiles[game->current_tile];
+//     tile = &game->deck.tiles[game->current_tile];
+       tile = game->current_tile;
 
        /* First, invalidate the region where the tile currently is. */
        gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);