Skip to content

Commit

Permalink
Add leaderboard trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed May 2, 2024
1 parent c6c9b41 commit 6c1d64b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2895,6 +2895,9 @@ static void se_drive_ready_callback(cloud_drive_t* drive){
printf("Something went wrong during cloud login\n");
}
}
ImFont* se_get_mono_font(){
return gui_state.mono_font;
}
void se_login_cloud(){
cloud_drive_create(se_drive_ready_callback);
}
Expand Down Expand Up @@ -6948,6 +6951,8 @@ static void frame(void) {

// TODO: why does screen_width not work correctly
retro_achievements_draw_progress_indicator(screen_x + screen_width - 30,menu_height + 30);

retro_achievements_draw_leaderboard_trackers(screen_x + 30, height - 200);
#endif

for(int i=0;i<SAPP_MAX_TOUCHPOINTS;++i){
Expand Down
122 changes: 94 additions & 28 deletions src/retro_achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void se_boxed_image_dual_label(const char* title, const char* description, const
sg_image image, int flags, ImVec2 uv0, ImVec2 uv1);
bool se_button(const char* label, ImVec2 size);
const char* se_localize_and_cache(const char* input_str);
ImFont* se_get_mono_font();
#include "retro_achievements.h"
}

Expand Down Expand Up @@ -58,6 +59,7 @@ const float padding = 7;

static bool draw_notifications = true;
static bool draw_progress_indicator = true;
static bool draw_trackers = true;

// atlases -> the currently existing atlases, each serving a different image
// width/height combo image_cache -> a mapping of image urls to their atlas and
Expand All @@ -73,19 +75,6 @@ struct ra_game_state_t;

using ra_game_state_ptr = std::shared_ptr<ra_game_state_t>;

struct ra_tracker_node_t
{
rc_client_leaderboard_tracker_t tracker;
ra_tracker_node_t* next;
};

struct ra_challenge_indicator_node_t
{
uint32_t id;
atlas_tile_t* image;
ra_challenge_indicator_node_t* next;
};

struct ra_achievement_wrapper_t
{
atlas_tile_t* tile = nullptr;
Expand All @@ -109,12 +98,16 @@ struct ra_achievement_list_wrapper_t
std::vector<ra_achievement_bucket_wrapper_t> buckets{};
};

struct ra_leaderboard_tracker_t
{
char display[24] = {0};
};

struct ra_progress_indicator_wrapper_t
{
atlas_tile_t* tile = nullptr;
std::string title;
std::string measured_progress;
std::chrono::time_point<std::chrono::steady_clock> start_time;
std::string title {};
std::string measured_progress {};
bool show = false;
};

Expand All @@ -139,6 +132,7 @@ struct ra_game_state_t
std::vector<atlas_t*> atlases{};
std::unordered_map<std::string, atlas_tile_t> image_cache{};
std::unique_ptr<ra_achievement_list_wrapper_t> achievement_list;
std::unordered_map<uint32_t, ra_leaderboard_tracker_t> leaderboard_trackers;
ra_progress_indicator_wrapper_t progress_indicator;
std::vector<ra_notification_t> notifications;
std::atomic_int outstanding_requests;
Expand Down Expand Up @@ -335,7 +329,6 @@ namespace
{
ra_state->download(game_state, url, [game_state, url]() {
game_state->progress_indicator.tile = &game_state->image_cache[url];
game_state->progress_indicator.start_time = std::chrono::steady_clock::now();
});
}
}
Expand Down Expand Up @@ -546,17 +539,26 @@ namespace
break;
}
case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_UPDATE:
printf("STUB: Leaderboard tracker update\n");
// se_ra_leaderboard_tracker_update(event->leaderboard_tracker);
break;
case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_SHOW:
printf("STUB: Leaderboard tracker show\n");
// se_ra_leaderboard_tracker_show(event->leaderboard_tracker);
case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_SHOW: {
ra_game_state_ptr game_state = ra_state->game_state;
printf("ID: %d, Display: %s\n", event->leaderboard_tracker->id, event->leaderboard_tracker->display);
std::unique_lock<std::mutex> lock(game_state->mutex);
ra_leaderboard_tracker_t tracker;
memcpy(tracker.display, event->leaderboard_tracker->display, sizeof(tracker.display));
game_state->leaderboard_trackers[event->leaderboard_tracker->id] = tracker;
game_state->leaderboard_trackers[event->leaderboard_tracker->id + 3] = tracker;
game_state->leaderboard_trackers[event->leaderboard_tracker->id + 7] = tracker;
game_state->leaderboard_trackers[event->leaderboard_tracker->id + 9] = tracker;
game_state->leaderboard_trackers[event->leaderboard_tracker->id + 10] = tracker;
break;
case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_HIDE:
printf("STUB: Leaderboard tracker hide\n");
// se_ra_leaderboard_tracker_hide(event->leaderboard_tracker);
}
case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_HIDE: {
ra_game_state_ptr game_state = ra_state->game_state;

std::unique_lock<std::mutex> lock(game_state->mutex);
game_state->leaderboard_trackers.erase(event->leaderboard_tracker->id);
break;
}
case RC_CLIENT_EVENT_ACHIEVEMENT_CHALLENGE_INDICATOR_SHOW:
printf("STUB: Challenge indicator show\n");
// se_ra_challenge_indicator_show(event->achievement);
Expand Down Expand Up @@ -1238,9 +1240,10 @@ void retro_achievements_draw_panel(int win_w)
std::string settings = ICON_FK_WRENCH " " + std::string(se_localize_and_cache("Settings"));
se_text(settings.c_str());

