//update returns if clicked bool Button::Update(ALLEGRO_MOUSE_STATE *mouse) { m_isActive = false; if(mouse->x > m_xPosition && mouse->x < m_yPosition + m_buttonWidth) { if(mouse->y > m_yPosition && mouse->y < m_yPosition + m_buttonHeight) { m_isActive = true; if(al_mouse_button_down(mouse, 1)) { if(m_prevClick == 1) { m_prevClick = 2; return true; } } else { m_prevClick = 1; } } } return false; }
void Game::processLoop() { //update units mpUnit->update( LOOP_TARGET_TIME/1000.0f ); mpAIUnit->update( LOOP_TARGET_TIME/1000.0f ); mpAIUnit2->update( LOOP_TARGET_TIME/1000.0f ); //draw background Sprite* pBackgroundSprite = mpSpriteManager->getSprite( BACKGROUND_SPRITE_ID ); pBackgroundSprite->draw( *(mpGraphicsSystem->getBackBuffer()), 0, 0 ); //draw units mpUnit->draw( GRAPHICS_SYSTEM->getBackBuffer() ); mpAIUnit->draw( GRAPHICS_SYSTEM->getBackBuffer() ); mpAIUnit2->draw( GRAPHICS_SYSTEM->getBackBuffer() ); mpMessageManager->processMessagesForThisframe(); //get input - should be moved someplace better ALLEGRO_MOUSE_STATE mouseState; al_get_mouse_state( &mouseState ); if( al_mouse_button_down( &mouseState, 1 ) )//left mouse click { Vector2D pos( mouseState.x, mouseState.y ); GameMessage* pMessage = new PlayerMoveToMessage( pos ); MESSAGE_MANAGER->addMessage( pMessage, 0 ); } //all this should be moved to InputManager!!! { //get mouse state ALLEGRO_MOUSE_STATE mouseState; al_get_mouse_state( &mouseState ); //create mouse text stringstream mousePos; mousePos << mouseState.x << ":" << mouseState.y; //write text at mouse position al_draw_text( mpFont, al_map_rgb( 255, 255, 255 ), mouseState.x, mouseState.y, ALLEGRO_ALIGN_CENTRE, mousePos.str().c_str() ); mpGraphicsSystem->swap(); //get current keyboard state ALLEGRO_KEYBOARD_STATE keyState; al_get_keyboard_state( &keyState ); //if escape key was down then exit the loop if( al_key_down( &keyState, ALLEGRO_KEY_ESCAPE ) ) { mShouldExit = true; } } }
void check_for_button_events(BUTTONPTR currentptr) { const int MOUSEL = 1;// MOUSER = 2, MOUSEM = 3; static int mx, my; static _Bool mdown = false; ALLEGRO_MOUSE_STATE mouse; al_get_mouse_state(&mouse); set_bflags_on_button(currentptr, BDRAW); if(al_mouse_button_down(&mouse, MOUSEL) && !mdown) { mx = mouse.x; my = mouse.y; mdown = true; } if(!al_mouse_button_down(&mouse, MOUSEL)) { mdown = false; } if(is_point_over_button(mx, my, currentptr)) { if(is_mouse_over_button(&mouse, currentptr)) { set_bflags_on_button(currentptr, BMOUSEOVER); } if(al_mouse_button_down(&mouse, MOUSEL) && (currentptr->bdata.bflags & BMOUSEOVER)) { set_bflags_on_button(currentptr, BLPRESSED); } if(!al_mouse_button_down(&mouse, MOUSEL) && (currentptr->bdata.bflags & BLPRESSED)) { set_bflags_on_button(currentptr, BLRELEASED); } } else { if(is_mouse_over_button(&mouse, currentptr)) { set_bflags_on_button(currentptr, BMOUSEOVER); } } }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *cursor; ALLEGRO_MOUSE_STATE msestate; ALLEGRO_KEYBOARD_STATE kbdstate; int i; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_init_primitives_addon(); al_install_mouse(); al_install_keyboard(); al_init_image_addon(); init_platform_specific(); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); } al_hide_mouse_cursor(display); cursor = al_load_bitmap("data/cursor.tga"); if (!cursor) { abort_example("Error loading cursor.tga\n"); } do { al_get_mouse_state(&msestate); al_get_keyboard_state(&kbdstate); al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0)); for (i = 1; i <= NUM_BUTTONS; i++) { draw_mouse_button(i, al_mouse_button_down(&msestate, i)); } al_draw_bitmap(cursor, msestate.x, msestate.y, 0); al_flip_display(); al_rest(0.005); } while (!al_key_down(&kbdstate, ALLEGRO_KEY_ESCAPE)); return 0; }
static void handle_mouse_capture(bool down, HWND hWnd) { int i; bool any_button_down = false; ALLEGRO_MOUSE_STATE state; if (!al_is_mouse_installed()) return; al_get_mouse_state(&state); for (i = 1; i <= 5; i++) { any_button_down |= al_mouse_button_down(&state, i); } if (down && GetCapture() != hWnd) { SetCapture(hWnd); } else if (!any_button_down) { ReleaseCapture(); } }
static LRESULT CALLBACK window_callback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { ALLEGRO_DISPLAY *d = NULL; ALLEGRO_DISPLAY_WIN *win_display = NULL; unsigned int i; ALLEGRO_EVENT_SOURCE *es = NULL; ALLEGRO_SYSTEM *system = al_get_system_driver(); if (message == _al_win_msg_call_proc) { ((void (*)(void*))wParam) ((void*)lParam); return 0; } if (!system) { return DefWindowProc(hWnd,message,wParam,lParam); } if (message == _al_win_msg_suicide && wParam) { win_display = (ALLEGRO_DISPLAY_WIN*)wParam; break_window_message_pump(win_display, hWnd); DestroyWindow(hWnd); return 0; } for (i = 0; i < system->displays._size; i++) { ALLEGRO_DISPLAY **dptr = _al_vector_ref(&system->displays, i); d = *dptr; win_display = (void*)d; if (win_display->window == hWnd) { es = &d->es; break; } } if (i == system->displays._size) return DefWindowProc(hWnd,message,wParam,lParam); if (message == _al_win_msg_suicide) { break_window_message_pump(win_display, hWnd); DestroyWindow(hWnd); return 0; } switch (message) { case WM_INPUT: { /* RAW Input is currently unused. */ UINT dwSize; LPBYTE lpb; RAWINPUT* raw; /* We can't uninstall WM_INPUT mesages. */ if (!al_is_mouse_installed()) break; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER)); lpb = al_malloc(sizeof(BYTE)*dwSize); if (lpb == NULL) break; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)); raw = (RAWINPUT*)lpb; if (raw->header.dwType != RIM_TYPEMOUSE) { al_free(lpb); break; } { RAWMOUSE *rm = &raw->data.mouse; int x = raw->data.mouse.lLastX; int y = raw->data.mouse.lLastY; bool abs = (rm->usFlags & (MOUSE_MOVE_ABSOLUTE | MOUSE_VIRTUAL_DESKTOP)) != 0; if (abs || x || y) _al_win_mouse_handle_move(x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_1_DOWN) _al_win_mouse_handle_button(1, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_1_UP) _al_win_mouse_handle_button(1, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_2_DOWN) _al_win_mouse_handle_button(2, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_2_UP) _al_win_mouse_handle_button(2, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_3_DOWN) _al_win_mouse_handle_button(3, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_3_UP) _al_win_mouse_handle_button(3, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_4_DOWN) _al_win_mouse_handle_button(4, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_4_UP) _al_win_mouse_handle_button(4, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_5_DOWN) _al_win_mouse_handle_button(5, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_5_UP) _al_win_mouse_handle_button(5, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_WHEEL) { SHORT z = (SHORT)rm->usButtonData; _al_win_mouse_handle_wheel(z / WHEEL_DELTA, false, win_display); } } al_free(lpb); break; } case WM_LBUTTONDOWN: case WM_LBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_LBUTTONDOWN); _al_win_mouse_handle_button(1, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_MBUTTONDOWN: case WM_MBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_MBUTTONDOWN); _al_win_mouse_handle_button(3, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_RBUTTONDOWN: case WM_RBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_RBUTTONDOWN); _al_win_mouse_handle_button(2, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_XBUTTONDOWN: case WM_XBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); int button = HIWORD(wParam); bool down = (message == WM_XBUTTONDOWN); if (button == XBUTTON1) _al_win_mouse_handle_button(4, down, mx, my, true, win_display); else if (button == XBUTTON2) _al_win_mouse_handle_button(5, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); return TRUE; } case WM_MOUSEWHEEL: { int d = GET_WHEEL_DELTA_WPARAM(wParam); _al_win_mouse_handle_wheel(d / WHEEL_DELTA, false, win_display); return TRUE; } case WM_MOUSEHWHEEL: { int d = GET_WHEEL_DELTA_WPARAM(wParam); _al_win_mouse_handle_hwheel(d / WHEEL_DELTA, false, win_display); return TRUE; } case WM_MOUSEMOVE: { TRACKMOUSEEVENT tme; int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); if (win_display->mouse_cursor_shown && we_hid_the_mouse) { we_hid_the_mouse = false; win_display->display.vt->hide_mouse_cursor((void*)win_display); } _al_win_mouse_handle_move(mx, my, true, win_display); if (mx >= 0 && my >= 0 && mx < d->w && my < d->h) { tme.cbSize = sizeof(tme); tme.dwFlags = TME_QUERY; if (TrackMouseEvent(&tme) && !tme.hwndTrack) { tme.dwFlags = TME_LEAVE; tme.hwndTrack = hWnd; tme.dwHoverTime = 0; TrackMouseEvent(&tme); _al_win_mouse_handle_enter(win_display); } } /* WM_SETCURSOR messages are not received while the mouse is * captured. We call SetCursor here so that changing the mouse * cursor has an effect while the user is holding down the mouse * button. */ if (GetCapture() == hWnd && win_display->mouse_cursor_shown) { SetCursor(win_display->mouse_selected_hcursor); } break; } case WM_MOUSELEAVE: { _al_win_mouse_handle_leave(win_display); break; } case WM_CAPTURECHANGED: { if (al_is_mouse_installed()) { int i; ALLEGRO_MOUSE_STATE state; if (!lParam || (HWND)lParam == hWnd) break; al_get_mouse_state(&state); for (i = 1; i <= 5; i++) { if (al_mouse_button_down(&state, i)) _al_win_mouse_handle_button(i, 0, 0, 0, true, win_display); } } break; } case WM_NCMOUSEMOVE: { if (!win_display->mouse_cursor_shown) { we_hid_the_mouse = true; win_display->display.vt->show_mouse_cursor((void*)win_display); } break; } case WM_SYSKEYDOWN: { int vcode = wParam; bool extended = (lParam >> 24) & 0x1; bool repeated = (lParam >> 30) & 0x1; _al_win_kbd_handle_key_press(0, vcode, extended, repeated, win_display); break; } case WM_KEYDOWN: { int vcode = wParam; int scode = (lParam >> 16) & 0xff; bool extended = (lParam >> 24) & 0x1; bool repeated = (lParam >> 30) & 0x1; /* We can't use TranslateMessage() because we don't know if it will produce a WM_CHAR or not. */ _al_win_kbd_handle_key_press(scode, vcode, extended, repeated, win_display); break; } case WM_SYSKEYUP: case WM_KEYUP: { int vcode = wParam; int scode = (lParam >> 16) & 0xff; bool extended = (lParam >> 24) & 0x1; _al_win_kbd_handle_key_release(scode, vcode, extended, win_display); break; } case WM_SYSCOMMAND: { if (_al_win_disable_screensaver && ((wParam & 0xfff0) == SC_MONITORPOWER || (wParam & 0xfff0) == SC_SCREENSAVE)) { return 0; } else if ((wParam & 0xfff0) == SC_KEYMENU) { /* Prevent Windows from intercepting the ALT key. (Disables opening menus via the ALT key.) */ return 0; } break; } case WM_PAINT: { if (win_display->display.flags & ALLEGRO_GENERATE_EXPOSE_EVENTS) { RECT r; HRGN hrgn; GetWindowRect(win_display->window, &r); hrgn = CreateRectRgn(r.left, r.top, r.right, r.bottom); if (GetUpdateRgn(win_display->window, hrgn, false) != ERROR) { PAINTSTRUCT ps; DWORD size; LPRGNDATA rgndata; int n; int i; RECT *rects; BeginPaint(win_display->window, &ps); size = GetRegionData(hrgn, 0, NULL); rgndata = al_malloc(size); GetRegionData(hrgn, size, rgndata); n = rgndata->rdh.nCount; rects = (RECT *)rgndata->Buffer; _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_EXPOSE; event.display.timestamp = al_get_time(); for (i = 0; i < n; i++) { event.display.x = rects[i].left; event.display.y = rects[i].top; event.display.width = rects[i].right - rects[i].left; event.display.height = rects[i].bottom - rects[i].top; _al_event_source_emit_event(es, &event); } } _al_event_source_unlock(es); al_free(rgndata); EndPaint(win_display->window, &ps); DeleteObject(hrgn); } return 0; } break; } case WM_SETCURSOR: switch (LOWORD(lParam)) { case HTLEFT: case HTRIGHT: SetCursor(LoadCursor(NULL, IDC_SIZEWE)); break; case HTBOTTOM: case HTTOP: SetCursor(LoadCursor(NULL, IDC_SIZENS)); break; case HTBOTTOMLEFT: case HTTOPRIGHT: SetCursor(LoadCursor(NULL, IDC_SIZENESW)); break; case HTBOTTOMRIGHT: case HTTOPLEFT: SetCursor(LoadCursor(NULL, IDC_SIZENWSE)); break; default: if (win_display->mouse_cursor_shown) { SetCursor(win_display->mouse_selected_hcursor); } else { SetCursor(NULL); } break; } return 1; case WM_ACTIVATE: if (HIWORD(wParam) && LOWORD(wParam) != WA_INACTIVE) break; if (HIWORD(wParam)) d->flags |= ALLEGRO_MINIMIZED; else d->flags &= ~ALLEGRO_MINIMIZED; if (LOWORD(wParam) != WA_INACTIVE) { // Make fullscreen windows TOPMOST again if (d->flags & ALLEGRO_FULLSCREEN_WINDOW) { SetWindowPos(win_display->window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); } if (d->vt->switch_in) d->vt->switch_in(d); _al_win_fix_modifiers(); _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; memset(&event, 0, sizeof(event)); event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_IN; event.display.timestamp = al_get_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); _al_win_grab_input(win_display); return 0; } else { // Remove TOPMOST flag from fullscreen windows so we can alt-tab. Also must raise the new activated window if (d->flags & ALLEGRO_FULLSCREEN_WINDOW) { SetWindowPos(win_display->window, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); SetWindowPos(GetForegroundWindow(), HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); } // Show the taskbar in case we hid it SetWindowPos(FindWindow("Shell_traywnd", ""), 0, 0, 0, 0, 0, SWP_SHOWWINDOW); if (d->flags & ALLEGRO_FULLSCREEN) { d->vt->switch_out(d); } _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; memset(&event, 0, sizeof(event)); event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_OUT; event.display.timestamp = al_get_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); return 0; } break; case WM_MENUCHAR : return (MNC_CLOSE << 16) | (wParam & 0xffff); case WM_CLOSE: _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; memset(&event, 0, sizeof(event)); event.display.type = ALLEGRO_EVENT_DISPLAY_CLOSE; event.display.timestamp = al_get_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); return 0; case WM_SIZE: if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED || wParam == SIZE_MINIMIZED) { /* * Delay the resize event so we don't get bogged down with them */ if (!resize_postponed) { resize_postponed = true; _beginthread(postpone_thread_proc, 0, (void *)d); } } return 0; case WM_ENTERSIZEMOVE: /* DefWindowProc for WM_ENTERSIZEMOVE enters a modal loop, which also * ends up blocking the loop in d3d_display_thread_proc (which is * where we are called from, if using D3D). Rather than batching up * intermediate resize events which the user cannot acknowledge in the * meantime anyway, make it so only a single resize event is generated * at WM_EXITSIZEMOVE. */ if (d->flags & ALLEGRO_DIRECT3D) { resize_postponed = true; } break; case WM_EXITSIZEMOVE: if (resize_postponed) { win_generate_resize_event(win_display); win_display->ignore_resize = false; resize_postponed = false; win_display->can_acknowledge = true; } break; } return DefWindowProc(hWnd,message,wParam,lParam); }
static LRESULT CALLBACK window_callback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { ALLEGRO_DISPLAY *d = NULL; ALLEGRO_DISPLAY_WIN *win_display = NULL; WINDOWINFO wi; int w; int h; int x; int y; unsigned int i; ALLEGRO_EVENT_SOURCE *es = NULL; ALLEGRO_SYSTEM *system = al_get_system_driver(); wi.cbSize = sizeof(WINDOWINFO); if (message == _al_win_msg_call_proc) { ((void (*)(void*))wParam) ((void*)lParam); return 0; } if (!system) { return DefWindowProc(hWnd,message,wParam,lParam); } if (message == _al_win_msg_suicide && wParam) { win_display = (ALLEGRO_DISPLAY_WIN*)wParam; win_display->end_thread = true; DestroyWindow(hWnd); return 0; } for (i = 0; i < system->displays._size; i++) { ALLEGRO_DISPLAY **dptr = _al_vector_ref(&system->displays, i); d = *dptr; win_display = (void*)d; if (win_display->window == hWnd) { es = &d->es; break; } } if (i == system->displays._size) return DefWindowProc(hWnd,message,wParam,lParam); if (message == _al_win_msg_suicide) { win_display->end_thread = true; DestroyWindow(hWnd); return 0; } switch (message) { case WM_INPUT: { UINT dwSize; LPBYTE lpb; RAWINPUT* raw; /* We can't uninstall WM_INPUT mesages. */ if (!al_is_mouse_installed()) break; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER)); lpb = malloc(sizeof(BYTE)*dwSize); if (lpb == NULL) break; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)); raw = (RAWINPUT*)lpb; if (raw->header.dwType != RIM_TYPEMOUSE) { free(lpb); break; } { RAWMOUSE *rm = &raw->data.mouse; int x = raw->data.mouse.lLastX; int y = raw->data.mouse.lLastY; bool abs = rm->usFlags & (MOUSE_MOVE_ABSOLUTE || MOUSE_VIRTUAL_DESKTOP); if (abs || x || y) _al_win_mouse_handle_move(x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_1_DOWN) _al_win_mouse_handle_button(1, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_1_UP) _al_win_mouse_handle_button(1, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_2_DOWN) _al_win_mouse_handle_button(2, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_2_UP) _al_win_mouse_handle_button(2, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_3_DOWN) _al_win_mouse_handle_button(3, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_3_UP) _al_win_mouse_handle_button(3, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_4_DOWN) _al_win_mouse_handle_button(4, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_4_UP) _al_win_mouse_handle_button(4, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_5_DOWN) _al_win_mouse_handle_button(5, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_5_UP) _al_win_mouse_handle_button(5, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_WHEEL) { SHORT z = (SHORT)rm->usButtonData; _al_win_mouse_handle_wheel(z / WHEEL_DELTA, false, win_display); } } free(lpb); break; } case WM_LBUTTONDOWN: case WM_LBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_LBUTTONDOWN); _al_win_mouse_handle_button(1, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_MBUTTONDOWN: case WM_MBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_MBUTTONDOWN); _al_win_mouse_handle_button(3, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_RBUTTONDOWN: case WM_RBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_RBUTTONDOWN); _al_win_mouse_handle_button(2, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_XBUTTONDOWN: case WM_XBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); int button = HIWORD(wParam); bool down = (message == WM_XBUTTONDOWN); if (button == XBUTTON1) _al_win_mouse_handle_button(4, down, mx, my, true, win_display); else if (button == XBUTTON2) _al_win_mouse_handle_button(5, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); return TRUE; } case WM_MOUSEWHEEL: { int d = GET_WHEEL_DELTA_WPARAM(wParam); _al_win_mouse_handle_wheel(d / WHEEL_DELTA, false, win_display); return TRUE; } case WM_MOUSEMOVE: { TRACKMOUSEEVENT tme; int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); if (win_display->mouse_cursor_shown && we_hid_the_mouse) { we_hid_the_mouse = false; win_display->display.vt->hide_mouse_cursor((void*)win_display); } _al_win_mouse_handle_move(mx, my, true, win_display); if (mx >= 0 && my >= 0 && mx < d->w && my < d->h) { tme.cbSize = sizeof(tme); tme.dwFlags = TME_QUERY; if (TrackMouseEvent(&tme) && !tme.hwndTrack) { tme.dwFlags = TME_LEAVE; tme.hwndTrack = hWnd; tme.dwHoverTime = 0; TrackMouseEvent(&tme); _al_win_mouse_handle_enter(win_display); } } break; } case WM_MOUSELEAVE: { _al_win_mouse_handle_leave(win_display); break; } case WM_CAPTURECHANGED: if (al_is_mouse_installed()) { int i; ALLEGRO_MOUSE_STATE state; if (!lParam || (HWND)lParam == hWnd) break; al_get_mouse_state(&state); for (i = 1; i <= 5; i++) { if (al_mouse_button_down(&state, i)) _al_win_mouse_handle_button(i, 0, 0, 0, true, win_display); } break; } case WM_NCMOUSEMOVE: { if (!win_display->mouse_cursor_shown) { we_hid_the_mouse = true; win_display->display.vt->show_mouse_cursor((void*)win_display); } break; } case WM_SYSKEYDOWN: { int vcode = wParam; bool repeated = (lParam >> 30) & 0x1; _al_win_kbd_handle_key_press(0, vcode, repeated, win_display); break; } case WM_KEYDOWN: { int vcode = wParam; int scode = (lParam >> 16) & 0xff; bool repeated = (lParam >> 30) & 0x1; /* We can't use TranslateMessage() because we don't know if it will produce a WM_CHAR or not. */ _al_win_kbd_handle_key_press(scode, vcode, repeated, win_display); break; } case WM_SYSKEYUP: case WM_KEYUP: { int vcode = wParam; _al_win_kbd_handle_key_release(vcode, win_display); break; } case WM_SYSCOMMAND: { if (_al_win_disable_screensaver && ((wParam & 0xfff0) == SC_MONITORPOWER || (wParam & 0xfff0) == SC_SCREENSAVE)) { return 0; } else if ((wParam & 0xfff0) == SC_KEYMENU) { /* Prevent Windows from intercepting the ALT key. (Disables opening menus via the ALT key.) */ return 0; } break; } case WM_PAINT: { if ((win_display->display.flags & ALLEGRO_GENERATE_EXPOSE_EVENTS) && _al_event_source_needs_to_generate_event(es)) { RECT r; HRGN hrgn; GetWindowRect(win_display->window, &r); hrgn = CreateRectRgn(r.left, r.top, r.right, r.bottom); if (GetUpdateRgn(win_display->window, hrgn, false) != ERROR) { PAINTSTRUCT ps; DWORD size; LPRGNDATA rgndata; int n; int i; RECT *rects; BeginPaint(win_display->window, &ps); size = GetRegionData(hrgn, 0, NULL); rgndata = _AL_MALLOC(size); GetRegionData(hrgn, size, rgndata); n = rgndata->rdh.nCount; rects = (RECT *)rgndata->Buffer; //GetWindowInfo(win_display->window, &wi); _al_event_source_lock(es); for (i = 0; i < n; i++) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_EXPOSE; event.display.timestamp = al_current_time(); event.display.x = rects[i].left; event.display.y = rects[i].top; event.display.width = rects[i].right - rects[i].left; event.display.height = rects[i].bottom - rects[i].top; _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); _AL_FREE(rgndata); EndPaint(win_display->window, &ps); DeleteObject(hrgn); } return 0; } break; } case WM_SETCURSOR: switch (LOWORD(lParam)) { case HTLEFT: case HTRIGHT: SetCursor(LoadCursor(NULL, IDC_SIZEWE)); break; case HTBOTTOM: case HTTOP: SetCursor(LoadCursor(NULL, IDC_SIZENS)); break; case HTBOTTOMLEFT: case HTTOPRIGHT: SetCursor(LoadCursor(NULL, IDC_SIZENESW)); break; case HTBOTTOMRIGHT: case HTTOPLEFT: SetCursor(LoadCursor(NULL, IDC_SIZENWSE)); break; default: if (win_display->mouse_cursor_shown) { SetCursor(win_display->mouse_selected_hcursor); } else { SetCursor(NULL); } break; } return 1; case WM_ACTIVATE: if (LOWORD(wParam) != WA_INACTIVE) { /* This SetWindowPos is for faux-fullscreen windows that lost focus * so they can get placed back on top */ // FIXME: this doesn't seem to work //SetWindowPos(win_display->window, HWND_TOP, 0, 0, 0, 0, // SWP_NOMOVE | SWP_NOSIZE); if (d->vt->switch_in) d->vt->switch_in(d); _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_IN; event.display.timestamp = al_current_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); _al_win_grab_input(win_display); return 0; } else { if (d->flags & ALLEGRO_FULLSCREEN) { d->vt->switch_out(d); } _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_OUT; event.display.timestamp = al_current_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); return 0; } break; case WM_MENUCHAR : return (MNC_CLOSE << 16) | (wParam & 0xffff); case WM_CLOSE: _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_CLOSE; event.display.timestamp = al_current_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); return 0; case WM_SIZE: if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED || wParam == SIZE_MINIMIZED) { /* * Delay the resize event so we don't get bogged down with them */ if (!resize_postponed) { resize_postponed = true; postpone_resize(win_display->window); } } return 0; case WM_USER+0: /* Generate a resize event if the size has changed. We cannot asynchronously * change the display size here yet, since the user will only know about a * changed size after receiving the resize event. Here we merely add the * event to the queue. */ GetWindowInfo(win_display->window, &wi); x = wi.rcClient.left; y = wi.rcClient.top; w = wi.rcClient.right - wi.rcClient.left; h = wi.rcClient.bottom - wi.rcClient.top; if (d->w != w || d->h != h) { _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_RESIZE; event.display.timestamp = al_current_time(); event.display.x = x; event.display.y = y; event.display.width = w; event.display.height = h; _al_event_source_emit_event(es, &event); } /* Generate an expose event. */ if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_EXPOSE; event.display.timestamp = al_current_time(); event.display.x = x; event.display.y = y; event.display.width = w; event.display.height = h; _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); } resize_postponed = false; win_display->can_acknowledge = true; return 0; } return DefWindowProc(hWnd,message,wParam,lParam); }
void ShopState::update(Engine* engine){ ALLEGRO_EVENT events; al_wait_for_event(event_queue, &events); if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ engine->quit(); } if(events.type == ALLEGRO_EVENT_TIMER){ al_get_mouse_state(&mouseState); al_get_keyboard_state(&keyState); //Update Mouse Variables + mouseX = al_get_mouse_state_axis(&mouseState,0); mouseY = al_get_mouse_state_axis(&mouseState,1); if(al_mouse_button_down(&mouseState, 1)){ if(!mouseButtonLeft) mouseButtonLeftClick = true; else mouseButtonLeftClick = false; mouseButtonLeft = true; }else{ mouseButtonLeft = false; } //Update Mouse Variables - //Player Input + if(al_key_down(&keyState, ALLEGRO_KEY_W)){ playerDeltaY = -playerMovementSpeed; }else if(al_key_down(&keyState, ALLEGRO_KEY_S)){ playerDeltaY = playerMovementSpeed; }if(al_key_down(&keyState, ALLEGRO_KEY_A)){ playerDeltaX = -playerMovementSpeed; }else if(al_key_down(&keyState, ALLEGRO_KEY_D)){ playerDeltaX = playerMovementSpeed; } if(al_key_down(&keyState, ALLEGRO_KEY_ESCAPE)){ if(lastKeyPress != ALLEGRO_KEY_ESCAPE){ engine->popState(); lastKeyPress = ALLEGRO_KEY_ESCAPE; } } //Player Input - //Update Player Variables + if(mouseButtonLeft){ if(playerFiringSpeedHelper >= playerFiringSpeed){ playerFiringSpeedHelper = 0; int x, y, width, height, damage; float moveSpeed, angle = 0; width = 8, height = 8, x = playerCenterX - width/2, y = playerCenterY - height/2, damage = 1; moveSpeed = playerBulletSpeed; for(int i = 0; i < playerShots; i++){ //Shots Angle Calculation -- START if(playerShots != 1){ angle = (-atan2(playerCenterX - mouseX, playerCenterY - mouseY)) + (-playerBulletSpread/2+(playerBulletSpread/(playerShots-1))*i); }else{ angle = -atan2(playerCenterX - mouseX, playerCenterY - mouseY); } int spread = round(playerBulletSpread*100); angle += (-spread/2+rand()%spread)/100.0; //Shots Angle Calculation -- END Bullet *newBullet = new Bullet(); newBullet->setPos(x, y); newBullet->setDimensions(width, height); newBullet->setDamage(damage); newBullet->setAngleSpeed(moveSpeed, angle); newBullet->setPlayerShot(true); addBulletToList(newBullet); } } } if(playerX+playerDeltaX >= 0 && playerX+playerDeltaX + playerWidth < 400 && playerY >= 0 && playerY + playerHeight < 400){ playerX += playerDeltaX; } if(playerX >= 0 && playerX + playerWidth < 400 && playerY+playerDeltaY >= 0 && playerY+playerDeltaY + playerHeight < 400){ playerY += playerDeltaY; } playerCenterX = playerX + playerWidth/2, playerCenterY = playerY + playerHeight/2; playerDeltaX = 0; playerDeltaY = 0; //Update Player Variables - //Update Entities + for(int i = 0; i < MAX_BUTTONS; i++){ if(buttonList[i] != NULL && buttonList[i]->checkActive()){ buttonList[i]->update(); if(buttonList[i]->clicked){ switch(buttonList[i]->buttonId){ case 0: engine->popState(); break; case 1: engine->changeState(PlayState::instance()); break; case 2: engine->changeState(EditorState::instance()); break; case 3: engine->quit(); break; } } } } for(int i = 0; i < MAX_BULLETS; i++){ if(bulletList[i] != NULL && bulletList[i]->checkActive()){ bulletList[i]->update(); } } //Update Entities - //Rest + if(!al_key_down(&keyState, lastKeyPress)){ lastKeyPress = NULL; } playerFiringSpeedHelper++; //Rest - engine->draw(); } }
int main(){ srand(time(0)); Engine engine; fitnessFile << "Current Generation|Best Fitness|Average Fitness" << endl; //Initialize the Engine engine.init("Neural Network", screenWidth, screenHeight, false); if(!loadConfiguration()){ al_show_native_message_box(display, "File not found", "File not found", "Check if the file config.ini exist inside the config folder", NULL, ALLEGRO_MESSAGEBOX_ERROR); engine.quit(); } //Load the Menu engine.changeState(PlayState::instance()); //Timestep Variables double t = 0.0; double dt = 1/logicSpeed; double currentTime = 0.0; double newTime = 0.0; double frameTime = 0.0; double accumulator = 0.0; //Main Loop while(engine.running()){ ALLEGRO_EVENT events; al_wait_for_event(event_queue, &events); timerEvent = false; if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ engine.quit(); } if(events.type == ALLEGRO_EVENT_TIMER){ timerEvent = true; } //Main Timer Event + if(timerEvent){ al_get_mouse_state(&mouseState); al_get_keyboard_state(&keyState); //Update Mouse Variables + mouseX = al_get_mouse_state_axis(&mouseState,0); mouseY = al_get_mouse_state_axis(&mouseState,1); mouseWheel = al_get_mouse_state_axis(&mouseState, 2); if(al_mouse_button_down(&mouseState, 1)){ mouseButtonLeft = true; mouseButtonLeftClick = false; }else if(mouseButtonLeft){ mouseButtonLeftClick = true; mouseButtonLeft = false; }else{ mouseButtonLeftClick = false; } if(al_mouse_button_down(&mouseState, 2)){ mouseButtonRight = true; mouseButtonRightClick = false; }else if(mouseButtonRight){ mouseButtonRightClick = true; mouseButtonRight = false; }else{ mouseButtonRightClick = false; } //Update Mouse Variables - //Rest + if(!al_key_down(&keyState, lastKeyPress)){ lastKeyPress = 0; } //Rest - if(events.timer.source == timer){ newTime = al_get_time(); frameTime = newTime - currentTime; if(frameTime > 0.25) frameTime = 0.25; // note: max frame time to avoid spiral of death currentTime = newTime; accumulator += frameTime; while(accumulator >= dt){ engine.update(); //Call the gameState specfic update dt = 1/logicSpeed; t += dt; accumulator -= dt; } engine.draw(); al_draw_bitmap(cursorImage, mouseX-4, mouseY-4, 0); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } } //Main Timer Event - } // cleanup the engine engine.cleanup(); fitnessFile.close(); }
bool MouseHandler :: isButtonDown(int btn) { al_get_mouse_state (&state); return (al_mouse_button_down (&state, btn)); }
void InputManager::update() { al_get_mouse_state(mpMouseState); al_get_keyboard_state(mpKeyboardState); for (int i = 0; i < (int)NUM_KEYS; i++) { mPreviousKeys[i] = mCurrentKeys[i]; } for (int i = 0; i < (int)NUM_BUTTONS; i++) { mPreviousMouseButtons[i] = mCurrentMouseButtons[i]; } mCurrentKeys[(int)F1_KEY] = al_key_down(mpKeyboardState, ALLEGRO_KEY_F1); mCurrentKeys[(int)H_KEY] = al_key_down(mpKeyboardState, ALLEGRO_KEY_H); mCurrentKeys[(int)A_KEY] = al_key_down(mpKeyboardState, ALLEGRO_KEY_A); mCurrentKeys[(int)C_KEY] = al_key_down(mpKeyboardState, ALLEGRO_KEY_C); mCurrentMouseButtons[(int)LEFT_CLICK] = al_mouse_button_down(mpMouseState, 1); mCurrentMouseButtons[(int)RIGHT_CLICK] = al_mouse_button_down(mpMouseState, 2); mCursorLocation.setX(mpMouseState->x); mCursorLocation.setY(mpMouseState->y); for (int i = 0; i < (int)NUM_KEYS; i++) { mReleasedKeys[i] = !mCurrentKeys[i] && mPreviousKeys[i]; } if ((mReleasedKeys[(int)C_KEY])) { GameMessage* pMessage = new DrawVisitedMessage(); dynamic_cast<GameApp*>(gpGame)->getMessageManager()->addMessage(pMessage, 0); } if ((mReleasedKeys[(int)H_KEY]) || (mReleasedKeys[(int)F1_KEY])) { GameMessage* pMessage = new ToggleHelpMessage(); dynamic_cast<GameApp*>(gpGame)->getMessageManager()->addMessage(pMessage, 0); } static Vector2D lastLeftPos(0.0f, 0.0f); static Vector2D lastRightPos(0.0f, 0.0f); if ((!mCurrentMouseButtons[(int)LEFT_CLICK] && mPreviousMouseButtons[(int)LEFT_CLICK]) )//|| { //checkForPortal; Vector2D pos(mpMouseState->x, mpMouseState->y); //if (lastLeftPos.getX() != pos.getX() || lastLeftPos.getY() != lastLeftPos.getY()) //{ // GameMessage* pMessage = new PathToMessage(pos, lastRightPos); // dynamic_cast<GameApp*>(gpGame)->getMessageManager()->addMessage(pMessage, 0); // lastLeftPos = pos; //} GameMessage* pMessage = new ChangeMapMessage(mpMouseState->x, mpMouseState->y, true); dynamic_cast<GameApp*>(gpGame)->getMessageManager()->addMessage(pMessage, 0); } if (!mCurrentMouseButtons[(int)RIGHT_CLICK] && mPreviousMouseButtons[(int)RIGHT_CLICK]) { GameMessage* pMessage = new ChangeMapMessage(mpMouseState->x, mpMouseState->y, false); dynamic_cast<GameApp*>(gpGame)->getMessageManager()->addMessage(pMessage, 0); //Vector2D pos(mpMouseState->x, mpMouseState->y); //if (lastRightPos.getX() != pos.getX() || lastRightPos.getY() != pos.getY()) //{ // GameMessage* pMessage = new PathToMessage(lastLeftPos, pos); // dynamic_cast<GameApp*>(gpGame)->getMessageManager()->addMessage(pMessage, 0); // lastRightPos = pos; //} } }
int main(int argc, const char* argv[]) { al_init(); al_init_font_addon(); al_init_ttf_addon(); al_init_image_addon(); al_init_primitives_addon(); ALLEGRO_DISPLAY *display; al_set_new_display_flags(ALLEGRO_WINDOWED); display = al_create_display(800, 600); al_install_keyboard(); al_install_mouse(); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)display); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); ALLEGRO_FONT* font = al_load_ttf_font("data/times.ttf", 12, 0); Vector2 camera(-400, -300); Platforms platforms; Bitmaps bitmaps; Sprites sprites; File* file = new File(font); file->platforms = &platforms; file->bitmaps = &bitmaps; file->sprites = &sprites; Menu menu; menu.Add_entry(new Create_platform(platforms, camera, font)); menu.Add_entry(new Edit_platform(platforms, camera, font)); menu.Add_entry(file); menu.Add_entry(new Create_sprite(bitmaps, sprites, camera, font)); menu.Add_entry(new Edit_sprite(sprites, camera, font)); if(argc==2) file->Load(argv[1]); while(1) { ALLEGRO_EVENT event; if (al_get_next_event(event_queue, &event)) { if (ALLEGRO_EVENT_DISPLAY_CLOSE == event.type || ALLEGRO_EVENT_KEY_DOWN == event.type && ALLEGRO_KEY_ESCAPE == event.keyboard.keycode) { break; } if(ALLEGRO_EVENT_MOUSE_AXES) { ALLEGRO_MOUSE_STATE mstate; al_get_mouse_state(&mstate); if(al_mouse_button_down(&mstate, 3)) { camera.x -= event.mouse.dx; camera.y -= event.mouse.dy; } } menu.Event(event); } for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) { (*i)->Draw(camera); } for(Platforms::iterator i = platforms.begin(); i != platforms.end(); ++i) { (*i)->Draw(camera, al_map_rgb_f(0, 1, 0)); } al_draw_line(0, -camera.y, 800, -camera.y, al_map_rgba_f(0, 1, 0, 0.5), 0); al_draw_line(-camera.x, 0, -camera.x, 600, al_map_rgba_f(0, 0, 1, 0.5), 0); menu.Draw(); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); al_rest(0.001); } al_destroy_event_queue(event_queue); al_destroy_display(display); }