diff --git a/src/Platform.h b/src/Platform.h index 90791100..bef66593 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -270,6 +270,9 @@ bool Mutex_TryLock(Mutex* mutex); void Sleep(u64 usecs); +u64 GetMSCount(); +u64 GetUSCount(); + // functions called when the NDS or GBA save files need to be written back to storage // savedata and savelen are always the entire save memory buffer and its full length diff --git a/src/frontend/qt_sdl/Platform.cpp b/src/frontend/qt_sdl/Platform.cpp index 920684e7..541b51f2 100644 --- a/src/frontend/qt_sdl/Platform.cpp +++ b/src/frontend/qt_sdl/Platform.cpp @@ -395,6 +395,16 @@ void Sleep(u64 usecs) QThread::usleep(usecs); } +u64 GetMSCount() +{ + return sysTimer.elapsed(); +} + +u64 GetUSCount() +{ + return sysTimer.nsecsElapsed() / 1000; +} + void WriteNDSSave(const u8* savedata, u32 savelen, u32 writeoffset, u32 writelen, void* userdata) { diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp index 06543af2..001d0f4f 100644 --- a/src/frontend/qt_sdl/main.cpp +++ b/src/frontend/qt_sdl/main.cpp @@ -73,7 +73,6 @@ using namespace melonDS; QString* systemThemeName; - QString emuDirectory; const int kMaxEmuInstances = 16; @@ -86,6 +85,9 @@ std::optional pcap; Net net; +QElapsedTimer sysTimer; + + void NetInit() { Config::Table cfg = Config::GetGlobalTable(); @@ -237,6 +239,7 @@ bool MelonApplication::event(QEvent *event) int main(int argc, char** argv) { + sysTimer.start(); srand(time(nullptr)); for (int i = 0; i < kMaxEmuInstances; i++) diff --git a/src/frontend/qt_sdl/main.h b/src/frontend/qt_sdl/main.h index e49a5cc3..6bd07d75 100644 --- a/src/frontend/qt_sdl/main.h +++ b/src/frontend/qt_sdl/main.h @@ -22,14 +22,8 @@ #include "glad/glad.h" #include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include #include "EmuInstance.h" #include "Window.h" @@ -48,6 +42,8 @@ public: extern QString* systemThemeName; extern QString emuDirectory; +extern QElapsedTimer sysTimer; + bool createEmuInstance(); void deleteEmuInstance(int id); void deleteAllEmuInstances(int first = 0); diff --git a/src/net/LAN.cpp b/src/net/LAN.cpp index b156143c..358bb92a 100644 --- a/src/net/LAN.cpp +++ b/src/net/LAN.cpp @@ -45,9 +45,6 @@ #define INVALID_SOCKET (socket_t)-1 #endif -// REMOVEME REMOVEME REMOVEME -#include - #include "LAN.h" @@ -227,7 +224,7 @@ bool LAN::StartDiscovery() return false; } - DiscoveryLastTick = SDL_GetTicks(); + DiscoveryLastTick = (u32)Platform::GetMSCount(); DiscoveryList.clear(); Active = true; @@ -324,11 +321,11 @@ bool LAN::StartClient(const char* playername, const char* host) ENetEvent event; int conn = 0; - u32 starttick = SDL_GetTicks(); + u32 starttick = (u32)Platform::GetMSCount(); const int conntimeout = 5000; for (;;) { - u32 curtick = SDL_GetTicks(); + u32 curtick = (u32)Platform::GetMSCount(); if (curtick < starttick) break; int timeout = conntimeout - (int)(curtick - starttick); if (timeout < 0) break; @@ -407,7 +404,7 @@ void LAN::ProcessDiscovery() if (DiscoverySocket == INVALID_SOCKET) return; - u32 tick = SDL_GetTicks(); + u32 tick = (u32)Platform::GetMSCount(); if ((tick - DiscoveryLastTick) < 1000) return; @@ -801,7 +798,7 @@ void LAN::ProcessLAN(int type) if (!Host) return; //printf("Process(%d): %d %d\n", type, RXQueue.empty(), RXQueue.size()); - u32 time_last = SDL_GetTicks(); + u32 time_last = (u32)Platform::GetMSCount(); // see if we have queued packets already, get rid of the stale ones // any incoming packet should be consumed by the core quickly, so if @@ -837,7 +834,7 @@ void LAN::ProcessLAN(int type) } int timeout = (type == 2) ? MPRecvTimeout : 0; - time_last = SDL_GetTicks(); + time_last = (u32)Platform::GetMSCount(); ENetEvent event; while (enet_host_service(Host, &event, timeout) > 0) @@ -861,7 +858,7 @@ void LAN::ProcessLAN(int type) else { // mark this packet with the time it was received - header->Magic = SDL_GetTicks(); + header->Magic = (u32)Platform::GetMSCount(); event.packet->userData = event.peer; RXQueue.push(event.packet); @@ -879,7 +876,7 @@ void LAN::ProcessLAN(int type) if (type == 2) { - u32 time = SDL_GetTicks(); + u32 time = (u32)Platform::GetMSCount(); if (time < time_last) return; timeout -= (int)(time - time_last); if (timeout <= 0) return;