// TODO: make saveable
// TODO: make these saveable
igCheckbox(se_localize_and_cache("Enable Notifications"), &draw_notifications);
igCheckbox(se_localize_and_cache("Enable Progress Indicator"), &draw_progress_indicator);
igCheckbox(se_localize_and_cache("Enable Leaderboard Trackers"), &draw_trackers);
// TODO: put hardcore mode checkbox here?

retro_achievements_draw_achievements();
Expand Down Expand Up @@ -1361,7 +1364,8 @@ void retro_achievements_draw_notifications(float left, float top)
}
}

void retro_achievements_draw_progress_indicator(float right, float top) {
void retro_achievements_draw_progress_indicator(float right, float top)
{
if (!draw_progress_indicator)
return;

Expand Down Expand Up @@ -1430,4 +1434,66 @@ void retro_achievements_draw_progress_indicator(float right, float top) {
ImVec2{x + padding + image_width + padding, y + placard_height - padding - progress_height},
0xff00aaaa, indicator.measured_progress.c_str(), NULL,
wrap_width, NULL);
}

void retro_achievements_draw_leaderboard_trackers(float left, float bottom)
{

if (!draw_trackers)
return;

ra_game_state_ptr game_state = ra_state->game_state;

if (!game_state)
return;

std::unique_lock<std::mutex> lock(game_state->mutex);

if (game_state->leaderboard_trackers.empty())
return;

ImVec2 out;
float max_text_width;
float text_height;
ImFont* font = se_get_mono_font();
ImFont_CalcTextSizeA(&out, font, 12.0f, std::numeric_limits<float>::max(), 0, "A", NULL, NULL);
text_height = out.y;

float start_x = left;
float start_y = bottom;
float x = start_x;
float y = start_y;
int i = 0;

igPushFont(font);
for (auto& tracker : game_state->leaderboard_trackers)
{
ImFont_CalcTextSizeA(&out, font, 12.0f, std::numeric_limits<float>::max(), 0, tracker.second.display, NULL,
NULL);
float max_text_width = out.x;

ImDrawList_AddRectFilled(igGetWindowDrawList(), ImVec2{x, y},
ImVec2{x + max_text_width + padding * 2, y + text_height + padding * 2},
0x80515151, 0, 0);

// TODO: needs mono font
ImDrawList_AddTextFontPtr(igGetWindowDrawList(), igGetFont(), 12.0f,
ImVec2{x + padding, y + padding},
0xffffffff, tracker.second.display, NULL,
std::numeric_limits<float>::max(), NULL);

if (i++ % 3 != 2)
{
x += max_text_width + padding * 3;
}
else
{
x = start_x;
y += text_height + padding * 3;
}

if (i == 9)
break; // show up to 9 trackers
}
igPopFont();
}
2 changes: 2 additions & 0 deletions src/retro_achievements.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ void retro_achievements_draw_notifications(float left, float top);

void retro_achievements_draw_progress_indicator(float right, float top);

void retro_achievements_draw_leaderboard_trackers(float left, float bottom);

#endif

0 comments on commit 6c1d64b

Please sign in to comment.