From 39d8336b06957f38c0ccf2e4d986bea2cabc99d8 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Fri, 30 Jun 2017 12:00:45 -0700 Subject: [PATCH] Moving files around; cleaned up example a little --- CMakeLists.txt | 1 + examples/simplest/CMakeLists.txt | 3 ++ examples/simplest/simple.cpp | 84 ++++++++++++++++++++++++++++++++ {src => include}/discord-rpc.h | 0 src/CMakeLists.txt | 5 +- src/full.cpp | 0 src/simple.cpp | 67 ------------------------- 7 files changed, 90 insertions(+), 70 deletions(-) create mode 100644 examples/simplest/CMakeLists.txt create mode 100644 examples/simplest/simple.cpp rename {src => include}/discord-rpc.h (100%) delete mode 100644 src/full.cpp delete mode 100644 src/simple.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f85b545..8bf90d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,3 +2,4 @@ cmake_minimum_required (VERSION 3.7.0) project (DiscordRPCExample) add_subdirectory(src) +add_subdirectory(examples/simplest) diff --git a/examples/simplest/CMakeLists.txt b/examples/simplest/CMakeLists.txt new file mode 100644 index 0000000..19488cd --- /dev/null +++ b/examples/simplest/CMakeLists.txt @@ -0,0 +1,3 @@ +include_directories(${PROJECT_SOURCE_DIR}/include) +add_executable(simple-client simple.cpp) +target_link_libraries(simple-client discord-rpc) diff --git a/examples/simplest/simple.cpp b/examples/simplest/simple.cpp new file mode 100644 index 0000000..6c6c011 --- /dev/null +++ b/examples/simplest/simple.cpp @@ -0,0 +1,84 @@ + +// ug +#define _CRT_SECURE_NO_WARNINGS + +#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]; + sprintf(buffer, "Frustration level: %d", FrustrationLevel); + discordPresence.details = buffer; + Discord_UpdatePresence(&discordPresence); +} + +static void handleDiscordReady() { + printf("Discord: ready\n"); +} + +static void handleDiscordDisconnected() { + printf("Discord: disconnected\n"); +} + +static void handleDiscordWantsPresence() { + printf("Discord: requests presence\n"); + updateDiscordPresence(); +} + +static bool prompt(char* line, size_t size) { + printf("\n> "); + fflush(stdout); + bool res = fgets(line, size, stdin) != nullptr; + line[size - 1] = 0; + char* nl = strchr(line, '\n'); + if (nl) { + *nl = 0; + } + return res; +} + +static void gameLoop() { + printf("You are standing in an open field west of a white house.\n"); + char line[512]; + while (prompt(line, sizeof(line))) { + if (time(NULL) & 1) { + printf("I don't understand that.\n"); + } else { + char* space = strchr(line, ' '); + if (space) { + *space = 0; + } + printf("I don't know the word \"%s\".\n", line); + } + + ++FrustrationLevel; + + updateDiscordPresence(); + } +} + +int main() { + DiscordEventHandlers handlers{}; + handlers.ready = handleDiscordReady; + handlers.disconnected = handleDiscordDisconnected; + handlers.wantsPresence = handleDiscordWantsPresence; + Discord_Initialize(APPLICATION_ID, &handlers); + + gameLoop(); + + Discord_Shutdown(); + return 0; +} + diff --git a/src/discord-rpc.h b/include/discord-rpc.h similarity index 100% rename from src/discord-rpc.h rename to include/discord-rpc.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3de2d5e..6b994c1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,2 @@ -add_library(discord-rpc discord-rpc.cpp) -add_executable(test-client simple.cpp) -target_link_libraries(test-client discord-rpc) +include_directories(${PROJECT_SOURCE_DIR}/include) +add_library(discord-rpc STATIC discord-rpc.cpp) diff --git a/src/full.cpp b/src/full.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/simple.cpp b/src/simple.cpp deleted file mode 100644 index 676f359..0000000 --- a/src/simple.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include - -#include "discord-rpc.h" - -static const char* APPLICATION_ID = "12345678910"; - -void updateDiscordPresence() { - DiscordRichPresence discordPresence{}; - - discordPresence.state = "In a Group"; - discordPresence.details = "Competitive\nIn a \"Match\""; - discordPresence.endTimestamp = time(nullptr) + ((60 * 5) + 23); - discordPresence.partyId = "12345"; - discordPresence.partySize = 3; - discordPresence.partyMax = 6; - discordPresence.matchSecret = "4b2fdce12f639de8bfa7e3591b71a0d679d7c93f"; - discordPresence.spectateSecret = "e7eb30d2ee025ed05c71ea495f770b76454ee4e0"; - discordPresence.instance = true; - - Discord_UpdatePresence(&discordPresence); -} - -void handleDiscordReady() { - printf("discord ready\n"); -} - -void handleDiscordDisconnected() { - printf("discord disconnected\n"); -} - -void handleDiscordWantsPresence() { - printf("discord requests presence\n"); - updateDiscordPresence(); -} - -void gameLoop() { - char line[512]; - printf("> "); - fflush(stdout); - while (fgets(line, 512, stdin)) { - line[511] = 0; - printf("I don't understand that.\n> "); - fflush(stdout); - - updateDiscordPresence(); - } -} - -int main() { - printf("Starting game client\n"); - - DiscordEventHandlers handlers{}; - - handlers.ready = handleDiscordReady; - handlers.disconnected = handleDiscordDisconnected; - handlers.wantsPresence = handleDiscordWantsPresence; - - Discord_Initialize(APPLICATION_ID, &handlers); - - gameLoop(); - - Discord_Shutdown(); - - return 0; -} -