1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-19 12:28:22 +01:00
Files
wmaker/WINGs/Examples/puzzle.c
Carlos R. Mafra 06f59b9928 wmaker: Fix compiler warnings about pointer <--> integer conversion
There may be issues with running applications in 64-bit mode when
they were written with tacit assumptions about 32-bit platforms.
For example,

    * Assuming that a pointer can be cast back and forth to an integer

The reason is that the size of the integer and pointer may be different.
See the description of "[PATCH] Warn when casting a pointer (constant)
to an integer of different size." in the gcc mailing list

http://gcc.gnu.org/ml/gcc-patches/2005-12/msg01881.html

where it was also suggested the use of casts to uintptr_t. This is
what this patch does.

As a result the following warnings are fixed, leaving us with an
almost warning-free compilation in 64-bit platforms:

defaults.c:1446: warning: cast to pointer from integer of different size
defaults.c:1457: warning: cast to pointer from integer of different size
defaults.c:1471: warning: cast to pointer from integer of different size
defaults.c:1486: warning: cast to pointer from integer of different size
icon.c:67: warning: cast from pointer to integer of different size
menu.c:112: warning: cast from pointer to integer of different size
switchmenu.c:452: warning: cast from pointer to integer of different size
window.c:140: warning: cast from pointer to integer of different size
window.c:2217: warning: cast to pointer from integer of different size
workspace.c:135: warning: cast to pointer from integer of different size
workspace.c:214: warning: cast to pointer from integer of different size
workspace.c:634: warning: cast to pointer from integer of different size
workspace.c:1330: warning: cast to pointer from integer of different size
workspace.c:1514: warning: cast to pointer from integer of different size
wfilepanel.c:135: warning: cast from pointer to integer of different size
wfilepanel.c:171: warning: cast from pointer to integer of different size
wfontpanel.c:499: warning: cast to pointer from integer of different size
wfontpanel.c:500: warning: cast to pointer from integer of different size
wfontpanel.c:505: warning: cast to pointer from integer of different size
wfontpanel.c:506: warning: cast to pointer from integer of different size
wfontpanel.c:776: warning: cast from pointer to integer of different size
wfontpanel.c:777: warning: cast from pointer to integer of different size
wfontpanel.c:877: warning: cast from pointer to integer of different size
wfontpanel.c:878: warning: cast from pointer to integer of different size
wpanel.c:363: warning: cast from pointer to integer of different size
fontl.c:42: warning: cast from pointer to integer of different size
fontl.c:42: warning: cast from pointer to integer of different size
fontl.c:42: warning: cast from pointer to integer of different size
fontl.c:90: warning: cast to pointer from integer of different size
puzzle.c:138: warning: cast from pointer to integer of different size
puzzle.c:225: warning: cast to pointer from integer of different size
wtableview.c:1031: warning: cast to pointer from integer of different size
wtableview.c:1067: warning: cast to pointer from integer of different size
wtableview.c:1069: warning: cast to pointer from integer of different size
wtableview.c:1074: warning: cast to pointer from integer of different size
wtabledelegates.c:234: warning: cast from pointer to integer of different size
wtabledelegates.c:250: warning: cast from pointer to integer of different size
wtabledelegates.c:265: warning: cast from pointer to integer of different size
wtabledelegates.c:287: warning: cast to pointer from integer of different size
wtabledelegates.c:351: warning: cast from pointer to integer of different size
wtabledelegates.c:372: warning: cast from pointer to integer of different size
wtabledelegates.c:393: warning: cast from pointer to integer of different size
wtabledelegates.c:410: warning: cast to pointer from integer of different size
test.c:44: warning: cast from pointer to integer of different size
test.c:47: warning: cast to pointer from integer of different size
test.c:55: warning: cast from pointer to integer of different size
test.c:58: warning: cast from pointer to integer of different size
2008-11-06 09:07:50 +01:00

