Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <gtk/gtk.h>
  2.  
  3. #define ACE 0
  4. #define CARDS 13
  5. #define SUITS 4
  6. #define MAX_CARDS 10
  7. #define SCREENLINECOUNT 17
  8.  
  9. struct table {
  10.  
  11. int cards_dealer[MAX_CARDS];
  12.  
  13. int cards_player[MAX_CARDS];
  14.  
  15. int points[MAX_CARDS]; /* Points for each hand */
  16.  
  17. int total_player, total_dealer; /* total points */
  18.  
  19. int hand_player, hand_dealer; /* number of cards dealt */
  20.  
  21. int aces_player, aces_dealer; /* N° of aces: These variable are used to attribute values 1/11 to Aces*/
  22.  
  23. int credit;
  24.  
  25. int bet;
  26.  
  27. GtkWidget *vbox, *canvas;
  28.  
  29. };
  30.  
  31. struct {
  32. cairo_surface_t *image[MAX_CARDS];
  33. int x[MAX_CARDS];
  34. } glob;
  35.  
  36. static void destroy (GtkWidget *window, gpointer data);
  37. void button_clicked(GtkWidget *widget, struct table *game);
  38. void init_game(struct table *game);
  39. static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, gpointer user_data);
  40. static void do_drawing(cairo_t *cr);
  41.  
  42. int main (int argc, char *argv[])
  43. {
  44. GtkWidget *window, *button_hit, *button_stand, *hbox;
  45.  
  46. struct table game;
  47.  
  48. int i;
  49.  
  50. init_game(&game);
  51.  
  52. gtk_init (&argc, &argv);
  53.  
  54. window = gtk_window_new (GTK_WINDOW_TOPLEVEL); /* create window */
  55. gtk_window_set_title (GTK_WINDOW (window), "Show Images");
  56. gtk_window_maximize (GTK_WINDOW (window));
  57.  
  58. GtkCssProvider *css_provider = gtk_css_provider_new(); /* Apply style */
  59. gtk_css_provider_load_from_path (css_provider, "style.css", NULL);
  60.  
  61. GdkScreen *myScreen= gdk_screen_get_default ();
  62. gtk_style_context_add_provider_for_screen
  63. (myScreen,
  64. GTK_STYLE_PROVIDER (css_provider),
  65. GTK_STYLE_PROVIDER_PRIORITY_USER);
  66.  
  67. game.vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  68. gtk_container_add(GTK_CONTAINER (window), game.vbox);
  69.  
  70. game.canvas = gtk_drawing_area_new();
  71. gtk_container_add (GTK_CONTAINER (game.vbox), game.canvas);
  72. g_object_set (game.canvas, "expand", TRUE, NULL); /* expand the canvas */
  73.  
  74. hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 30 ); /* hbox for buttons */
  75. gtk_container_add (GTK_CONTAINER (game.vbox), hbox);
  76. button_hit = gtk_button_new_with_mnemonic ("Hit");
  77. gtk_container_add (GTK_CONTAINER (hbox), button_hit);
  78.  
  79. g_signal_connect(G_OBJECT(button_hit), "clicked", G_CALLBACK(button_clicked), &game);
  80. g_signal_connect (G_OBJECT (window), "destroy",
  81. G_CALLBACK (destroy), NULL);
  82. g_signal_connect(G_OBJECT(game.canvas), "draw",
  83. G_CALLBACK(on_draw_event), NULL);
  84.  
  85. button_stand = gtk_button_new_with_mnemonic ("Stand");
  86. gtk_container_add (GTK_CONTAINER (hbox), button_stand);
  87.  
  88. gtk_widget_show_all (window);
  89.  
  90. gtk_main();
  91.  
  92. for (i = 0; i < MAX_CARDS; i++)
  93. cairo_surface_destroy(glob.image[i]);
  94.  
  95. return 0;
  96. }
  97.  
  98. void button_clicked(GtkWidget *widget, struct table *game)
  99. {
  100. const gchar *cards_dealer[] = {"resized_cards/010.png", "resized_cards/011.png", "resized_cards/312.png", "resized_cards/01.png"};
  101. /*const gchar *cards_player[] = {"resized_cards/00.png", "resized_cards/01.png", "resized_cards/03.png", "resized_cards/312.png"};*/
  102.  
  103. int current_hand = game->hand_dealer;
  104.  
  105. glob.image[current_hand] = cairo_image_surface_create_from_png(cards_dealer[current_hand]);
  106.  
  107. game->hand_dealer++;
  108.  
  109. }
  110.  
  111. static void destroy (GtkWidget *window, gpointer data)
  112. {
  113. gtk_main_quit ();
  114. }
  115.  
  116. void init_game(struct table *game) {
  117.  
  118. game->total_player = game->total_dealer = 0;
  119.  
  120. game->aces_player = game->aces_dealer = 0;
  121.  
  122. game->hand_dealer = game->hand_player = 0;
  123.  
  124. /*draw_cards(player);*/
  125.  
  126. /*assign_points(player); */
  127.  
  128. /*assign_points(player); */
  129.  
  130. game->credit = 50;
  131.  
  132. glob.image[MAX_CARDS] = NULL;
  133.  
  134. glob.x[0] = 10;
  135.  
  136. int i;
  137.  
  138. for (i = 1; i < MAX_CARDS; i++)
  139. glob.x[i] += 220;
  140. }
  141.  
  142. static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, gpointer user_data)
  143. {
  144.  
  145. do_drawing(cr);
  146.  
  147. return FALSE;
  148. }
  149.  
  150. static void do_drawing(cairo_t *cr)
  151. {
  152. int i;
  153.  
  154. for (i = 0; i < MAX_CARDS; i++) {
  155. if (glob.image[i] != NULL)
  156. cairo_set_source_surface(cr, glob.image[i], glob.x[i], 10);
  157. cairo_paint(cr);
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement