home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xpool-10.zip / Xpool / init.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  12KB  |  463 lines

  1.  
  2.  
  3.  
  4. /* I. ARIT 1992 Hidirbeyli,AYDIN,TR.  09400
  5.                 Golden,    CO,   USA. 80401
  6.  
  7.  
  8.   Copyright (C) 1992 Ismail ARIT
  9.  
  10.   This file  is distributed in the hope that it will be useful,
  11.   but without any warranty.  No author or distributor accepts
  12.   responsibility to anyone for the consequences of using it or for
  13.   whether it serves any particular purpose or works at all.
  14.  
  15.  
  16.   Everyone is granted permission to copy, modify and redistribute
  17.   this file under the following conditions:
  18.  
  19.  
  20.      Permission is granted to anyone to make or distribute copies
  21.      of the source code, either as received or modified, in any
  22.      medium, provided that all copyright notices, permission and
  23.      nonwarranty notices are preserved, and that the distributor
  24.      grants the recipient permission for further redistribution as
  25.      permitted by this document.
  26.      No part of this program can be used in any commercial product.
  27.  
  28.  
  29. */
  30.  
  31.  
  32.  
  33.  
  34.  
  35. #include <X11/X.h>
  36. #include    <stdio.h>
  37. #include    <string.h>
  38. #include    <X11/Xlib.h>
  39. #include    <X11/Xutil.h>
  40. #include    <X11/cursorfont.h>
  41. #include    "definition.h"
  42. #include    "ball.bmp"
  43. #include    "tile.bmp"
  44.  
  45.  
  46. /* some global variables */
  47. Display * disp;
  48. int     screen;
  49. Colormap Cmap;
  50. Window main_window, Pane;
  51. GC gc;
  52. GC XorGc;
  53.  
  54. XWindowAttributes xwa;
  55. Pixmap BallBitmap;
  56. Pixmap TileBitmap;
  57.  
  58.  
  59.  
  60. Window exitbutton, newgame, Yourscoreboard, Myscoreboard;
  61. Window WhoseTurnWindow;
  62. Window MyPlus, YourPlus, MyMinus, YourMinus;
  63. Window TraJectory;
  64.  
  65.  
  66. char   *BallBitmapFileName = "ball.bmp";
  67. int     ballWidth,
  68.         ballHeight,
  69.         ballHotX,
  70.         ballHotY;
  71.  
  72. unsigned int    linewidth = 8;
  73. int     linestyle = LineSolid;
  74. int     capstyle = CapRound;
  75. int     joinstyle = JoinRound;
  76.  
  77. extern  void setcolor (int color);
  78.  
  79. int     ascent,
  80.         descent;
  81. unsigned long   fg,
  82.                 bg,
  83.                 bd;
  84. int     depth;
  85.  
  86. XWMHints xwmh =
  87. {
  88.     (InputHint | StateHint),    /* flags */
  89.     False,            /* input */
  90.     NormalState,        /* initial_state */
  91.     0,            /* icon pixmap */
  92.     0,            /* icon window */
  93.     0, 0,            /* icon location */
  94.     0,            /* icon mask */
  95.     0,
  96. };
  97.  
  98.  
  99.  
  100.  /* every parameters should go in to the this string here */
  101.  
  102. Cursor buttonCursor;
  103.  
  104. XFontStruct * fontstruct;    /* Fonts */
  105.  
  106.  
  107.  
  108. char   *progname = "pool table";
  109.  
  110. void shadow (button, x, y, xx, yy)
  111. Window button;
  112. int     x,
  113.         y,
  114.         xx,
  115.         yy;
  116. {
  117.     XSetLineAttributes (disp, gc, 1, linestyle, capstyle, joinstyle);
  118.  
  119.     XSetForeground (disp, gc, get_color ("White"));
  120.     XDrawLine (disp, button, gc, x, y, xx, y);
  121.     XDrawLine (disp, button, gc, x + 1, y + 1, xx - 1, y + 1);
  122.     XDrawLine (disp, button, gc, x, y, x, yy);
  123.     XDrawLine (disp, button, gc, x + 1, y + 1, x + 1, yy - 1);
  124.     XSetForeground (disp, gc, get_color ("Black"));
  125.     XDrawLine (disp, button, gc, xx, yy, xx, y);
  126.     XDrawLine (disp, button, gc, xx - 1, yy, xx - 1, y + 1);
  127.     XDrawLine (disp, button, gc, xx, yy, x, yy);
  128.     XDrawLine (disp, button, gc, xx - 1, yy - 1, x + 1, yy - 1);
  129. /* go back to thick style */
  130.     XSetLineAttributes (disp, gc, linewidth, linestyle, capstyle, joinstyle);
  131.  
  132.     XSync (disp, 0);
  133. }
  134.  
  135.  
  136. Window create_button (parent, x, y, w, h, fg, bg)
  137. Window parent;
  138. int     x,
  139.         y,
  140.         w,
  141.         h,
  142.         fg,
  143.         bg;
  144. {
  145.     Window button;
  146.     button = XCreateSimpleWindow (disp, parent, x, y, w, h, 1, fg, bg);
  147.     XDefineCursor (disp, button, buttonCursor);
  148.     XSelectInput (disp, button, ExposureMask | ButtonPressMask);
  149.     XMapWindow (disp, button);
  150.     XSync (disp, 0);
  151.     shadow (button, 0, 0, w - 1, h - 1);
  152.     return (button);
  153.  
  154. }
  155.  
  156.  
  157. void PutIn (win, string, gc, fg, bg)
  158. Window win;
  159. char   *string;
  160. GC gc;
  161. int     fg,
  162.         bg;
  163. {
  164.  
  165.     int     x,
  166.             y,
  167.             w,
  168.             h,
  169.             len;
  170.     XWindowAttributes attrib;
  171.     XGetWindowAttributes (disp, win, &attrib);
  172.     w = attrib.width;
  173.     h = attrib.height;
  174.  
  175.     XSetWindowBackground (disp, win, bg);
  176.     XSetForeground (disp, gc, fg);
  177.     XSetBackground (disp, gc, bg);
  178.  
  179.     XClearWindow (disp, win);
  180.     x = (w - XTextWidth (fontstruct, string, (len = strlen (string)))) / 2;
  181.     y = 1 + (h + fontstruct -> max_bounds.ascent - fontstruct -> max_bounds.descent) / 2;
  182.     XDrawImageString (disp, win, gc, x, y, string, len);
  183.     shadow (win, 0, 0, w - 1, h - 1);
  184.     XSync (disp, 0);
  185.  
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. initgraph (argc, argv)
  198. int     argc;
  199. char  **argv;
  200. {
  201.     int     button_wide;
  202.     int     button_height;
  203.     int     slide_wide,
  204.             slide_height;
  205.     unsigned long   bw;        /* Border width */
  206.     XGCValues gcv;        /* Struct for creating GC */
  207.     XSizeHints xsh;        /* Size hints for window manager */
  208.     int     direction;
  209.     XCharStruct * xchar;
  210.     int     tableBorder;
  211.  
  212.  
  213.     if ((disp = XOpenDisplay (NULL)) == NULL) {
  214.     fprintf (stderr, "%s: can't open %s\n", progname, XDisplayName (NULL));
  215.     exit (1);
  216.     }
  217.  
  218.  
  219.     Cmap = DefaultColormap (disp, DefaultScreen (disp));
  220.  
  221.  
  222.     if ((fontstruct = XLoadQueryFont (disp, FONT)) == NULL) {
  223.     fprintf (stderr, "display %s doesn't know font %s\n",
  224.         DisplayString (disp), FONT);
  225.     exit (1);
  226.     }
  227.  
  228.     bd = WhitePixel (disp, DefaultScreen (disp));
  229.     /* 
  230.      bg = BlackPixel(disp, DefaultScreen(disp));
  231.      */
  232.  
  233.     bg = get_color (MAIN_BG);
  234.     fg = BlackPixel (disp, DefaultScreen (disp));
  235.  
  236.     /* 
  237.      * Set the border width of the window,  and the gap between the text
  238.      * and the edge of the window.
  239.      */
  240.     bw = BORDER;
  241.     xsh.flags = (PPosition | PSize);
  242.     xsh.height = 500 + 20;
  243.     xsh.width = 875 + 20;    /* these are window initial sizes */
  244.     xsh.x = 0;
  245.     xsh.y = 0;
  246.  
  247.     /* 
  248.      * Create the Window with the information in the XSizeHints, the
  249.      * border width,  and the border & background pixels. 
  250.      */
  251.     main_window = XCreateSimpleWindow (disp, DefaultRootWindow (disp),
  252.         xsh.x, xsh.y, xsh.width, xsh.height,
  253.         bw, bd, bg);
  254.  
  255.  
  256. /* None is the icon_bitmap */
  257.  
  258.     XSetStandardProperties (disp, main_window, progname, progname, None,
  259.         argv, argc, &xsh);
  260.     XSetWMHints (disp, main_window, &xwmh);
  261.  
  262.     xwa.colormap = DefaultColormap (disp, DefaultScreen (disp));
  263.     xwa.bit_gravity = CenterGravity;
  264.  
  265.     XChangeWindowAttributes (disp, main_window, (CWColormap | CWBitGravity), &xwa);
  266.  
  267.     gcv.font = fontstruct -> fid;
  268.     gcv.foreground = fg;
  269.     gcv.background = bg;
  270.  
  271.     gc = XCreateGC (disp, main_window, (GCFont | GCForeground | GCBackground), &gcv);
  272.     if (!gc)
  273.     printf ("problem with the gc..\n");
  274.  
  275.     gcv.function = GXxor;
  276.     XorGc = XCreateGC (disp, main_window, (GCFunction | GCFont | GCForeground
  277.         | GCBackground), &gcv);
  278.     if (!XorGc)
  279.     printf ("problem with the XorGc ..\n");
  280.  
  281.     XSetLineAttributes (disp, gc, linewidth, linestyle, capstyle, joinstyle);
  282.     XSetBackground (disp, XorGc, get_color (PANE_BG));
  283.  
  284.     XSelectInput (disp, main_window, ExposureMask | KeyPressMask | ButtonReleaseMask | ButtonPressMask | StructureNotifyMask);
  285.  
  286.     XTextExtents (fontstruct, "W", 1, &direction, &ascent, &descent, &xchar);
  287.  
  288.     button_wide = 100;
  289.     button_height = 15;
  290.     slide_wide = 15;
  291.     slide_height = 150;
  292.     fg = get_color (MENU_FG);
  293.     bg = get_color (MENU_BG);
  294.  
  295.     buttonCursor = XCreateFontCursor (disp, BUTTONCURSOR);
  296.  
  297.     fg = get_color (PANE_FG);
  298.     bg = get_color (PANE_BG);
  299.     tableBorder = 3;
  300.     Pane = XCreateSimpleWindow (disp, main_window,
  301.         (button_wide + 20), 30, (xsh.width - button_wide - 30 - 20),
  302.         (xsh.height - 40 - 20), tableBorder, get_color("Black"), bg);
  303.     XSelectInput (disp, Pane, ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
  304.         | ExposureMask | PointerMotionHintMask);
  305.     XMapWindow (disp, Pane);
  306.  
  307.     XMapWindow (disp, main_window);
  308.     XSync (disp, 0);
  309.  
  310. /*exitbutton,newgame,Yourscoreboard,Myscoreboard*/
  311.  
  312.     newgame = create_button (main_window, 10, 70, button_wide, button_height, fg, bg);
  313.     exitbutton = create_button (main_window, 10, 70 + (button_height + 10) * 1, button_wide, button_height, fg, bg);
  314.  
  315.     Yourscoreboard = create_button (main_window, 10, 70 + (button_height + 10) * 2, button_wide / 2 - 20, button_height * 2, fg, bg);
  316.     YourPlus = create_button (main_window, 10 + button_wide / 2 - 20, 70 + (button_height + 10) * 2, 20, button_height, fg, bg);
  317.     YourMinus = create_button (main_window, 10 + button_wide / 2 - 20, 70 + (button_height + 10) * 2 + button_height, 20, button_height, fg, bg);
  318.  
  319.  
  320.     Myscoreboard = create_button (main_window, 10 + button_wide / 2 + 2, 70 + (button_height + 10) * 2, button_wide / 2 - 20, button_height * 2, fg, bg);
  321.     MyPlus = create_button (main_window, 10 + button_wide - 20, 70 + (button_height + 10) * 2, 20, button_height, fg, bg);
  322.     MyMinus = create_button (main_window, 10 + button_wide - 20, 70 + (button_height + 10) * 2 + button_height, 20, button_height, fg, bg);
  323.  
  324.  
  325.  
  326.  
  327.     WhoseTurnWindow = create_button (main_window, 10, 480, button_wide - 20, button_height * 2, fg, bg);
  328.     TraJectory = create_button (main_window, 10,460,button_wide - 20,button_height, fg, bg);
  329.  
  330.  
  331.  
  332.  
  333.  
  334. /* one last thing before we go,    
  335. ballWidth,ballHeight,ballHotX, ballHotY */
  336.  
  337.     depth = XAllPlanes ();
  338.  
  339.  
  340.  
  341.     BallBitmap = XCreateBitmapFromData (disp, Pane, ball_bits, ball_width, ball_height);
  342.     if (!BallBitmap) {
  343.     printf ("can't read bitmap ..\n");
  344.     exit (0);
  345.     };
  346.     TileBitmap = XCreateBitmapFromData (disp, main_window, tile_bits, tile_width, tile_height);
  347.     if (!TileBitmap) {
  348.     printf ("can't read bitmap ..\n");
  349.     exit (0);
  350.     };
  351.  
  352.  
  353.  
  354. }
  355.  
  356. void LayTheFloor (void) {
  357.     int     i,
  358.             j;
  359.     setbkcolor (get_color (MAIN_BG));
  360.     for (i = 0; i < 900; i += tile_width)
  361.     for (j = 0; j < 600; j += tile_height) {
  362.         XCopyPlane (disp, TileBitmap, main_window, gc, 0, 0, tile_width, tile_height, i, j, 0x01);
  363.     }
  364.     XSync (disp, 0);
  365.     setbkcolor (get_color (PANE_BG));
  366. };
  367.  
  368.  
  369.  
  370. void
  371. showBall (int x, int y) {
  372.     XCopyPlane (disp, BallBitmap, Pane, gc, 0, 0, ball_width, ball_height, x, y, 0x01);
  373.     XSync (disp, 0);
  374. }
  375.  
  376. void
  377. showBallOut (int x, int y, int color) {
  378.     setcolor (color);
  379.     setbkcolor (get_color (MAIN_BG));
  380.     XCopyPlane (disp, BallBitmap, main_window, gc, 0, 0, ball_width, ball_height, x, y, 0x01);
  381.     XSync (disp, 0);
  382.     setbkcolor (get_color (PANE_BG));
  383. }
  384.  
  385.  
  386. void line (int x, int y, int xx, int yy) {
  387.     XDrawLine (disp, Pane, gc, x, y, xx, yy);
  388.     XSync (disp, 0);
  389. };
  390.  
  391. void circle (int x, int y, int rad) {
  392.     XDrawArc (disp, Pane, gc, x - rad, y - rad, rad * 2, rad * 2, 0, 360 * 64);
  393.     XSync (disp, 0);
  394. };
  395.  
  396.  
  397. void circleOut (int x, int y, int rad, int color) {
  398.     setcolor (color);
  399.     setbkcolor (get_color (MAIN_BG));
  400.     XDrawArc (disp, main_window, gc, x - rad, y - rad, rad * 2, rad * 2, 0, 360 * 64);
  401.     XSync (disp, 0);
  402.     setbkcolor (get_color (PANE_BG));
  403. };
  404.  
  405.  
  406.  
  407.  
  408. void Fillcircle (int x, int y, int rad) {
  409.     /* this is for drawing pockets ,,     table 0,0  at 120,30 */
  410.     setcolor (get_color ("black"));
  411.     XFillArc (disp, main_window, gc, x - rad + 120, y - rad + 30, rad * 2, rad * 2, 0, 360 * 64);
  412. };
  413.  
  414.  
  415.  
  416. void rectangle (int x, int y, int xx, int yy) {
  417.     XDrawRectangle (disp, Pane, gc, x, y, xx, yy);
  418.     XSync (disp, 0);
  419. };
  420.  
  421. void Fillrectangle (int x, int y, int xx, int yy) {
  422.     setcolor (get_color (BORDER_COLOR));
  423.     XFillRectangle (disp, main_window, gc, x + 120, y + 30, xx, yy);
  424.     XSync (disp, 0);
  425. };
  426.  
  427.  
  428.  
  429.  
  430.  
  431. void SyncDisplay (void) {
  432.     XSync (disp, 0);
  433. };
  434.  
  435. void ClearTable (void) {
  436.     XClearWindow (disp, Pane);
  437. };
  438.  
  439. void RubberLineOnPane (int type,int x, int y, int xx, int yy, int color) {
  440. if(type==TRAJECTORY)
  441.     XSetLineAttributes (disp, gc, 1, linestyle, capstyle, joinstyle);
  442.     XSetFunction (disp, gc, GXxor);
  443.     setcolor (color);
  444.     XDrawLine (disp, Pane, gc, x, y, xx, yy);
  445.     XSync (disp, 0);
  446.     XSetFunction (disp, gc, GXcopy);
  447.     XSetLineAttributes (disp, gc, linewidth, linestyle, capstyle, joinstyle);
  448. };
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.