246 lines
5.3 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <WINGs/WINGs.h>
#include <stdint.h>
#define MAX_SIZE 10*10
WMWindow *win;
WMButton *Button[MAX_SIZE];
char Map[MAX_SIZE];
int Size = 4;
int MoveCount;
#define MAP(x,y) Map[(x)+(y)*Size]
int WinSize = 120;
Bool
CheckWin(void)
{
int i;
for (i = 0; i < Size*Size-1; i++) {
if (Map[i] != i)
return False;
}
return True;
}
void
MoveButton(int button, int x, int y)
{
WMMoveWidget(Button[button], x*(WinSize/Size), y*(WinSize/Size));
}
Bool
SlideButton(int button)
{
int x=0, y=0, done = 0;
/* locate the button */
for (y = 0; y < Size; y++) {
for (x = 0; x < Size; x++) {
if (MAP(x,y) == button) {
done = 1;
break;
}
}
if (done)
break;
}
if (x > 0 && MAP(x-1, y) < 0) {
MAP(x,y) = -1;
MoveButton(button, x-1, y);
MAP(x-1,y) = button;
} else if (x < Size-1 && MAP(x+1, y) < 0) {
MAP(x,y) = -1;
MoveButton(button, x+1, y);
MAP(x+1,y) = button;
} else if (y > 0 && MAP(x, y-1) < 0) {
MAP(x,y) = -1;
MoveButton(button, x, y-1);
MAP(x,y-1) = button;
} else if (y < Size-1 && MAP(x, y+1) < 0) {
MAP(x,y) = -1;
MoveButton(button, x, y+1);
MAP(x,y+1) = button;
} else {
return False;
}
return True;
}
#define SWAP(a,b) {int tmp; tmp=a; a=b; b=tmp;}
void ResetGame(void)
{
int i, x, y, ox, oy;
MoveCount = 0;
for (i = 0; i < Size*Size-1; i++) {
Map[i] = i;
}
Map[i] = -1;
ox = x = Size-1;
oy = y = Size-1;
for (i = 0; i < 1000; i++) {
int ok;
ok = 1;
switch (rand()%4) {
case 0:
if (x > 0) x--; else ok = 0;
break;
case 2:
if (x < Size-1) x++; else ok = 0;
break;
case 1:
if (y > 0) y--; else ok = 0;
break;
case 3:
if (y < Size-1) y++; else ok = 0;
break;
}
if (ok) {
MoveButton(MAP(x,y), ox, oy);
SWAP(MAP(ox, oy), MAP(x, y));
while (XPending(WMScreenDisplay(WMWidgetScreen(win)))) {
XEvent ev;
WMNextEvent(WMScreenDisplay(WMWidgetScreen(win)), &ev);
WMHandleEvent(&ev);
}
ox = x;
oy = y;
}
}
}
void buttonClick(WMWidget *w, void *ptr)
{
char buffer[300];
if (SlideButton((int)(uintptr_t)ptr)) {
MoveCount++;
if (CheckWin()) {
sprintf(buffer, "You finished the game in %i moves.", MoveCount);
if (WMRunAlertPanel(WMWidgetScreen(w), win, "You Won!", buffer,
"Wee!", "Gah! Lemme retry!", NULL) == WAPRDefault) {
exit(0);
}
ResetGame();
}
}
}
static void resizeObserver(void *self, WMNotification *notif)
{
WMSize size = WMGetViewSize(WMWidgetView(win));
int x, y;
WinSize = size.width;
for (y = 0; y < Size; y++) {
for (x = 0; x < Size; x++) {
if (MAP(x,y) >= 0) {
WMResizeWidget(Button[(int)MAP(x,y)],
WinSize/Size, WinSize/Size);
WMMoveWidget(Button[(int)MAP(x,y)],
x*(WinSize/Size), y*(WinSize/Size));
}
}
}
}
int main(int argc, char **argv)
{
Display *dpy;
WMScreen *scr;
int x, y, i;
WMInitializeApplication("Puzzle", &argc, argv);
dpy = XOpenDisplay("");
if (!dpy) {
printf("could not open display\n");
exit(1);
}
scr = WMCreateScreen(dpy, DefaultScreen(dpy));
win = WMCreateWindow(scr, "puzzle");
WMResizeWidget(win, WinSize, WinSize);
WMSetWindowTitle(win, "zuPzel");
WMSetWindowMinSize(win, 80, 80);
WMSetWindowAspectRatio(win, 2, 2, 2, 2);
WMSetWindowResizeIncrements(win, Size, Size);
WMSetViewNotifySizeChanges(WMWidgetView(win), True);
WMAddNotificationObserver(resizeObserver, NULL,
WMViewSizeDidChangeNotification,
WMWidgetView(win));
for (i = y = 0; y < Size && i < Size*Size-1; y++) {
for (x = 0; x < Size && i < Size*Size-1; x++) {
char buf[32];
WMColor *color;
RColor col;
RHSVColor hsv;
hsv.hue = i*360/(Size*Size-1);
hsv.saturation = 120;
hsv.value = 200;
RHSVtoRGB(&hsv, &col);
color = WMCreateRGBColor(scr, col.red<<8, col.green<<8,
col.blue<<8, False);
MAP(x,y) = i;
Button[i] = WMCreateButton(win, WBTMomentaryLight);
WMSetWidgetBackgroundColor(Button[i], color);
WMReleaseColor(color);
WMSetButtonAction(Button[i], buttonClick, (void*)(uintptr_t)i);
WMResizeWidget(Button[i], WinSize/Size, WinSize/Size);
WMMoveWidget(Button[i], x*(WinSize/Size), y*(WinSize/Size));
sprintf(buf, "%i", i+1);
WMSetButtonText(Button[i], buf);
WMSetButtonTextAlignment(Button[i], WACenter);
i++;
}
}
WMMapSubwidgets(win);
WMMapWidget(win);
WMRealizeWidget(win);
ResetGame();
WMScreenMainLoop(scr);
return 0;
}