Guest User

Untitled

a guest
Feb 13th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.89 KB | None | 0 0
  1. // LICENSE - WTFPL
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6.  
  7. #include <xcb/xcb.h>
  8. #include <inttypes.h>
  9.  
  10. #define WIN_W 800
  11. #define WIN_H 800
  12. #define BOARD_W 30
  13. #define BOARD_H 30
  14. #define TILE_W 20
  15. #define TILE_H 20
  16. #define START_DR_X 20
  17. #define START_DR_Y 20
  18.  
  19.  
  20. xcb_point_t coord_to_num(int x, int y)
  21. {
  22.     x -= START_DR_X;
  23.     y -= START_DR_Y;
  24.     if
  25.     (
  26.         (x < 0) || (x >= (BOARD_W*TILE_W) )  ||
  27.         (y < 0) || (y >= (BOARD_H*TILE_H) )
  28.     )
  29.     {
  30.         return (xcb_point_t){-1, -1};
  31.     }
  32.     //printf("%i %i\n", x, y);
  33.     return (xcb_point_t){(x/TILE_W), (y/TILE_H)};
  34. }
  35.  
  36.  
  37.  
  38.  
  39. void draw_board(xcb_connection_t *connection, xcb_drawable_t  window, xcb_gcontext_t  foreground)
  40. {
  41.     int shell_w = TILE_W * BOARD_W;
  42.     int shell_h = TILE_H * BOARD_H;
  43.     int shell_x = START_DR_X;
  44.     int shell_y = START_DR_Y;
  45.     xcb_rectangle_t shell[] =
  46.     {
  47.         {shell_x, shell_y, shell_w, shell_h},
  48.     };
  49.  
  50.     uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  51.     uint32_t color[2]  = {0x000000, 0};
  52.     xcb_change_gc (connection, foreground, mask, color);
  53.     xcb_poly_rectangle(connection, window, foreground, 1, shell);
  54.  
  55.     for (int i = 1; i < BOARD_W; ++i)
  56.     {
  57.         xcb_point_t line[] = {{shell_x + i*TILE_W, shell_y}, {0, shell_h}};
  58.         xcb_poly_line (connection, XCB_COORD_MODE_PREVIOUS, window, foreground, 2, line);
  59.     }
  60.  
  61.     for (int i = 1; i < BOARD_H; ++i)
  62.     {
  63.         xcb_point_t line[] = {{shell_x, shell_y + i*TILE_H}, {shell_w, 0}};
  64.         xcb_poly_line (connection, XCB_COORD_MODE_PREVIOUS, window, foreground, 2, line);
  65.     }
  66. }
  67.  
  68. // указатель на ф-цию которая рисует крестик или нолик
  69. typedef void (*draw_func)(xcb_connection_t *, xcb_drawable_t, xcb_gcontext_t , xcb_point_t);
  70.  
  71.  
  72. typedef struct
  73. {
  74.     draw_func func;
  75.     xcb_point_t point;
  76. } draw_this;
  77.  
  78. typedef struct
  79. {
  80.     draw_func func;
  81.     char* name;
  82. } Player;
  83.  
  84.  
  85.  
  86. void draw_smth(draw_this drw, xcb_connection_t *connection, xcb_drawable_t  window, xcb_gcontext_t  foreground)
  87. {
  88.     drw.func(connection, window, foreground, drw.point);
  89. }
  90.  
  91. void draw_all_figures(draw_this *drw, size_t num_figures, xcb_connection_t *connection, xcb_drawable_t  window, xcb_gcontext_t  foreground)
  92. {
  93.     for(size_t i = 0; i < num_figures; i++)
  94.     {
  95.         draw_smth(drw[i], connection, window, foreground);
  96.     }
  97. }
  98.  
  99. void draw_circle(xcb_connection_t *connection, xcb_drawable_t  window, xcb_gcontext_t  foreground, xcb_point_t point)
  100. {
  101.     xcb_arc_t arcs[] = {{point.x, point.y, TILE_W, TILE_H, 0, (360 << 6)}};
  102.     xcb_poly_arc (connection, window, foreground, 1, arcs);
  103. }
  104.  
  105. void draw_cross(xcb_connection_t *connection, xcb_drawable_t  window, xcb_gcontext_t  foreground, xcb_point_t point)
  106. {
  107.     xcb_point_t line[] = {{point.x, point.y}, {TILE_W, TILE_H}};
  108.     xcb_point_t line2[] = {{point.x, point.y + TILE_H}, {TILE_W, -TILE_H}};
  109.  
  110.     xcb_poly_line (connection, XCB_COORD_MODE_PREVIOUS, window, foreground, 2, line);
  111.     xcb_poly_line (connection, XCB_COORD_MODE_PREVIOUS, window, foreground, 2, line2);
  112. }
  113.  
  114.  
  115. void* create_board()
  116. {
  117.     int (* board)[BOARD_H][BOARD_W] = malloc( sizeof(int) * BOARD_W * BOARD_H);
  118.  
  119.     if (board)
  120.     {
  121.         for (size_t i = 0; i < BOARD_W; ++i)
  122.         {
  123.             //board[i] = malloc(sizeof *board[i] * BOARD_H);
  124.             for (size_t j = 0; j < BOARD_H; ++j)
  125.             {
  126.                 (*board)[i][j] = -1; // empty tile
  127.                 // тут вообще можно сделать одним for-ом т.к. этот массив board[][] идет непрерывным куском
  128.             }
  129.         }
  130.     }
  131.  
  132.     return board;
  133. }
  134.  
  135. int main(int argc, char const *argv[])
  136. {
  137.     xcb_connection_t *connection = xcb_connect (NULL, NULL);
  138.     /* Get the first screen */
  139.     xcb_screen_t *screen = xcb_setup_roots_iterator (xcb_get_setup (connection)).data;
  140.     /* Create black (foreground) graphic context */
  141.     xcb_drawable_t  window     = screen->root;
  142.     xcb_gcontext_t  foreground = xcb_generate_id (connection);
  143.     uint32_t    mask       = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  144.     uint32_t    values[2]  = {0xff0000, 0};
  145.  
  146.     xcb_create_gc (connection, foreground, window, mask, values);
  147.     window = xcb_generate_id (connection);
  148.     mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
  149.     values[0] = screen->white_pixel;
  150.     //values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE; не нужно столько эвентов, достаточно EXPOSURE и BUTTON_PRESS
  151.     values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS ;
  152.  
  153.     xcb_create_window(
  154.         connection,                    /* connection          */
  155.         XCB_COPY_FROM_PARENT,          /* depth               */
  156.         window,                        /* window Id           */
  157.         screen->root,                  /* parent window       */
  158.         0, 0,                          /* x, y                */
  159.         WIN_W, WIN_H,                  /* width, height       */
  160.         10,                            /* border_width        */
  161.         XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
  162.         screen->root_visual,           /* visual              */
  163.         mask, values                   /* masks               */
  164.         );
  165.  
  166.     xcb_map_window (connection, window);
  167.     xcb_flush (connection);
  168.     int (*board)[BOARD_H][BOARD_W] = create_board();
  169.     xcb_generic_event_t *event;
  170.  
  171.     draw_this figures[BOARD_W*BOARD_H]; size_t num_figures = 0;
  172.     xcb_point_t point;
  173.     bool turn = false;
  174.     Player players[2] = { {draw_cross, "Cross"}, {draw_circle, "Circle"} };
  175.  
  176.     while ((event = xcb_wait_for_event (connection)))
  177.     {
  178.         switch (event->response_type & ~0x80)
  179.         {
  180.             case XCB_EXPOSE:
  181.                 draw_board(connection, window, foreground);
  182.                 draw_all_figures(figures, num_figures, connection, window, foreground);
  183.                 xcb_flush (connection);
  184.  
  185.             case XCB_BUTTON_PRESS:
  186.             {
  187.                 xcb_button_press_event_t *bp = (xcb_button_press_event_t *)event;
  188.                 //switch (bp->detail) // НЕНУЖНО Т.К. ТОЛЬКО ОДНА ШТУКА default В switch
  189.                 //{
  190.                 //    default:
  191.                         ;
  192.                         /*printf ("Button %"PRIu8" pressed in window %"PRIu32", at coordinates (%"PRIi16",%"PRIi16")\n",
  193.                                 bp->detail, bp->event, bp->event_x, bp->event_y );*/
  194.  
  195.                         //xcb_point_t click = click_on_board(bp->event_x, bp->event_y, hitboxes);
  196.                         xcb_point_t click = coord_to_num(bp->event_x, bp->event_y);
  197.                         if ((click.x != -1) && ((*board)[click.x][click.y] == -1))
  198.                         {
  199.                             (*board)[click.x][click.y] = turn;
  200.                             figures[num_figures] = (draw_this){ players[turn].func, {click.x * TILE_W + START_DR_X, click.y*TILE_H + START_DR_Y}};
  201.                             num_figures++;
  202.                             players[turn].func(connection, window, foreground, (xcb_point_t){click.x * TILE_W  + START_DR_Y, click.y * TILE_H  + START_DR_Y} );
  203.                             xcb_flush (connection);
  204.                             (*board)[click.x][click.y] = turn;
  205.                             turn = !turn;
  206.                         }
  207. /*
  208.                         if (click.x != -1 && click.y != -1)
  209.                         {
  210.                             if ((*board)[click.x][click.y] == -1)
  211.                             {
  212.  
  213.                                 printf(
  214.                                         "%" PRId16" %" PRId16" -- %" PRId16" %" PRId16"\n",
  215.                                         click.x, click.y, bp->event_x, bp->event_y
  216.                                       );
  217.                                 (*board)[click.x][click.y] = turn; // это помечает что тут крестик или нолик
  218.                                 figures[num_figures] = (draw_this){ players[turn].func, {hitboxes[click.x][click.y].x, hitboxes[click.x][click.y].y}};
  219.                                 num_figures++;
  220.                                 players[turn].func(connection, window, foreground, (xcb_point_t){hitboxes[click.x][click.y].x, hitboxes[click.x][click.y].y} );
  221.                                 xcb_flush (connection);
  222.                                 (*board)[click.x][click.y] = turn;
  223.                                 turn = !turn;
  224.                             }
  225.                         }
  226. */
  227.  
  228.                 //}
  229.             }
  230.             default:
  231.                 break;
  232.         }
  233.         free(event);
  234.     }
  235.  
  236.     return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment