diff --git a/Engine.cpp b/Engine.cpp index 878683e..cdddaa0 100644 --- a/Engine.cpp +++ b/Engine.cpp @@ -33,6 +33,9 @@ static XClassHint * classhint = NULL; static XWMHints * wmhints = NULL; static XSizeHints * sizehints = NULL; static char title[] = "game"; +static int mouse_x = 0; +static int mouse_y = 0; +static bool mouse_btn_down[5] = { 0 }; static void term_sig_handler(int) { @@ -46,6 +49,21 @@ bool is_key_pressed(int button_vk_code) return keys[button_vk_code]; } +bool is_mouse_button_pressed(int mouse_button) +{ + return mouse_btn_down[mouse_button]; +} + +int get_cursor_x() +{ + return mouse_x; +} + +int get_cursor_y() +{ + return mouse_y; +} + static void on_key_event(XKeyEvent & event, bool pressed) { const int buf_size = 256; @@ -121,7 +139,7 @@ int main(int, const char **) pixmap = XCreatePixmap(display, window, SCREEN_WIDTH, SCREEN_HEIGHT, 24); - XSelectInput(display, window, ExposureMask | KeyPressMask | KeyReleaseMask); + XSelectInput(display, window, ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask); XMapWindow(display, window); wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", false); XSetWMProtocols(display, window, &wmDeleteMessage, 1); @@ -151,8 +169,25 @@ int main(int, const char **) if (event.type == KeyRelease) on_key_event(event.xkey, false); + if (event.type == ButtonPress && event.xbutton.button > 0 && event.xbutton.button <= 5) + mouse_btn_down[event.xbutton.button - 1] = true; + + if (event.type == ButtonRelease && event.xbutton.button > 0 && event.xbutton.button <= 5) + mouse_btn_down[event.xbutton.button - 1] = false; + if (event.type == ClientMessage && event.xclient.data.l[0] == (int)wmDeleteMessage) quit = true; + + Window root_return, child_return; + int root_x_return, root_y_return; + int win_x_return, win_y_return; + unsigned int mask_return; + if (XQueryPointer(display, window, &root_return, &child_return, &root_x_return, &root_y_return, + &win_x_return, &win_y_return, &mask_return)) + { + mouse_x = win_x_return; + mouse_y = win_y_return; + } } uint64_t curTime = get_nsec(); diff --git a/Engine.h b/Engine.h index 0396c8a..5f3b51d 100644 --- a/Engine.h +++ b/Engine.h @@ -27,6 +27,9 @@ enum // VK_SPACE, VK_RIGHT, VK_LEFT, VK_UP, VK_DOWN, etc. bool is_key_pressed(int button_vk_code); +bool is_mouse_button_pressed(int mouse_button); +int get_cursor_x(); +int get_cursor_y(); void initialize(); void finalize(); diff --git a/Game.cpp b/Game.cpp index 8cbbd92..589b305 100644 --- a/Game.cpp +++ b/Game.cpp @@ -11,10 +11,11 @@ // is_key_pressed(int button_vk_code) - check if a key is pressed, // use keycodes (VK_SPACE, VK_RIGHT, VK_LEFT, VK_UP, VK_DOWN, VK_RETURN) // +// get_cursor_x(), get_cursor_y() - get mouse cursor position +// is_mouse_button_pressed(int button) - check if mouse button is pressed (0 - left button, 1 - right button) // schedule_quit_game() - quit game after act() - // initialize game data in this function void initialize() {