1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/*
* Example program for the Allegro library, by Peter Wang.
*
* This program tests joystick events.
*/
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_primitives.h>
#include "common.c"
#define MAX_AXES 3
#define MAX_STICKS 16
#define MAX_BUTTONS 32
/* globals */
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_FONT *font;
ALLEGRO_COLOR black;
ALLEGRO_COLOR grey;
ALLEGRO_COLOR white;
int num_sticks = 0;
int num_buttons = 0;
int num_axes[MAX_STICKS] = { 0 };
float joys[MAX_STICKS][MAX_AXES] = {{ 0 }};
bool joys_buttons[MAX_BUTTONS] = { 0 };
static void setup_joystick_values(ALLEGRO_JOYSTICK *joy)
{
ALLEGRO_JOYSTICK_STATE jst;
int i, j;
if (joy == NULL) {
num_sticks = 0;
num_buttons = 0;
return;
}
al_get_joystick_state(joy, &jst);
num_sticks = al_get_joystick_num_sticks(joy);
if (num_sticks > MAX_STICKS)
num_sticks = MAX_STICKS;
for (i = 0; i < num_sticks; i++) {
num_axes[i] = al_get_joystick_num_axes(joy, i);
for (j = 0; j < num_axes[i]; ++j)
joys[i][j] = jst.stick[i].axis[j];
}
num_buttons = al_get_joystick_num_buttons(joy);
if (num_buttons > MAX_BUTTONS) {
num_buttons = MAX_BUTTONS;
}
for (i = 0; i < num_buttons; i++) {
joys_buttons[i] = (jst.button[i] >= 16384);
}
}
static void draw_joystick_axes(ALLEGRO_JOYSTICK *joy, int cx, int cy, int stick)
{
const int size = 30;
const int csize = 5;
const int osize = size + csize;
int zx = cx + osize + csize * 2;
int x = cx + joys[stick][0] * size;
int y = cy + joys[stick][1] * size;
int z = cy + joys[stick][2] * size;
int i;
al_draw_filled_rectangle(cx-osize, cy-osize, cx+osize, cy+osize, grey);
al_draw_rectangle(cx-osize+0.5, cy-osize+0.5, cx+osize-0.5, cy+osize-0.5, black, 0);
al_draw_filled_rectangle(x-5, y-5, x+5, y+5, black);
if (num_axes[stick] >= 3) {
al_draw_filled_rectangle(zx-csize, cy-osize, zx+csize, cy+osize, grey);
al_draw_rectangle(zx-csize+0.5f, cy-osize+0.5f, zx+csize-0.5f, cy+osize-0.5f, black, 0);
al_draw_filled_rectangle(zx-5, z-5, zx+5, z+5, black);
}
if (joy) {
al_draw_text(font, black, cx, cy + osize + 1, ALLEGRO_ALIGN_CENTRE,
al_get_joystick_stick_name(joy, stick));
for (i = 0; i < num_axes[stick]; i++) {
al_draw_text(font, black, cx, cy + osize + (1 + i) * 10,
ALLEGRO_ALIGN_CENTRE,
al_get_joystick_axis_name(joy, stick, i));
}
}
}
static void draw_joystick_button(ALLEGRO_JOYSTICK *joy, int button, bool down)
{
ALLEGRO_BITMAP *bmp = al_get_target_bitmap();
int x = al_get_bitmap_width(bmp)/2-120 + (button % 8) * 30;
int y = al_get_bitmap_height(bmp)-120 + (button / 8) * 30;
ALLEGRO_COLOR fg;
al_draw_filled_rectangle(x, y, x + 25, y + 25, grey);
al_draw_rectangle(x+0.5, y+0.5, x + 24.5, y + 24.5, black, 0);
if (down) {
al_draw_filled_rectangle(x + 2, y + 2, x + 23, y + 23, black);
fg = white;
}
else {
fg = black;
}
if (joy) {
const char *name = al_get_joystick_button_name(joy, button);
if (strlen(name) < 4) {
al_draw_text(font, fg, x + 13, y + 8, ALLEGRO_ALIGN_CENTRE, name);
}
}
}
static void draw_all(ALLEGRO_JOYSTICK *joy)
{
ALLEGRO_BITMAP *bmp = al_get_target_bitmap();
int width = al_get_bitmap_width(bmp);
int height = al_get_bitmap_height(bmp);
int i;
al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0));
if (joy) {
al_draw_textf(font, black, width / 2, 10, ALLEGRO_ALIGN_CENTRE,
"Joystick: %s", al_get_joystick_name(joy));
}
for (i = 0; i < num_sticks; i++) {
int u = i%4;
int v = i/4;
int cx = (u + 0.5) * width/4;
int cy = (v + 0.5) * height/6;
draw_joystick_axes(joy, cx, cy, i);
}
for (i = 0; i < num_buttons; i++) {
draw_joystick_button(joy, i, joys_buttons[i]);
}
al_flip_display();
}
static void main_loop(void)
{
ALLEGRO_EVENT event;
while (true) {
if (al_is_event_queue_empty(event_queue))
draw_all(al_get_joystick(0));
al_wait_for_event(event_queue, &event);
switch (event.type) {
/* ALLEGRO_EVENT_JOYSTICK_AXIS - a joystick axis value changed.
*/
case ALLEGRO_EVENT_JOYSTICK_AXIS:
if (event.joystick.stick < MAX_STICKS && event.joystick.axis < MAX_AXES) {
joys[event.joystick.stick][event.joystick.axis] = event.joystick.pos;
}
break;
/* ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN - a joystick button was pressed.
*/
case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
joys_buttons[event.joystick.button] = true;
break;
/* ALLEGRO_EVENT_JOYSTICK_BUTTON_UP - a joystick button was released.
*/
case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
joys_buttons[event.joystick.button] = false;
break;
case ALLEGRO_EVENT_KEY_DOWN:
if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
return;
break;
/* ALLEGRO_EVENT_DISPLAY_CLOSE - the window close button was pressed.
*/
case ALLEGRO_EVENT_DISPLAY_CLOSE:
return;
case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
al_reconfigure_joysticks();
setup_joystick_values(al_get_joystick(0));
break;
/* We received an event of some type we don't know about.
* Just ignore it.
*/
default:
break;
}
}
}
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display;
(void)argc;
(void)argv;
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
al_init_primitives_addon();
al_init_font_addon();
display = al_create_display(1024, 768);
if (!display) {
abort_example("al_create_display failed\n");
}
al_install_keyboard();
black = al_map_rgb(0, 0, 0);
grey = al_map_rgb(0xe0, 0xe0, 0xe0);
white = al_map_rgb(255, 255, 255);
font = al_create_builtin_font();
al_install_joystick();
event_queue = al_create_event_queue();
if (!event_queue) {
abort_example("al_create_event_queue failed\n");
}
if (al_get_keyboard_event_source()) {
al_register_event_source(event_queue, al_get_keyboard_event_source());
}
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_joystick_event_source());
setup_joystick_values(al_get_joystick(0));
main_loop();
al_destroy_font(font);
return 0;
}
/* vim: set ts=8 sts=3 sw=3 et: */
|