Hey look, I can still do C if I really try.

This commit is contained in:
Chris Marsh 2017-06-30 14:31:48 -07:00
parent 2be56aebab
commit df02787af6
3 changed files with 37 additions and 23 deletions

View file

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

View file

@ -1,24 +1,24 @@
/*
This is a simple example in C of using the rich presence API syncronously.
*/
// ug #define _CRT_SECURE_NO_WARNINGS /* thanks Microsoft */
#define _CRT_SECURE_NO_WARNINGS
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "discord-rpc.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 const char* APPLICATION_ID = "12345678910";
static int FrustrationLevel = 0; static int FrustrationLevel = 0;
static void updateDiscordPresence() { static void updateDiscordPresence() {
DiscordRichPresence discordPresence{};
discordPresence.state = "West of House";
char buffer[256]; char buffer[256];
DiscordRichPresence discordPresence;
memset(&discordPresence, 0, sizeof(discordPresence));
discordPresence.state = "West of House";
sprintf(buffer, "Frustration level: %d", FrustrationLevel); sprintf(buffer, "Frustration level: %d", FrustrationLevel);
discordPresence.details = buffer; discordPresence.details = buffer;
Discord_UpdatePresence(&discordPresence); Discord_UpdatePresence(&discordPresence);
@ -37,12 +37,14 @@ static void handleDiscordWantsPresence() {
updateDiscordPresence(); updateDiscordPresence();
} }
static bool prompt(char* line, size_t size) { static int prompt(char* line, size_t size) {
int res;
char* nl;
printf("\n> "); printf("\n> ");
fflush(stdout); fflush(stdout);
bool res = fgets(line, size, stdin) != nullptr; res = fgets(line, size, stdin) ? 1 : 0;
line[size - 1] = 0; line[size - 1] = 0;
char* nl = strchr(line, '\n'); nl = strchr(line, '\n');
if (nl) { if (nl) {
*nl = 0; *nl = 0;
} }
@ -50,13 +52,15 @@ static bool prompt(char* line, size_t size) {
} }
static void gameLoop() { static void gameLoop() {
printf("You are standing in an open field west of a white house.\n");
char line[512]; char line[512];
char* space;
printf("You are standing in an open field west of a white house.\n");
while (prompt(line, sizeof(line))) { while (prompt(line, sizeof(line))) {
if (time(NULL) & 1) { if (time(NULL) & 1) {
printf("I don't understand that.\n"); printf("I don't understand that.\n");
} else { } else {
char* space = strchr(line, ' '); space = strchr(line, ' ');
if (space) { if (space) {
*space = 0; *space = 0;
} }
@ -70,7 +74,8 @@ static void gameLoop() {
} }
int main() { int main() {
DiscordEventHandlers handlers{}; DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
handlers.ready = handleDiscordReady; handlers.ready = handleDiscordReady;
handlers.disconnected = handleDiscordDisconnected; handlers.disconnected = handleDiscordDisconnected;
handlers.wantsPresence = handleDiscordWantsPresence; handlers.wantsPresence = handleDiscordWantsPresence;

View file

@ -1,7 +1,15 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
struct DiscordRichPresence { #ifdef __cplusplus
extern "C" {
#else
#endif
typedef struct {
const char* state; const char* state;
const char* details; const char* details;
int64_t startTimestamp; int64_t startTimestamp;
@ -16,19 +24,16 @@ struct DiscordRichPresence {
const char* matchSecret; const char* matchSecret;
const char* joinSecret; const char* joinSecret;
const char* spectateSecret; const char* spectateSecret;
bool instance; int8_t instance;
}; } DiscordRichPresence;
struct DiscordEventHandlers { typedef struct {
// required.
void (*ready)(); void (*ready)();
void (*disconnected)(); void (*disconnected)();
// optional for rich presence
void (*wantsPresence)(); void (*wantsPresence)();
void (*joinGame)(const char* joinSecret); void (*joinGame)(const char* joinSecret);
void (*spectateGame)(const char* spectateSecret); void (*spectateGame)(const char* spectateSecret);
}; } DiscordEventHandlers;
void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers); void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers);
void Discord_Shutdown(); void Discord_Shutdown();
@ -59,3 +64,7 @@ void Discord_SelectTextChannel();
void Discord_SendMessage(); void Discord_SendMessage();
*/ */
#ifdef __cplusplus
} /* extern "C" */
#endif