diff --git a/examples/simplest/CMakeLists.txt b/examples/simplest/CMakeLists.txt index 3ad9831..d3c9023 100644 --- a/examples/simplest/CMakeLists.txt +++ b/examples/simplest/CMakeLists.txt @@ -1,3 +1,3 @@ include_directories(${PROJECT_SOURCE_DIR}/include) -add_executable(simple-client simple.cpp) +add_executable(simple-client simple.c) target_link_libraries(simple-client discord-rpc-sync) diff --git a/examples/simplest/simple.cpp b/examples/simplest/simple.c similarity index 74% rename from examples/simplest/simple.cpp rename to examples/simplest/simple.c index 6c6c011..73a1019 100644 --- a/examples/simplest/simple.cpp +++ b/examples/simplest/simple.c @@ -1,24 +1,24 @@ +/* + This is a simple example in C of using the rich presence API syncronously. +*/ -// ug -#define _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* thanks Microsoft */ +#include #include #include #include #include "discord-rpc.h" -/* - This is a C++ (but really mostly C) simple example of just using the rich presence API. -*/ - static const char* APPLICATION_ID = "12345678910"; static int FrustrationLevel = 0; static void updateDiscordPresence() { - DiscordRichPresence discordPresence{}; - discordPresence.state = "West of House"; char buffer[256]; + DiscordRichPresence discordPresence; + memset(&discordPresence, 0, sizeof(discordPresence)); + discordPresence.state = "West of House"; sprintf(buffer, "Frustration level: %d", FrustrationLevel); discordPresence.details = buffer; Discord_UpdatePresence(&discordPresence); @@ -37,12 +37,14 @@ static void handleDiscordWantsPresence() { updateDiscordPresence(); } -static bool prompt(char* line, size_t size) { +static int prompt(char* line, size_t size) { + int res; + char* nl; printf("\n> "); fflush(stdout); - bool res = fgets(line, size, stdin) != nullptr; + res = fgets(line, size, stdin) ? 1 : 0; line[size - 1] = 0; - char* nl = strchr(line, '\n'); + nl = strchr(line, '\n'); if (nl) { *nl = 0; } @@ -50,13 +52,15 @@ static bool prompt(char* line, size_t size) { } static void gameLoop() { - printf("You are standing in an open field west of a white house.\n"); char line[512]; + char* space; + + printf("You are standing in an open field west of a white house.\n"); while (prompt(line, sizeof(line))) { if (time(NULL) & 1) { printf("I don't understand that.\n"); } else { - char* space = strchr(line, ' '); + space = strchr(line, ' '); if (space) { *space = 0; } @@ -70,7 +74,8 @@ static void gameLoop() { } int main() { - DiscordEventHandlers handlers{}; + DiscordEventHandlers handlers; + memset(&handlers, 0, sizeof(handlers)); handlers.ready = handleDiscordReady; handlers.disconnected = handleDiscordDisconnected; handlers.wantsPresence = handleDiscordWantsPresence; diff --git a/include/discord-rpc.h b/include/discord-rpc.h index 21dcff4..b58055b 100644 --- a/include/discord-rpc.h +++ b/include/discord-rpc.h @@ -1,7 +1,15 @@ #pragma once #include -struct DiscordRichPresence { +#ifdef __cplusplus +extern "C" { +#else + + + +#endif + +typedef struct { const char* state; const char* details; int64_t startTimestamp; @@ -16,19 +24,16 @@ struct DiscordRichPresence { const char* matchSecret; const char* joinSecret; const char* spectateSecret; - bool instance; -}; + int8_t instance; +} DiscordRichPresence; -struct DiscordEventHandlers { - // required. +typedef struct { void (*ready)(); void (*disconnected)(); - - // optional for rich presence void (*wantsPresence)(); void (*joinGame)(const char* joinSecret); void (*spectateGame)(const char* spectateSecret); -}; +} DiscordEventHandlers; void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers); void Discord_Shutdown(); @@ -59,3 +64,7 @@ void Discord_SelectTextChannel(); void Discord_SendMessage(); */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif