Give Discord.user_avatar an optional size argument

This commit is contained in:
Inori 2019-09-05 03:26:21 -04:00 committed by Inori
parent 4fbe56803f
commit 1e3661fdee
6 changed files with 30 additions and 14 deletions

View file

@ -72,7 +72,7 @@ The syntax is: `--<option>=<value>`
Example: `./mkxp --gameFolder="my game" --vsync=true --fixedFramerate=60`
## Discord Support
## Discord GameSDK
mkxp-z can optionally be built with support for the Discord GameSDK. Currently only basic Activity (rich presence) functionality is implemented. The Discord module is being actively fleshed out.
@ -82,9 +82,15 @@ A different Client ID may be specified in the configuration file.
if Discord.connected?
p Discord.user_name, Discord.user_discriminator, Discord.user_id
avatar = Sprite.new
avatar.bitmap = Discord.user_avatar(64)
avatar.visible = true
Discord::Activity.new do |activity|
activity.start_time = Time.now.to_i
activity.details = MKXP.game_title
activity.large_image = "someimage"
activity.large_text = "sometext"
end
end
```

View file

@ -50,8 +50,10 @@ void bitmapInitProps(Bitmap *b, VALUE self);
RB_METHOD(DiscordGetUserAvatar)
{
RB_UNUSED_PARAM;
int size = 32;
rb_get_args(argc, argv, "|i", &size RB_ARG_END);
Bitmap *result = shState->discord().userAvatar();
Bitmap *result = shState->discord().userAvatar(size);
if (!result) return RUBY_Qnil;
VALUE ret = wrapObject(result, BitmapType);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 226 KiB

After

Width:  |  Height:  |  Size: 53 KiB

View file

@ -211,30 +211,38 @@ DiscordUserId DiscordState::userId()
typedef struct { DiscordStatePrivate *pri; Bitmap *bmp; } AvatarCbData;
Bitmap *DiscordState::getAvatar(DiscordUserId userId, int size)
{
size = clamp(size, 32, 256);
while ((size & (size-1)) != 0) {
size++;
}
if (!isConnected()) return 0;
AvatarCbData cbData{};
cbData.bmp = new Bitmap(size, size);
cbData.pri = p;
AvatarCbData *cbData = new AvatarCbData;
Bitmap *ret = new Bitmap(size, size);
cbData->bmp = ret;
cbData->pri = p;
DiscordImageHandle handle{};
handle.id = userId;
handle.size = size;
p->app.images->fetch(p->app.images, handle, true, &cbData,
p->app.images->fetch(p->app.images, handle, true, cbData,
[](void *callback_data, enum EDiscordResult result, struct DiscordImageHandle handle_result){
if (result == DiscordResult_Ok)
{
AvatarCbData *data = (AvatarCbData*)callback_data;
if (data->bmp->isDisposed()) return;
int sz = data->bmp->width()*data->bmp->height()*4;
uint8_t *buf = new uint8_t[sz];
data->pri->app.images->get_data(data->pri->app.images, handle_result, buf, sz);
data->bmp->replaceRaw(buf, data->bmp->width(), data->bmp->height());
delete[] buf;
delete data;
}
});
return cbData.bmp;
return ret;
}
Bitmap *DiscordState::userAvatar()
Bitmap *DiscordState::userAvatar(int size)
{
return (p->userPresent) ? getAvatar(p->currentUser.id, 256) : 0;
return (p->userPresent) ? getAvatar(p->currentUser.id, size) : 0;
}

View file

@ -27,7 +27,7 @@ public:
DiscordUserId userId();
Bitmap *getAvatar(DiscordUserId userId, int size);
Bitmap *userAvatar();
Bitmap *userAvatar(int size);
private:
DiscordStatePrivate *p;

View file

@ -669,6 +669,10 @@ void Graphics::update()
{
p->checkShutDownReset();
p->checkSyncLock();
#ifdef HAVE_DISCORDSDK
shState->discord().update();
#endif
if (p->frozen)
return;
@ -691,10 +695,6 @@ void Graphics::update()
}
}
#ifdef HAVE_DISCORDSDK
if (p->frameCount % 10 == 0) shState->discord().update();
#endif
p->checkResize();
p->redrawScreen();
}