Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / Examples / puzzle.c
blob7e8b79973ec76ce0a39859d7d31b87e131fc2a31
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <WINGs/WINGs.h>
5 #include <stdint.h>
7 #define MAX_SIZE 10*10
9 WMWindow *win;
10 WMButton *Button[MAX_SIZE];
11 char Map[MAX_SIZE];
12 int Size = 4;
13 int MoveCount;
15 #define MAP(x,y) Map[(x)+(y)*Size]
17 int WinSize = 120;
19 Bool CheckWin(void)
21 int i;
23 for (i = 0; i < Size * Size - 1; i++) {
24 if (Map[i] != i)
25 return False;
28 return True;
31 void MoveButton(int button, int x, int y)
33 WMMoveWidget(Button[button], x * (WinSize / Size), y * (WinSize / Size));
36 Bool SlideButton(int button)
38 int x = 0, y = 0, done = 0;
40 /* locate the button */
41 for (y = 0; y < Size; y++) {
42 for (x = 0; x < Size; x++) {
43 if (MAP(x, y) == button) {
44 done = 1;
45 break;
48 if (done)
49 break;
52 if (x > 0 && MAP(x - 1, y) < 0) {
53 MAP(x, y) = -1;
54 MoveButton(button, x - 1, y);
55 MAP(x - 1, y) = button;
56 } else if (x < Size - 1 && MAP(x + 1, y) < 0) {
57 MAP(x, y) = -1;
58 MoveButton(button, x + 1, y);
59 MAP(x + 1, y) = button;
60 } else if (y > 0 && MAP(x, y - 1) < 0) {
61 MAP(x, y) = -1;
62 MoveButton(button, x, y - 1);
63 MAP(x, y - 1) = button;
64 } else if (y < Size - 1 && MAP(x, y + 1) < 0) {
65 MAP(x, y) = -1;
66 MoveButton(button, x, y + 1);
67 MAP(x, y + 1) = button;
68 } else {
69 return False;
71 return True;
74 #define SWAP(a,b) {int tmp; tmp=a; a=b; b=tmp;}
76 void ResetGame(void)
78 int i, x, y, ox, oy;
80 MoveCount = 0;
82 for (i = 0; i < Size * Size - 1; i++) {
83 Map[i] = i;
85 Map[i] = -1;
86 ox = x = Size - 1;
87 oy = y = Size - 1;
88 for (i = 0; i < 1000; i++) {
89 int ok;
90 ok = 1;
91 switch (rand() % 4) {
92 case 0:
93 if (x > 0)
94 x--;
95 else
96 ok = 0;
97 break;
98 case 2:
99 if (x < Size - 1)
100 x++;
101 else
102 ok = 0;
103 break;
104 case 1:
105 if (y > 0)
106 y--;
107 else
108 ok = 0;
109 break;
110 case 3:
111 if (y < Size - 1)
112 y++;
113 else
114 ok = 0;
115 break;
117 if (ok) {
118 MoveButton(MAP(x, y), ox, oy);
120 SWAP(MAP(ox, oy), MAP(x, y));
122 while (XPending(WMScreenDisplay(WMWidgetScreen(win)))) {
123 XEvent ev;
124 WMNextEvent(WMScreenDisplay(WMWidgetScreen(win)), &ev);
125 WMHandleEvent(&ev);
127 ox = x;
128 oy = y;
133 void buttonClick(WMWidget * w, void *ptr)
135 char buffer[300];
137 if (SlideButton((int)(uintptr_t) ptr)) {
138 MoveCount++;
140 if (CheckWin()) {
141 sprintf(buffer, "You finished the game in %i moves.", MoveCount);
143 if (WMRunAlertPanel(WMWidgetScreen(w), win, "You Won!", buffer,
144 "Wee!", "Gah! Lemme retry!", NULL) == WAPRDefault) {
145 exit(0);
148 ResetGame();
153 static void resizeObserver(void *self, WMNotification * notif)
155 WMSize size = WMGetViewSize(WMWidgetView(win));
156 int x, y;
158 WinSize = size.width;
159 for (y = 0; y < Size; y++) {
160 for (x = 0; x < Size; x++) {
161 if (MAP(x, y) >= 0) {
162 WMResizeWidget(Button[(int)MAP(x, y)], WinSize / Size, WinSize / Size);
163 WMMoveWidget(Button[(int)MAP(x, y)], x * (WinSize / Size), y * (WinSize / Size));
170 int main(int argc, char **argv)
172 Display *dpy;
173 WMScreen *scr;
174 int x, y, i;
176 WMInitializeApplication("Puzzle", &argc, argv);
178 dpy = XOpenDisplay("");
179 if (!dpy) {
180 printf("could not open display\n");
181 exit(1);
184 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
186 win = WMCreateWindow(scr, "puzzle");
187 WMResizeWidget(win, WinSize, WinSize);
188 WMSetWindowTitle(win, "zuPzel");
189 WMSetWindowMinSize(win, 80, 80);
190 WMSetWindowAspectRatio(win, 2, 2, 2, 2);
191 WMSetWindowResizeIncrements(win, Size, Size);
192 WMSetViewNotifySizeChanges(WMWidgetView(win), True);
193 WMAddNotificationObserver(resizeObserver, NULL, WMViewSizeDidChangeNotification, WMWidgetView(win));
195 for (i = y = 0; y < Size && i < Size * Size - 1; y++) {
196 for (x = 0; x < Size && i < Size * Size - 1; x++) {
197 char buf[32];
198 WMColor *color;
199 RColor col;
200 RHSVColor hsv;
202 hsv.hue = i * 360 / (Size * Size - 1);
203 hsv.saturation = 120;
204 hsv.value = 200;
206 RHSVtoRGB(&hsv, &col);
208 color = WMCreateRGBColor(scr, col.red << 8, col.green << 8, col.blue << 8, False);
210 MAP(x, y) = i;
211 Button[i] = WMCreateButton(win, WBTMomentaryLight);
212 WMSetWidgetBackgroundColor(Button[i], color);
213 WMReleaseColor(color);
214 WMSetButtonAction(Button[i], buttonClick, (void *)(uintptr_t) i);
215 WMResizeWidget(Button[i], WinSize / Size, WinSize / Size);
216 WMMoveWidget(Button[i], x * (WinSize / Size), y * (WinSize / Size));
217 sprintf(buf, "%i", i + 1);
218 WMSetButtonText(Button[i], buf);
219 WMSetButtonTextAlignment(Button[i], WACenter);
220 i++;
224 WMMapSubwidgets(win);
225 WMMapWidget(win);
226 WMRealizeWidget(win);
228 ResetGame();
230 WMScreenMainLoop(scr);
232 return 0;