comparison nuklear_ui/blastem_nuklear.c @ 1609:9c8f58740450

Added PS4 controller image. Added code to use PS4 image for Playstation controllers
author Michael Pavone <pavone@retrodev.com>
date Fri, 10 Aug 2018 19:10:20 -0700
parents aa7533000161
children 18a946ec74c8
comparison
equal deleted inserted replaced
1608:419a0a133b5c 1609:9c8f58740450
502 } 502 }
503 } 503 }
504 504
505 static int selected_controller; 505 static int selected_controller;
506 static controller_info selected_controller_info; 506 static controller_info selected_controller_info;
507 static struct nk_image controller_360_image; 507 static struct nk_image controller_360_image, controller_ps4_image;
508 static uint32_t controller_360_width, controller_360_height; 508 static uint32_t controller_360_width, controller_360_height, controller_ps4_width, controller_ps4_height;
509 //#define MIN_BIND_BOX_WIDTH 140 509 //#define MIN_BIND_BOX_WIDTH 140
510 #define MAX_BIND_BOX_WIDTH 350 510 #define MAX_BIND_BOX_WIDTH 350
511 511
512 #define AXIS 0x40000000 512 #define AXIS 0x40000000
513 #define STICKDIR 0x30000000 513 #define STICKDIR 0x30000000
636 float orig_height = def_font->handle.height; 636 float orig_height = def_font->handle.height;
637 def_font->handle.height *= 0.5f; 637 def_font->handle.height *= 0.5f;
638 638
639 uint32_t avail_height = render_height() - 2 * orig_height; 639 uint32_t avail_height = render_height() - 2 * orig_height;
640 float desired_width = render_width() * 0.5f, desired_height = avail_height * 0.5f; 640 float desired_width = render_width() * 0.5f, desired_height = avail_height * 0.5f;
641 float controller_ratio = (float)controller_360_width / (float)controller_360_height; 641 uint32_t controller_width, controller_height;
642 if (selected_controller_info.type == TYPE_PSX) {
643 controller_width = controller_ps4_width;
644 controller_height = controller_ps4_height;
645 } else {
646 controller_width = controller_360_width;
647 controller_height = controller_360_height;
648 }
649
650 float controller_ratio = (float)controller_width / (float)controller_height;
642 651
643 const struct nk_user_font *font = context->style.font; 652 const struct nk_user_font *font = context->style.font;
644 int MIN_BIND_BOX_WIDTH = font->width(font->userdata, font->height, "Right", strlen("Right")) 653 int MIN_BIND_BOX_WIDTH = font->width(font->userdata, font->height, "Right", strlen("Right"))
645 + def_font->handle.width(font->userdata, font->height, "Internal Screenshot", strlen("Internal Screenshot")); 654 + def_font->handle.width(font->userdata, font->height, "Internal Screenshot", strlen("Internal Screenshot"));
646 655
657 float img_top = avail_height / 2.0f - desired_height / 2.0f; 666 float img_top = avail_height / 2.0f - desired_height / 2.0f;
658 float img_right = img_left + desired_width; 667 float img_right = img_left + desired_width;
659 float img_bot = img_top + desired_height; 668 float img_bot = img_top + desired_height;
660 nk_layout_space_begin(context, NK_STATIC, avail_height, INT_MAX); 669 nk_layout_space_begin(context, NK_STATIC, avail_height, INT_MAX);
661 nk_layout_space_push(context, nk_rect(img_left, img_top, desired_width, desired_height)); 670 nk_layout_space_push(context, nk_rect(img_left, img_top, desired_width, desired_height));
662 nk_image(context, controller_360_image); 671 nk_image(context, selected_controller_info.type == TYPE_PSX ? controller_ps4_image : controller_360_image);
663 672
664 float bind_box_width = (render_width() - img_right) * 0.8f; 673 float bind_box_width = (render_width() - img_right) * 0.8f;
665 if (bind_box_width < MIN_BIND_BOX_WIDTH) { 674 if (bind_box_width < MIN_BIND_BOX_WIDTH) {
666 bind_box_width = render_width() - img_right; 675 bind_box_width = render_width() - img_right;
667 if (bind_box_width > MIN_BIND_BOX_WIDTH) { 676 if (bind_box_width > MIN_BIND_BOX_WIDTH) {
935 944
936 void view_controllers(struct nk_context *context) 945 void view_controllers(struct nk_context *context)
937 { 946 {
938 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { 947 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) {
939 int height = (render_width() - 2*context->style.font->height) / MAX_JOYSTICKS; 948 int height = (render_width() - 2*context->style.font->height) / MAX_JOYSTICKS;
940 int image_width = height * controller_360_width / controller_360_height;
941 for (int i = 0; i < MAX_JOYSTICKS; i++) 949 for (int i = 0; i < MAX_JOYSTICKS; i++)
942 { 950 {
943 SDL_Joystick *joy = render_get_joystick(i); 951 SDL_Joystick *joy = render_get_joystick(i);
944 if (joy) { 952 if (joy) {
945 controller_info info = get_controller_info(i); 953 controller_info info = get_controller_info(i);
954 uint32_t controller_width, controller_height;
955 if (info.type == TYPE_PSX) {
956 controller_width = controller_ps4_width;
957 controller_height = controller_ps4_height;
958 } else {
959 controller_width = controller_360_width;
960 controller_height = controller_360_height;
961 }
962 int image_width = height * controller_width / controller_height;
946 nk_layout_row_begin(context, NK_STATIC, height, 2); 963 nk_layout_row_begin(context, NK_STATIC, height, 2);
947 nk_layout_row_push(context, image_width); 964 nk_layout_row_push(context, image_width);
948 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) { 965 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) {
949 nk_label(context, "?", NK_TEXT_CENTERED); 966 nk_label(context, "?", NK_TEXT_CENTERED);
950 } else { 967 } else {
951 nk_image(context, controller_360_image); 968 nk_image(context, info.type == TYPE_PSX ? controller_ps4_image : controller_360_image);
952 } 969 }
953 nk_layout_row_push(context, render_width() - image_width - 2 * context->style.font->height); 970 nk_layout_row_push(context, render_width() - image_width - 2 * context->style.font->height);
954 if (nk_button_label(context, info.name)) { 971 if (nk_button_label(context, info.name)) {
955 selected_controller = i; 972 selected_controller = i;
956 selected_controller_info = info; 973 selected_controller_info = info;
1447 static void context_destroyed(void) 1464 static void context_destroyed(void)
1448 { 1465 {
1449 nk_sdl_shutdown(); 1466 nk_sdl_shutdown();
1450 } 1467 }
1451 1468
1452 static uint32_t *controller_360_buf; 1469 static uint32_t *controller_360_buf, *controller_ps4_buf;
1470
1471 static struct nk_image load_image_texture(uint32_t *buf, uint32_t width, uint32_t height)
1472 {
1473 GLuint tex;
1474 glGenTextures(1, &tex);
1475 glBindTexture(GL_TEXTURE_2D, tex);
1476 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1477 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1478 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1479 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1480 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, buf);
1481 return nk_image_id((int)tex);
1482 }
1453 1483
1454 static void texture_init(void) 1484 static void texture_init(void)
1455 { 1485 {
1456 struct nk_font_atlas *atlas; 1486 struct nk_font_atlas *atlas;
1457 nk_sdl_font_stash_begin(&atlas); 1487 nk_sdl_font_stash_begin(&atlas);
1463 def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, render_height() / 16, NULL); 1493 def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, render_height() / 16, NULL);
1464 free(font); 1494 free(font);
1465 nk_sdl_font_stash_end(); 1495 nk_sdl_font_stash_end();
1466 nk_style_set_font(context, &def_font->handle); 1496 nk_style_set_font(context, &def_font->handle);
1467 if (controller_360_buf) { 1497 if (controller_360_buf) {
1468 GLuint tex; 1498 controller_360_image = load_image_texture(controller_360_buf, controller_360_width, controller_360_height);
1469 glGenTextures(1, &tex); 1499 }
1470 glBindTexture(GL_TEXTURE_2D, tex); 1500 if (controller_ps4_buf) {
1471 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1501 controller_ps4_image = load_image_texture(controller_ps4_buf, controller_ps4_width, controller_ps4_height);
1472 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1473 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1474 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1475 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, controller_360_width, controller_360_height, 0, GL_BGRA, GL_UNSIGNED_BYTE, controller_360_buf);
1476 controller_360_image = nk_image_id((int)tex);
1477 } 1502 }
1478 } 1503 }
1479 1504
1480 static void context_created(void) 1505 static void context_created(void)
1481 { 1506 {
1529 uint32_t buf_size; 1554 uint32_t buf_size;
1530 uint8_t *buf = (uint8_t *)read_bundled_file("images/360.png", &buf_size); 1555 uint8_t *buf = (uint8_t *)read_bundled_file("images/360.png", &buf_size);
1531 if (buf) { 1556 if (buf) {
1532 controller_360_buf = load_png(buf, buf_size, &controller_360_width, &controller_360_height); 1557 controller_360_buf = load_png(buf, buf_size, &controller_360_width, &controller_360_height);
1533 free(buf); 1558 free(buf);
1534 } 1559 }
1560 buf = (uint8_t *)read_bundled_file("images/ps4.png", &buf_size);
1561 if (buf) {
1562 controller_ps4_buf = load_png(buf, buf_size, &controller_ps4_width, &controller_ps4_height);
1563 free(buf);
1564 }
1535 texture_init(); 1565 texture_init();
1536 1566
1537 current_view = file_loaded ? view_play : view_menu; 1567 current_view = file_loaded ? view_play : view_menu;
1538 render_set_ui_render_fun(blastem_nuklear_render); 1568 render_set_ui_render_fun(blastem_nuklear_render);
1539 render_set_event_handler(handle_event); 1569 render_set_event_handler(handle_event);