X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=kub.c;h=1e508b7b3decc4c2b0609cf32dee74e008a21883;hb=3a85458521e665ba1866507f9fad75a3f2e78987;hp=bad09fdb9ccaf3ba73150be4a6539efcb396b443;hpb=ab303017f7875e536abebf83acb864ec2de52e3d;p=kub diff --git a/kub.c b/kub.c index bad09fd..1e508b7 100644 --- a/kub.c +++ b/kub.c @@ -5,6 +5,42 @@ #include #include +/* + * 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) @@ -15,6 +51,8 @@ typedef enum {BLACK, BLUE, RED, YELLOW} color_t; typedef struct tile { color_t color; int number; + int x; + int y; } tile_t; #define DECK_MAX_TILES 104 @@ -52,14 +90,31 @@ typedef struct game { board_t board; deck_t deck; RsvgHandle *blanktile; + + int current_tile; + int diff_x, diff_y; } game_t; +static void tile_init (tile_t *tile, color_t color, int number) +{ + tile->color = color; + tile->number = number; + tile->x = 0; + 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, int x, int y) +static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr) { char number_string[3]; int len; @@ -69,7 +124,7 @@ static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, int x, int y) FATAL_ERROR ("snprintf failed"); cairo_save(cr); - cairo_translate(cr, x, y); + cairo_translate(cr, tile->x, tile->y); rsvg_handle_render_cairo (game->blanktile, cr); if (tile->color == BLACK) @@ -273,9 +328,7 @@ static void deck_init(deck_t *deck) { for (j = 0; j <= 12; ++j) { - deck->tiles[j + (i * 13) + (h * 52)].color = i; - deck->tiles[j + (i * 13) + (h * 52)].number = j; - deck->num_tiles += 1; + tile_init (&deck->tiles[deck->num_tiles++], i, j); printf ("There are %d tiles in the deck\n", deck->num_tiles); } } @@ -312,26 +365,51 @@ 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, 45 * j, 55 * i); + 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) +static void hand_print(game_t *game, int player) { int i; - for (i = 0; i < game->players[0].hand.num_tiles; ++i) + for (i = 0; i < game->players[player].hand.num_tiles; i++) { - tile_print(game->players[0].hand.tiles[i]); + tile_print(game->players[player].hand.tiles[i]); + } +} + +static void hand_draw(game_t *game, int player, cairo_t *cr) +{ + int 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_draw(game, &game->players[player].hand.tiles[i], cr); } } @@ -355,6 +433,9 @@ 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; + game->diff_x = game->diff_y = 0; } static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game) @@ -365,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; @@ -379,18 +462,52 @@ 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); + 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; + } + } 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; } +static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event, + game_t *game, cairo_t *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; +} + int main(int argc, char *argv[]) { GtkWidget *window; @@ -402,9 +519,10 @@ int main(int argc, char *argv[]) game_init(&game); deck_print(&game.deck); - //deck_deal(&game, &game.deck); - hand_print(&game); - deck_print(&game.deck); + deck_spread(&game.deck); + 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 */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); @@ -415,6 +533,7 @@ int main(int argc, char *argv[]) gtk_widget_set_events (window, GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK | + GDK_BUTTON_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); @@ -428,6 +547,9 @@ int main(int argc, char *argv[]) G_CALLBACK (on_button_press_event), &game); g_signal_connect (G_OBJECT (window), "button_release_event", G_CALLBACK (on_button_release_event), &game); + g_signal_connect (G_OBJECT (window), "motion_notify_event", + G_CALLBACK (on_button_motion_event), &game); + gtk_widget_show_all (window); gtk_main ();