Skip to content

Commit

Permalink
load configuration before showing window so it can be created at the …
Browse files Browse the repository at this point in the history
…remembered location (#376)
  • Loading branch information
Jamiras authored May 20, 2023
1 parent 038481c commit 5dc3aab
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 51 deletions.
115 changes: 65 additions & 50 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ bool Application::init(const char* title, int width, int height)
kConfigInited,
kAllocatorInited,
kSdlInited,
kKeyBindsInited,
kWindowInited,
kAudioDeviceInited,
kFifoInited,
kAudioInited,
kInputInited,
kKeyBindsInited,
kVideoContextInited,
kGlInited,
kVideoInited
Expand Down Expand Up @@ -147,28 +147,70 @@ bool Application::init(const char* title, int width, int height)

inited = kSdlInited;

// Setup window
if (SDL_GL_LoadLibrary(NULL) != 0)
// keybinds must be initialized before calling loadConfiguration
if (!_keybinds.init(&_logger))
{
goto error;
}

inited = kKeyBindsInited;

// states must be initialized before calling loadConfiguration
if (!_states.init(&_logger, &_config, &_video))
{
_logger.error(TAG "SDL_GL_LoadLibrary: %s", SDL_GetError());
goto error;
}

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
// Load the configuration from previous runs - primarily looking for the window size/location.
{
int window_x = SDL_WINDOWPOS_CENTERED, window_y = SDL_WINDOWPOS_CENTERED;

loadConfiguration(&window_x, &window_y, &width, &height);
if (window_y != SDL_WINDOWPOS_CENTERED)
{
// captured window position includes menu bar, which won't exist at initial positioning
// adjust for it.
window_y -= GetSystemMetrics(SM_CYMENU);
}

// Setup window
if (SDL_GL_LoadLibrary(NULL) != 0)
{
_logger.error(TAG "SDL_GL_LoadLibrary: %s", SDL_GetError());
goto error;
}

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);

_window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
_window = SDL_CreateWindow(title, window_x, window_y, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
}

if (_window == NULL)
{
_logger.error(TAG "SDL_CreateWindow: %s", SDL_GetError());
goto error;
}
else
{
SDL_SysWMinfo wminfo;
SDL_VERSION(&wminfo.version);

if (SDL_GetWindowWMInfo(_window, &wminfo) != SDL_TRUE)
{
_logger.error(TAG "SDL_GetWindowWMInfo: %s", SDL_GetError());
goto error;
}

g_mainWindow = wminfo.info.win.window;
_menu = LoadMenu(NULL, "MAIN");
SetMenu(g_mainWindow, _menu);
}

inited = kWindowInited;

Expand Down Expand Up @@ -221,13 +263,6 @@ bool Application::init(const char* title, int width, int height)

inited = kInputInited;

if (!_keybinds.init(&_logger))
{
goto error;
}

inited = kKeyBindsInited;

if (!_videoContext.init(&_logger, _window))
{
goto error;
Expand All @@ -254,31 +289,16 @@ bool Application::init(const char* title, int width, int height)
inited = kVideoInited;

{
SDL_SysWMinfo wminfo;
SDL_VERSION(&wminfo.version);

if (SDL_GetWindowWMInfo(_window, &wminfo) != SDL_TRUE)
{
_logger.error(TAG "SDL_GetWindowWMInfo: %s", SDL_GetError());
goto error;
}

g_mainWindow = wminfo.info.win.window;

_menu = LoadMenu(NULL, "MAIN");
_cdRomMenu = GetSubMenu(GetSubMenu(_menu, 0), CDROM_MENU_INDEX);
assert(GetMenuItemID(_cdRomMenu, 0) == IDM_CD_OPEN_TRAY);

SetMenu(g_mainWindow, _menu);

if (!loadCores(&_config, &_logger))
{
MessageBox(g_mainWindow, "Could not open Cores\\cores.json.", "Initialization failed", MB_OK);
goto error;
}

buildSystemsMenu();
loadConfiguration();

extern void RA_Init(HWND hwnd);
RA_Init(g_mainWindow);
Expand All @@ -289,11 +309,6 @@ bool Application::init(const char* title, int width, int height)
goto error;
}

if (!_states.init(&_logger, &_config, &_video))
{
goto error;
}

SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
_coreName.clear();
_gameData = NULL;
Expand All @@ -311,12 +326,12 @@ bool Application::init(const char* title, int width, int height)
case kVideoInited: _video.destroy();
case kGlInited: // nothing to undo
case kVideoContextInited: _videoContext.destroy();
case kKeyBindsInited: _keybinds.destroy();
case kInputInited: _input.destroy();
case kAudioInited: _audio.destroy();
case kFifoInited: _fifo.destroy();
case kAudioDeviceInited: SDL_CloseAudioDevice(_audioDev);
case kWindowInited: SDL_DestroyWindow(_window);
case kKeyBindsInited: _keybinds.destroy();
case kSdlInited: SDL_Quit();
case kAllocatorInited: _allocator.destroy();
case kConfigInited: _config.destroy();
Expand Down Expand Up @@ -1906,7 +1921,7 @@ void Application::buildSystemsMenu()
}
}

void Application::loadConfiguration()
void Application::loadConfiguration(int* window_x, int* window_y, int* window_width, int* window_height)
{
_recentList.clear();
_logger.debug(TAG "Recent file list cleared");
Expand Down Expand Up @@ -2016,15 +2031,6 @@ void Application::loadConfiguration()
else if (ud->key == "h")
ud->h = (int)strtoul(str, NULL, 10);
}
else if (event == JSONSAX_OBJECT && num == 0)
{
if (ud->w > 0 && ud->h > 0)
{
SDL_SetWindowPosition(ud->self->_window, ud->x, ud->y);
SDL_SetWindowSize(ud->self->_window, ud->w, ud->h);
ud->self->_logger.debug(TAG "Remembered window position %d,%d (%dx%d)", ud->x, ud->y, ud->w, ud->h);
}
}

return 0;
});
Expand Down Expand Up @@ -2070,6 +2076,15 @@ void Application::loadConfiguration()
return 0;
});

if (ud.w > 0 && ud.h > 0)
{
_logger.debug(TAG "Remembered window position %d,%d (%dx%d)", ud.x, ud.y, ud.w, ud.h);
*window_x = ud.x;
*window_y = ud.y;
*window_width = ud.w;
*window_height = ud.h;
}

free(data);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Application
void handle(const SDL_MouseButtonEvent* button);
void handle(const KeyBinds::Action action, unsigned extra);
void buildSystemsMenu();
void loadConfiguration();
void loadConfiguration(int* window_x, int* window_y, int* window_width, int* window_height);
void saveConfiguration();
std::string serializeRecentList();
void toggleFastForwarding(unsigned extra);
Expand Down

0 comments on commit 5dc3aab

Please sign in to comment.