int main(int argc, char** argv) { Slot* slots; GLFWmonitor* monitor = NULL; int ch, i, width, height, count = 1; setlocale(LC_ALL, ""); glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); printf("Library initialized\n"); glfwSetMonitorCallback(monitor_callback); glfwSetJoystickCallback(joystick_callback); while ((ch = getopt(argc, argv, "hfn:")) != -1) { switch (ch) { case 'h': usage(); exit(EXIT_SUCCESS); case 'f': monitor = glfwGetPrimaryMonitor(); break; case 'n': count = (int) strtol(optarg, NULL, 10); break; default: usage(); exit(EXIT_FAILURE); } } if (monitor) { const GLFWvidmode* mode = glfwGetVideoMode(monitor); glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate); glfwWindowHint(GLFW_RED_BITS, mode->redBits); glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits); glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits); width = mode->width; height = mode->height; } else { width = 640; height = 480; } if (!count) { fprintf(stderr, "Invalid user\n"); exit(EXIT_FAILURE); } slots = calloc(count, sizeof(Slot)); for (i = 0; i < count; i++) { char title[128]; slots[i].closeable = GLFW_TRUE; slots[i].number = i + 1; snprintf(title, sizeof(title), "Event Linter (Window %i)", slots[i].number); if (monitor) { printf("Creating full screen window %i (%ix%i on %s)\n", slots[i].number, width, height, glfwGetMonitorName(monitor)); } else { printf("Creating windowed mode window %i (%ix%i)\n", slots[i].number, width, height); } slots[i].window = glfwCreateWindow(width, height, title, monitor, NULL); if (!slots[i].window) { free(slots); glfwTerminate(); exit(EXIT_FAILURE); } glfwSetWindowUserPointer(slots[i].window, slots + i); glfwSetWindowPosCallback(slots[i].window, window_pos_callback); glfwSetWindowSizeCallback(slots[i].window, window_size_callback); glfwSetFramebufferSizeCallback(slots[i].window, framebuffer_size_callback); glfwSetWindowCloseCallback(slots[i].window, window_close_callback); glfwSetWindowRefreshCallback(slots[i].window, window_refresh_callback); glfwSetWindowFocusCallback(slots[i].window, window_focus_callback); glfwSetWindowIconifyCallback(slots[i].window, window_iconify_callback); glfwSetWindowMaximizeCallback(slots[i].window, window_maximize_callback); glfwSetMouseButtonCallback(slots[i].window, mouse_button_callback); glfwSetCursorPosCallback(slots[i].window, cursor_position_callback); glfwSetCursorEnterCallback(slots[i].window, cursor_enter_callback); glfwSetScrollCallback(slots[i].window, scroll_callback); glfwSetKeyCallback(slots[i].window, key_callback); glfwSetCharCallback(slots[i].window, char_callback); glfwSetCharModsCallback(slots[i].window, char_mods_callback); glfwSetDropCallback(slots[i].window, drop_callback); glfwMakeContextCurrent(slots[i].window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); } printf("Main loop starting\n"); for (;;) { for (i = 0; i < count; i++) { if (glfwWindowShouldClose(slots[i].window)) break; } if (i < count) break; glfwWaitEvents(); // Workaround for an issue with msvcrt and mintty fflush(stdout); } free(slots); glfwTerminate(); exit(EXIT_SUCCESS); }
int main(void) { int jid, hat_buttons = GLFW_FALSE; struct nk_context* nk; struct nk_font_atlas* atlas; memset(joysticks, 0, sizeof(joysticks)); glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(800, 600, "Joystick Test", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS); nk_glfw3_font_stash_begin(&atlas); nk_glfw3_font_stash_end(); for (jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; jid++) { if (glfwJoystickPresent(jid)) joysticks[joystick_count++] = jid; } glfwSetJoystickCallback(joystick_callback); while (!glfwWindowShouldClose(window)) { int i, width, height; glfwGetWindowSize(window, &width, &height); glClear(GL_COLOR_BUFFER_BIT); nk_glfw3_new_frame(); if (nk_begin(nk, "Joysticks", nk_rect(width - 200.f, 0.f, 200.f, (float) height), NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE)) { nk_layout_row_dynamic(nk, 30, 1); nk_checkbox_label(nk, "Hat buttons", &hat_buttons); 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); for (i = 0; i < joystick_count; i++) { if (nk_begin(nk, joystick_label(joysticks[i]), nk_rect(i * 20.f, i * 20.f, 550.f, 570.f), NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE)) { int j, axis_count, button_count, hat_count; const float* axes; const unsigned char* buttons; const unsigned char* hats; GLFWgamepadstate state; nk_layout_row_dynamic(nk, 30, 1); nk_label(nk, "Joystick state", NK_TEXT_LEFT); axes = glfwGetJoystickAxes(joysticks[i], &axis_count); buttons = glfwGetJoystickButtons(joysticks[i], &button_count); hats = glfwGetJoystickHats(joysticks[i], &hat_count); if (!hat_buttons) button_count -= hat_count * 4; 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, 12); for (j = 0; j < button_count; j++) { char name[16]; snprintf(name, sizeof(name), "%i", j + 1); nk_select_label(nk, name, NK_TEXT_CENTERED, buttons[j]); } nk_layout_row_dynamic(nk, 30, 8); for (j = 0; j < hat_count; j++) hat_widget(nk, hats[j]); nk_layout_row_dynamic(nk, 30, 1); if (glfwGetGamepadState(joysticks[i], &state)) { int hat = 0; const char* names[GLFW_GAMEPAD_BUTTON_LAST + 1 - 4] = { "A", "B", "X", "Y", "LB", "RB", "Back", "Start", "Guide", "LT", "RT", }; nk_label(nk, "Gamepad state", NK_TEXT_LEFT); nk_layout_row_dynamic(nk, 30, 2); for (j = 0; j <= GLFW_GAMEPAD_AXIS_LAST; j++) nk_slide_float(nk, -1.f, state.axes[j], 1.f, 0.1f); nk_layout_row_dynamic(nk, 30, GLFW_GAMEPAD_BUTTON_LAST + 1 - 4); for (j = 0; j <= GLFW_GAMEPAD_BUTTON_LAST - 4; j++) nk_select_label(nk, names[j], NK_TEXT_CENTERED, state.buttons[j]); if (state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_UP]) hat |= GLFW_HAT_UP; if (state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT]) hat |= GLFW_HAT_RIGHT; if (state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_DOWN]) hat |= GLFW_HAT_DOWN; if (state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_LEFT]) hat |= GLFW_HAT_LEFT; nk_layout_row_dynamic(nk, 30, 8); hat_widget(nk, hat); } else nk_label(nk, "Joystick has no gamepad mapping", NK_TEXT_LEFT); } nk_end(nk); } nk_glfw3_render(NK_ANTI_ALIASING_ON, 10000, 1000); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); exit(EXIT_SUCCESS); }