From 7b54e9389344e42a2e29b5d8ea207f7f3d3c5b1d Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Wed, 29 Aug 2018 22:13:22 +0200 Subject: [PATCH] Implemented leaderboard updates --- CMakeLists.txt | 1 + src/main.c | 4 ++ src/steam/steamworks_api_wrapper.c | 17 ++++- src/steam/steamworks_api_wrapper.h | 4 +- .../src/steamworks_c_wrapper.cpp | 69 +++++++++++++++---- .../src/steamworks_c_wrapper.h | 11 ++- 6 files changed, 89 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c968c1..0ad95ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,7 @@ endif (NOT MSVC) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG") if (STEAM) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DSTEAM_BUILD") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DSTEAM_BUILD") endif () if (NOT MSVC) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D__FNAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'") diff --git a/src/main.c b/src/main.c index 36fdcac..756241f 100644 --- a/src/main.c +++ b/src/main.c @@ -632,6 +632,7 @@ end_game_details(void) { gui_log("You earned %.2f gold", gPlayer->gold); gui_event_message("You earned %.2f gold", gPlayer->gold); + if (hiscore_get_top_gold() < gPlayer->gold) { gui_event_message("NEW HIGHSCORE"); gui_log("NEW HIGHSCORE"); @@ -854,6 +855,9 @@ run_game(void) gGameState = GAME_OVER; createInGameGameOverMenu(); hiscore_register(gPlayer, cLevel); +#ifdef STEAM_BUILD + steam_register_score((int)hiscore_get_top_gold()); +#endif // STEAM_BUILD } else { check_next_level(); diff --git a/src/steam/steamworks_api_wrapper.c b/src/steam/steamworks_api_wrapper.c index 6307f0f..2f1e89f 100644 --- a/src/steam/steamworks_api_wrapper.c +++ b/src/steam/steamworks_api_wrapper.c @@ -16,6 +16,7 @@ static Uint8 numAchievements = 5; static bool m_Initiated = false; static Sint64 m_AppID = 0; +static Sint64 m_hLeaderboard = 0; static bool steam_request_stats() @@ -37,14 +38,21 @@ stats_stored(void) debug("Steam stats stored"); } +static void +leaderboard_received(Sint64 hLeaderboard) +{ + m_hLeaderboard = hLeaderboard; +} + void steam_init() { + c_SteamAPI_SetCallbacks(stats_received, stats_stored, leaderboard_received); m_AppID = c_SteamAPI_Init(); m_Initiated = m_AppID != 0; if (m_Initiated) { - c_SteamAPI_SetCallbacks(stats_received, stats_stored); steam_request_stats(); + c_SteamUserStats_FindLeaderboard("Highscore"); } } @@ -69,3 +77,10 @@ void steam_set_achievement(EAchievement eAch) } } } + +void steam_register_score(Sint32 nScore) +{ + if (!m_hLeaderboard) + return; + c_SteamUserStats_UploadLeaderboardScore(m_hLeaderboard, nScore); +} diff --git a/src/steam/steamworks_api_wrapper.h b/src/steam/steamworks_api_wrapper.h index a3ca8bc..42f0ba1 100644 --- a/src/steam/steamworks_api_wrapper.h +++ b/src/steam/steamworks_api_wrapper.h @@ -29,4 +29,6 @@ void steam_shutdown(void); void steam_run_callbacks(void); -void steam_set_achievement(EAchievement eAch); \ No newline at end of file +void steam_set_achievement(EAchievement eAch); + +void steam_register_score(Sint32 nScore); \ No newline at end of file diff --git a/steamworks_c_wrapper/src/steamworks_c_wrapper.cpp b/steamworks_c_wrapper/src/steamworks_c_wrapper.cpp index 2596778..957f078 100644 --- a/steamworks_c_wrapper/src/steamworks_c_wrapper.cpp +++ b/steamworks_c_wrapper/src/steamworks_c_wrapper.cpp @@ -11,9 +11,11 @@ static bool m_RecvCB = false; static void(*statsReceivedCb)(void) = NULL; static void(*statsStoredCb)(void) = NULL; +static void(*leaderBoardReceived)(int64) = NULL; -extern "C" int64 c_SteamAPI_Init() +extern "C" int64 +c_SteamAPI_Init() { if (SteamAPI_Init()) { m_AppId = SteamUtils()->GetAppID(); @@ -24,13 +26,8 @@ extern "C" int64 c_SteamAPI_Init() return 0; } -extern "C" void c_SteamAPI_SetCallbacks(void(*recvCB)(void), void(*storCB)(void)) -{ - statsReceivedCb = recvCB; - statsStoredCb = storCB; -} - -extern "C" int64 c_SteamAPI_GetAppID() +extern "C" int64 +c_SteamAPI_GetAppID() { if (!m_Initiated) return 0; @@ -38,19 +35,29 @@ extern "C" int64 c_SteamAPI_GetAppID() return m_AppId; } -void c_SteamAPI_RunCallbacks(void) +void +c_SteamAPI_RunCallbacks(void) { if (m_Initiated) SteamAPI_RunCallbacks(); } -extern "C" void c_SteamAPI_Shutdown() +extern "C" void c_SteamAPI_SetCallbacks(void(*recvCB)(void), void(*storCB)(void), void(*recvLB)(int64_t)) +{ + statsReceivedCb = recvCB; + statsStoredCb = storCB; + leaderBoardReceived = recvLB; +} + +extern "C" void +c_SteamAPI_Shutdown() { delete m_CallbackHandler; SteamAPI_Shutdown(); } -extern "C" bool c_SteamUserStats_RequestCurrentStats() +extern "C" bool +c_SteamUserStats_RequestCurrentStats() { if (NULL == SteamUserStats() || NULL == SteamUser()) return false; @@ -60,7 +67,8 @@ extern "C" bool c_SteamUserStats_RequestCurrentStats() return SteamUserStats()->RequestCurrentStats(); } -extern "C" bool c_SteamUserStats_SetAchievement(const char *pchName) +extern "C" bool +c_SteamUserStats_SetAchievement(const char *pchName) { if (!m_RecvCB) return false; @@ -71,20 +79,44 @@ extern "C" bool c_SteamUserStats_SetAchievement(const char *pchName) return result; } -extern "C" void c_SteamUserStats_GetAchievement(const char *achId, bool *achieved) +extern "C" void +c_SteamUserStats_GetAchievement(const char *achId, bool *achieved) { + if (!m_Initiated) + return; + SteamUserStats()->GetAchievement(achId, achieved); } -extern "C" const char* c_SteamUserStats_GetAchievementDisplayAttribute(const char *achId, const char *attrName) +extern "C" const char* +c_SteamUserStats_GetAchievementDisplayAttribute(const char *achId, const char *attrName) { return SteamUserStats()->GetAchievementDisplayAttribute(achId, attrName); } +extern "C" void +c_SteamUserStats_FindLeaderboard(const char * name) +{ + if (!m_Initiated) + return; + + SteamAPICall_t hSteamAPICall = SteamUserStats()->FindLeaderboard(name); + m_CallbackHandler->m_FindLeaderboardCallResult.Set(hSteamAPICall, m_CallbackHandler, &CallbackHandler::OnFindLeaderboard); +} + +extern "C" void c_SteamUserStats_UploadLeaderboardScore(int64_t hLeaderboard, int32 nScore) +{ + if (!hLeaderboard || !m_Initiated) + return; + + SteamUserStats()->UploadLeaderboardScore(hLeaderboard, k_ELeaderboardUploadScoreMethodKeepBest, nScore, nullptr, 0); +} + CallbackHandler::CallbackHandler() : m_CallbackUserStatsReceived(this, &CallbackHandler::OnUserStatsReceived), m_CallbackUserStatsStored(this, &CallbackHandler::OnUserStatsStored) { + // Nothing } void @@ -106,3 +138,12 @@ CallbackHandler::OnUserStatsStored(UserStatsStored_t *pCallback) if (statsStoredCb && k_EResultOK == pCallback->m_eResult) statsStoredCb(); } + +void CallbackHandler::OnFindLeaderboard(LeaderboardFindResult_t * pCallback, bool bIOFailiure) +{ + if (bIOFailiure || !pCallback->m_bLeaderboardFound) + return; + + if (leaderBoardReceived) + leaderBoardReceived(pCallback->m_hSteamLeaderboard); +} diff --git a/steamworks_c_wrapper/src/steamworks_c_wrapper.h b/steamworks_c_wrapper/src/steamworks_c_wrapper.h index c482ef3..29a0b72 100644 --- a/steamworks_c_wrapper/src/steamworks_c_wrapper.h +++ b/steamworks_c_wrapper/src/steamworks_c_wrapper.h @@ -12,7 +12,7 @@ void c_SteamAPI_RunCallbacks(void); void -c_SteamAPI_SetCallbacks(void(*)(void), void(*)(void)); +c_SteamAPI_SetCallbacks(void(*)(void), void(*)(void), void(*)(int64_t)); bool c_SteamUserStats_RequestCurrentStats(); @@ -26,6 +26,12 @@ c_SteamUserStats_GetAchievement(const char *achId, bool *achieved); const char * c_SteamUserStats_GetAchievementDisplayAttribute(const char *achId, const char *attrName); +void +c_SteamUserStats_FindLeaderboard(const char *name); + +void +c_SteamUserStats_UploadLeaderboardScore(int64_t hLeaderboard, int32_t nScore); + void c_SteamAPI_Shutdown(); @@ -36,5 +42,8 @@ public: CallbackHandler(); STEAM_CALLBACK(CallbackHandler, OnUserStatsReceived, UserStatsReceived_t, m_CallbackUserStatsReceived); STEAM_CALLBACK(CallbackHandler, OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored); + CCallResult m_FindLeaderboardCallResult; + + void OnFindLeaderboard(LeaderboardFindResult_t *pCallback, bool bIOFailiure); }; #endif // __cplusplus \ No newline at end of file