Skip to content

Commit

Permalink
Associate native window to NFD parent window
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Jan 5, 2025
1 parent d9eb331 commit 27cad8e
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 26 deletions.
31 changes: 15 additions & 16 deletions platforms/desktop-shared/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#define APPLICATION_IMPORT
#include "application.h"

static SDL_Window* sdl_window;
static SDL_GLContext gl_context;
static bool running = true;
static bool paused_when_focus_lost = false;
Expand Down Expand Up @@ -65,7 +64,7 @@ int application_init(const char* rom_file, const char* symbol_file)

gui_init();

ImGui_ImplSDL2_InitForOpenGL(sdl_window, gl_context);
ImGui_ImplSDL2_InitForOpenGL(application_sdl_window, gl_context);

renderer_init();

Expand Down Expand Up @@ -124,12 +123,12 @@ void application_trigger_quit(void)

void application_trigger_fullscreen(bool fullscreen)
{
SDL_SetWindowFullscreen(sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
SDL_SetWindowFullscreen(application_sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
}

void application_trigger_fit_to_content(int width, int height)
{
SDL_SetWindowSize(sdl_window, width, height);
SDL_SetWindowSize(application_sdl_window, width, height);
}

static int sdl_init(void)
Expand Down Expand Up @@ -157,12 +156,12 @@ static int sdl_init(void)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);

sdl_window = SDL_CreateWindow(GEARCOLECO_TITLE " " GEARCOLECO_VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, config_emulator.window_width, config_emulator.window_height, window_flags);
gl_context = SDL_GL_CreateContext(sdl_window);
SDL_GL_MakeCurrent(sdl_window, gl_context);
application_sdl_window = SDL_CreateWindow(GEARCOLECO_TITLE " " GEARCOLECO_VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, config_emulator.window_width, config_emulator.window_height, window_flags);
gl_context = SDL_GL_CreateContext(application_sdl_window);
SDL_GL_MakeCurrent(application_sdl_window, gl_context);
SDL_GL_SetSwapInterval(0);

SDL_SetWindowMinimumSize(sdl_window, 500, 300);
SDL_SetWindowMinimumSize(application_sdl_window, 500, 300);

application_gamepad_mappings = SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile("gamecontrollerdb.txt", "rb"), 1);

Expand Down Expand Up @@ -199,8 +198,8 @@ static int sdl_init(void)

int w, h;
int display_w, display_h;
SDL_GetWindowSize(sdl_window, &w, &h);
SDL_GL_GetDrawableSize(sdl_window, &display_w, &display_h);
SDL_GetWindowSize(application_sdl_window, &w, &h);
SDL_GL_GetDrawableSize(application_sdl_window, &display_w, &display_h);

if (w > 0 && h > 0)
{
Expand All @@ -220,7 +219,7 @@ static void sdl_destroy(void)
SDL_GameControllerClose(application_gamepad[0]);
SDL_GameControllerClose(application_gamepad[1]);
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(sdl_window);
SDL_DestroyWindow(application_sdl_window);
SDL_Quit();
}

Expand Down Expand Up @@ -254,7 +253,7 @@ static void sdl_events(void)
break;
}

if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(sdl_window))
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(application_sdl_window))
{
running = false;
break;
Expand Down Expand Up @@ -362,7 +361,7 @@ static void sdl_events_emu(const SDL_Event* event)
char* dropped_filedir = event->drop.file;
gui_load_rom(dropped_filedir);
SDL_free(dropped_filedir);
SDL_SetWindowInputFocus(sdl_window);
SDL_SetWindowInputFocus(application_sdl_window);
}
break;

Expand Down Expand Up @@ -755,7 +754,7 @@ static void run_emulator(void)

char title[256];
snprintf(title, sizeof(title), "%s %s - %s", GEARCOLECO_TITLE, GEARCOLECO_VERSION, emu_get_core()->GetCartridge()->GetFileName());
SDL_SetWindowTitle(sdl_window, title);
SDL_SetWindowTitle(application_sdl_window, title);
}
}
config_emulator.paused = emu_is_paused();
Expand All @@ -771,7 +770,7 @@ static void render(void)
renderer_render();
renderer_end_render();

SDL_GL_SwapWindow(sdl_window);
SDL_GL_SwapWindow(application_sdl_window);
}

static void frame_throttle(void)
Expand Down Expand Up @@ -820,7 +819,7 @@ static void save_window_size(void)
if (!config_emulator.fullscreen)
{
int width, height;
SDL_GetWindowSize(sdl_window, &width, &height);
SDL_GetWindowSize(application_sdl_window, &width, &height);
config_emulator.window_width = width;
config_emulator.window_height = height;
}
Expand Down
1 change: 1 addition & 0 deletions platforms/desktop-shared/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define EXTERN extern
#endif

