1 #include <stdio.h>
2 #include "components.h"
3 
4 Mahjong Mp[36];
5 
Mahjong(void)6 Mahjong::Mahjong(void)
7 {
8     data = 0;
9     datag = 0;
10     rdata = 0;
11     rdatag = 0;
12 }
13 
14 // filename:  regular piece xpm data
15 // num:       ID for object instance
16 // closeness: Color closeness for xpm library
17 void
ReadFile(Widget w,char * filename,int num,int closeness)18 Mahjong::ReadFile(Widget w, char *filename, int num, int closeness)
19 {
20     XpmAttributes attributes;
21     int result;
22     Pixmap mask;
23     Colormap colormap = XDefaultColormapOfScreen(XtScreen(w));
24     GC gc;
25     XGCValues gcv;
26 
27     if (rdata != 0 && data != rdata) {
28         XFreePixmap(XtDisplay(w), rdata);
29         XFreePixmap(XtDisplay(w), rdatag);
30     }
31     if (data != 0) {
32         XFreePixmap(XtDisplay(w), data);
33         XFreePixmap(XtDisplay(w), datag);
34     }
35     attributes.valuemask = XpmColormap | XpmSize | XpmCloseness;
36     attributes.colormap  = colormap;
37     attributes.exactColors = False;
38     attributes.closeness = closeness;
39     id = num;
40     result = XpmReadFileToPixmap(XtDisplay(w), XtWindow(w), filename,
41                                   &data, &mask, &attributes);
42     width  = original_width  = attributes.width;
43     height = original_height = attributes.height;
44     if (result != XpmSuccess && result != XpmColorError) {
45         fprintf(stderr, "XpmReadFileToPixmap failed ");
46         switch(result) {
47         case XpmOpenFailed:
48             fprintf(stderr, "(Cannot open xpm files)\n");
49             break;
50         case XpmFileInvalid:
51             fprintf(stderr, "(xpm file may be broken)\n");
52             break;
53         case XpmNoMemory:
54             fprintf(stderr, "(memory exhausted)\n");
55             break;
56         case XpmColorFailed:
57             fprintf(stderr, "(color allocation failed [closeness:%d])\n",
58                     closeness);
59             break;
60         default:
61             fprintf(stderr, "(reason unknown)\n");
62             break;
63         }
64         fprintf(stderr, " %s\n", filename);
65         exit(1);
66     } else {
67         attributes.valuemask = XpmColormap;
68         gcv.foreground = WhitePixelOfScreen(XtScreen(w));
69         gc = XCreateGC(XtDisplay(w), XtWindow(w), GCForeground, &gcv);
70         datag = MakeHalfBrightPixmap(data, gc);
71         result = XpmSuccess;
72         XFreeGC(XtDisplay(w), gc);
73     }
74     rdata  = data;
75     rdatag = datag;
76     resized = 0;
77 }
78 
79 void
Resize(Widget w,GC gc,unsigned int new_width,unsigned int new_height)80 Mahjong::Resize(Widget w, GC gc, unsigned int new_width, unsigned int new_height)
81 {
82 #if DEBUG > 2
83     fprintf(stderr, "piece[%2.2d] resize (%d,%d)->(%d,%d)\n",
84             id, width, height, new_width, new_height);
85 #endif
86     if (new_width == width && new_height == height) {
87         return;
88     }
89 
90     if (resized) {
91         XFreePixmap(XtDisplay(w), rdata);
92         XFreePixmap(XtDisplay(w), rdatag);
93     }
94 
95     if (new_width == original_width && new_height == original_height) {
96         rdata  = data;
97         rdatag = datag;
98         resized = 0;
99     }
100     else {
101         rdata  = ResizePixmap(data,  gc, new_width, new_height);
102         rdatag = MakeHalfBrightPixmap(rdata, gc);
103         resized = 1;
104     }
105     width  = new_width;
106     height = new_height;
107 }
108 
109 void
GetSize(unsigned int & w,unsigned int & h)110 Mahjong::GetSize(unsigned int &w, unsigned int &h)
111 {
112     w = width;
113     h = height;
114 }
115 
116 void
Draw(Widget w,GC gc,int x,int y,int d)117 Mahjong::Draw(Widget w, GC gc, int x, int y, int d)
118 {
119     switch(d) {
120     case 1:
121         XCopyArea(XtDisplay(w), rdata,  XtWindow(w), gc, 0, 0, width, height, x, y);
122         break;
123     case 2:
124         XCopyArea(XtDisplay(w), rdatag, XtWindow(w), gc, 0, 0, width, height, x, y);
125         break;
126     }
127 }
128