comparison nuklear_ui/blastem_nuklear.c @ 1570:bc96bb3a0998

Use read_bundle_file for controller PNG. Re-upload texture when GL context is recreated
author Michael Pavone <pavone@retrodev.com>
date Thu, 19 Apr 2018 09:44:15 -0700
parents 0ec89dadb36d
children 3def7b216a5b
comparison
equal deleted inserted replaced
1569:0ec89dadb36d 1570:bc96bb3a0998
494 set_binding = set_label = NULL; 494 set_binding = set_label = NULL;
495 } 495 }
496 nk_end(context); 496 nk_end(context);
497 } 497 }
498 } 498 }
499 static struct nk_image controller_image; 499 static struct nk_image controller_360_image;
500 static uint32_t controller_360_width, controller_360_height;
500 void view_controllers(struct nk_context *context) 501 void view_controllers(struct nk_context *context)
501 { 502 {
502 if (nk_begin(context, "Controller Bindings", nk_rect(0, 0, render_width(), render_height()), 0)) { 503 if (nk_begin(context, "Controller Bindings", nk_rect(0, 0, render_width(), render_height()), 0)) {
503 nk_layout_row_static(context, render_height() - 50, render_width() - 80, 1); 504 nk_layout_row_static(context, render_height() - 50, render_width() - 80, 1);
504 nk_image(context, controller_image); 505 nk_image(context, controller_360_image);
505 nk_layout_row_static(context, 34, (render_width() - 80) / 2, 1); 506 nk_layout_row_static(context, 34, (render_width() - 80) / 2, 1);
506 if (nk_button_label(context, "Back")) { 507 if (nk_button_label(context, "Back")) {
507 pop_view(); 508 pop_view();
508 } 509 }
509 nk_end(context); 510 nk_end(context);
953 954
954 static void context_destroyed(void) 955 static void context_destroyed(void)
955 { 956 {
956 nk_sdl_device_destroy(); 957 nk_sdl_device_destroy();
957 } 958 }
958 static void context_created(void) 959
959 { 960 static uint32_t *controller_360_buf;
960 nk_sdl_device_create(); 961
962 static void texture_init(void)
963 {
961 struct nk_font_atlas *atlas; 964 struct nk_font_atlas *atlas;
962 nk_sdl_font_stash_begin(&atlas); 965 nk_sdl_font_stash_begin(&atlas);
963 uint32_t font_size; 966 uint32_t font_size;
964 uint8_t *font = default_font(&font_size); 967 uint8_t *font = default_font(&font_size);
965 if (!font) { 968 if (!font) {
966 fatal_error("Failed to find default font path\n"); 969 fatal_error("Failed to find default font path\n");
967 } 970 }
968 struct nk_font *def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, 30, NULL); 971 struct nk_font *def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, 30, NULL);
969 nk_sdl_font_stash_end(); 972 nk_sdl_font_stash_end();
970 nk_style_set_font(context, &def_font->handle); 973 nk_style_set_font(context, &def_font->handle);
974 if (controller_360_buf) {
975 GLuint tex;
976 glGenTextures(1, &tex);
977 glBindTexture(GL_TEXTURE_2D, tex);
978 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
979 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
980 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
981 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
982 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, controller_360_width, controller_360_height, 0, GL_BGRA, GL_UNSIGNED_BYTE, controller_360_buf);
983 controller_360_image = nk_image_id((int)tex);
984 }
985 }
986
987 static void context_created(void)
988 {
989 nk_sdl_device_create();
990 texture_init();
971 } 991 }
972 992
973 void show_pause_menu(void) 993 void show_pause_menu(void)
974 { 994 {
975 context->style.window.background = nk_rgba(0, 0, 0, 128); 995 context->style.window.background = nk_rgba(0, 0, 0, 128);
999 1019
1000 void blastem_nuklear_init(uint8_t file_loaded) 1020 void blastem_nuklear_init(uint8_t file_loaded)
1001 { 1021 {
1002 context = nk_sdl_init(render_get_window()); 1022 context = nk_sdl_init(render_get_window());
1003 1023
1004 struct nk_font_atlas *atlas; 1024 uint32_t buf_size;
1005 nk_sdl_font_stash_begin(&atlas); 1025 uint8_t *buf = (uint8_t *)read_bundled_file("images/360.png", &buf_size);
1006 //char *font = default_font_path(); 1026 if (buf) {
1007 uint32_t font_size; 1027 controller_360_buf = load_png(buf, buf_size, &controller_360_width, &controller_360_height);
1008 uint8_t *font = default_font(&font_size); 1028 free(buf);
1009 if (!font) { 1029 }
1010 fatal_error("Failed to find default font path\n"); 1030 texture_init();
1011 } 1031
1012 //struct nk_font *def_font = nk_font_atlas_add_from_file(atlas, font, 30, NULL);
1013 struct nk_font *def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, 30, NULL);
1014 nk_sdl_font_stash_end();
1015 nk_style_set_font(context, &def_font->handle);
1016 current_view = file_loaded ? view_play : view_menu; 1032 current_view = file_loaded ? view_play : view_menu;
1017 render_set_ui_render_fun(blastem_nuklear_render); 1033 render_set_ui_render_fun(blastem_nuklear_render);
1018 render_set_event_handler(handle_event); 1034 render_set_event_handler(handle_event);
1019 render_set_gl_context_handlers(context_destroyed, context_created); 1035 render_set_gl_context_handlers(context_destroyed, context_created);
1020 1036
1021 FILE *f = fopen("images/360.png", "rb"); 1037
1022 long buf_size = file_size(f);
1023 uint8_t *buf = malloc(buf_size);
1024 if (buf_size == fread(buf, 1, buf_size, f)) {
1025 uint32_t width, height;
1026 uint32_t *pixels = load_png(buf, buf_size, &width, &height);
1027 if (pixels) {
1028 GLuint tex;
1029 glGenTextures(1, &tex);
1030 glBindTexture(GL_TEXTURE_2D, tex);
1031 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1032 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1033 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1034 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1035 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
1036 free(pixels);
1037 controller_image = nk_image_id((int)tex);
1038 }
1039 }
1040 free(buf);
1041 1038
1042 active = 1; 1039 active = 1;
1043 ui_idle_loop(); 1040 ui_idle_loop();
1044 } 1041 }