2012-06-06 00:04:15 +00:00
|
|
|
//========================================================================
|
|
|
|
// Joystick input test
|
2016-11-21 15:23:59 +00:00
|
|
|
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
|
2012-06-06 00:04:15 +00:00
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would
|
|
|
|
// be appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not
|
|
|
|
// be misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
//
|
|
|
|
// This test displays the state of every button and axis of every connected
|
|
|
|
// joystick and/or gamepad
|
|
|
|
//
|
|
|
|
//========================================================================
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2015-10-14 01:11:20 +00:00
|
|
|
#include <glad/glad.h>
|
2013-05-22 20:46:34 +00:00
|
|
|
#include <GLFW/glfw3.h>
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2016-09-26 22:50:32 +00:00
|
|
|
#define NK_IMPLEMENTATION
|
|
|
|
#define NK_INCLUDE_FIXED_TYPES
|
|
|
|
#define NK_INCLUDE_FONT_BAKING
|
|
|
|
#define NK_INCLUDE_DEFAULT_FONT
|
|
|
|
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
|
|
|
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
|
|
|
|
#define NK_INCLUDE_STANDARD_VARARGS
|
2016-10-06 17:08:35 +00:00
|
|
|
#define NK_BUTTON_TRIGGER_ON_RELEASE
|
2016-09-26 22:50:32 +00:00
|
|
|
#include <nuklear.h>
|
|
|
|
|
|
|
|
#define NK_GLFW_GL2_IMPLEMENTATION
|
|
|
|
#include <nuklear_glfw_gl2.h>
|
|
|
|
|
2016-10-06 17:07:28 +00:00
|
|
|
#include <stdio.h>
|
2012-06-06 00:04:15 +00:00
|
|
|
#include <string.h>
|
2010-09-11 12:32:05 +00:00
|
|
|
#include <stdlib.h>
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-01-06 20:02:57 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define strdup(x) _strdup(x)
|
|
|
|
#endif
|
|
|
|
|
2017-05-11 16:29:08 +00:00
|
|
|
static GLFWwindow* window;
|
2015-12-13 16:38:50 +00:00
|
|
|
static int joysticks[GLFW_JOYSTICK_LAST + 1];
|
2012-06-06 00:04:15 +00:00
|
|
|
static int joystick_count = 0;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-12-30 00:42:14 +00:00
|
|
|
static void error_callback(int error, const char* description)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: %s\n", description);
|
|
|
|
}
|
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
static void joystick_callback(int jid, int event)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2015-12-13 16:38:50 +00:00
|
|
|
if (event == GLFW_CONNECTED)
|
2016-10-10 01:24:07 +00:00
|
|
|
joysticks[joystick_count++] = jid;
|
2015-12-13 16:38:50 +00:00
|
|
|
else if (event == GLFW_DISCONNECTED)
|
|
|
|
{
|
|
|
|
int i;
|
2012-06-06 00:04:15 +00:00
|
|
|
|
2015-12-13 16:38:50 +00:00
|
|
|
for (i = 0; i < joystick_count; i++)
|
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
if (joysticks[i] == jid)
|
2015-12-13 16:38:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-06-06 00:04:15 +00:00
|
|
|
|
2015-12-13 16:38:50 +00:00
|
|
|
for (i = i + 1; i < joystick_count; i++)
|
|
|
|
joysticks[i - 1] = joysticks[i];
|
2012-06-06 00:04:15 +00:00
|
|
|
|
2015-12-13 16:38:50 +00:00
|
|
|
joystick_count--;
|
|
|
|
}
|
2017-05-11 16:29:08 +00:00
|
|
|
|
|
|
|
if (!glfwGetWindowAttrib(window, GLFW_FOCUSED))
|
|
|
|
glfwRequestWindowAttention(window);
|
2015-12-13 16:38:50 +00:00
|
|
|
}
|
2012-09-06 23:01:34 +00:00
|
|
|
|
2016-10-06 17:08:35 +00:00
|
|
|
static const char* joystick_label(int jid)
|
|
|
|
{
|
|
|
|
static char label[1024];
|
|
|
|
snprintf(label, sizeof(label), "%i: %s", jid + 1, glfwGetJoystickName(jid));
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2010-09-07 15:34:51 +00:00
|
|
|
int main(void)
|
|
|
|
{
|
2017-03-06 17:23:06 +00:00
|
|
|
int jid, hat_buttons = GLFW_FALSE;
|
2016-09-26 22:50:32 +00:00
|
|
|
struct nk_context* nk;
|
|
|
|
struct nk_font_atlas* atlas;
|
2012-06-06 00:04:15 +00:00
|
|
|
|
|
|
|
memset(joysticks, 0, sizeof(joysticks));
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-12-30 00:42:14 +00:00
|
|
|
glfwSetErrorCallback(error_callback);
|
|
|
|
|
2010-09-11 12:32:05 +00:00
|
|
|
if (!glfwInit())
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
for (jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2016-09-26 22:50:32 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
if (glfwJoystickPresent(jid))
|
|
|
|
joystick_callback(jid, GLFW_CONNECTED);
|
2016-09-26 22:50:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 16:38:50 +00:00
|
|
|
glfwSetJoystickCallback(joystick_callback);
|
|
|
|
|
2012-11-27 15:55:04 +00:00
|
|
|
window = glfwCreateWindow(640, 480, "Joystick Test", NULL, NULL);
|
2012-06-06 00:04:15 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-10 13:29:45 +00:00
|
|
|
glfwMakeContextCurrent(window);
|
2015-10-14 01:11:20 +00:00
|
|
|
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
|
2012-06-06 00:04:15 +00:00
|
|
|
glfwSwapInterval(1);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2016-09-26 22:50:32 +00:00
|
|
|
nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
|
|
|
|
nk_glfw3_font_stash_begin(&atlas);
|
|
|
|
nk_glfw3_font_stash_end();
|
|
|
|
|
2013-03-01 12:45:12 +00:00
|
|
|
while (!glfwWindowShouldClose(window))
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2016-10-06 17:08:35 +00:00
|
|
|
int i, width, height;
|
2016-09-26 22:50:32 +00:00
|
|
|
|
2016-10-06 17:08:35 +00:00
|
|
|
glfwGetWindowSize(window, &width, &height);
|
|
|
|
|
2012-06-06 00:04:15 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2016-09-26 22:50:32 +00:00
|
|
|
nk_glfw3_new_frame();
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2016-11-27 12:29:07 +00:00
|
|
|
if (nk_begin(nk,
|
2016-10-06 17:08:35 +00:00
|
|
|
"Joysticks",
|
2017-02-07 18:12:22 +00:00
|
|
|
nk_rect(width - 200.f, 0.f, 200.f, (float) height),
|
2016-10-06 17:08:35 +00:00
|
|
|
NK_WINDOW_MINIMIZABLE |
|
|
|
|
NK_WINDOW_TITLE))
|
2016-09-26 22:50:32 +00:00
|
|
|
{
|
2016-10-06 17:08:35 +00:00
|
|
|
nk_layout_row_dynamic(nk, 30, 1);
|
|
|
|
|
2017-03-06 17:23:06 +00:00
|
|
|
nk_checkbox_label(nk, "Hat buttons", &hat_buttons);
|
2017-03-01 22:27:20 +00:00
|
|
|
|
2016-10-06 17:08:35 +00:00
|
|
|
if (joystick_count)
|
|
|
|
{
|
|
|
|
for (i = 0; i < joystick_count; i++)
|
|
|
|
{
|
|
|
|
if (nk_button_label(nk, joystick_label(joysticks[i])))
|
|
|
|
nk_window_set_focus(nk, joystick_label(joysticks[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nk_label(nk, "No joysticks connected", NK_TEXT_LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
nk_end(nk);
|
2016-09-26 22:50:32 +00:00
|
|
|
|
2016-10-06 17:08:35 +00:00
|
|
|
for (i = 0; i < joystick_count; i++)
|
|
|
|
{
|
2016-11-27 12:29:07 +00:00
|
|
|
if (nk_begin(nk,
|
2016-10-06 17:08:35 +00:00
|
|
|
joystick_label(joysticks[i]),
|
2016-09-26 22:50:32 +00:00
|
|
|
nk_rect(i * 20.f, i * 20.f, 400.f, 400.f),
|
|
|
|
NK_WINDOW_BORDER |
|
|
|
|
NK_WINDOW_MOVABLE |
|
|
|
|
NK_WINDOW_SCALABLE |
|
|
|
|
NK_WINDOW_MINIMIZABLE |
|
|
|
|
NK_WINDOW_TITLE))
|
|
|
|
{
|
2017-03-01 22:27:20 +00:00
|
|
|
int j, axis_count, button_count, hat_count;
|
2016-09-26 22:50:32 +00:00
|
|
|
const float* axes;
|
|
|
|
const unsigned char* buttons;
|
2017-03-01 22:27:20 +00:00
|
|
|
const unsigned char* hats;
|
2016-09-26 22:50:32 +00:00
|
|
|
|
|
|
|
nk_layout_row_dynamic(nk, 30, 1);
|
|
|
|
|
|
|
|
axes = glfwGetJoystickAxes(joysticks[i], &axis_count);
|
2017-03-06 17:23:06 +00:00
|
|
|
buttons = glfwGetJoystickButtons(joysticks[i], &button_count);
|
|
|
|
hats = glfwGetJoystickHats(joysticks[i], &hat_count);
|
|
|
|
|
|
|
|
if (!hat_buttons)
|
|
|
|
button_count -= hat_count * 4;
|
|
|
|
|
2017-03-01 22:27:20 +00:00
|
|
|
for (j = 0; j < axis_count; j++)
|
|
|
|
nk_slide_float(nk, -1.f, axes[j], 1.f, 0.1f);
|
|
|
|
|
|
|
|
nk_layout_row_dynamic(nk, 30, 8);
|
|
|
|
|
|
|
|
for (j = 0; j < button_count; j++)
|
2016-09-26 22:50:32 +00:00
|
|
|
{
|
2017-03-01 22:27:20 +00:00
|
|
|
char name[16];
|
|
|
|
snprintf(name, sizeof(name), "%i", j + 1);
|
|
|
|
nk_select_label(nk, name, NK_TEXT_CENTERED, buttons[j]);
|
2016-09-26 22:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nk_layout_row_dynamic(nk, 30, 8);
|
|
|
|
|
2017-03-01 22:27:20 +00:00
|
|
|
for (j = 0; j < hat_count; j++)
|
2016-09-26 22:50:32 +00:00
|
|
|
{
|
2017-03-01 22:27:20 +00:00
|
|
|
float radius;
|
|
|
|
struct nk_rect area;
|
|
|
|
struct nk_vec2 center;
|
|
|
|
|
|
|
|
if (nk_widget(&area, nk) != NK_WIDGET_VALID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
center = nk_vec2(area.x + area.w / 2.f,
|
|
|
|
area.y + area.h / 2.f);
|
|
|
|
radius = NK_MIN(area.w, area.h) / 2.f;
|
|
|
|
|
|
|
|
nk_stroke_circle(nk_window_get_canvas(nk),
|
|
|
|
nk_rect(center.x - radius,
|
|
|
|
center.y - radius,
|
|
|
|
radius * 2.f,
|
|
|
|
radius * 2.f),
|
|
|
|
1.f,
|
|
|
|
nk_rgb(175, 175, 175));
|
|
|
|
|
|
|
|
if (hats[j])
|
2016-09-26 22:50:32 +00:00
|
|
|
{
|
2017-03-01 22:27:20 +00:00
|
|
|
const float angles[] =
|
|
|
|
{
|
|
|
|
0.f, 0.f,
|
|
|
|
NK_PI * 1.5f, NK_PI * 1.75f,
|
|
|
|
NK_PI, 0.f,
|
|
|
|
NK_PI * 1.25f, 0.f,
|
|
|
|
NK_PI * 0.5f, NK_PI * 0.25f,
|
|
|
|
0.f, 0.f,
|
|
|
|
NK_PI * 0.75f, 0.f,
|
|
|
|
};
|
|
|
|
const float cosa = nk_cos(angles[hats[j]]);
|
|
|
|
const float sina = nk_sin(angles[hats[j]]);
|
|
|
|
const struct nk_vec2 p0 = nk_vec2(0.f, -radius);
|
|
|
|
const struct nk_vec2 p1 = nk_vec2( radius / 2.f, -radius / 3.f);
|
|
|
|
const struct nk_vec2 p2 = nk_vec2(-radius / 2.f, -radius / 3.f);
|
|
|
|
|
|
|
|
nk_fill_triangle(nk_window_get_canvas(nk),
|
|
|
|
center.x + cosa * p0.x + sina * p0.y,
|
|
|
|
center.y + cosa * p0.y - sina * p0.x,
|
|
|
|
center.x + cosa * p1.x + sina * p1.y,
|
|
|
|
center.y + cosa * p1.y - sina * p1.x,
|
|
|
|
center.x + cosa * p2.x + sina * p2.y,
|
|
|
|
center.y + cosa * p2.y - sina * p2.x,
|
|
|
|
nk_rgb(175, 175, 175));
|
2016-09-26 22:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nk_end(nk);
|
|
|
|
}
|
|
|
|
|
|
|
|
nk_glfw3_render(NK_ANTI_ALIASING_ON, 10000, 1000);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-06 16:13:37 +00:00
|
|
|
glfwSwapBuffers(window);
|
2012-06-06 00:04:15 +00:00
|
|
|
glfwPollEvents();
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glfwTerminate();
|
2012-06-06 00:04:15 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|