Hey look, I can still do C if I really try.
This commit is contained in:
parent
2be56aebab
commit
df02787af6
3 changed files with 37 additions and 23 deletions
|
@ -1,3 +1,3 @@
|
|||
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)
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
/*
|
||||
This is a simple example in C of using the rich presence API syncronously.
|
||||
*/
|
||||
|
||||
// ug
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#define _CRT_SECURE_NO_WARNINGS /* thanks Microsoft */
|
||||
|
||||
#include <stdint.h>
|
||||
#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];
|
||||
DiscordRichPresence discordPresence;
|
||||
memset(&discordPresence, 0, sizeof(discordPresence));
|
||||
discordPresence.state = "West of House";
|
||||
sprintf(buffer, "Frustration level: %d", FrustrationLevel);
|
||||
discordPresence.details = buffer;
|
||||
Discord_UpdatePresence(&discordPresence);
|
||||
|
@ -37,12 +37,14 @@ static void handleDiscordWantsPresence() {
|
|||
updateDiscordPresence();
|
||||
}
|
||||
|
||||
static bool prompt(char* line, size_t size) {
|
||||
static int prompt(char* line, size_t size) {
|
||||
int res;
|
||||
char* nl;
|
||||
printf("\n> ");
|
||||
fflush(stdout);
|
||||
bool res = fgets(line, size, stdin) != nullptr;
|
||||
res = fgets(line, size, stdin) ? 1 : 0;
|
||||
line[size - 1] = 0;
|
||||
char* nl = strchr(line, '\n');
|
||||
nl = strchr(line, '\n');
|
||||
if (nl) {
|
||||
*nl = 0;
|
||||
}
|
||||
|
@ -50,13 +52,15 @@ static bool prompt(char* line, size_t size) {
|
|||
}
|
||||
|
||||
static void gameLoop() {
|
||||
printf("You are standing in an open field west of a white house.\n");
|
||||
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 {
|
||||
char* space = strchr(line, ' ');
|
||||
space = strchr(line, ' ');
|
||||
if (space) {
|
||||
*space = 0;
|
||||
}
|
||||
|
@ -70,7 +74,8 @@ static void gameLoop() {
|
|||
}
|
||||
|
||||
int main() {
|
||||
DiscordEventHandlers handlers{};
|
||||
DiscordEventHandlers handlers;
|
||||
memset(&handlers, 0, sizeof(handlers));
|
||||
handlers.ready = handleDiscordReady;
|
||||
handlers.disconnected = handleDiscordDisconnected;
|
||||
handlers.wantsPresence = handleDiscordWantsPresence;
|
|
@ -1,7 +1,15 @@
|
|||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
struct DiscordRichPresence {
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#else
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
const char* state;
|
||||
const char* details;
|
||||
int64_t startTimestamp;
|
||||
|
@ -16,19 +24,16 @@ struct DiscordRichPresence {
|
|||
const char* matchSecret;
|
||||
const char* joinSecret;
|
||||
const char* spectateSecret;
|
||||
bool instance;
|
||||
};
|
||||
int8_t instance;
|
||||
} DiscordRichPresence;
|
||||
|
||||
struct DiscordEventHandlers {
|
||||
// required.
|
||||
typedef struct {
|
||||
void (*ready)();
|
||||
void (*disconnected)();
|
||||
|
||||
// optional for rich presence
|
||||
void (*wantsPresence)();
|
||||
void (*joinGame)(const char* joinSecret);
|
||||
void (*spectateGame)(const char* spectateSecret);
|
||||
};
|
||||
} DiscordEventHandlers;
|
||||
|
||||
void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers);
|
||||
void Discord_Shutdown();
|
||||
|
@ -59,3 +64,7 @@ void Discord_SelectTextChannel();
|
|||
void Discord_SendMessage();
|
||||
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue