Include and link physfs properly.
[tuxanci.git] / src / client / mouse_buffer.c
blobf301b28422be9cae0c86912b1e552ecb444bb795
1 #include <SDL.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <assert.h>
7 #include "main.h"
8 #include "list.h"
9 #include "mouse_buffer.h"
11 static list_t *list_event;
13 typedef struct mouse_event_struct {
14 Uint8 button;
15 int x;
16 int y;
17 } mouse_event_t;
19 static mouse_event_t *mouse_event_new(int x, int y, Uint8 button)
21 mouse_event_t *mouse_event;
23 mouse_event = malloc(sizeof(mouse_event_t));
24 mouse_event->x = x;
25 mouse_event->y = y;
26 mouse_event->button = button;
28 return mouse_event;
31 static void mouse_event_destroy(mouse_event_t *mouse_event)
33 assert(mouse_event != NULL);
34 free(mouse_event);
37 int mouse_buffer_init()
39 list_event = list_new();
41 return 0;
44 int mouse_buffer_event(SDL_MouseButtonEvent *button)
46 assert(button != NULL);
47 assert(list_event != NULL);
49 switch (button->button) {
50 case SDL_BUTTON_LEFT:
51 case SDL_BUTTON_RIGHT:
52 /*printf("mouse button - pos(%d,%d)\n", button->x, button->y);*/
54 list_add(list_event, mouse_event_new(button->x, button->y, button->button));
56 return 1;
57 break;
59 default:
60 break;
63 return 0;
66 int mouse_buffer_clean()
68 assert(list_event != NULL);
70 list_destroy_item(list_event, mouse_event_destroy);
71 list_event = list_new();
73 return 0;
76 bool_t mouse_buffer_is_on_area(int x, int y, int w, int h, unsigned int flag)
78 mouse_event_t *mouse_event;
79 int i;
81 assert(list_event != NULL);
83 if (flag & MOUSE_BUF_MOTION) {
84 int mouse_x, mouse_y;
86 SDL_GetMouseState(&mouse_x, &mouse_y);
88 if (mouse_x >= x && mouse_y >= y &&
89 mouse_x < x+w && mouse_y < y+h) {
90 return TRUE;
92 } else {
93 for (i = 0; i < list_event->count; i++) {
94 mouse_event = (mouse_event_t *) list_event->list[i];
96 if ((mouse_event->x >= x && mouse_event->y >= y &&
97 mouse_event->x < x + w && mouse_event->y < y + h) ||
98 (flag & MOUSE_BUF_AREA_NONE)) {
99 if (flag & MOUSE_BUF_CLICK_LEFT) {
100 if (mouse_event->button == SDL_BUTTON_LEFT) {
101 return TRUE;
102 } else {
103 return FALSE;
107 if (flag & MOUSE_BUF_CLICK_RIGHT) {
108 if (mouse_event->button == SDL_BUTTON_RIGHT) {
109 return TRUE;
110 } else {
111 return FALSE;
115 if (flag & MOUSE_BUF_CLICK) {
116 if (mouse_event->button == SDL_BUTTON_LEFT ||
117 mouse_event->button == SDL_BUTTON_RIGHT) {
118 return TRUE;
119 } else {
120 return FALSE;
124 return TRUE;
129 return FALSE;
132 int mouse_buffer_quit()
134 assert(list_event != NULL);
135 list_destroy_item(list_event, mouse_event_destroy);
137 return 0;