1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-23 10:48:51 -05:00
glfw/tests/joysticks.c

184 lines
5.1 KiB
C
Raw Normal View History

2012-06-05 20:04:15 -04:00
//========================================================================
// Joystick input test
2016-06-01 10:17:06 -04:00
// Copyright (c) Camilla Berglund <elmindreda@glfw.org>
2012-06-05 20:04:15 -04: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 11:34:51 -04:00
2015-10-13 21:11:20 -04:00
#include <glad/glad.h>
#include <GLFW/glfw3.h>
2010-09-07 11:34:51 -04:00
2016-09-26 18:50:32 -04: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
#include <nuklear.h>
#define NK_GLFW_GL2_IMPLEMENTATION
#include <nuklear_glfw_gl2.h>
2012-06-05 20:04:15 -04:00
#include <string.h>
#include <stdlib.h>
2010-09-07 11:34:51 -04:00
2013-01-06 15:02:57 -05:00
#ifdef _MSC_VER
#define strdup(x) _strdup(x)
#endif
2015-12-13 11:38:50 -05:00
static int joysticks[GLFW_JOYSTICK_LAST + 1];
2012-06-05 20:04:15 -04:00
static int joystick_count = 0;
2010-09-07 11:34:51 -04:00
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
2015-12-13 11:38:50 -05:00
static void joystick_callback(int joy, int event)
2010-09-07 11:34:51 -04:00
{
2015-12-13 11:38:50 -05:00
if (event == GLFW_CONNECTED)
joysticks[joystick_count++] = joy;
else if (event == GLFW_DISCONNECTED)
{
int i;
2012-06-05 20:04:15 -04:00
2015-12-13 11:38:50 -05:00
for (i = 0; i < joystick_count; i++)
{
if (joysticks[i] == joy)
break;
}
2012-06-05 20:04:15 -04:00
2015-12-13 11:38:50 -05:00
for (i = i + 1; i < joystick_count; i++)
joysticks[i - 1] = joysticks[i];
2012-06-05 20:04:15 -04:00
2015-12-13 11:38:50 -05:00
joystick_count--;
}
}
2010-09-07 11:34:51 -04:00
int main(void)
{
2016-09-26 18:50:32 -04:00
int joy;
GLFWwindow* window;
2016-09-26 18:50:32 -04:00
struct nk_context* nk;
struct nk_font_atlas* atlas;
2012-06-05 20:04:15 -04:00
memset(joysticks, 0, sizeof(joysticks));
2010-09-07 11:34:51 -04:00
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
2016-09-26 18:50:32 -04:00
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{
if (glfwJoystickPresent(joy))
joystick_callback(joy, GLFW_CONNECTED);
}
2015-12-13 11:38:50 -05:00
glfwSetJoystickCallback(joystick_callback);
window = glfwCreateWindow(640, 480, "Joystick Test", NULL, NULL);
2012-06-05 20:04:15 -04:00
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
2010-09-07 11:34:51 -04:00
glfwMakeContextCurrent(window);
2015-10-13 21:11:20 -04:00
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
2012-06-05 20:04:15 -04:00
glfwSwapInterval(1);
2010-09-07 11:34:51 -04:00
2016-09-26 18:50:32 -04:00
nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
nk_glfw3_font_stash_begin(&atlas);
nk_glfw3_font_stash_end();
while (!glfwWindowShouldClose(window))
2010-09-07 11:34:51 -04:00
{
2016-09-26 18:50:32 -04:00
int i;
struct nk_panel layout;
2012-06-05 20:04:15 -04:00
glClear(GL_COLOR_BUFFER_BIT);
2016-09-26 18:50:32 -04:00
nk_glfw3_new_frame();
2010-09-07 11:34:51 -04:00
2016-09-26 18:50:32 -04:00
for (i = 0; i < joystick_count; i++)
{
char name[1024];
snprintf(name, sizeof(name), "%i: %s",
joysticks[i] + 1,
glfwGetJoystickName(joysticks[i]));
if (nk_begin(nk, &layout,
name,
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))
{
int j, axis_count, button_count;
const float* axes;
const unsigned char* buttons;
nk_layout_row_dynamic(nk, 30, 1);
axes = glfwGetJoystickAxes(joysticks[i], &axis_count);
if (axis_count)
{
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);
buttons = glfwGetJoystickButtons(joysticks[i], &button_count);
if (button_count)
{
for (j = 0; j < button_count; j++)
{
snprintf(name, sizeof(name), "%i", j + 1);
nk_select_label(nk, name, NK_TEXT_CENTERED, buttons[j]);
}
}
nk_layout_row_end(nk);
}
nk_end(nk);
}
nk_glfw3_render(NK_ANTI_ALIASING_ON, 10000, 1000);
2010-09-07 11:34:51 -04:00
glfwSwapBuffers(window);
2012-06-05 20:04:15 -04:00
glfwPollEvents();
2010-09-07 11:34:51 -04:00
}
glfwTerminate();
2012-06-05 20:04:15 -04:00
exit(EXIT_SUCCESS);
2010-09-07 11:34:51 -04:00
}