home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / flying-6.11 / xpball.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-30  |  5.4 KB  |  234 lines

  1.  
  2. extern Pixmap    pointer_pmap;                            // aus Modul graph.C ...
  3. extern int        pointer_off_x, pointer_off_y;        //    dto.
  4.  
  5. #ifndef _cursor_h
  6. #    include "cursor.h"
  7. #endif
  8.  
  9. int    PBall::instance_count = 0;
  10. int    PBall::pwidth  = 0;
  11. int    PBall::pheight = 0;
  12. Pixmap PBall::pmap    = 0;
  13. GC     PBall::gc_bit  = 0;
  14.  
  15. PBall::PBall( PBallType type, char *disp_name ) :
  16.     PBallTop( type )
  17. {
  18. //
  19. // Remote Display ÷ffnen
  20. //
  21.     rem_dpy = XOpenDisplay( disp_name );
  22.     if (!rem_dpy) {
  23.         fprintf( stderr, "ERROR: can't open display '%s'.\n", disp_name );
  24.         exit(0);
  25.     }
  26.     scr=DefaultScreen(rem_dpy);
  27. //
  28. // Cursor ⁿberdefinieren
  29. //
  30. Cursor    cursor;
  31. Pixmap    pixmap;
  32. XColor    col1;
  33.     pixmap = XCreateBitmapFromData(rem_dpy,
  34.                     RootWindow( rem_dpy, scr ),
  35.                     cursor_bits, cursor_width, cursor_height );
  36.     XParseColor(rem_dpy,DefaultColormap(rem_dpy,scr), "black", &col1 );
  37.     cursor = XCreatePixmapCursor( rem_dpy, pixmap, pixmap, &col1, &col1,
  38.                             cursor_x_hot, cursor_y_hot );
  39.     XGrabPointer( rem_dpy, RootWindow( rem_dpy, scr ),
  40.             False, (unsigned)(ButtonPressMask|ButtonReleaseMask|PointerMotionMask),
  41.             GrabModeAsync, GrabModeAsync, None, cursor, CurrentTime );
  42.  
  43. //
  44. // Transformation festlegen
  45. //
  46.     w2n_x = DisplayWidth( rem_dpy,scr)/world_x;
  47.     w2n_y = DisplayHeight(rem_dpy,scr)/world_y;
  48. //
  49. // Pointer auf Bildschirm initialisieren.
  50. //
  51.     x_old = -20;    // ausserhalb des Bildschirms (wegen erstem CopyArea)
  52.     y_old = -20;
  53.  
  54.     XFreePixmap( rem_dpy, pixmap );
  55.     XFreeCursor( rem_dpy, cursor );
  56.  
  57.  
  58.     if (!instance_count++) {
  59.         pwidth  = 30;
  60.         pheight = 30;
  61.         pmap    = XCreatePixmap(dpy,win,pwidth,pheight,1);
  62.         gc_bit  = XCreateGC(dpy,pmap,0,0);
  63.         XSetState(dpy,gc_bit,1,0,GXxor,1);
  64.     }
  65. }
  66.  
  67. PBall::~PBall() {
  68.     if (!--instance_count) {
  69.         XFreePixmap(dpy,pmap);
  70.         XFreeGC(dpy,gc_bit);
  71.     }
  72. //
  73. // Pointer vom Bildschirm l÷schen
  74. //
  75.     XCopyArea( dpy, pointer_pmap, win, gc_cursor, 0, 0, 16, 16,
  76.                     x_old-pointer_off_x, y_old-pointer_off_y );
  77.  
  78.     XSync( rem_dpy, 0 );
  79.     XCloseDisplay( rem_dpy );
  80. }
  81.  
  82. void PBall::Update() {
  83. XEvent    event;
  84.  
  85.     while (XEventsQueued( rem_dpy, QueuedAfterFlush )) {
  86.         XNextEvent( rem_dpy, &event );
  87.         switch( event.xany.type ) {
  88.         case MotionNotify:
  89.             while( XCheckMaskEvent( rem_dpy, PointerMotionMask, &event ));
  90.             PointerMoveTo(Vec2(Real(event.xmotion.x)/w2n_x,
  91.                                                 Real(event.xmotion.y)/w2n_y));
  92.             break;
  93.         case ButtonPress:
  94.             Press( event.xbutton.button );
  95.             break; // case ButtonPress
  96.  
  97.         case ButtonRelease:
  98.             Release( event.xbutton.button );
  99.         } // switch(event.type)
  100.     }
  101.  
  102.     PBallTop::Update();
  103. }
  104.  
  105.  
  106. void PBall::Warp( const Vec2 &dest ) {
  107. XEvent    event;
  108.  
  109.     XWarpPointer( rem_dpy, None, RootWindow( rem_dpy, scr ),
  110.             0,0,0,0 ,
  111.             (int)(dest.X()*w2n_x),
  112.             (int)(dest.Y()*w2n_y) );
  113.  
  114.     if (mode&UsingTool)    MoveAimingTool();
  115.  
  116.     XSync( rem_dpy, 0 );
  117.     while( XCheckMaskEvent( rem_dpy, PointerMotionMask, &event ));
  118. }
  119.  
  120.  
  121. void PBall::RedrawPointer() {
  122.     XCopyArea( dpy, pointer_pmap, win, gc_cursor, 0, 0, 16, 16,
  123.                     x_old-pointer_off_x, y_old-pointer_off_y );
  124. }
  125.  
  126. void PBall::SetPointer( int x, int y ) {
  127.     XCopyArea( dpy, pointer_pmap, win, gc_cursor, 0, 0, 16, 16,
  128.                     x_old-pointer_off_x, y_old-pointer_off_y );
  129.     x_old = x;
  130.     y_old = y;
  131. //    RedrawPointer:
  132.     XCopyArea( dpy, pointer_pmap, win, gc_cursor, 0, 0, 16, 16,
  133.                     x_old-pointer_off_x, y_old-pointer_off_y );
  134. }
  135.  
  136.  
  137. // ------------------------------------------------------------
  138.  
  139. #ifndef XPix
  140. #    define XPix(x)   ((int)((x)*w2n))
  141. #    define YPix(y)   ((int)((y)*w2n))
  142. #    define Pix(r)    ((int)((r)*w2n))
  143. #endif
  144.  
  145. void PBall::DrawQueue() {
  146.     if (valid_queue_position) {
  147.         XPoint    p[3];
  148.  
  149.         p[0].x = XPix(q_end_s.X());
  150.         p[0].y = YPix(q_end_s.Y());
  151.         p[1].x = XPix(q_s1_s.X());
  152.         p[1].y = YPix(q_s1_s.Y());
  153.         p[2].x = XPix(q_s2_s.X());
  154.         p[2].y = YPix(q_s2_s.Y());
  155.         XFillPolygon(dpy,win,gc_current,p,3,0,0);
  156.     }
  157. }
  158.  
  159. void PBall::StartQueue(const Vec2 &end, const Vec2 &s1, const Vec2 &s2) {
  160.     q_end_s = end;
  161.     q_s1_s  = s1;
  162.     q_s2_s  = s2;
  163.     valid_queue_position = 1;
  164.     DrawQueue();
  165. }
  166.  
  167. void PBall::MoveQueue(const Vec2 &end, const Vec2 &s1, const Vec2 &s2) {
  168. XPoint    p[6];
  169. int        i;
  170.  
  171.     p[0].x = XPix(q_end_s.X());
  172.     p[0].y = YPix(q_end_s.Y());
  173.     p[1].x = XPix(q_s1_s.X());
  174.     p[1].y = YPix(q_s1_s.Y());
  175.     p[2].x = XPix(q_s2_s.X());
  176.     p[2].y = YPix(q_s2_s.Y());
  177.  
  178.     q_end_s = end;
  179.     q_s1_s  = s1;
  180.     q_s2_s  = s2;
  181.     p[3].x = XPix(q_end_s.X());
  182.     p[3].y = YPix(q_end_s.Y());
  183.     p[4].x = XPix(q_s1_s.X());
  184.     p[4].y = YPix(q_s1_s.Y());
  185.     p[5].x = XPix(q_s2_s.X());
  186.     p[5].y = YPix(q_s2_s.Y());
  187.  
  188.     valid_queue_position = 1;
  189.  
  190. int    min_x = p[0].x;
  191. int    max_x = p[0].x;
  192. int    min_y = p[0].y;
  193. int    max_y = p[0].y;
  194.  
  195. // Minimum suchen
  196.     for (i=1;i<sizeof(p)/sizeof(XPoint);i++) {
  197.         if (p[i].x < min_x)        min_x = p[i].x;
  198.         if (p[i].x > max_x)        max_x = p[i].x;
  199.         if (p[i].y < min_y)        min_y = p[i].y;
  200.         if (p[i].y > max_y)        max_y = p[i].y;
  201.     }
  202.  
  203. // Punkte nun relativ zur Bitmap
  204.     for (i=0;i<sizeof(p)/sizeof(XPoint);i++) {
  205.         p[i].x -= min_x;
  206.         p[i].y -= min_y;
  207.     }
  208.  
  209. int width  = max_x - min_x;
  210. int height = max_y - min_y;
  211.  
  212. // ggf. vergroesserte Pixmap anlegen
  213.     if (width>pwidth||height>pheight) {
  214.         XFreePixmap(dpy,pmap);
  215.         if (width>pwidth)            pwidth =width +10;
  216.         if (height>pheight)        pheight=height+10;
  217.         pmap = XCreatePixmap(dpy,win,pwidth,pheight,1);
  218.     }
  219.  
  220. // Pixmap aufbauen und ins Fenster verknuepfen
  221.     XSetFunction(dpy,gc_bit,GXclear);
  222.     XFillRectangle(dpy,pmap,gc_bit,0,0,width,height);
  223.     XSetFunction(dpy,gc_bit,GXxor);
  224.     XFillPolygon(dpy,pmap,gc_bit,&p[0],3,0,0);
  225.     XFillPolygon(dpy,pmap,gc_bit,&p[3],3,0,0);
  226.     XCopyPlane(dpy,pmap,win,gc_current,0,0,width,height,min_x,min_y,1);
  227. }
  228.  
  229. void PBall::EndQueue() {
  230.     DrawQueue();
  231.     valid_queue_position = 0;
  232. }
  233.  
  234.