X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=kub.c;h=2b659b036095dd63e61fae0746a5618d3e5269ca;hb=e6463d87baeb74db2065f00e657eaac571b98592;hp=e879e8b1f5f86c5bbf52e8149806669f43392093;hpb=7d059f058ddcb67baa6921c66389381968ea080c;p=kub diff --git a/kub.c b/kub.c index e879e8b..2b659b0 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,15 +103,29 @@ 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); } -static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr) +static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region) { char number_string[3]; int len; + GdkRectangle rectangle; + + rectangle.x = tile->x - 1; + rectangle.y = tile->y - 1; + rectangle.width = TILE_WIDTH + 2; + rectangle.height = TILE_HEIGHT + 2; + if (gdk_region_rect_in (region, &rectangle) == GDK_OVERLAP_RECTANGLE_OUT) + return; len = snprintf (number_string, 3, "%d", tile->number + 1); if (len < 0 || len >= 3) @@ -128,8 +142,11 @@ static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr) if (tile->color == RED) cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); if (tile->color == YELLOW) - cairo_set_source_rgb (cr, 1.0, .843, 0.0); - cairo_move_to (cr, 10, 25); + cairo_set_source_rgb (cr, 1.0, .843, 0.0); + if (tile->number + 1 > 9) + cairo_move_to (cr, 1, 25); + else + cairo_move_to (cr, 10, 25); cairo_set_font_size(cr, 25); cairo_show_text (cr, number_string); @@ -372,21 +389,38 @@ static void deck_spread(deck_t *deck) } } -static void deck_draw(game_t *game, cairo_t *cr) +static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region) { int i; for (i = 0; i < game->deck.num_tiles; i++) { - tile_draw(game, &game->deck.tiles[i], cr); + tile_draw(game, &game->deck.tiles[i], cr, region); } } -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, GdkRegion *region) { 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_print(game->players[0].hand.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_draw(game, &game->players[player].hand.tiles[i], cr, region); } } @@ -412,6 +446,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) @@ -420,7 +455,9 @@ static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_ cr = gdk_cairo_create (widget->window); - deck_draw(game, cr); + deck_draw(game, cr, event->region); + + hand_draw(game, 0, cr, event->region); cairo_destroy (cr); @@ -437,42 +474,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; + 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 +502,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 +531,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 */