EXTERN SDL_Window* application_sdl_window;
EXTERN SDL_GameController* application_gamepad[2];
EXTERN int application_gamepad_mappings;
EXTERN float application_display_scale;
Expand Down
83 changes: 74 additions & 9 deletions platforms/desktop-shared/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "imgui/fonts/RobotoMedium.h"
#include "imgui/keyboard.h"
#include "nfd/nfd.h"
#include "nfd/nfd_sdl2.h"
#include "config.h"
#include "emu.h"
#include "../../src/gearcoleco.h"
Expand Down Expand Up @@ -63,6 +64,7 @@ static void file_dialog_choose_savestate_path(void);
static void file_dialog_load_bios(void);
static void file_dialog_load_symbols(void);
static void file_dialog_save_screenshot(void);
static void file_dialog_set_native_window(SDL_Window* window, nfdwindowhandle_t* native_window);
static void keyboard_configuration_item(const char* text, SDL_Scancode* key, int player);
static void gamepad_configuration_item(const char* text, int* button, int player);
static void popup_modal_keyboard();
Expand Down Expand Up @@ -1120,7 +1122,13 @@ static void file_dialog_open_rom(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "ROM Files", "col,cv,rom,bin,zip" } };
nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 1, config_emulator.last_open_path.c_str());
nfdopendialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = config_emulator.last_open_path.c_str();
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
std::string path = outPath;
Expand All @@ -1139,7 +1147,13 @@ static void file_dialog_load_ram(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "RAM Files", "sav" } };
nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 1, NULL);
nfdopendialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = config_emulator.last_open_path.c_str();
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
Cartridge::ForceConfiguration config;
Expand All @@ -1159,7 +1173,14 @@ static void file_dialog_save_ram(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "RAM Files", "sav" } };
nfdresult_t result = NFD_SaveDialog(&outPath, filterItem, 1, NULL, NULL);
nfdsavedialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = config_emulator.last_open_path.c_str();
args.defaultName = NULL;
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_SaveDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
emu_save_ram(outPath);
Expand All @@ -1175,7 +1196,13 @@ static void file_dialog_load_state(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "Save State Files", "state" } };
nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 1, NULL);
nfdopendialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = config_emulator.last_open_path.c_str();
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
std::string message("Loading state from ");
Expand All @@ -1194,7 +1221,14 @@ static void file_dialog_save_state(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "Save State Files", "state" } };
nfdresult_t result = NFD_SaveDialog(&outPath, filterItem, 1, NULL, NULL);
nfdsavedialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = config_emulator.last_open_path.c_str();
args.defaultName = NULL;
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_SaveDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
std::string message("Saving state to ");
Expand All @@ -1212,7 +1246,11 @@ static void file_dialog_save_state(void)
static void file_dialog_choose_savestate_path(void)
{
nfdchar_t *outPath;
nfdresult_t result = NFD_PickFolder(&outPath, savestates_path);
nfdpickfolderu8args_t args = { };
args.defaultPath = savestates_path;
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_PickFolderU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
strcpy(savestates_path, outPath);
Expand All @@ -1229,7 +1267,13 @@ static void file_dialog_load_bios(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "BIOS Files", "bin,rom,bios,cv,col" } };
nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 1, NULL);
nfdopendialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = config_emulator.last_open_path.c_str();
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
strcpy(bios_path, outPath);
Expand All @@ -1247,7 +1291,13 @@ static void file_dialog_load_symbols(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "Symbol Files", "sym" } };
nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 1, NULL);
nfdopendialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = NULL;
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
gui_debug_reset_symbols();
Expand All @@ -1264,7 +1314,14 @@ static void file_dialog_save_screenshot(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "PNG Files", "png" } };
nfdresult_t result = NFD_SaveDialog(&outPath, filterItem, 1, NULL, NULL);
nfdsavedialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = NULL;
args.defaultName = NULL;
file_dialog_set_native_window(application_sdl_window, &args.parentWindow);

nfdresult_t result = NFD_SaveDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
call_save_screenshot(outPath);
Expand All @@ -1276,6 +1333,14 @@ static void file_dialog_save_screenshot(void)
}
}

static void file_dialog_set_native_window(SDL_Window* window, nfdwindowhandle_t* native_window)
{
if (!NFD_GetNativeWindowFromSDLWindow(window, native_window))
{
Log("NFD_GetNativeWindowFromSDLWindow failed: %s\n", SDL_GetError());
}
}

static void keyboard_configuration_item(const char* text, SDL_Scancode* key, int player)
{
ImGui::Text("%s", text);
Expand Down
14 changes: 13 additions & 1 deletion platforms/desktop-shared/gui_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include "imgui/memory_editor.h"
#include "imgui/colors.h"
#include "nfd/nfd.h"
#include "nfd/nfd_sdl2.h"
#include "config.h"
#include "emu.h"
#include "renderer.h"
#include "../../src/gearcoleco.h"
#include "gui.h"
#include "gui_debug_constants.h"
#include "application.h"

#define GUI_DEBUG_IMPORT
#include "gui_debug.h"
Expand Down Expand Up @@ -218,7 +220,17 @@ static void memory_editor_menu(void)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "Memory Dump Files", "txt" } };
nfdresult_t result = NFD_SaveDialog(&outPath, filterItem, 1, NULL, NULL);
nfdsavedialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
args.defaultPath = NULL;
args.defaultName = NULL;
if (!NFD_GetNativeWindowFromSDLWindow(application_sdl_window, &args.parentWindow))
{
Log("NFD_GetNativeWindowFromSDLWindow failed: %s\n", SDL_GetError());
}

nfdresult_t result = NFD_SaveDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
mem_edit[current_mem_edit].SaveToFile(outPath);
Expand Down
Loading

0 comments on commit 27cad8e

Please sign in to comment.