Skip to content

Commit

Permalink
changed to using sdl main callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
hazelwiss committed Aug 25, 2024
1 parent 76186fb commit 4971f40
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 115 deletions.
33 changes: 32 additions & 1 deletion src/CubeChip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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>

Expand All @@ -12,22 +13,28 @@

#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"

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[]) {

SDL_InitSubSystem(SDL_INIT_EVENTS);

// VS OTHER
#if !defined(NDEBUG) || defined(DEBUG)
{
Expand All @@ -52,9 +59,10 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char* argv[]) {
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d");
SDL_SetHint(SDL_HINT_WINDOWS_RAW_KEYBOARD, "0");
#endif
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "0"); // until the UI is independent
SDL_SetHint(SDL_HINT_APP_NAME, "CubeChip");

global.Mut.lock();

try {
global.HDM.emplace("CubeChip_SDL");
global.BVS.emplace();
Expand All @@ -65,15 +73,38 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char* argv[]) {
argc <= 1 ? nullptr : argv[1],
*global.HDM, *global.BVS, *global.BAS
);
global.Host->prepareGuest(global.Limiter);

global.Mut.unlock();

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;
}

Expand Down
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);
};
193 changes: 87 additions & 106 deletions src/HostClass/HostFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,106 +49,91 @@ bool VM_Host::initGameCore() {
return iGuest ? true : false;
}

