Skip to content

Commit

Permalink
Prepare for atlas prettification
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Aug 14, 2024
1 parent 0393f5b commit af11e7a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 119 deletions.
128 changes: 15 additions & 113 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,6 @@ typedef struct{
#define SE_NO_SORT 0
#define SE_SORT_ALPHA_ASC 1
#define SE_SORT_ALPHA_DESC 2
#define SE_RAY_COUNT 100
struct {
float distance;
float velocity;
float angle;
float angle_velocity;
float alpha;
float alpha_velocity;
} glow_rays[SE_RAY_COUNT];
typedef struct{
uint16_t start_pixel;
uint16_t end_pixel;
Expand Down Expand Up @@ -4437,82 +4428,6 @@ bool se_selectable_with_box(const char * first_label, const char* second_label,
#endif
return clicked;
}
void se_update_glow(){
const float pi_180 = 3.14159265358979323846f / 180.0f;
for(int i = 0; i < SE_RAY_COUNT; i++) {
glow_rays[i].angle += glow_rays[i].angle_velocity;
if (glow_rays[i].angle > 360) {
glow_rays[i].angle -= 360;
}
glow_rays[i].distance += glow_rays[i].velocity;
if (glow_rays[i].distance < 30) {
glow_rays[i].distance = 30;
glow_rays[i].velocity = 0.1f + (float)(rand() % 10) / 10.0f;
} else if (glow_rays[i].distance > 40) {
glow_rays[i].distance = 40;
glow_rays[i].velocity = -0.1f - (float)(rand() % 10) / 10.0f;
}
glow_rays[i].alpha += glow_rays[i].alpha_velocity;
if (glow_rays[i].alpha < 0.4f) {
glow_rays[i].alpha = 0.4f;
glow_rays[i].alpha_velocity = 0.0015f + (float)(rand() % 10) / 1000.0f;
} else if (glow_rays[i].alpha > 0.55f) {
glow_rays[i].alpha = 0.55f;
glow_rays[i].alpha_velocity = -0.0015f - (float)(rand() % 10) / 1000.0f;
}
}
}
void se_draw_glow(ImVec2 center){
const float pi_180 = 3.14159265358979323846f / 180.0f;
ImDrawList* ig = igGetWindowDrawList();
for (int i = 0; i < SE_RAY_COUNT; i++) {
float angle = glow_rays[i].angle;
float distance = glow_rays[i].distance;
float distance_2 = distance / 3;
float distance_x = distance * cosf(angle * pi_180);
float distance_y = distance * sinf(angle * pi_180);

// Keep the distance within a square
if (distance_x > 30) {
distance_x = 30;
} else if (distance_x < -30) {
distance_x = -30;
}

if (distance_y > 30) {
distance_y = 30;
} else if (distance_y < -30) {
distance_y = -30;
}

ImVec2 ray_center = center;
ImVec2 target = {ray_center.x + distance_x, ray_center.y + distance_y};
ray_center.x += distance_x / 2;
ray_center.y += distance_y / 2;

ImVec2 target_1 = {ray_center.x + distance_2 * cosf((angle + 70) * pi_180),
ray_center.y + distance_2 * sinf((angle + 70) * pi_180)};
ImVec2 target_2 = {ray_center.x + distance_2 * cosf((angle - 70) * pi_180),
ray_center.y + distance_2 * sinf((angle - 70) * pi_180)};

int vert_start = ig->VtxBuffer.Size;
ImDrawList_PathLineTo(ig, ray_center);
ImDrawList_PathLineTo(ig, target_1);
ImDrawList_PathLineTo(ig, target);
ImDrawList_PathLineTo(ig, target_2);
ImDrawList_PathFillConvex(ig, 0);

ImDrawVert* vert = ig->VtxBuffer.Data + vert_start;
vert->col = 0x8ffffd | ((uint32_t)(glow_rays[i].alpha * 255) << 24);
vert++;
vert->col = 0;
vert++;
vert->col = 0;
vert++;
vert->col = 0;
}
}

void se_boxed_image_triple_label(const char * first_label, const char* second_label, const char* third_label, uint32_t third_label_color, const char* box, sg_image image, int reduce_width, ImVec2 uv0, ImVec2 uv1, bool glow){
ImVec2 win_min,win_sz,win_max;
win_min.x=0;
Expand Down Expand Up @@ -4574,7 +4489,7 @@ void se_boxed_image_triple_label(const char * first_label, const char* second_la
// ImDrawList_AddRect(ig, top_left, bottom_right, 0xff0000ff, 0, 0, 1.0f);

igPushIDStr(second_label);
if (glow)se_draw_glow((ImVec2){screen_pos.x+box_w*0.5,screen_pos.y+box_h*0.5+padding});
// TODO: if (glow)se_draw_glow((ImVec2){screen_pos.x+box_w*0.5,screen_pos.y+box_h*0.5+padding});
igSetCursorPosX(curr_pos.x+box_w+padding);
igSetCursorPosY(curr_pos.y-padding);
se_text("%s", first_label);
Expand Down Expand Up @@ -6327,7 +6242,7 @@ void se_draw_menu_panel(){
}
}
#ifdef ENABLE_RETRO_ACHIEVEMENTS
se_section(ICON_FK_TROPHY " Retro Achievements");
se_section(ICON_FK_TROPHY " RetroAchievements");
const rc_client_user_t* user = rc_client_get_user_info(retro_achievements_get_client());
igPushIDStr("RetroAchievements");
if (!user)
Expand Down Expand Up @@ -6369,23 +6284,19 @@ void se_draw_menu_panel(){
sg_image image = {SG_INVALID_ID};
ImVec2 offset1 = {0, 0};
ImVec2 offset2 = {1, 1};
const char* play_string = "No Game Loaded";
char line1[256];
char line2[256];
snprintf(line1, 256, se_localize_and_cache("Logged in as %s"), user->display_name);
atlas_tile_t* game_image = retro_achievements_get_game_image();
if (game && game_image)
{
image.id = game_image->atlas_id;
offset1 = (ImVec2){game_image->x1, game_image->y1};
offset2 = (ImVec2){game_image->x2, game_image->y2};
snprintf(line2, 256, se_localize_and_cache("Playing: %s"), game->title);
}
else
atlas_tile_t* user_image = retro_achievements_get_user_image();
if (user_image)
{
snprintf(line2, 256, "%s", se_localize_and_cache("No Game Loaded"));
image.id = user_image->atlas_id;
offset1 = (ImVec2){user_image->x1, user_image->y1};
offset2 = (ImVec2){user_image->x2, user_image->y2};
}
se_boxed_image_triple_label(line1, line2, NULL, 0, ICON_FK_TROPHY, image, 0, offset1, offset2, false);
static char line1[256];
static char line2[256];
bool hardcore = gui_state.settings.hardcore_mode;
snprintf(line1, 256, se_localize_and_cache("Logged in as %s"), user->display_name);
snprintf(line2, 256, se_localize_and_cache("Points: %d"), hardcore ? user->score : user->score_softcore);
se_boxed_image_triple_label(line1, line2, NULL, 0, ICON_FK_USER, image, 0, offset1, offset2, false);
if (se_button(ICON_FK_SIGN_OUT " Logout", (ImVec2){0, 0}))
{
char buffer[SB_FILE_PATH_SIZE];
Expand Down Expand Up @@ -7186,7 +7097,7 @@ static void frame(void) {

#ifdef ENABLE_RETRO_ACHIEVEMENTS
if(retro_achievements_has_game_loaded()){
se_panel_toggle(SE_REGION_BLANK,&gui_state.retro_achievements_sidebar_open,ICON_FK_TROPHY,"Show/Hide Retro Achievements Panel");
se_panel_toggle(SE_REGION_BLANK,&gui_state.retro_achievements_sidebar_open,ICON_FK_TROPHY,"Show/Hide RetroAchievements Panel");
}
#endif

Expand Down Expand Up @@ -7424,8 +7335,7 @@ static void frame(void) {
if(gui_state.retro_achievements_sidebar_open){
igSetNextWindowPos((ImVec2){screen_x,menu_height}, ImGuiCond_Always, (ImVec2){0,0});
igSetNextWindowSize((ImVec2){sidebar_w, (gui_state.screen_height-menu_height*se_dpi_scale())/se_dpi_scale()}, ImGuiCond_Always);
igBegin(se_localize_and_cache(ICON_FK_TROPHY " Retro Achievements"),&gui_state.retro_achievements_sidebar_open, ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoResize);
se_update_glow();
igBegin(se_localize_and_cache(ICON_FK_TROPHY " RetroAchievements"),&gui_state.retro_achievements_sidebar_open, ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoResize);
retro_achievements_draw_panel();
igEnd();
screen_x += sidebar_w;
Expand Down Expand Up @@ -8281,14 +8191,6 @@ static void init(void) {
#ifdef SE_PLATFORM_ANDROID
se_android_request_permissions();
#endif
for (int i = 0; i < SE_RAY_COUNT; i++) {
glow_rays[i].distance = 30 + (float)(rand() % 30);
glow_rays[i].velocity = 0.1f + (float)(rand() % 100) / 100.0f;
glow_rays[i].angle = (float)(rand() % 360);
glow_rays[i].angle_velocity = 0.1f + (float)(rand() % 10) / 100.0f;
glow_rays[i].alpha = 0.4f + (float)(rand() % 10) / 100.0f;
glow_rays[i].alpha_velocity = 0.1f + (float)(rand() % 10) / 10.0f;
}
}
static void cleanup(void) {
simgui_shutdown();
Expand Down
53 changes: 47 additions & 6 deletions src/retro_achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,15 @@ namespace
delete game_state_ptr; // delete the pointer that was allocated to pass through ffi
}

void retro_achievements_download_user_image(const std::string& url)
{
// TODO: implement me, requires generalizing atlas stuff
}

void retro_achievements_login_callback(int result, const char* error_message,
rc_client_t* client, void* userdata)
{
static char buffer[256];
// TODO: show cool "logged in" banner or something
ra_state_t* state = (ra_state_t*)userdata;
const rc_client_user_t* user = rc_client_get_user_info(client);

Expand All @@ -504,6 +508,13 @@ namespace
sb_save_file_data(path.c_str(), (const uint8_t*)data.data(), data.size());
se_emscripten_flush_fs();
ra_state->error_message.store(nullptr);

std::string url;
url.resize(256);
if (rc_client_user_get_image_url(user, &url[0], url.size()) == RC_OK)
{
retro_achievements_download_user_image(url);
}
} else {
snprintf(buffer, sizeof(buffer), "Login failed: %s", error_message);
ra_state->error_message.store(buffer);
Expand Down Expand Up @@ -727,16 +738,41 @@ namespace

void retro_achievements_draw_achievements()
{
if (!ra_state->game_state)
ra_game_state_ptr game_state = ra_state->game_state;
if (!game_state)
return;

std::unique_lock<std::mutex> lock(ra_state->game_state->mutex);
for (int i = 0; i < ra_state->game_state->achievement_list.buckets.size(); i++)
std::unique_lock<std::mutex> lock(game_state->mutex);
const rc_client_game_t* game = rc_client_get_game_info(ra_state->rc_client);
rc_client_user_game_summary_t summary;
rc_client_get_user_game_summary(ra_state->rc_client, &summary);
auto title = std::string("Playing ") + game->title;
auto description = std::to_string(summary.points_unlocked) + "/" +
std::to_string(summary.points_core) + " points, " +
std::to_string(summary.num_unlocked_achievements) + "/" +
std::to_string(summary.num_core_achievements) + " achievements";
bool hardcore = rc_client_get_hardcore_enabled(ra_state->rc_client);
auto hardcore_str = hardcore ? "Hardcore mode" : "Softcore mode";
uint32_t hardcore_color = hardcore ? 0xff0000ff : 0xff00ff00; // TODO: make me nicer
sg_image image = {SG_INVALID_ID};
atlas_tile_t* tile = game_state->game_image;
ImVec2 uv0 = ImVec2{0, 0};
ImVec2 uv1 = ImVec2{1, 1};
if (tile)
{
image.id = tile->atlas_id;
uv0 = ImVec2{tile->x1, tile->y1};
uv1 = ImVec2{tile->x2, tile->y2};
}
se_boxed_image_triple_label(title.c_str(), description.c_str(), hardcore_str, hardcore_color,
ICON_FK_GAMEPAD, image, 0, uv0, uv1, false);
igSeparator();
for (int i = 0; i < game_state->achievement_list.buckets.size(); i++)
{
if (ra_state->game_state->achievement_list.buckets[i].achievements.empty())
if (game_state->achievement_list.buckets[i].achievements.empty())
continue;

ra_bucket_t* bucket = &ra_state->game_state->achievement_list.buckets[i];
ra_bucket_t* bucket = &game_state->achievement_list.buckets[i];
std::string label = category_to_icon(bucket->bucket_id) + " " +
se_localize_and_cache(bucket->label.c_str());
se_section("%s", label.c_str());
Expand Down Expand Up @@ -1211,6 +1247,11 @@ atlas_tile_t* retro_achievements_get_game_image()
return game_state->game_image;
}

atlas_tile_t* retro_achievements_get_user_image()
{
return nullptr;
}

void retro_achievements_update_atlases()
{
if (!ra_state->game_state)
Expand Down
2 changes: 2 additions & 0 deletions src/retro_achievements.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void retro_achievements_draw_panel();

atlas_tile_t* retro_achievements_get_game_image();

atlas_tile_t* retro_achievements_get_user_image();

void retro_achievements_login(const char* username, const char* password);

bool retro_achievements_is_pending_login();
Expand Down

0 comments on commit af11e7a

Please sign in to comment.