2017-07-01 01:18:54 +02:00
|
|
|
/*
|
2017-07-06 20:46:13 +02:00
|
|
|
This is a simple example in C of using the rich presence API asyncronously.
|
2017-07-01 01:18:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS /* thanks Microsoft */
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "discord-rpc.h"
|
|
|
|
|
2017-08-16 20:48:27 +02:00
|
|
|
static const char* APPLICATION_ID = "345229890980937739";
|
2017-07-01 01:18:54 +02:00
|
|
|
static int FrustrationLevel = 0;
|
2017-07-28 22:43:43 +02:00
|
|
|
static int64_t StartTime;
|
2017-07-01 01:18:54 +02:00
|
|
|
|
2017-07-25 18:27:48 +02:00
|
|
|
static void updateDiscordPresence()
|
|
|
|
{
|
2017-07-01 01:18:54 +02:00
|
|
|
char buffer[256];
|
|
|
|
DiscordRichPresence discordPresence;
|
|
|
|
memset(&discordPresence, 0, sizeof(discordPresence));
|
|
|
|
discordPresence.state = "West of House";
|
|
|
|
sprintf(buffer, "Frustration level: %d", FrustrationLevel);
|
|
|
|
discordPresence.details = buffer;
|
2017-07-28 22:43:43 +02:00
|
|
|
discordPresence.startTimestamp = StartTime;
|
|
|
|
discordPresence.endTimestamp = time(0) + 5 * 60;
|
2017-08-16 20:48:27 +02:00
|
|
|
discordPresence.largeImageKey = "canary-large";
|
|
|
|
discordPresence.smallImageKey = "ptb-small";
|
2017-07-28 22:43:43 +02:00
|
|
|
discordPresence.partyId = "party1234";
|
|
|
|
discordPresence.partySize = 1;
|
|
|
|
discordPresence.partyMax = 6;
|
|
|
|
discordPresence.matchSecret = "xyzzy";
|
|
|
|
discordPresence.joinSecret = "join";
|
|
|
|
discordPresence.spectateSecret = "look";
|
|
|
|
discordPresence.instance = 0;
|
2017-07-01 01:18:54 +02:00
|
|
|
Discord_UpdatePresence(&discordPresence);
|
|
|
|
}
|
|
|
|
|
2017-07-25 18:27:48 +02:00
|
|
|
static void handleDiscordReady()
|
|
|
|
{
|
2017-07-06 20:46:13 +02:00
|
|
|
printf("\nDiscord: ready\n");
|
2017-07-01 01:18:54 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 18:27:48 +02:00
|
|
|
static void handleDiscordDisconnected(int errcode, const char* message)
|
|
|
|
{
|
2017-07-18 18:46:49 +02:00
|
|
|
printf("\nDiscord: disconnected (%d: %s)\n", errcode, message);
|
2017-07-01 01:18:54 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 18:27:48 +02:00
|
|
|
static void handleDiscordError(int errcode, const char* message)
|
|
|
|
{
|
2017-07-24 23:58:53 +02:00
|
|
|
printf("\nDiscord: error (%d: %s)\n", errcode, message);
|
|
|
|
}
|
|
|
|
|
2017-07-28 22:53:05 +02:00
|
|
|
static void handleDiscordJoin(const char* secret)
|
|
|
|
{
|
|
|
|
printf("\nDiscord: join (%s)\n", secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleDiscordSpectate(const char* secret)
|
|
|
|
{
|
|
|
|
printf("\nDiscord: spectate (%s)\n", secret);
|
|
|
|
}
|
|
|
|
|
2017-07-25 18:27:48 +02:00
|
|
|
static int prompt(char* line, size_t size)
|
|
|
|
{
|
2017-07-01 01:18:54 +02:00
|
|
|
int res;
|
|
|
|
char* nl;
|
|
|
|
printf("\n> ");
|
|
|
|
fflush(stdout);
|
2017-08-03 02:37:58 +02:00
|
|
|
res = fgets(line, (int)size, stdin) ? 1 : 0;
|
2017-07-01 01:18:54 +02:00
|
|
|
line[size - 1] = 0;
|
|
|
|
nl = strchr(line, '\n');
|
|
|
|
if (nl) {
|
|
|
|
*nl = 0;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-08-03 19:47:27 +02:00
|
|
|
static void discordInit()
|
|
|
|
{
|
|
|
|
DiscordEventHandlers handlers;
|
|
|
|
memset(&handlers, 0, sizeof(handlers));
|
|
|
|
handlers.ready = handleDiscordReady;
|
|
|
|
handlers.disconnected = handleDiscordDisconnected;
|
|
|
|
handlers.errored = handleDiscordError;
|
|
|
|
handlers.joinGame = handleDiscordJoin;
|
|
|
|
handlers.spectateGame = handleDiscordSpectate;
|
|
|
|
Discord_Initialize(APPLICATION_ID, &handlers, 1);
|
|
|
|
}
|
|
|
|
|
2017-07-25 18:27:48 +02:00
|
|
|
static void gameLoop()
|
|
|
|
{
|
2017-07-01 01:18:54 +02:00
|
|
|
char line[512];
|
|
|
|
char* space;
|
|
|
|
|
2017-07-28 22:43:43 +02:00
|
|
|
StartTime = time(0);
|
|
|
|
|
2017-07-01 01:18:54 +02:00
|
|
|
printf("You are standing in an open field west of a white house.\n");
|
|
|
|
while (prompt(line, sizeof(line))) {
|
2017-07-25 23:33:12 +02:00
|
|
|
if (line[0]) {
|
|
|
|
if (line[0] == 'q') {
|
|
|
|
break;
|
|
|
|
}
|
2017-08-03 19:47:27 +02:00
|
|
|
|
|
|
|
if (line[0] == 't') {
|
|
|
|
printf("Shutting off Discord.\n");
|
|
|
|
Discord_Shutdown();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] == 'y') {
|
|
|
|
printf("Reinit Discord.\n");
|
|
|
|
discordInit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-25 23:33:12 +02:00
|
|
|
if (time(NULL) & 1) {
|
|
|
|
printf("I don't understand that.\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
space = strchr(line, ' ');
|
|
|
|
if (space) {
|
|
|
|
*space = 0;
|
|
|
|
}
|
|
|
|
printf("I don't know the word \"%s\".\n", line);
|
2017-07-01 01:18:54 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 23:33:12 +02:00
|
|
|
++FrustrationLevel;
|
2017-07-25 18:27:48 +02:00
|
|
|
|
2017-07-25 23:33:12 +02:00
|
|
|
updateDiscordPresence();
|
|
|
|
}
|
2017-07-25 18:27:48 +02:00
|
|
|
|
2017-07-18 18:47:33 +02:00
|
|
|
#ifdef DISCORD_DISABLE_IO_THREAD
|
|
|
|
Discord_UpdateConnection();
|
|
|
|
#endif
|
|
|
|
Discord_RunCallbacks();
|
2017-07-01 01:18:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 20:56:19 +02:00
|
|
|
int main(int argc, char* argv[])
|
2017-07-25 18:27:48 +02:00
|
|
|
{
|
2017-08-03 19:47:27 +02:00
|
|
|
discordInit();
|
2017-07-01 01:18:54 +02:00
|
|
|
|
|
|
|
gameLoop();
|
2017-07-25 18:27:48 +02:00
|
|
|
|
2017-07-01 01:18:54 +02:00
|
|
|
Discord_Shutdown();
|
|
|
|
return 0;
|
|
|
|
}
|