discord-rpc/examples/send-presence/send-presence.c

108 lines
2.4 KiB
C
Raw Normal View History

2017-06-30 23:18:54 +00:00
/*
This is a simple example in C of using the rich presence API asyncronously.
2017-06-30 23:18:54 +00: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-07-21 20:54:52 +00:00
static const char* APPLICATION_ID = "338030514596216832";
2017-06-30 23:18:54 +00:00
static int FrustrationLevel = 0;
2017-07-25 16:27:48 +00:00
static void updateDiscordPresence()
{
2017-06-30 23:18:54 +00: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;
Discord_UpdatePresence(&discordPresence);
}
2017-07-25 16:27:48 +00:00
static void handleDiscordReady()
{
printf("\nDiscord: ready\n");
2017-06-30 23:18:54 +00:00
}
2017-07-25 16:27:48 +00:00
static void handleDiscordDisconnected(int errcode, const char* message)
{
2017-07-18 16:46:49 +00:00
printf("\nDiscord: disconnected (%d: %s)\n", errcode, message);
2017-06-30 23:18:54 +00:00
}
2017-07-25 16:27:48 +00:00
static void handleDiscordError(int errcode, const char* message)
{
2017-07-24 21:58:53 +00:00
printf("\nDiscord: error (%d: %s)\n", errcode, message);
}
2017-07-25 16:27:48 +00:00
static void handleDiscordPresenceRequested()
{
printf("\nDiscord: requests presence\n");
2017-06-30 23:18:54 +00:00
updateDiscordPresence();
}
2017-07-25 16:27:48 +00:00
static int prompt(char* line, size_t size)
{
2017-06-30 23:18:54 +00:00
int res;
char* nl;
printf("\n> ");
fflush(stdout);
res = fgets(line, size, stdin) ? 1 : 0;
line[size - 1] = 0;
nl = strchr(line, '\n');
if (nl) {
*nl = 0;
}
return res;
}
2017-07-25 16:27:48 +00:00
static void gameLoop()
{
2017-06-30 23:18:54 +00:00
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");
2017-07-25 16:27:48 +00:00
}
else {
2017-06-30 23:18:54 +00:00
space = strchr(line, ' ');
if (space) {
*space = 0;
}
printf("I don't know the word \"%s\".\n", line);
}
++FrustrationLevel;
2017-07-25 16:27:48 +00:00
2017-06-30 23:18:54 +00:00
updateDiscordPresence();
2017-07-25 16:27:48 +00:00
2017-07-18 16:47:33 +00:00
#ifdef DISCORD_DISABLE_IO_THREAD
Discord_UpdateConnection();
#endif
Discord_RunCallbacks();
2017-06-30 23:18:54 +00:00
}
}
2017-07-25 16:27:48 +00:00
int main()
{
2017-06-30 23:18:54 +00:00
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
handlers.ready = handleDiscordReady;
handlers.disconnected = handleDiscordDisconnected;
2017-07-24 21:58:53 +00:00
handlers.errored = handleDiscordError;
2017-07-19 21:40:54 +00:00
handlers.presenceRequested = handleDiscordPresenceRequested;
2017-06-30 23:18:54 +00:00
Discord_Initialize(APPLICATION_ID, &handlers);
gameLoop();
2017-07-25 16:27:48 +00:00
2017-06-30 23:18:54 +00:00
Discord_Shutdown();
return 0;
}