comparison nuklear_ui/blastem_nuklear.c @ 1573:a051d8ee4528

Only save config file if something has changed. Re-initialize audio and video with new settings if config has changed
author Michael Pavone <pavone@retrodev.com>
date Fri, 27 Apr 2018 20:08:47 -0700
parents 5efeca06d942
children ccb3a8ae7ad0
comparison
equal deleted inserted replaced
1572:5efeca06d942 1573:a051d8ee4528
21 static view_fun current_view; 21 static view_fun current_view;
22 static view_fun *previous_views; 22 static view_fun *previous_views;
23 static uint32_t view_storage; 23 static uint32_t view_storage;
24 static uint32_t num_prev; 24 static uint32_t num_prev;
25 static struct nk_font *def_font; 25 static struct nk_font *def_font;
26 static uint8_t config_dirty;
26 27
27 static void push_view(view_fun new_view) 28 static void push_view(view_fun new_view)
28 { 29 {
29 if (num_prev == view_storage) { 30 if (num_prev == view_storage) {
30 view_storage = view_storage ? 2*view_storage : 2; 31 view_storage = view_storage ? 2*view_storage : 2;
484 char *path = malloc(prefix_len + suffix_len + 1); 485 char *path = malloc(prefix_len + suffix_len + 1);
485 memcpy(path, "bindings\0keys\0", prefix_len); 486 memcpy(path, "bindings\0keys\0", prefix_len);
486 memcpy(path + prefix_len, name, suffix_len); 487 memcpy(path + prefix_len, name, suffix_len);
487 path[prefix_len + suffix_len] = 0; 488 path[prefix_len + suffix_len] = 0;
488 489
490 config_dirty = 1;
489 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(set_binding)}, TVAL_PTR); 491 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(set_binding)}, TVAL_PTR);
490 free(path); 492 free(path);
491 free(name); 493 free(name);
492 tern_free(binding_lookup); 494 tern_free(binding_lookup);
493 binding_lookup = NULL; 495 binding_lookup = NULL;
596 { 598 {
597 uint8_t curval = !strcmp("on", tern_find_path_default(config, path, (tern_val){.ptrval = def ? "on": "off"}, TVAL_PTR).ptrval); 599 uint8_t curval = !strcmp("on", tern_find_path_default(config, path, (tern_val){.ptrval = def ? "on": "off"}, TVAL_PTR).ptrval);
598 nk_label(context, label, NK_TEXT_LEFT); 600 nk_label(context, label, NK_TEXT_LEFT);
599 uint8_t newval = nk_check_label(context, "", curval); 601 uint8_t newval = nk_check_label(context, "", curval);
600 if (newval != curval) { 602 if (newval != curval) {
603 config_dirty = 1;
601 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(newval ? "on" : "off")}, TVAL_PTR); 604 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(newval ? "on" : "off")}, TVAL_PTR);
602 } 605 }
603 } 606 }
604 607
605 void settings_int_input(struct nk_context *context, char *label, char *path, char *def) 608 void settings_int_input(struct nk_context *context, char *label, char *path, char *def)
614 } 617 }
615 memcpy(buffer, curstr, len); 618 memcpy(buffer, curstr, len);
616 nk_edit_string(context, NK_EDIT_SIMPLE, buffer, &len, sizeof(buffer)-1, nk_filter_decimal); 619 nk_edit_string(context, NK_EDIT_SIMPLE, buffer, &len, sizeof(buffer)-1, nk_filter_decimal);
617 buffer[len] = 0; 620 buffer[len] = 0;
618 if (strcmp(buffer, curstr)) { 621 if (strcmp(buffer, curstr)) {
622 config_dirty = 1;
619 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(buffer)}, TVAL_PTR); 623 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(buffer)}, TVAL_PTR);
620 } 624 }
621 } 625 }
622 626
623 void settings_int_property(struct nk_context *context, char *label, char *name, char *path, int def, int min, int max) 627 void settings_int_property(struct nk_context *context, char *label, char *name, char *path, int def, int min, int max)
628 int val = curval; 632 int val = curval;
629 nk_property_int(context, name, min, &val, max, 1, 1.0f); 633 nk_property_int(context, name, min, &val, max, 1, 1.0f);
630 if (val != curval) { 634 if (val != curval) {
631 char buffer[12]; 635 char buffer[12];
632 sprintf(buffer, "%d", val); 636 sprintf(buffer, "%d", val);
637 config_dirty = 1;
633 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(buffer)}, TVAL_PTR); 638 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(buffer)}, TVAL_PTR);
634 } 639 }
635 } 640 }
636 641
637 typedef struct { 642 typedef struct {
741 int32_t settings_dropdown_ex(struct nk_context *context, char *label, const char **options, const char **opt_display, uint32_t num_options, int32_t current, char *path) 746 int32_t settings_dropdown_ex(struct nk_context *context, char *label, const char **options, const char **opt_display, uint32_t num_options, int32_t current, char *path)
742 { 747 {
743 nk_label(context, label, NK_TEXT_LEFT); 748 nk_label(context, label, NK_TEXT_LEFT);
744 int32_t next = nk_combo(context, opt_display, num_options, current, 30, nk_vec2(300, 300)); 749 int32_t next = nk_combo(context, opt_display, num_options, current, 30, nk_vec2(300, 300));
745 if (next != current) { 750 if (next != current) {
751 config_dirty = 1;
746 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(options[next])}, TVAL_PTR); 752 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(options[next])}, TVAL_PTR);
747 } 753 }
748 return next; 754 return next;
749 } 755 }
750 756
802 settings_int_input(context, "Windowed Width", "video\0width\0", "640"); 808 settings_int_input(context, "Windowed Width", "video\0width\0", "640");
803 nk_label(context, "Shader", NK_TEXT_LEFT); 809 nk_label(context, "Shader", NK_TEXT_LEFT);
804 uint32_t next_selected = nk_combo(context, (const char **)prog_names, num_progs, selected_prog, 30, nk_vec2(300, 300)); 810 uint32_t next_selected = nk_combo(context, (const char **)prog_names, num_progs, selected_prog, 30, nk_vec2(300, 300));
805 if (next_selected != selected_prog) { 811 if (next_selected != selected_prog) {
806 selected_prog = next_selected; 812 selected_prog = next_selected;
813 config_dirty = 1;
807 config = tern_insert_path(config, "video\0fragment_shader\0", (tern_val){.ptrval = strdup(progs[next_selected].fragment)}, TVAL_PTR); 814 config = tern_insert_path(config, "video\0fragment_shader\0", (tern_val){.ptrval = strdup(progs[next_selected].fragment)}, TVAL_PTR);
808 config = tern_insert_path(config, "video\0vertex_shader\0", (tern_val){.ptrval = strdup(progs[next_selected].vertex)}, TVAL_PTR); 815 config = tern_insert_path(config, "video\0vertex_shader\0", (tern_val){.ptrval = strdup(progs[next_selected].vertex)}, TVAL_PTR);
809 } 816 }
810 settings_int_property(context, "NTSC Overscan", "Top", "video\0ntsc\0overscan\0top\0", 2, 0, 32); 817 settings_int_property(context, "NTSC Overscan", "Top", "video\0ntsc\0overscan\0top\0", 2, 0, 32);
811 settings_int_property(context, "", "Bottom", "video\0ntsc\0overscan\0bottom\0", 17, 0, 32); 818 settings_int_property(context, "", "Bottom", "video\0ntsc\0overscan\0bottom\0", 17, 0, 32);
1022 render_sleep_ms(MIN_UI_DELAY - (current - last) - 1); 1029 render_sleep_ms(MIN_UI_DELAY - (current - last) - 1);
1023 } 1030 }
1024 last = current; 1031 last = current;
1025 render_update_display(); 1032 render_update_display();
1026 } 1033 }
1034 if (config_dirty) {
1035 apply_updated_config();
1036 persist_config(config);
1037 config_dirty = 0;
1038 }
1027 } 1039 }
1028 static void handle_event(SDL_Event *event) 1040 static void handle_event(SDL_Event *event)
1029 { 1041 {
1030 if (event->type == SDL_KEYDOWN) { 1042 if (event->type == SDL_KEYDOWN) {
1031 keycode = event->key.keysym.sym; 1043 keycode = event->key.keysym.sym;
1033 nk_sdl_handle_event(event); 1045 nk_sdl_handle_event(event);
1034 } 1046 }
1035 1047
1036 static void context_destroyed(void) 1048 static void context_destroyed(void)
1037 { 1049 {
1038 nk_sdl_device_destroy(); 1050 nk_sdl_shutdown();
1039 } 1051 }
1040 1052
1041 static uint32_t *controller_360_buf; 1053 static uint32_t *controller_360_buf;
1042 1054
1043 static void texture_init(void) 1055 static void texture_init(void)
1065 } 1077 }
1066 } 1078 }
1067 1079
1068 static void context_created(void) 1080 static void context_created(void)
1069 { 1081 {
1070 nk_sdl_device_create(); 1082 context = nk_sdl_init(render_get_window());
1071 texture_init(); 1083 texture_init();
1072 } 1084 }
1073 1085
1074 void show_pause_menu(void) 1086 void show_pause_menu(void)
1075 { 1087 {