Skip to content

Commit

Permalink
Merge pull request #5 from Kim-Dewelski/SDL-main-callback
Browse files Browse the repository at this point in the history
Sdl main callback
  • Loading branch information
coornio authored Aug 26, 2024
2 parents 819b272 + 4971f40 commit cd718ef
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 130 deletions.
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ add_executable(CubeChipSDL
src/HostClass/HomeDirManager.cpp
src/HostClass/HostFunctions.cpp
src/HostClass/BasicVideoSpec.cpp
src/GuestClass/GuestFunctions.cpp
# src/GuestClass/GuestFunctions.cpp
src/GuestClass/GameFileChecker.cpp
src/GuestClass/HexInput.cpp
src/GuestClass/InstructionSets/_LegacySC.cpp
src/GuestClass/InstructionSets/_Gigachip.cpp
src/GuestClass/InstructionSets/_Classic8.cpp
src/GuestClass/InstructionSets/_ModernXO.cpp
src/GuestClass/InstructionSets/_Megachip.cpp
# src/GuestClass/InstructionSets/_LegacySC.cpp
# src/GuestClass/InstructionSets/_Gigachip.cpp
# src/GuestClass/InstructionSets/_Classic8.cpp
# src/GuestClass/InstructionSets/_ModernXO.cpp
# src/GuestClass/InstructionSets/_Megachip.cpp
src/GuestClass/EmuCores/CHIP8_MODERN.cpp
src/GuestClass/EmuCores/EmuCores.cpp
src/GuestClass/Init.cpp
# src/GuestClass/Init.cpp
src/Assistants/BasicLogger.cpp
src/Assistants/BasicInput.cpp
src/Assistants/FrameLimiter.cpp
Expand Down
1 change: 0 additions & 1 deletion src/Assistants/BasicLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ void BasicLogger::newEntry(const BLOG type, const std::string& message) noexcept
output << ++lineCount << " :: " << getSeverity(type) << " :: " << message;

std::cout << output.str() << std::endl;
if (lineCount % 10 == 0) [[unlikely]] { std::cout.flush(); }

std::ofstream logFile(mLogPath, std::ios::app);
if (!logFile) {
Expand Down
77 changes: 60 additions & 17 deletions src/CubeChip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "Assistants/FrameLimiter.hpp"
#include <SDL3/SDL.h>
#include <SDL3/SDL_init.h>

#ifdef SDL_PLATFORM_WIN32
#include <SDL3/SDL_main.h>
#endif
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL_main.h>

#include <optional>
#include <memory>
#include <mutex>

#include "HostClass/HomeDirManager.hpp"
#include "HostClass/BasicVideoSpec.hpp"
#include "HostClass/BasicAudioSpec.hpp"
#include "Assistants/BasicInput.hpp"

#include "HostClass/Host.hpp"

int main(int argc, char* argv[]) {
struct {
std::optional<HomeDirManager> HDM;
std::optional<BasicVideoSpec> BVS;
std::optional<BasicAudioSpec> BAS;
FrameLimiter Limiter;
std::unique_ptr<VM_Host> Host;
std::mutex Mut;
} global;

SDL_AppResult SDL_AppInit(void **appstate, int argc, char* argv[]) {

atexit(SDL_Quit);
SDL_InitSubSystem(SDL_INIT_EVENTS);

// VS OTHER
#if !defined(NDEBUG) || defined(DEBUG)
Expand All @@ -44,25 +57,55 @@ int main(int argc, char* argv[]) {

#ifdef SDL_PLATFORM_WIN32
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d");
#endif
SDL_SetHint(SDL_HINT_WINDOWS_RAW_KEYBOARD, "0");
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "0"); // until the UI is independent
#endif
SDL_SetHint(SDL_HINT_APP_NAME, "CubeChip");

std::optional<HomeDirManager> HDM;
std::optional<BasicVideoSpec> BVS;
std::optional<BasicAudioSpec> BAS;
global.Mut.lock();

try {
HDM.emplace("CubeChip_SDL");
BVS.emplace();
BAS.emplace();
} catch (...) { return EXIT_FAILURE; }
global.HDM.emplace("CubeChip_SDL");
global.BVS.emplace();
global.BAS.emplace();
} catch (...) { return SDL_APP_FAILURE; }

VM_Host Host(
global.Host = std::make_unique<VM_Host>(
argc <= 1 ? nullptr : argv[1],
*HDM, *BVS, *BAS
*global.HDM, *global.BVS, *global.BAS
);
global.Host->prepareGuest(global.Limiter);

global.Mut.unlock();

return Host.runHost();
return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppIterate(void* appstate) {
global.Mut.lock();

if (!global.Host->runFrame(global.Limiter)) {
global.Mut.unlock();
return SDL_APP_SUCCESS;
}

global.Host->BVS.renderPresent();

global.Mut.unlock();

return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event) {
global.Mut.lock();

if (!global.Host->handleEventSDL(global.Limiter, event)) {
global.Mut.unlock();
return SDL_APP_SUCCESS;
}

global.Mut.unlock();

return SDL_APP_CONTINUE;
}

void SDL_AppQuit(void *appstate) {}
2 changes: 2 additions & 0 deletions src/GuestClass/EmuCores/CHIP8_MODERN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void CHIP8_MODERN::handlePreFrameInterrupt() noexcept {
mCyclesPerFrame = 0;
}
return;
default:;
}
}

Expand All @@ -81,6 +82,7 @@ void CHIP8_MODERN::handleEndFrameInterrupt() noexcept {
case Interrupt::FINAL:
mCyclesPerFrame = 0;
return;
default:;
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/HostClass/Host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,26 @@ class BasicAudioSpec;
class FrameLimiter;
class EmuInterface;

union SDL_Event;

class VM_Host final {

std::unique_ptr<EmuInterface>
iGuest;

HomeDirManager& HDM;
BasicVideoSpec& BVS;
BasicAudioSpec& BAS;

bool _doBench{};

[[nodiscard]]
bool doBench() const noexcept;
void doBench(bool) noexcept;

void prepareGuest(FrameLimiter&);
bool eventLoopSDL(FrameLimiter&);

bool initGameCore();

public:
HomeDirManager& HDM;
BasicVideoSpec& BVS;
BasicAudioSpec& BAS;

explicit VM_Host(
const char* const,
HomeDirManager&,
Expand All @@ -42,5 +41,7 @@ class VM_Host final {
);
~VM_Host();

bool runHost();
void prepareGuest(FrameLimiter&);
bool handleEventSDL(FrameLimiter&, const SDL_Event*);
bool runFrame(FrameLimiter& Limiter);
};
Loading

0 comments on commit cd718ef

Please sign in to comment.