1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-27 04:28:52 -05:00
glfw/tests/joysticks.c

217 lines
5.6 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
#include <stdio.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);
}
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
2010-09-07 11:34:51 -04:00
{
2012-06-05 20:04:15 -04:00
glViewport(0, 0, width, height);
}
2010-09-07 11:34:51 -04:00
2015-12-13 11:38:50 -05:00
static void draw_joystick(int index, int x, int y, int width, int height)
2012-06-05 20:04:15 -04:00
{
int i;
2015-12-13 11:38:50 -05:00
int axis_count, button_count;
const float* axes;
const unsigned char* buttons;
2013-11-07 13:29:33 -05:00
const int axis_height = 3 * height / 4;
const int button_height = height / 4;
2012-06-05 20:04:15 -04:00
2015-12-13 11:38:50 -05:00
axes = glfwGetJoystickAxes(joysticks[index], &axis_count);
if (axis_count)
2013-11-07 13:29:33 -05:00
{
2015-12-13 11:38:50 -05:00
const int axis_width = width / axis_count;
2010-09-07 11:34:51 -04:00
2015-12-13 11:38:50 -05:00
for (i = 0; i < axis_count; i++)
2013-11-07 13:29:33 -05:00
{
2015-12-13 11:38:50 -05:00
float value = axes[i] / 2.f + 0.5f;
2012-06-05 20:04:15 -04:00
2013-11-07 13:29:33 -05:00
glColor3f(0.3f, 0.3f, 0.3f);
glRecti(x + i * axis_width,
y,
x + (i + 1) * axis_width,
y + axis_height);
2010-09-07 11:34:51 -04:00
2012-06-05 20:04:15 -04:00
glColor3f(1.f, 1.f, 1.f);
2013-11-07 13:29:33 -05:00
glRecti(x + i * axis_width,
y + (int) (value * (axis_height - 5)),
x + (i + 1) * axis_width,
y + 5 + (int) (value * (axis_height - 5)));
}
}
2012-06-05 20:04:15 -04:00
2015-12-13 11:38:50 -05:00
buttons = glfwGetJoystickButtons(joysticks[index], &button_count);
if (button_count)
2013-11-07 13:29:33 -05:00
{
2015-12-13 11:38:50 -05:00
const int button_width = width / button_count;
2013-11-07 13:29:33 -05:00
2015-12-13 11:38:50 -05:00
for (i = 0; i < button_count; i++)
2013-11-07 13:29:33 -05:00
{
2015-12-13 11:38:50 -05:00
if (buttons[i])
2013-11-07 13:29:33 -05:00
glColor3f(1.f, 1.f, 1.f);
else
glColor3f(0.3f, 0.3f, 0.3f);
glRecti(x + i * button_width,
y + axis_height,
x + (i + 1) * button_width,
y + axis_height + button_height);
}
2012-06-05 20:04:15 -04:00
}
}
2010-09-07 11:34:51 -04:00
static void draw_joysticks(GLFWwindow* window)
2010-09-07 11:34:51 -04:00
{
int i, width, height, offset = 0;
2012-06-05 20:04:15 -04:00
glfwGetFramebufferSize(window, &width, &height);
2010-09-07 11:34:51 -04:00
2012-06-05 20:04:15 -04:00
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.f, width, height, 0.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
2015-12-13 11:38:50 -05:00
for (i = 0; i < joystick_count; i++)
2010-09-07 11:34:51 -04:00
{
2015-12-13 11:38:50 -05:00
draw_joystick(i,
0, offset * height / joystick_count,
width, height / joystick_count);
offset++;
2010-09-07 11:34:51 -04:00
}
}
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)
2010-09-07 11:34:51 -04:00
{
2015-12-13 11:38:50 -05:00
int axis_count, button_count;
2010-09-07 11:34:51 -04:00
2015-12-13 11:38:50 -05:00
glfwGetJoystickAxes(joy, &axis_count);
glfwGetJoystickButtons(joy, &button_count);
2010-09-07 11:34:51 -04:00
2015-12-13 11:38:50 -05:00
printf("Found joystick %i named \'%s\' with %i axes, %i buttons\n",
joy + 1,
glfwGetJoystickName(joy),
axis_count,
button_count);
2012-06-05 20:04:15 -04:00
2015-12-13 11:38:50 -05:00
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
printf("Lost joystick %i\n", joy + 1);
joystick_count--;
}
}
2015-12-13 11:38:50 -05:00
static void find_joysticks(void)
{
int joy;
2010-09-07 11:34:51 -04:00
2015-12-13 11:38:50 -05:00
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{
if (glfwJoystickPresent(joy))
joystick_callback(joy, GLFW_CONNECTED);
2010-09-07 11:34:51 -04:00
}
}
int main(void)
{
GLFWwindow* window;
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);
2015-12-13 11:38:50 -05:00
find_joysticks();
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
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
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
while (!glfwWindowShouldClose(window))
2010-09-07 11:34:51 -04:00
{
2012-06-05 20:04:15 -04:00
glClear(GL_COLOR_BUFFER_BIT);
2010-09-07 11:34:51 -04:00
draw_joysticks(window);
2010-09-07 11:34:51 -04:00
glfwSwapBuffers(window);
2012-06-05 20:04:15 -04:00
glfwPollEvents();
2016-03-31 04:45:40 -04:00
// Workaround for an issue with msvcrt and mintty
fflush(stdout);
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
}