From da2a9424154e7825d39e74375b08cd7f4ca415e2 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Thu, 14 Sep 2023 18:47:42 +0300 Subject: [PATCH] better event handling --- src/emulator.cpp | 54 +++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/emulator.cpp b/src/emulator.cpp index 2c0a0e54..4dc11e0b 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -5,6 +5,7 @@ namespace Emulator { static WindowCtx* g_window_ctx = nullptr; +bool m_emu_needs_exit = false; void emuInit(u32 width, u32 height) { g_window_ctx = new WindowCtx; @@ -23,17 +24,17 @@ static void CreateSdlWindow(WindowCtx* ctx) { } ctx->m_window = SDL_CreateWindowWithPosition("shadps4", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, - (static_cast(SDL_WINDOW_HIDDEN) | static_cast(SDL_WINDOW_VULKAN))); + (static_cast(SDL_WINDOW_HIDDEN) | static_cast(SDL_WINDOW_VULKAN))); - ctx->is_window_hidden = true;//hide window until we need to show something (should draw something in buffers) + ctx->is_window_hidden = true; // hide window until we need to show something (should draw something in buffers) if (ctx->m_window == nullptr) { printf("%s\n", SDL_GetError()); std::_Exit(0); } - SDL_SetWindowResizable(ctx->m_window, SDL_FALSE);//we don't support resizable atm - SDL_ShowWindow(g_window_ctx->m_window);//TODO should be removed just left it over to make it fancy :D + SDL_SetWindowResizable(ctx->m_window, SDL_FALSE); // we don't support resizable atm + SDL_ShowWindow(g_window_ctx->m_window); // TODO should be removed just left it over to make it fancy :D } void emuRun() { g_window_ctx->m_mutex.LockMutex(); @@ -44,25 +45,40 @@ void emuRun() { g_window_ctx->m_graphic_initialized_cond.SignalCondVar(); } g_window_ctx->m_mutex.UnlockMutex(); - - auto* pdata = new EmuPrivate; - pdata->m_graphic_ctx = static_cast(&g_window_ctx->m_graphic_ctx); - - pdata->data1 = pdata; - pdata->data2 = new SDL_Event; - - pdata->m_screen_width = pdata->m_graphic_ctx->screen_width; - pdata->m_screen_height = pdata->m_graphic_ctx->screen_height; + bool exit_loop = false; for (;;) { - SDL_Event* ev = static_cast(pdata->data2); - if (SDL_PollEvent(ev)!= 0) - { - printf("Event: 0x%04\n", ev->type); - printf("]"); + if (exit_loop) { + break; + } + + SDL_Event event; + if (SDL_PollEvent(&event) != 0) { + printf("Event: 0x%04\n", event.type); + switch (event.type) { + case SDL_EVENT_QUIT: m_emu_needs_exit = true; break; + + case SDL_EVENT_TERMINATING: m_emu_needs_exit = true; break; + + case SDL_EVENT_WILL_ENTER_BACKGROUND: break; + + case SDL_EVENT_DID_ENTER_BACKGROUND: break; + + case SDL_EVENT_WILL_ENTER_FOREGROUND: break; + + case SDL_EVENT_DID_ENTER_FOREGROUND: break; + + case SDL_EVENT_KEY_DOWN: + case SDL_EVENT_KEY_UP: break; + } + continue; + } + exit_loop = m_emu_needs_exit; + + if (!exit_loop) { + HLE::Libs::Graphics::VideoOut::videoOutFlip(100000); // flip every 0.1 sec } - HLE::Libs::Graphics::VideoOut::videoOutFlip(100000); // flip every 0.1 sec } std::_Exit(0); }