diff --git a/examples/simple/simple.c b/examples/simple/simple.c index 402d863..05ef986 100644 --- a/examples/simple/simple.c +++ b/examples/simple/simple.c @@ -70,6 +70,7 @@ static void gameLoop() { ++FrustrationLevel; updateDiscordPresence(); + Discord_Update(); } } diff --git a/include/discord-rpc.h b/include/discord-rpc.h index bf775d6..a65d8ee 100644 --- a/include/discord-rpc.h +++ b/include/discord-rpc.h @@ -35,6 +35,9 @@ void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handler void Discord_Shutdown(); void Discord_UpdatePresence(const DiscordRichPresence* presence); +/* checks for incoming messages, dispatches callbacks */ +void Discord_Update(); + /* later struct DiscordChannelEventHandlers { diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index 1d3cece..32c47fa 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -6,6 +6,8 @@ static RpcConnection* MyConnection = nullptr; static char ApplicationId[64]{}; static DiscordEventHandlers Handlers{}; +static bool wasJustConnected = false; +static bool wasJustDisconnected = false; void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers) { @@ -18,8 +20,8 @@ void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handler } MyConnection = RpcConnection::Create(); - MyConnection->onConnect = Handlers.ready; - MyConnection->onDisconnect = Handlers.disconnected; + MyConnection->onConnect = []() { wasJustConnected = true; }; + MyConnection->onDisconnect = []() { wasJustDisconnected = true; }; MyConnection->Open(); } @@ -38,3 +40,20 @@ void Discord_UpdatePresence(const DiscordRichPresence* presence) frame->length = sizeof(uint32_t) + (jsonWrite - frame->message); MyConnection->WriteFrame(frame); } + +void Discord_Update() +{ + // check for messages + // todo + + // fire callbacks + if (wasJustDisconnected && Handlers.disconnected) { + wasJustDisconnected = false; + Handlers.disconnected(); + } + + if (wasJustConnected && Handlers.ready) { + wasJustConnected = false; + Handlers.ready(); + } +}