Making another one, cleaning up some
This commit is contained in:
3
examples/simple/CMakeLists.txt
Normal file
3
examples/simple/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
add_executable(simple-client simple.c)
|
||||
target_link_libraries(simple-client discord-rpc)
|
89
examples/simple/simple.c
Normal file
89
examples/simple/simple.c
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
This is a simple example in C of using the rich presence API syncronously.
|
||||
*/
|
||||
|
||||
#define _CRT_SECURE_NO_WARNINGS /* thanks Microsoft */
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "discord-rpc.h"
|
||||
|
||||
static const char* APPLICATION_ID = "12345678910";
|
||||
static int FrustrationLevel = 0;
|
||||
|
||||
static void updateDiscordPresence() {
|
||||
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);
|
||||
}
|
||||
|
||||
static void handleDiscordReady() {
|
||||
printf("Discord: ready\n");
|
||||
}
|
||||
|
||||
static void handleDiscordDisconnected() {
|
||||
printf("Discord: disconnected\n");
|
||||
}
|
||||
|
||||
static void handleDiscordWantsPresence() {
|
||||
printf("Discord: requests presence\n");
|
||||
updateDiscordPresence();
|
||||
}
|
||||
|
||||
static int prompt(char* line, size_t size) {
|
||||
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;
|
||||
}
|
||||
|
||||
static void gameLoop() {
|
||||
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 {
|
||||
space = strchr(line, ' ');
|
||||
if (space) {
|
||||
*space = 0;
|
||||
}
|
||||
printf("I don't know the word \"%s\".\n", line);
|
||||
}
|
||||
|
||||
++FrustrationLevel;
|
||||
|
||||
updateDiscordPresence();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
DiscordEventHandlers handlers;
|
||||
memset(&handlers, 0, sizeof(handlers));
|
||||
handlers.ready = handleDiscordReady;
|
||||
handlers.disconnected = handleDiscordDisconnected;
|
||||
handlers.wantsPresence = handleDiscordWantsPresence;
|
||||
Discord_Initialize(APPLICATION_ID, &handlers);
|
||||
|
||||
gameLoop();
|
||||
|
||||
Discord_Shutdown();
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
add_executable(simple-client simple.c)
|
||||
target_link_libraries(simple-client discord-rpc-sync)
|
||||
add_executable(simplest-client simple.c)
|
||||
target_link_libraries(simplest-client discord-rpc-simple)
|
||||
|
Reference in New Issue
Block a user