Implemented leaderboard updates

This commit is contained in:
Linus Probert 2018-08-29 22:13:22 +02:00
parent d36c1dd954
commit 7b54e93893
6 changed files with 89 additions and 17 deletions

View File

@ -143,6 +143,7 @@ endif (NOT MSVC)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
if (STEAM) if (STEAM)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DSTEAM_BUILD") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DSTEAM_BUILD")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DSTEAM_BUILD")
endif () endif ()
if (NOT MSVC) if (NOT MSVC)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D__FNAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D__FNAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")

View File

@ -632,6 +632,7 @@ end_game_details(void)
{ {
gui_log("You earned %.2f gold", gPlayer->gold); gui_log("You earned %.2f gold", gPlayer->gold);
gui_event_message("You earned %.2f gold", gPlayer->gold); gui_event_message("You earned %.2f gold", gPlayer->gold);
if (hiscore_get_top_gold() < gPlayer->gold) { if (hiscore_get_top_gold() < gPlayer->gold) {
gui_event_message("NEW HIGHSCORE"); gui_event_message("NEW HIGHSCORE");
gui_log("NEW HIGHSCORE"); gui_log("NEW HIGHSCORE");
@ -854,6 +855,9 @@ run_game(void)
gGameState = GAME_OVER; gGameState = GAME_OVER;
createInGameGameOverMenu(); createInGameGameOverMenu();
hiscore_register(gPlayer, cLevel); hiscore_register(gPlayer, cLevel);
#ifdef STEAM_BUILD
steam_register_score((int)hiscore_get_top_gold());
#endif // STEAM_BUILD
} else { } else {
check_next_level(); check_next_level();

View File

@ -16,6 +16,7 @@ static Uint8 numAchievements = 5;
static bool m_Initiated = false; static bool m_Initiated = false;
static Sint64 m_AppID = 0; static Sint64 m_AppID = 0;
static Sint64 m_hLeaderboard = 0;
static bool static bool
steam_request_stats() steam_request_stats()
@ -37,14 +38,21 @@ stats_stored(void)
debug("Steam stats stored"); debug("Steam stats stored");
} }
static void
leaderboard_received(Sint64 hLeaderboard)
{
m_hLeaderboard = hLeaderboard;
}
void void
steam_init() steam_init()
{ {
c_SteamAPI_SetCallbacks(stats_received, stats_stored, leaderboard_received);
m_AppID = c_SteamAPI_Init(); m_AppID = c_SteamAPI_Init();
m_Initiated = m_AppID != 0; m_Initiated = m_AppID != 0;
if (m_Initiated) { if (m_Initiated) {
c_SteamAPI_SetCallbacks(stats_received, stats_stored);
steam_request_stats(); 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);
}

View File

@ -29,4 +29,6 @@ void steam_shutdown(void);
void steam_run_callbacks(void); void steam_run_callbacks(void);
void steam_set_achievement(EAchievement eAch); void steam_set_achievement(EAchievement eAch);
void steam_register_score(Sint32 nScore);

View File

@ -11,9 +11,11 @@ static bool m_RecvCB = false;
static void(*statsReceivedCb)(void) = NULL; static void(*statsReceivedCb)(void) = NULL;
static void(*statsStoredCb)(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()) { if (SteamAPI_Init()) {
m_AppId = SteamUtils()->GetAppID(); m_AppId = SteamUtils()->GetAppID();
@ -24,13 +26,8 @@ extern "C" int64 c_SteamAPI_Init()
return 0; return 0;
} }
extern "C" void c_SteamAPI_SetCallbacks(void(*recvCB)(void), void(*storCB)(void)) extern "C" int64
{ c_SteamAPI_GetAppID()
statsReceivedCb = recvCB;
statsStoredCb = storCB;
}
extern "C" int64 c_SteamAPI_GetAppID()
{ {
if (!m_Initiated) if (!m_Initiated)
return 0; return 0;
@ -38,19 +35,29 @@ extern "C" int64 c_SteamAPI_GetAppID()
return m_AppId; return m_AppId;
} }
void c_SteamAPI_RunCallbacks(void) void
c_SteamAPI_RunCallbacks(void)
{ {
if (m_Initiated) if (m_Initiated)
SteamAPI_RunCallbacks(); 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; delete m_CallbackHandler;
SteamAPI_Shutdown(); SteamAPI_Shutdown();
} }
extern "C" bool c_SteamUserStats_RequestCurrentStats() extern "C" bool
c_SteamUserStats_RequestCurrentStats()
{ {
if (NULL == SteamUserStats() || NULL == SteamUser()) if (NULL == SteamUserStats() || NULL == SteamUser())
return false; return false;
@ -60,7 +67,8 @@ extern "C" bool c_SteamUserStats_RequestCurrentStats()
return 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) if (!m_RecvCB)
return false; return false;
@ -71,20 +79,44 @@ extern "C" bool c_SteamUserStats_SetAchievement(const char *pchName)
return result; 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); 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); 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() : CallbackHandler::CallbackHandler() :
m_CallbackUserStatsReceived(this, &CallbackHandler::OnUserStatsReceived), m_CallbackUserStatsReceived(this, &CallbackHandler::OnUserStatsReceived),
m_CallbackUserStatsStored(this, &CallbackHandler::OnUserStatsStored) m_CallbackUserStatsStored(this, &CallbackHandler::OnUserStatsStored)
{ {
// Nothing
} }
void void
@ -106,3 +138,12 @@ CallbackHandler::OnUserStatsStored(UserStatsStored_t *pCallback)
if (statsStoredCb && k_EResultOK == pCallback->m_eResult) if (statsStoredCb && k_EResultOK == pCallback->m_eResult)
statsStoredCb(); statsStoredCb();
} }
void CallbackHandler::OnFindLeaderboard(LeaderboardFindResult_t * pCallback, bool bIOFailiure)
{
if (bIOFailiure || !pCallback->m_bLeaderboardFound)
return;
if (leaderBoardReceived)
leaderBoardReceived(pCallback->m_hSteamLeaderboard);
}

View File

@ -12,7 +12,7 @@ void
c_SteamAPI_RunCallbacks(void); c_SteamAPI_RunCallbacks(void);
void void
c_SteamAPI_SetCallbacks(void(*)(void), void(*)(void)); c_SteamAPI_SetCallbacks(void(*)(void), void(*)(void), void(*)(int64_t));
bool bool
c_SteamUserStats_RequestCurrentStats(); c_SteamUserStats_RequestCurrentStats();
@ -26,6 +26,12 @@ c_SteamUserStats_GetAchievement(const char *achId, bool *achieved);
const char * const char *
c_SteamUserStats_GetAchievementDisplayAttribute(const char *achId, const char *attrName); 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 void
c_SteamAPI_Shutdown(); c_SteamAPI_Shutdown();
@ -36,5 +42,8 @@ public:
CallbackHandler(); CallbackHandler();
STEAM_CALLBACK(CallbackHandler, OnUserStatsReceived, UserStatsReceived_t, m_CallbackUserStatsReceived); STEAM_CALLBACK(CallbackHandler, OnUserStatsReceived, UserStatsReceived_t, m_CallbackUserStatsReceived);
STEAM_CALLBACK(CallbackHandler, OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored); STEAM_CALLBACK(CallbackHandler, OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored);
CCallResult<CallbackHandler, LeaderboardFindResult_t> m_FindLeaderboardCallResult;
void OnFindLeaderboard(LeaderboardFindResult_t *pCallback, bool bIOFailiure);
}; };
#endif // __cplusplus #endif // __cplusplus