Moving files around; cleaned up example a little

This commit is contained in:
Chris Marsh 2017-06-30 12:00:45 -07:00
parent 0768b06ec1
commit 39d8336b06
7 changed files with 90 additions and 70 deletions

View file

@ -2,3 +2,4 @@ cmake_minimum_required (VERSION 3.7.0)
project (DiscordRPCExample) project (DiscordRPCExample)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(examples/simplest)

View file

@ -0,0 +1,3 @@
include_directories(${PROJECT_SOURCE_DIR}/include)
add_executable(simple-client simple.cpp)
target_link_libraries(simple-client discord-rpc)

View file

@ -0,0 +1,84 @@
// ug
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <time.h>
#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;
}

View file

@ -1,3 +1,2 @@
add_library(discord-rpc discord-rpc.cpp) include_directories(${PROJECT_SOURCE_DIR}/include)
add_executable(test-client simple.cpp) add_library(discord-rpc STATIC discord-rpc.cpp)
target_link_libraries(test-client discord-rpc)

View file

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <time.h>
#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;
}