/*..........................................................................*/ 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. */ OwnerDrawnButton_init(&l_userBtn, 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: { GraphicDisplay_init(&l_oled, BSP_SCREEN_WIDTH, 2U, /* scale horizontally by 2 */ BSP_SCREEN_HEIGHT, 2U, /* scale vertically by 2 */ GetDlgItem(hWnd, IDC_LCD), c_offColor); SegmentDisplay_init(&l_userLED, 1U, /* 1 "segment" (the LED itself) */ 2U); /* 2 bitmaps (for LED OFF/ON states) */ SegmentDisplay_initSegment(&l_userLED, 0U, GetDlgItem(hWnd, IDC_LED)); SegmentDisplay_initBitmap(&l_userLED, 0U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_LED_OFF))); SegmentDisplay_initBitmap(&l_userLED, 1U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_LED_ON))); SegmentDisplay_init(&l_scoreBoard, 4U, /* 4 "segments" (digits 0-3) */ 10U); /* 10 bitmaps (for 0-9 states) */ SegmentDisplay_initSegment(&l_scoreBoard, 0U, GetDlgItem(hWnd, IDC_SEG0)); SegmentDisplay_initSegment(&l_scoreBoard, 1U, GetDlgItem(hWnd, IDC_SEG1)); SegmentDisplay_initSegment(&l_scoreBoard, 2U, GetDlgItem(hWnd, IDC_SEG2)); SegmentDisplay_initSegment(&l_scoreBoard, 3U, GetDlgItem(hWnd, IDC_SEG3)); SegmentDisplay_initBitmap(&l_scoreBoard, 0U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG0))); SegmentDisplay_initBitmap(&l_scoreBoard, 1U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG1))); SegmentDisplay_initBitmap(&l_scoreBoard, 2U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG2))); SegmentDisplay_initBitmap(&l_scoreBoard, 3U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG3))); SegmentDisplay_initBitmap(&l_scoreBoard, 4U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG4))); SegmentDisplay_initBitmap(&l_scoreBoard, 5U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG5))); SegmentDisplay_initBitmap(&l_scoreBoard, 6U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG6))); SegmentDisplay_initBitmap(&l_scoreBoard, 7U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG7))); SegmentDisplay_initBitmap(&l_scoreBoard, 8U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG8))); SegmentDisplay_initBitmap(&l_scoreBoard, 9U, LoadBitmap(l_hInst, MAKEINTRESOURCE(IDB_SEG9))); BSP_updateScore(0U); /* --> QP: spawn the application thread to run main_gui() */ 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: { BSP_terminate(0); break; } } return 0; } /* owner-drawn buttons... */ case WM_DRAWITEM: { LPDRAWITEMSTRUCT pdis = (LPDRAWITEMSTRUCT)lParam; switch (pdis->CtlID) { case IDC_USER: { /* USER owner-drawn button */ switch (OwnerDrawnButton_draw(&l_userBtn, pdis)) { case BTN_DEPRESSED: { playerTrigger(); SegmentDisplay_setSegment(&l_userLED, 0U, 1U); break; } case BTN_RELEASED: { SegmentDisplay_setSegment(&l_userLED, 0U, 0U); break; } default: { break; } } break; } } return 0; } /* mouse input... */ case WM_MOUSEWHEEL: { if ((HIWORD(wParam) & 0x8000U) == 0U) {/* wheel turned forward? */ moveShipUp(); } else { /* the wheel was turned backwards */ moveShipDown(); } return 0; } /* keyboard input... */ case WM_KEYDOWN: { switch (wParam) { case VK_UP: moveShipUp(); break; case VK_DOWN: moveShipDown(); break; case VK_SPACE: playerTrigger(); break; } return 0; } } return DefWindowProc(hWnd, iMsg, wParam, lParam) ; }
//prototype int main(void){ bool done = false; bool redraw = true; const int FPS = 60; bool isGameOver = false; //object variables spaceShip ship; Bullet bullets[NUM_BULLETS]; Comet comets[NUM_COMETS]; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *eventQueue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font18 = NULL; //check if allegro is initialised <allegro_native_dialog.h> if(!al_init()){ return -1; } display = al_create_display(WIDTH,HEIGHT); //check if display was created if(!display){ return -1; } al_init_primitives_addon(); al_install_keyboard(); al_init_font_addon(); al_init_ttf_addon(); eventQueue = al_create_event_queue(); timer= al_create_timer(1.0/FPS); srand(time(NULL)); // gives the rand() some value initShip(ship); initBullet(bullets, NUM_BULLETS); initComet(comets, NUM_COMETS); font18 = al_load_font("arial.ttf",18,0); al_register_event_source(eventQueue, al_get_keyboard_event_source()); al_register_event_source(eventQueue, al_get_timer_event_source(timer)); al_register_event_source(eventQueue, al_get_display_event_source(display)); //event os display resize closing, moving al_start_timer(timer); while(!done){ ALLEGRO_EVENT ev; al_wait_for_event(eventQueue, &ev); if (ev.type==ALLEGRO_EVENT_TIMER){ redraw=true; if(keys[UP]){ moveShipUp(ship); } if(keys[DOWN]){ moveShipDown(ship); } if(keys[LEFT]){ moveShipLeft(ship); } if(keys[RIGHT]){ moveShipRight(ship); } if(!isGameOver){ updateBullet(bullets,NUM_BULLETS); startComet(comets, NUM_COMETS); updateComet(comets, NUM_COMETS); collideBullet(bullets,NUM_BULLETS, comets, NUM_BULLETS, ship); collideComet(comets, NUM_COMETS, ship); if (ship.lives <= 0){ isGameOver = true; } } } // when clicling on red X icon to close the game else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ done=true; } else if (ev.type==ALLEGRO_EVENT_KEY_DOWN){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_ESCAPE: done=true; break; case ALLEGRO_KEY_UP: keys[UP]=true; break; case ALLEGRO_KEY_DOWN: keys[DOWN]=true; break; case ALLEGRO_KEY_LEFT: keys[LEFT]=true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT]=true; break; case ALLEGRO_KEY_SPACE: keys[SPACE]=true; fireBullet(bullets, NUM_BULLETS, ship); break; } } else if (ev.type==ALLEGRO_EVENT_KEY_UP){ switch(ev.keyboard.keycode){ //make sure when exiting the program exit with esc button case ALLEGRO_KEY_ESCAPE: done=true; break; case ALLEGRO_KEY_UP: keys[UP]=false; break; case ALLEGRO_KEY_DOWN: keys[DOWN]=false; break; case ALLEGRO_KEY_LEFT: keys[LEFT]=false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT]=false; break; case ALLEGRO_KEY_SPACE: keys[SPACE]=false; break; } } if (redraw && al_is_event_queue_empty(eventQueue)){ // I only draw the screen if there no more event redraw=false; if(!isGameOver){ drawShip(ship); drawBullet(bullets,NUM_BULLETS); drawComet(comets, NUM_COMETS); al_draw_textf(font18, al_map_rgb(255,0,255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score); } else{ al_draw_textf(font18, al_map_rgb(0,255,255),WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over, Pal. Final Score: %i", ship.score); } al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); //that prevents the filled rectangle draw imga just like snake style } //make sure when exiting the program exit with esc button // when clicling on red X icon to close the game } //al_rest(5.0); al_destroy_display(display); //hadouken return 0; }
//prototype int main(void){ bool done = false; bool redraw = true; const int FPS = 60; //object variables spaceShip ship; Bullet bullets[5]; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *eventQueue = NULL; ALLEGRO_TIMER *timer = NULL; //check if allegro is initialised <allegro_native_dialog.h> if(!al_init()){ return -1; } display = al_create_display(WIDTH,HEIGHT); //check if display was created if(!display){ return -1; } al_init_primitives_addon(); al_install_keyboard(); //to capture keyboard events eventQueue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); initShip(ship); initBullet(bullets,NUM_BULLETS); //register the keyboard to eventQueue al_register_event_source(eventQueue,al_get_keyboard_event_source()); al_register_event_source(eventQueue,al_get_timer_event_source(timer)); al_register_event_source(eventQueue,al_get_display_event_source(display)); al_start_timer(timer); while(!done){ ALLEGRO_EVENT ev; al_wait_for_event(eventQueue,&ev); if(ev.type == ALLEGRO_EVENT_TIMER){ redraw=true; if(keys[UP]){ moveShipUp(ship); } if(keys[DOWN]){ moveShipDown(ship); } if(keys[LEFT]){ moveShipLeft(ship); } if(keys[RIGHT]){ moveShipRight(ship); } updateBullet(bullets,NUM_BULLETS); } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ done=true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; fireBullet(bullets, NUM_BULLETS, ship); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } if(redraw && al_is_event_queue_empty(eventQueue)){ redraw = false; drawShip(ship); //without updating the bullets they will be only create a static spot in the screen, limited to n number of bullets drawBullet(bullets,NUM_BULLETS); // when clicling on red X icon to close the game al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); //that prevents the filled rectangle draw imga just like snake style } //make sure when exiting the program exit with esc button } //al_rest(5.0); al_destroy_display(display); //hadouken return 0; }