/********************************************************************************** * amiLights.c * * Copyright © 1995 Douglas M. Dyer * * This source code is made available for everyone's mischief :) * It demonstrates several ClassAct and SAS GUI features: * * +using the window class to support iconification * +attaching and using a menu to the window class * +on-line amigaguide help (with an assist from the window class) * +layout tricks (the grid of buttons is not allocated on the stack, * you may modify NUM_ROW and NUM_COL to change the sizes) * +the button gadget VarArgs feature is used * +SAS/C WA_Arg support (arguments are transparently from the tooltypes or * CLI parameters) * * Obviously, I haven't had much time to document as I have figuring out the * little nuances and cool features of ClassAct. Ill try to improve this * in the next update. * * Requirements to compile: * SAS/C 6.50 or better * OS2.04 or better includes and libs * ClassAct development kit * * Modifications: * ***********************************************************************************/ #define __USE_SYSBASE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* protos */ #include #include #include #include #include #include #include #include #include #include #include #include #include /* gadgets */ #include #include #include #include #include #include #include #include #include #include /* classes */ #include #include /* clibs */ #include #include #include #include #include static const char *VersionString = "\0$VER:" ALVERSION " " __AMIGADATE__; /* amigaguide stuff */ #define HELP_KEY 0x5f AMIGAGUIDECONTEXT agc = NULL; struct NewAmigaGuide newAG = {NULL}; char *guidePath = "PROGDIR:amiLights.guide"; VOID wait_for_close(void); #define MAXPOINTS 25 #define NUM_ROW 5 #define NUM_COL 5 #define GRID(p,i,j) {i=(p)/NUM_ROW; j=(p)%NUM_COL;} typedef enum { G_LAYOUT = ((NUM_ROW+1)*NUM_COL) + 1, G_POINTS, G_SCORE, G_SMALL, G_BOARD, G_NEW, G_RESTART, G_MAX } GadgetIDs; static struct Gadget *GList[G_MAX]; static struct Gadget *GButtons[NUM_ROW][NUM_COL]; static struct Gadget *layoutRows[NUM_ROW]; static char grid[NUM_ROW][NUM_COL]; static char orig[MAXPOINTS]; static struct Window *win = NULL; static struct Screen *screen = NULL; static struct Menu *menubar = NULL; static struct MsgPort *app_port; static APTR vi = NULL; struct NewMenu amiMenu[] = { {NM_TITLE, "File", 0,0,0,0,}, {NM_ITEM, "About...", "A",0,0,0,}, {NM_ITEM, NM_BARLABEL, 0,0,0,0,}, {NM_ITEM, "Quit...", "Q",0,0,0,}, {NM_END, NULL, 0,0,0,0,}, }; /* menu defines */ #define FILETITLE 0 #define FILEABOUT 0 #define FILEQUIT 2 Object *appWindow, *layout, *playGrid; ULONG lightcolor = 7; ULONG darkcolor = 0; ULONG difficulty = 5; ULONG points=0, score[2]={0,0}; BOOL wonBoard = FALSE; struct Library *AmigaGuideBase = NULL; struct ClassLibrary *WindowBase = NULL; struct ClassLibrary *ListBrowserBase = NULL; struct ClassLibrary *LabelBase = NULL; struct ClassLibrary *LayoutBase = NULL; struct ClassLibrary *ButtonBase = NULL; struct ClassLibrary *RadioButtonBase = NULL; struct ClassLibrary *CheckBoxBase = NULL; struct ClassLibrary *ChooserBase = NULL; struct ClassLibrary *ScrollerBase = NULL; struct ClassLibrary *IntegerBase = NULL; struct ClassLibrary *StringBase = NULL; struct ClassLibrary *ClickTabBase = NULL; void main(int, char **); void parseCmd(int, char **); LONG easy_req(struct Window *, char *, char *, char *, ...); BOOL libopen(void); void libclose(void); LONG AmiBaseHelp(void); void AmiHelpMsg(void); void startGame(BOOL); void restartGame(void); void selectLight(int, BOOL); BOOL checkWin(void); void applyLights(void); void disableGrid(BOOL); void scoreBoard(void); void aboutLights(void); /**************************************************** *****************************************************/ void main(int argc, char **argv) { UWORD i,j; /* grab the screen! */ if ( (screen = LockPubScreen(NULL)) == NULL) { easy_req(NULL,"Could not open destination screen", "Quit", ""); exit(1); } /* lets take a look at our arguments */ if (argc==0) { argc = _WBArgc; argv = _WBArgv; } parseCmd(argc,argv); if (!libopen()) { easy_req(NULL,"Could not open libraries", "Quit", ""); exit(1); } /* create various necessities */ app_port = CreateMsgPort(); menubar = CreateMenus(amiMenu,TAG_DONE); vi = GetVisualInfo(screen, TAG_DONE); LayoutMenus(menubar, vi, GTMN_NewLookMenus, TRUE, TAG_DONE); /* create button layout */ playGrid = VLayoutObject, End; for (i=0;imp_SigBit) | AmigaGuideSignal(agc)); if (signal & AmigaGuideSignal(agc)) { AmiHelpMsg(); continue; } /* react to signals on window */ while ((result = CA_HandleInput (appWindow, &code)) != WMHI_LASTMSG) { switch (result & WMHI_CLASSMASK) { case WMHI_ICONIFY: if (CA_Iconify(appWindow)) win = NULL; break; case WMHI_UNICONIFY: win = CA_OpenWindow(appWindow); break; case WMHI_GADGETUP: switch (result & WMHI_GADGETMASK) { case G_NEW: startGame(TRUE); break; case G_RESTART: startGame(FALSE); break; default: selectLight(result&WMHI_GADGETMASK, TRUE); if (points > 0) points--; scoreBoard(); RethinkLayout((struct Gadget *)playGrid,win,NULL,TRUE); if (checkWin()) { wonBoard = TRUE; easy_req(NULL,"You WON!","OK",""); disableGrid(TRUE); } break; } break; case WMHI_MENUPICK: menunum = result&WMHI_MENUMASK; while (menunum != MENUNULL) { menu = ItemAddress( menubar, menunum); title = MENUNUM(menunum); item = ITEMNUM(menunum); sub = SUBNUM(menunum); /* which items were selected? */ switch (title) { case FILETITLE: switch (item) { case FILEABOUT: aboutLights(); break; case FILEQUIT: done = TRUE; break; } } menunum = menu->NextSelect; } break; case WMHI_HELPKEY: if (AmigaGuideBase != NULL && agc != NULL) SendAmigaGuideCmd(agc,"LINK MAIN",NULL); break; case WMHI_CLOSEWINDOW: done = TRUE; break; } } /* end collection of messages */ } /* while not done */ } /*************************************************************************************** ****************************************************************************************/ BOOL libopen(void) { if ((WindowBase = (struct ClassLibrary*)OpenLibrary("window.class",0L)) == NULL) return (FALSE); if ((LayoutBase = (struct ClassLibrary*)OpenLibrary("gadgets/layout.gadget",0L)) == NULL) return (FALSE); if ((ButtonBase = (struct ClassLibrary*)OpenLibrary("gadgets/button.gadget",0L)) == NULL) return (FALSE); if ((LabelBase = (struct ClassLibrary*)OpenLibrary("images/label.image",0L)) == NULL) return (FALSE); AmigaGuideBase = (struct Library *)OpenLibrary("amigaguide.library",0L); return TRUE; } /************************************************************************* **************************************************************************/ void libclose(void) { if (LayoutBase != NULL) CloseLibrary((struct Library *)LayoutBase); if (WindowBase != NULL) CloseLibrary((struct Library *)WindowBase); if (LabelBase != NULL) CloseLibrary((struct Library *)LabelBase); if (CheckBoxBase != NULL) CloseLibrary((struct Library *)CheckBoxBase); if (IntegerBase != NULL) CloseLibrary((struct Library *)IntegerBase); if (StringBase != NULL) CloseLibrary((struct Library *)StringBase); if (ClickTabBase != NULL) CloseLibrary((struct Library *)ClickTabBase); if (ChooserBase != NULL) CloseLibrary((struct Library *)ChooserBase); if (ButtonBase != NULL) CloseLibrary((struct Library *)ButtonBase); if (ListBrowserBase != NULL) CloseLibrary((struct Library *)ListBrowserBase); if (RadioButtonBase != NULL) CloseLibrary((struct Library *)RadioButtonBase); if (AmigaGuideBase != NULL) CloseLibrary((struct Library *)AmigaGuideBase); } /****************************************************************** *******************************************************************/ void parseCmd(int argc, char **argv) { char *ptr; while (argc > 1) { if (strnicmp(argv[argc-1], "LIGHTCOLOR",strlen("LIGHTCOLOR")) == 0) { ptr = strchr(argv[argc-1],'='); if (ptr != NULL) { sscanf(ptr+1,"%d",&lightcolor); } } else if (strnicmp(argv[argc-1],"DARKCOLOR",strlen("DARKCOLOR")) == 0) { ptr = strchr(argv[argc-1],'='); if (ptr != NULL) { sscanf(ptr+1,"%d",&darkcolor); } } else if (strnicmp(argv[argc-1],"DIFFICULTY",strlen("DIFFICULTY")) == 0) { ptr = strchr(argv[argc-1],'='); if (ptr != NULL) { sscanf(ptr+1,"%d",&difficulty); if (difficulty > MAXPOINTS) difficulty = MAXPOINTS; } } argc--; } } /****************************************************************** *******************************************************************/ void startGame(BOOL new) { int i,j; if (new) { SetGadgetAttrs(GList[G_RESTART], win, NULL, GA_Disabled, FALSE, TAG_END); RefreshGList(GList[G_SMALL], win, NULL, 1); if (wonBoard) { score[0] += points; wonBoard = FALSE; } score[1] ++; } points = difficulty+1; scoreBoard(); /* first, clear grid */ for (i=0;i0) grid[i-1][j] = ~grid[i-1][j]; if (i0) grid[i][j-1] = ~grid[i][j-1]; if (j0) SetAttrs(GButtons[i-1][j], BUTTON_BackgroundPen, (grid[i-1][j])?lightcolor:darkcolor, TAG_END); if (i0) SetAttrs(GButtons[i][j-1], BUTTON_BackgroundPen, (grid[i][j-1])?lightcolor:darkcolor, TAG_END); if (jWScreen; newAG.nag_BaseName = "amiLights"; /* Open the AmigaGuide client */ if ( !(agc = OpenAmigaGuideAsync(&newAG, TAG_DONE))) { /* Get the reason for failure */ retval = IoErr(); } return (retval); } /********************************************************* **********************************************************/ void AmiHelpMsg(void) { struct AmigaGuideMsg *agm; while ( agm = GetAmigaGuideMsg(agc)) { ReplyAmigaGuideMsg(agm); } } /******************************************************** *********************************************************/ void aboutLights(void) { char msg[200]; sprintf(msg,"amiLights\nCopyright © 1995 Douglas M. Dyer\n\n%s %s",ALVERSION, __AMIGADATE__); easy_req(NULL,msg,"OK",""); }