mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-16 03:18:43 +00:00
support mouse cursor
This commit is contained in:
parent
f86c1a22ac
commit
a1430a42aa
3 changed files with 41 additions and 2 deletions
37
Engine.cpp
37
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();
|
||||
|
|
|
|||
3
Engine.h
3
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();
|
||||
|
|
|
|||
3
Game.cpp
3
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()
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue