//............................................................................ extern "C" int main(int argc, char *argv[ ]) { if (argc > 1) { // file name provided? l_outFile = fopen(argv[1], "w"); } if (l_outFile == (FILE *)0) { // interactive version? struct termios newt; tcgetattr(STDIN_FILENO, &l_oldt); // save the terminal state newt = l_oldt; newt.c_lflag &= ~ICANON; tcsetattr(STDIN_FILENO, TCSANOW, &newt); // set non-canonical mode l_outFile = stdout; // use the stdout as the output file printf("QHsmTst example, built on %s at %s\n" "QEP: %s.\nPress ESC to quit...\n", __DATE__, __TIME__, QP::QEP::getVersion()); the_hsm->init(); // trigger the initial tran. in the test HSM for (;;) { // event loop printf("\n>"); int c = getchar(); // get a character from stdin printf(": "); QP::QEvt e = QEVT_INITIALIZER(0); if ('a' <= c && c <= 'i') { // in range? e.sig = (QP::QSignal)(c - 'a' + A_SIG); } else if ('A' <= c && c <= 'I') { // in range? e.sig = (QP::QSignal)(c - 'A' + A_SIG); } else if (c == '\33') { // the ESC key? e.sig = TERMINATE_SIG; // terminate the interactive test } else { e.sig = IGNORE_SIG; } the_hsm->dispatch(&e); // dispatch the event } } else { // batch version printf("QHsmTst, output saved to %s\n", argv[1]); fprintf(l_outFile, "QHsmTst example, QEP %s\n", QP::QEP::getVersion()); the_hsm->init(); // trigger the initial tran. in the test HSM // dynamic transitions dispatch(A_SIG); dispatch(B_SIG); dispatch(D_SIG); dispatch(E_SIG); dispatch(I_SIG); dispatch(F_SIG); dispatch(I_SIG); dispatch(I_SIG); dispatch(F_SIG); dispatch(A_SIG); dispatch(B_SIG); dispatch(D_SIG); dispatch(D_SIG); dispatch(E_SIG); dispatch(G_SIG); dispatch(H_SIG); dispatch(H_SIG); dispatch(C_SIG); dispatch(G_SIG); dispatch(C_SIG); dispatch(C_SIG); fclose(l_outFile); } return 0; }
//............................................................................ static void dispatch(QP::QSignal sig) { Q_REQUIRE((A_SIG <= sig) && (sig <= I_SIG)); fprintf(l_outFile, "\n%c:", 'A' + sig - A_SIG); QP::QEvt e = QEVT_INITIALIZER(sig); the_hsm->dispatch(&e); // dispatch the event }
//............................................................................ extern "C" int main(int argc, char *argv[ ]) { if (argc > 1) { // file name provided? fopen_s(&l_outFile, argv[1], "w"); } if (l_outFile == (FILE *)0) { // interactive version? l_outFile = stdout; printf("QHsmTst example, built on %s at %s\n" "QEP: %s.\nPress ESC to quit...\n", __DATE__, __TIME__, QP::QEP::getVersion()); the_hsm->init(); // trigger the initial tran. in the test HSM for (;;) { // event loop printf("\n>"); int c; c = _getche(); // get a character from the console with echo printf(": "); QP::QEvt e = QEVT_INITIALIZER(0); if ('a' <= c && c <= 'i') { // in range? e.sig = (QP::QSignal)(c - 'a' + A_SIG); } else if ('A' <= c && c <= 'I') { // in range? e.sig = (QP::QSignal)(c - 'A' + A_SIG); } else if (c == '\33') { // the ESC key? e.sig = TERMINATE_SIG; // terminate the interactive test } else { e.sig = IGNORE_SIG; } the_hsm->dispatch(&e); // dispatch the event } } else { // batch version printf("QHsmTst, output saved to %s\n", argv[1]); fprintf(l_outFile, "QHsmTst example, QEP %s\n", QP::QEP::getVersion()); the_hsm->init(); // trigger the initial tran. in the test HSM // dynamic transitions dispatch(A_SIG); dispatch(B_SIG); dispatch(D_SIG); dispatch(E_SIG); dispatch(I_SIG); dispatch(F_SIG); dispatch(I_SIG); dispatch(I_SIG); dispatch(F_SIG); dispatch(A_SIG); dispatch(B_SIG); dispatch(D_SIG); dispatch(D_SIG); dispatch(E_SIG); dispatch(G_SIG); dispatch(H_SIG); dispatch(H_SIG); dispatch(C_SIG); dispatch(G_SIG); dispatch(C_SIG); dispatch(C_SIG); fclose(l_outFile); } return 0; }
//............................................................................ static LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { // Perform initialization upon cration of the main dialog window // NOTE: Any child-windows are NOT created yet at this time, so // the GetDlgItem() function can't be used (it will return NULL). // case WM_CREATE: { l_hWnd = hWnd; // save the window handle // initialize the owner-drawn buttons // NOTE: must be done *before* the first drawing of the buttons, // so WM_INITDIALOG is too late. // l_pauseBtn.init(LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_BTN_UP)), LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_BTN_DWN)), LoadCursor(NULL, IDC_HAND)); return 0; } // Perform initialization after all child windows have been created case WM_INITDIALOG: { l_philos.init(N_PHILO, // N_PHILO "segments" for the Philos 3U); // 3 bitmaps (for thinking/hungry/eating) l_philos.initSegment(0U, GetDlgItem(hWnd, IDC_PHILO_0)); l_philos.initSegment(1U, GetDlgItem(hWnd, IDC_PHILO_1)); l_philos.initSegment(2U, GetDlgItem(hWnd, IDC_PHILO_2)); l_philos.initSegment(3U, GetDlgItem(hWnd, IDC_PHILO_3)); l_philos.initSegment(4U, GetDlgItem(hWnd, IDC_PHILO_4)); l_philos.initBitmap(0U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_THINKING))); l_philos.initBitmap(1U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_HUNGRY))); l_philos.initBitmap(2U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_EATING))); // --> QP: spawn the application thread to run main() Q_ALLEGE(CreateThread(NULL, 0, &appThread, NULL, 0, NULL) != (HANDLE)0); return 0; } case WM_DESTROY: { BSP_terminate(0); return 0; } // commands from regular buttons and menus... case WM_COMMAND: { SetFocus(hWnd); switch (wParam) { case IDOK: case IDCANCEL: { //QP::QF::PUBLISH(Q_NEW(QP::QEvt, TERMINATE_SIG), (void *)0); BSP_terminate(0); break; } } return 0; } // owner-drawn buttons... case WM_DRAWITEM: { LPDRAWITEMSTRUCT pdis = (LPDRAWITEMSTRUCT)lParam; switch (pdis->CtlID) { case IDC_PAUSE: { // PAUSE owner-drawn button static QP::QEvt pe = QEVT_INITIALIZER(PAUSE_SIG); switch (l_pauseBtn.draw(pdis)) { case OwnerDrawnButton::BTN_DEPRESSED: { AO_Table->POST(&pe, (void *)0); break; } case OwnerDrawnButton::BTN_RELEASED: { AO_Table->POST(&pe, (void *)0); break; } } break; } } return 0; } // mouse input... case WM_MOUSEWHEEL: { return 0; } // keyboard input... case WM_KEYDOWN: { return 0; } } return DefWindowProc(hWnd, iMsg, wParam, lParam) ; }
//............................................................................ void QP::QF_onClockTick(void) { static QP::QEvt const tickEvt = QEVT_INITIALIZER(GAME::TIME_TICK_SIG); QP::QF::TICK(&GAME::l_clock_tick); //perform the QF clock tick processing QP::QF::PUBLISH(&tickEvt, &GAME::l_clock_tick); // publish the tick event }
//............................................................................ void BSP_playerTrigger(void) { static QP::QEvt const fireEvt = QEVT_INITIALIZER(PLAYER_TRIGGER_SIG); QP::QF::PUBLISH(&fireEvt, (void*)0); }