/* lunar - Simple X11 Lunar Lander Copyright (C) 2005 Ian Cowburn (ianc@noddybox.demon.co.uk) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ------------------------------------------------------------------------- */ static char rcs_id[]="$Id$"; #include #include #include #include #include #include #include #include #include "Xbit.h" #define WINX 100 #define WINY 100 #define WINW 640 #define WINH 640 #define RND(x) (rand()%(x)) XSizeHints size_hints; ulong black,white; Display *disp; Window window; Colormap cm; XFontStruct *font; XFuncControl Key(Window w, XPressRelease mode, XEvent *e); XFuncControl Mouse(Window w, XPressRelease mode, int b, int x, int y); XFuncControl Motion(Window w, int x, int y); XFuncControl Process(void); void ReadFile(void); void WriteFile(void); XWindowKeyCallback key[2]={{0,Key},{0,NULL}}; XWindowButtonCallback mouse[2]={{0,Mouse},{0,NULL}}; XWindowMoveCallBack motion[2]={{0,Motion},{0,NULL}}; #define NOCOL 7 const char *colnames[NOCOL]= {"black","white","red","yellow","blue","green","cyan"}; ulong pix[NOCOL]; #define BLACK pix[0] #define WHITE pix[1] #define RED pix[2] #define YELLOW pix[3] #define BLUE pix[4] #define GREEN pix[5] #define CYAN pix[6] typedef struct { int x,y; } Pt; #define MNT 1 #define MINE 2 #define ASTCW 3 #define ASTACW 4 #define PAD 5 char *ty_type[]={"BUG?!","MNT","MINE","ASTCW","ASTACW","PAD"}; char ty_code[]={'?','M','O','X','Y','P'}; int CONST =3; #define STOMX(x) (((x)-WINW/2)*CONST+gx) #define STOMY(y) (((y)-WINH/2)*CONST+gy) #define MTOSX(x) (((x-gx)/CONST)+(WINW/2)) #define MTOSY(y) (((y-gy)/CONST)+(WINH/2)) #define MAXLINE 32 typedef struct { int ty; Pt o; int no; Pt p[MAXLINE]; } Obj; #define MAXOBJ 64 Obj obj[MAXOBJ]; int no; int cur; char *fname; char *title="LEVEL TITLE"; int mx,my; int gx=0,gy=0; int main(int argc, char *argv[]) { unsigned long evmask; srand(getpid()); size_hints.flags=PPosition|PSize|PMinSize|PMaxSize; size_hints.x=WINX; size_hints.y=WINY; size_hints.width=size_hints.min_width=WINW; size_hints.height=size_hints.min_height=WINH; size_hints.max_width=WINW; size_hints.max_height=WINH; evmask=ButtonPressMask|ButtonReleaseMask| KeyPressMask|KeyReleaseMask|PointerMotionMask; motion[0].w=mouse[0].w=key[0].w=window= OpenWin(argc,argv,argv[0], WINX,WINY,WINW,WINH, WINW,WINH, evmask, &size_hints,&black,&white); if (argc>1) { fname=argv[1]; ReadFile(); } else { fprintf(stderr,"Must supply a level file\n"); exit(1); } if (argc>2) CONST=atoi(argv[2]); DisableDoubleBuffer(); disp=GetDisplay(); AllocColors(NOCOL,pix,colnames); XDoWindows(mouse,motion,key,Process); } XFuncControl Key(Window w,XPressRelease m, XEvent *e) { KeySym k; if (m==XPRESS) return XFUNCCONT; switch(k=XLookupKeysym((XKeyEvent*)e,ShiftMapIndex)) { case XK_G: /* Set centre */ case XK_g: gx=STOMX(e->xkey.x); gy=STOMY(e->xkey.y); break; case XK_H: /* Clear centre */ case XK_h: gx=0; gy=0; break; case XK_C: /* Create poly */ case XK_c: if (noxkey.x); obj[no].o.y=STOMY(e->xkey.y); cur=no++; } break; case XK_O: /* Set origin */ case XK_o: if (no) { obj[cur].o.x=STOMX(e->xkey.x); obj[cur].o.y=STOMY(e->xkey.y); } break; case XK_D: /* Delete poly */ case XK_d: if (no) if (cur==(--no)) cur--; break; case XK_Left: /* Prev poly */ if (no) if (cur) cur--; break; case XK_Right: /* Prev poly */ if (no) if (cur<(no-1)) cur++; break; case XK_Up: /* Scale up */ if (CONST>1) CONST--; break; case XK_Down: /* Scale down */ CONST++; break; case XK_S: /* Save */ case XK_s: WriteFile(); break; case XK_Q: /* Quit */ case XK_q: return (XFUNCSTOP); break; case XK_1: case XK_2: case XK_3: case XK_4: case XK_5: if (no) obj[cur].ty=k-XK_0; break; default: break; } return XFUNCCONT; } XFuncControl Mouse(Window w, XPressRelease m, int b, int x, int y) { int mapx,mapy; if ((m==XPRESS)||(cur==-1)) return XFUNCCONT; switch(b) { case 1: /* Add point */ if (obj[cur].no