comparison nuklear_ui/blastem_nuklear.c @ 2693:46dba737b931

WIP Nuklear UI in VDP debug windows
author Michael Pavone <pavone@retrodev.com>
date Thu, 19 Jun 2025 19:59:05 -0700
parents 143cb5762ec9
children a6a27d4aa8ab
comparison
equal deleted inserted replaced
2692:effbb52ab3f0 2693:46dba737b931
2624 fb_context->fb.pixels = render_get_framebuffer(FRAMEBUFFER_UI, &fb_context->fb.pitch); 2624 fb_context->fb.pixels = render_get_framebuffer(FRAMEBUFFER_UI, &fb_context->fb.pitch);
2625 nk_rawfb_render(fb_context, nk_rgb(0,0,0), 0); 2625 nk_rawfb_render(fb_context, nk_rgb(0,0,0), 0);
2626 render_framebuffer_updated(FRAMEBUFFER_UI, render_width()); 2626 render_framebuffer_updated(FRAMEBUFFER_UI, render_width());
2627 } else { 2627 } else {
2628 #ifndef DISABLE_OPENGL 2628 #ifndef DISABLE_OPENGL
2629 nk_sdl_render(NK_ANTI_ALIASING_ON, 512 * 1024, 128 * 1024); 2629 nk_sdl_render(context, NK_ANTI_ALIASING_ON, 512 * 1024, 128 * 1024);
2630 #endif 2630 #endif
2631 } 2631 }
2632 nk_input_begin(context); 2632 nk_input_begin(context);
2633 } else { 2633 } else {
2634 render_force_cursor(0); 2634 render_force_cursor(0);
2666 render_update_display(); 2666 render_update_display();
2667 } 2667 }
2668 ui_exit(); 2668 ui_exit();
2669 #endif 2669 #endif
2670 } 2670 }
2671 static void handle_event(SDL_Event *event) 2671 static void handle_event(uint8_t which, SDL_Event *event)
2672 { 2672 {
2673 SDL_Joystick *joy = render_get_joystick(selected_controller); 2673 SDL_Joystick *joy = render_get_joystick(selected_controller);
2674 if (event->type == SDL_KEYDOWN) { 2674 if (event->type == SDL_KEYDOWN) {
2675 keycode = event->key.keysym.sym; 2675 keycode = event->key.keysym.sym;
2676 } else if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == 0) { 2676 } else if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == 0) {
2693 } 2693 }
2694 } 2694 }
2695 if (stick_nav_disabled && event->type == SDL_CONTROLLERAXISMOTION) { 2695 if (stick_nav_disabled && event->type == SDL_CONTROLLERAXISMOTION) {
2696 return; 2696 return;
2697 } 2697 }
2698 nk_sdl_handle_event(event); 2698 nk_sdl_handle_event(context, event);
2699 } 2699 }
2700 2700
2701 static void context_destroyed(void) 2701 static void context_destroyed(void)
2702 { 2702 {
2703 if (context) 2703 if (context)
2704 { 2704 {
2705 nk_sdl_shutdown(); 2705 nk_sdl_shutdown(context);
2706 context = NULL; 2706 context = NULL;
2707 } 2707 }
2708 } 2708 }
2709 2709
2710 #ifndef DISABLE_OPENGL 2710 #ifndef DISABLE_OPENGL
2735 fbimg->h = height; 2735 fbimg->h = height;
2736 fbimg->format = NK_FONT_ATLAS_RGBA32; 2736 fbimg->format = NK_FONT_ATLAS_RGBA32;
2737 return nk_image_ptr(fbimg); 2737 return nk_image_ptr(fbimg);
2738 } 2738 }
2739 2739
2740 static void texture_init(void) 2740 static void font_init(struct nk_context *ctx)
2741 { 2741 {
2742 struct nk_font_atlas *atlas; 2742 struct nk_font_atlas *atlas;
2743 if (fb_context) { 2743 if (fb_context) {
2744 nk_rawfb_font_stash_begin(fb_context, &atlas); 2744 nk_rawfb_font_stash_begin(fb_context, &atlas);
2745 } else { 2745 } else {
2746 #ifndef DISABLE_OPENGL 2746 #ifndef DISABLE_OPENGL
2747 nk_sdl_font_stash_begin(&atlas); 2747 nk_sdl_font_stash_begin(ctx, &atlas);
2748 #endif 2748 #endif
2749 } 2749 }
2750 uint32_t font_size; 2750 uint32_t font_size;
2751 uint8_t *font = default_font(&font_size); 2751 uint8_t *font = default_font(&font_size);
2752 if (!font) { 2752 if (!font) {
2756 free(font); 2756 free(font);
2757 if (fb_context) { 2757 if (fb_context) {
2758 nk_rawfb_font_stash_end(fb_context); 2758 nk_rawfb_font_stash_end(fb_context);
2759 } else { 2759 } else {
2760 #ifndef DISABLE_OPENGL 2760 #ifndef DISABLE_OPENGL
2761 nk_sdl_font_stash_end(); 2761 nk_sdl_font_stash_end(ctx);
2762 #endif 2762 #endif
2763 } 2763 }
2764 nk_style_set_font(context, &def_font->handle); 2764 nk_style_set_font(ctx, &def_font->handle);
2765 }
2766
2767 static void texture_init(void)
2768 {
2765 for (uint32_t i = 0; i < num_ui_images; i++) 2769 for (uint32_t i = 0; i < num_ui_images; i++)
2766 { 2770 {
2767 #ifndef DISABLE_OPENGL 2771 #ifndef DISABLE_OPENGL
2768 if (fb_context) { 2772 if (fb_context) {
2769 #endif 2773 #endif
2774 } 2778 }
2775 #endif 2779 #endif
2776 } 2780 }
2777 } 2781 }
2778 2782
2779 static void style_init(void) 2783 static void style_init(struct nk_context *ctx)
2780 { 2784 {
2781 context->style.checkbox.padding.x = render_height() / 120; 2785 ctx->style.checkbox.padding.x = render_height() / 120;
2782 context->style.checkbox.padding.y = render_height() / 120; 2786 ctx->style.checkbox.padding.y = render_height() / 120;
2783 context->style.checkbox.border = render_height() / 240; 2787 ctx->style.checkbox.border = render_height() / 240;
2784 context->style.checkbox.cursor_normal.type = NK_STYLE_ITEM_COLOR; 2788 ctx->style.checkbox.cursor_normal.type = NK_STYLE_ITEM_COLOR;
2785 context->style.checkbox.cursor_normal.data.color = (struct nk_color){ 2789 ctx->style.checkbox.cursor_normal.data.color = (struct nk_color){
2786 .r = 255, .g = 128, .b = 0, .a = 255 2790 .r = 255, .g = 128, .b = 0, .a = 255
2787 }; 2791 };
2788 context->style.checkbox.cursor_hover = context->style.checkbox.cursor_normal; 2792 ctx->style.checkbox.cursor_hover = ctx->style.checkbox.cursor_normal;
2789 context->style.property.inc_button.text_hover = (struct nk_color){ 2793 ctx->style.property.inc_button.text_hover = (struct nk_color){
2790 .r = 255, .g = 128, .b = 0, .a = 255 2794 .r = 255, .g = 128, .b = 0, .a = 255
2791 }; 2795 };
2792 context->style.property.dec_button.text_hover = context->style.property.inc_button.text_hover; 2796 ctx->style.property.dec_button.text_hover = ctx->style.property.inc_button.text_hover;
2793 context->style.combo.button.text_hover = context->style.property.inc_button.text_hover; 2797 ctx->style.combo.button.text_hover = ctx->style.property.inc_button.text_hover;
2794 } 2798 }
2795 2799
2796 static void fb_resize(void) 2800 static void fb_resize(void)
2797 { 2801 {
2798 nk_rawfb_resize_fb(fb_context, NULL, render_width(), render_height(), 0); 2802 nk_rawfb_resize_fb(fb_context, NULL, render_width(), render_height(), 0);
2799 style_init(); 2803 style_init(context);
2800 texture_init(); 2804 texture_init();
2801 } 2805 }
2802 2806
2803 static void context_created(void) 2807 static void context_created(void)
2804 { 2808 {
2805 context = nk_sdl_init(render_get_window()); 2809 context = shared_nuklear_init(FRAMEBUFFER_UI);
2806 #ifndef DISABLE_OPENGL
2807 if (render_has_gl()) {
2808 nk_sdl_device_create();
2809 } else {
2810 #endif
2811 fb_context = nk_rawfb_init(NULL, context, render_width(), render_height(), 0);
2812 render_set_ui_fb_resize_handler(fb_resize);
2813 #ifndef DISABLE_OPENGL
2814 }
2815 #endif
2816 style_init();
2817 texture_init(); 2810 texture_init();
2818 } 2811 }
2819 2812
2820 void show_pause_menu(void) 2813 void show_pause_menu(void)
2821 { 2814 {
2895 } else { 2888 } else {
2896 return NULL; 2889 return NULL;
2897 } 2890 }
2898 } 2891 }
2899 2892
2900 void blastem_nuklear_init(uint8_t file_loaded) 2893 struct nk_context *shared_nuklear_init(uint8_t window)
2901 { 2894 {
2902 context = nk_sdl_init(render_get_window()); 2895 if (window >= FRAMEBUFFER_USER_START) {
2896 #ifdef DISABLE_OPENGL
2897 return NULL;
2898 #else
2899 if (!render_has_gl()) {
2900 return NULL;
2901 }
2902 #endif
2903 }
2904 struct nk_context *ret = nk_sdl_init(render_get_window(window));
2903 #ifndef DISABLE_OPENGL 2905 #ifndef DISABLE_OPENGL
2904 if (render_has_gl()) { 2906 if (render_has_gl()) {
2905 nk_sdl_device_create(); 2907 nk_sdl_device_create(ret);
2906 } else { 2908 } else {
2907 #endif 2909 #endif
2908 fb_context = nk_rawfb_init(NULL, context, render_width(), render_height(), 0); 2910 fb_context = nk_rawfb_init(NULL, context, render_width(), render_height(), 0);
2909 render_set_ui_fb_resize_handler(fb_resize); 2911 render_set_ui_fb_resize_handler(fb_resize);
2910 #ifndef DISABLE_OPENGL 2912 #ifndef DISABLE_OPENGL
2911 } 2913 }
2912 #endif 2914 #endif
2913 style_init(); 2915 style_init(ret);
2916 font_init(ret);
2917 return ret;
2918 }
2919
2920 void blastem_nuklear_init(uint8_t file_loaded)
2921 {
2922 context = shared_nuklear_init(FRAMEBUFFER_UI);
2914 2923
2915 controller_360 = load_ui_image("images/360.png"); 2924 controller_360 = load_ui_image("images/360.png");
2916 controller_ps4 = load_ui_image("images/ps4.png"); 2925 controller_ps4 = load_ui_image("images/ps4.png");
2917 controller_ps4_6b = load_ui_image("images/ps4_6b.png"); 2926 controller_ps4_6b = load_ui_image("images/ps4_6b.png");
2918 controller_wiiu = load_ui_image("images/wiiu.png"); 2927 controller_wiiu = load_ui_image("images/wiiu.png");
2924 current_view = view_play; 2933 current_view = view_play;
2925 } else { 2934 } else {
2926 current_view = view_menu; 2935 current_view = view_menu;
2927 set_content_binding_state(0); 2936 set_content_binding_state(0);
2928 } 2937 }
2929 render_set_ui_render_fun(blastem_nuklear_render); 2938 render_set_ui_render_fun(FRAMEBUFFER_UI, blastem_nuklear_render);
2930 render_set_event_handler(handle_event); 2939 render_set_event_handler(FRAMEBUFFER_UI, handle_event);
2931 render_set_gl_context_handlers(context_destroyed, context_created); 2940 render_set_gl_context_handlers(context_destroyed, context_created);
2932 char *unf = tern_find_path(config, "ui\0use_native_filechooser\0", TVAL_PTR).ptrval; 2941 char *unf = tern_find_path(config, "ui\0use_native_filechooser\0", TVAL_PTR).ptrval;
2933 use_native_filechooser = unf && !strcmp(unf, "on"); 2942 use_native_filechooser = unf && !strcmp(unf, "on");
2934 2943
2935 atexit(persist_config_exit); 2944 atexit(persist_config_exit);