bool VM_Host::runHost() {
FrameLimiter Frame;

bool VM_Host::runFrame(FrameLimiter& Limiter) {
using namespace bic;

prepareGuest(Frame);
if (!Limiter.checkTime()) [[likely]] { return true; }

while (true) {
if (!Frame.checkTime()) [[likely]] { continue; }
if (kb.isPressed(KEY(RIGHT))) {
BAS.changeVolume(+15);
}
if (kb.isPressed(KEY(LEFT))) {
BAS.changeVolume(-15);
}

if (eventLoopSDL(Frame)) {
return EXIT_SUCCESS;
if (iGuest) {
if (kb.isPressed(KEY(ESCAPE))) {
BVS.resetWindow();
GameFileChecker::delCore();
prepareGuest(Limiter);
// continue
return true;
}

if (kb.isPressed(KEY(RIGHT))) {
BAS.changeVolume(+15);
if (kb.isPressed(KEY(BACKSPACE))) {
if (HDM.validateGameFile(HDM.getFilePath().c_str())) {
prepareGuest(Limiter);
}
// continue
return true;
}
if (kb.isPressed(KEY(LEFT))) {
BAS.changeVolume(-15);
if (kb.isPressed(KEY(RSHIFT))) {
if (doBench()) {
doBench(false);
BVS.changeTitle(HDM.getFileStem().c_str());
std::cout << "\33[1;1H\33[3J" << std::endl;
} else {
doBench(true);
BVS.changeTitle(std::to_string(iGuest->fetchCPF()));
std::cout << "\33[1;1H\33[2J"
<< "Cycle time: . ms"
<< "\nTime since last frame: "
<< "\nCPF: ";
}
}

if (iGuest) {
if (kb.isPressed(KEY(ESCAPE))) {
BVS.resetWindow();
GameFileChecker::delCore();
prepareGuest(Frame);
continue;
}
if (kb.isPressed(KEY(BACKSPACE))) {
if (HDM.validateGameFile(HDM.getFilePath().c_str())) {
prepareGuest(Frame);
}
continue;
}
if (kb.isPressed(KEY(RSHIFT))) {
if (doBench()) {
doBench(false);
BVS.changeTitle(HDM.getFileStem().c_str());
std::cout << "\33[1;1H\33[3J" << std::endl;
} else {
doBench(true);
BVS.changeTitle(std::to_string(iGuest->fetchCPF()));
std::cout << "\33[1;1H\33[2J"
<< "Cycle time: . ms"
<< "\nTime since last frame: "
<<<<<<< HEAD
<< "\nIPF: ";
=======
<< "\nCPF: ";
>>>>>>> other/master
}
}
if (kb.isPressed(KEY(PAGEDOWN))){
BVS.changeFrameMultiplier(-1);
}
if (kb.isPressed(KEY(PAGEUP))) {
BVS.changeFrameMultiplier(+1);
}

if (kb.isPressed(KEY(PAGEDOWN))){
BVS.changeFrameMultiplier(-1);
if (doBench()) [[likely]] {
if (kb.isPressed(KEY(UP))) {
BVS.changeTitle(std::to_string(iGuest->changeCPF(+50'000)));
}
if (kb.isPressed(KEY(PAGEUP))) {
BVS.changeFrameMultiplier(+1);
if (kb.isPressed(KEY(DOWN))){
BVS.changeTitle(std::to_string(iGuest->changeCPF(-50'000)));
}

if (doBench()) [[likely]] {
if (kb.isPressed(KEY(UP))) {
BVS.changeTitle(std::to_string(iGuest->changeCPF(+50'000)));
}
if (kb.isPressed(KEY(DOWN))){
BVS.changeTitle(std::to_string(iGuest->changeCPF(-50'000)));
}

iGuest->processFrame();

if (Frame.getValidFrameCounter() & 0x1) {
const auto micros{ Frame.getElapsedMicrosSince() };
std::cout
<< "\33[1;12H"
<< std::setfill(' ') << std::setw(4) << micros / 1000
<< "\33[1C"
<< std::setfill('0') << std::setw(3) << micros % 1000
<< "\33[2;25H"
<< Frame.getElapsedMillisLast()
<< "\33[3;6H"
<<<<<<< HEAD
<< Guest.fetchCPF()
<< " "
=======
<< iGuest->fetchCPF() << " "
>>>>>>> other/master
<< std::endl;
}
} else { iGuest->processFrame(); }
} else {
if (kb.isPressed(KEY(ESCAPE))) {
return EXIT_SUCCESS;
iGuest->processFrame();

if (Limiter.getValidFrameCounter() & 0x1) {
const auto micros{ Limiter.getElapsedMicrosSince() };
std::cout
<< "\33[1;12H"
<< std::setfill(' ') << std::setw(4) << micros / 1000
<< "\33[1C"
<< std::setfill('0') << std::setw(3) << micros % 1000
<< "\33[2;25H"
<< Limiter.getElapsedMillisLast()
<< "\33[3;6H"
<< iGuest->fetchCPF() << " "
<< std::endl;
}
} else {
iGuest->processFrame();
}
} else {
if (kb.isPressed(KEY(ESCAPE))) {
// this one stops execution
return false;
}

BVS.renderPresent();

kb.updateCopy();
mb.updateCopy();
}

bic::kb.updateCopy();
bic::mb.updateCopy();
return true;
}

void VM_Host::prepareGuest(FrameLimiter& Frame) {
Expand All @@ -164,29 +149,25 @@ void VM_Host::prepareGuest(FrameLimiter& Frame) {
}
}

bool VM_Host::eventLoopSDL(FrameLimiter& Frame) {
SDL_Event Event;

while (SDL_PollEvent(&Event)) {
switch (Event.type) {
case SDL_EVENT_QUIT:
return true;
bool VM_Host::handleEventSDL(FrameLimiter& Frame, const SDL_Event* Event) {
switch (Event->type) {
case SDL_EVENT_QUIT:
return false;

case SDL_EVENT_DROP_FILE:
BVS.raiseWindow();
if (HDM.validateGameFile(Event.drop.data)) {
prepareGuest(Frame);
}
break;
case SDL_EVENT_DROP_FILE:
BVS.raiseWindow();
if (HDM.validateGameFile(Event->drop.data)) {
prepareGuest(Frame);
}
break;

case SDL_EVENT_WINDOW_MINIMIZED:
if (iGuest) { iGuest->isSystemStopped(true); }
break;
case SDL_EVENT_WINDOW_MINIMIZED:
if (iGuest) { iGuest->isSystemStopped(true); }
break;

case SDL_EVENT_WINDOW_RESTORED:
if (iGuest) { iGuest->isSystemStopped(false); }
break;
}
case SDL_EVENT_WINDOW_RESTORED:
if (iGuest) { iGuest->isSystemStopped(false); }
break;
}
return false;
return true;
}

0 comments on commit 4971f40

Please sign in to comment.