X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=kub.c;h=1e508b7b3decc4c2b0609cf32dee74e008a21883;hb=3a85458521e665ba1866507f9fad75a3f2e78987;hp=e879e8b1f5f86c5bbf52e8149806669f43392093;hpb=7d059f058ddcb67baa6921c66389381968ea080c;p=kub diff --git a/kub.c b/kub.c index e879e8b..1e508b7 100644 --- a/kub.c +++ b/kub.c @@ -92,10 +92,10 @@ typedef struct game { RsvgHandle *blanktile; int current_tile; + int diff_x, diff_y; } game_t; -static void -tile_init (tile_t *tile, color_t color, int number) +static void tile_init (tile_t *tile, color_t color, int number) { tile->color = color; tile->number = number; @@ -103,6 +103,12 @@ tile_init (tile_t *tile, color_t color, int number) tile->y = 0; } +static void tile_set_x_y (tile_t *tile, int x, int y) +{ + tile->x = x; + tile->y = y; +} + static void tile_print(tile_t tile) { printf("%6s %2d\n", colors[tile.color], tile.number + 1); @@ -381,12 +387,29 @@ static void deck_draw(game_t *game, cairo_t *cr) } } -static void hand_print(game_t *game) +static void hand_print(game_t *game, int player) +{ + int i; + for (i = 0; i < game->players[player].hand.num_tiles; i++) + { + tile_print(game->players[player].hand.tiles[i]); + } +} + +static void hand_draw(game_t *game, int player, cairo_t *cr) { int i; - for (i = 0; i < game->players[0].hand.num_tiles; ++i) + int gwdw = GAME_WINDOW_DEFAULT_WIDTH; + int gwdh = 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) ); + } + for (i = 0; i < game->players[player].hand.num_tiles; i++) { - tile_print(game->players[0].hand.tiles[i]); + tile_draw(game, &game->players[player].hand.tiles[i], cr); } } @@ -412,6 +435,7 @@ static void game_init(game_t *game) FATAL_ERROR (error->message); game->current_tile = game->deck.num_tiles - 1; + game->diff_x = game->diff_y = 0; } static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game) @@ -422,6 +446,8 @@ static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_ deck_draw(game, cr); + hand_draw(game, 0, cr); + cairo_destroy (cr); return TRUE; @@ -437,42 +463,26 @@ static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game) { int i, tile_x, tile_y; - + for (i = 0; i < game->deck.num_tiles; i++) { 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) && event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) ) - + { game->current_tile = i; + game->diff_x = event->x - tile_x; + game->diff_y = event->y - tile_y; + } } - /*Carl's Code*/ - 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; } static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game) { + game->current_tile = -1; printf ("You released button %d\n", event->button); return TRUE; @@ -481,11 +491,20 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event, game_t *game, cairo_t *cr) { - game->deck.tiles[game->current_tile].x = event->x; - game->deck.tiles[game->current_tile].y = event->y; - - tile_draw(game, &game->deck.tiles[game->current_tile], cr); - + tile_t *tile; + + tile = &game->deck.tiles[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); + + /* Then, move the tile */ + tile->x = event->x - game->diff_x; + tile->y = event->y - game->diff_y; + + /* Finally, invalidate the region where the tile is now. */ + gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2); + return TRUE; } @@ -501,8 +520,8 @@ 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_deal(&game, &game.deck); + //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0]) //deck_print(&game.deck); /* Create a new window */