comparison nuklear_ui/blastem_nuklear.c @ 1825:56a1171e29b9

Allow Nuklear UI to be used when OpenGL is disabled
author Michael Pavone <pavone@retrodev.com>
date Thu, 04 Apr 2019 23:08:45 -0700
parents 1db1510c7506
children c945a25aa75c
comparison
equal deleted inserted replaced
1824:62dd62c83b05 1825:56a1171e29b9
1 #define NK_IMPLEMENTATION 1 #define NK_IMPLEMENTATION
2 #define NK_SDL_GLES2_IMPLEMENTATION 2 #define NK_SDL_GLES2_IMPLEMENTATION
3 #define NK_RAWFB_IMPLEMENTATION
4 #define RAWFB_RGBX_8888
3 5
4 #include <stdlib.h> 6 #include <stdlib.h>
5 #include <limits.h> 7 #include <limits.h>
6 #include "blastem_nuklear.h" 8 #include "blastem_nuklear.h"
9 #include "nuklear_rawfb.h"
7 #include "font.h" 10 #include "font.h"
8 #include "../render.h" 11 #include "../render.h"
9 #include "../render_sdl.h" 12 #include "../render_sdl.h"
10 #include "../util.h" 13 #include "../util.h"
11 #include "../paths.h" 14 #include "../paths.h"
16 #include "../png.h" 19 #include "../png.h"
17 #include "../controller_info.h" 20 #include "../controller_info.h"
18 #include "../bindings.h" 21 #include "../bindings.h"
19 22
20 static struct nk_context *context; 23 static struct nk_context *context;
24 static struct rawfb_context *fb_context;
21 25
22 typedef struct 26 typedef struct
23 { 27 {
24 uint32_t *image_data; 28 uint32_t *image_data;
25 uint32_t width, height; 29 uint32_t width, height;
1897 void blastem_nuklear_render(void) 1901 void blastem_nuklear_render(void)
1898 { 1902 {
1899 if (current_view != view_play) { 1903 if (current_view != view_play) {
1900 nk_input_end(context); 1904 nk_input_end(context);
1901 current_view(context); 1905 current_view(context);
1902 nk_sdl_render(NK_ANTI_ALIASING_ON, 512 * 1024, 128 * 1024); 1906 if (fb_context) {
1907 fb_context->fb.pixels = render_get_framebuffer(FRAMEBUFFER_UI, &fb_context->fb.pitch);
1908 nk_rawfb_render(fb_context, nk_rgb(0,0,0), 0);
1909 render_framebuffer_updated(FRAMEBUFFER_UI, render_width());
1910 } else {
1911 #ifndef DISABLE_OPENGL
1912 nk_sdl_render(NK_ANTI_ALIASING_ON, 512 * 1024, 128 * 1024);
1913 #endif
1914 }
1903 nk_input_begin(context); 1915 nk_input_begin(context);
1904 } 1916 }
1905 } 1917 }
1906 1918
1907 void ui_idle_loop(void) 1919 void ui_idle_loop(void)
1955 nk_sdl_shutdown(); 1967 nk_sdl_shutdown();
1956 context = NULL; 1968 context = NULL;
1957 } 1969 }
1958 } 1970 }
1959 1971
1972 static void fb_resize(void)
1973 {
1974 nk_rawfb_resize_fb(fb_context, NULL, render_width(), render_height(), 0);
1975 }
1976
1977 #ifndef DISABLE_OPENGL
1960 static struct nk_image load_image_texture(uint32_t *buf, uint32_t width, uint32_t height) 1978 static struct nk_image load_image_texture(uint32_t *buf, uint32_t width, uint32_t height)
1961 { 1979 {
1962 GLuint tex; 1980 GLuint tex;
1963 glGenTextures(1, &tex); 1981 glGenTextures(1, &tex);
1964 glBindTexture(GL_TEXTURE_2D, tex); 1982 glBindTexture(GL_TEXTURE_2D, tex);
1971 #else 1989 #else
1972 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, buf); 1990 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, buf);
1973 #endif 1991 #endif
1974 return nk_image_id((int)tex); 1992 return nk_image_id((int)tex);
1975 } 1993 }
1994 #endif
1995
1996 static struct nk_image load_image_rawfb(uint32_t *buf, uint32_t width, uint32_t height)
1997 {
1998 struct rawfb_image *fbimg = calloc(1, sizeof(struct rawfb_image));
1999 fbimg->pixels = buf;
2000 fbimg->pitch = width * sizeof(uint32_t);
2001 fbimg->w = width;
2002 fbimg->h = height;
2003 fbimg->format = NK_FONT_ATLAS_RGBA32;
2004 return nk_image_ptr(fbimg);
2005 }
1976 2006
1977 static void texture_init(void) 2007 static void texture_init(void)
1978 { 2008 {
1979 struct nk_font_atlas *atlas; 2009 struct nk_font_atlas *atlas;
1980 nk_sdl_font_stash_begin(&atlas); 2010 if (fb_context) {
2011 nk_rawfb_font_stash_begin(fb_context, &atlas);
2012 } else {
2013 #ifndef DISABLE_OPENGL
2014 nk_sdl_font_stash_begin(&atlas);
2015 #endif
2016 }
1981 uint32_t font_size; 2017 uint32_t font_size;
1982 uint8_t *font = default_font(&font_size); 2018 uint8_t *font = default_font(&font_size);
1983 if (!font) { 2019 if (!font) {
1984 fatal_error("Failed to find default font path\n"); 2020 fatal_error("Failed to find default font path\n");
1985 } 2021 }
1986 def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, render_height() / 16, NULL); 2022 def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, render_height() / 16, NULL);
1987 free(font); 2023 free(font);
1988 nk_sdl_font_stash_end(); 2024 if (fb_context) {
2025 nk_rawfb_font_stash_end(fb_context);
2026 } else {
2027 #ifndef DISABLE_OPENGL
2028 nk_sdl_font_stash_end();
2029 #endif
2030 }
1989 nk_style_set_font(context, &def_font->handle); 2031 nk_style_set_font(context, &def_font->handle);
1990 for (uint32_t i = 0; i < num_ui_images; i++) 2032 for (uint32_t i = 0; i < num_ui_images; i++)
1991 { 2033 {
1992 ui_images[i]->ui = load_image_texture(ui_images[i]->image_data, ui_images[i]->width, ui_images[i]->height); 2034 #ifndef DISABLE_OPENGL
2035 if (fb_context) {
2036 #endif
2037 ui_images[i]->ui = load_image_rawfb(ui_images[i]->image_data, ui_images[i]->width, ui_images[i]->height);
2038 #ifndef DISABLE_OPENGL
2039 } else {
2040 ui_images[i]->ui = load_image_texture(ui_images[i]->image_data, ui_images[i]->width, ui_images[i]->height);
2041 }
2042 #endif
1993 } 2043 }
1994 } 2044 }
1995 2045
1996 static void style_init(void) 2046 static void style_init(void)
1997 { 2047 {
2038 return active; 2088 return active;
2039 } 2089 }
2040 2090
2041 uint8_t is_nuklear_available(void) 2091 uint8_t is_nuklear_available(void)
2042 { 2092 {
2043 if (!render_has_gl()) { 2093 /*if (!render_has_gl()) {
2044 //currently no fallback if GL2 unavailable 2094 //currently no fallback if GL2 unavailable
2045 return 0; 2095 return 0;
2046 } 2096 }*/
2047 char *style = tern_find_path(config, "ui\0style\0", TVAL_PTR).ptrval; 2097 char *style = tern_find_path(config, "ui\0style\0", TVAL_PTR).ptrval;
2048 if (!style) { 2098 if (!style) {
2049 return 1; 2099 return 1;
2050 } 2100 }
2051 return strcmp(style, "rom") != 0; 2101 return strcmp(style, "rom") != 0;
2091 } 2141 }
2092 2142
2093 void blastem_nuklear_init(uint8_t file_loaded) 2143 void blastem_nuklear_init(uint8_t file_loaded)
2094 { 2144 {
2095 context = nk_sdl_init(render_get_window()); 2145 context = nk_sdl_init(render_get_window());
2146 #ifndef DISABLE_OPENGL
2147 if (render_has_gl()) {
2148 nk_sdl_device_create();
2149 } else {
2150 #endif
2151 fb_context = nk_rawfb_init(NULL, context, render_width(), render_height(), 0);
2152 render_set_ui_fb_resize_handler(fb_resize);
2153 #ifndef DISABLE_OPENGL
2154 }
2155 #endif
2096 style_init(); 2156 style_init();
2097 2157
2098 controller_360 = load_ui_image("images/360.png"); 2158 controller_360 = load_ui_image("images/360.png");
2099 controller_ps4 = load_ui_image("images/ps4.png"); 2159 controller_ps4 = load_ui_image("images/ps4.png");
2100 controller_ps4_6b = load_ui_image("images/ps4_6b.png"); 2160 controller_ps4_6b = load_ui_image("images/ps4_6b.png");