diff --git a/src/main.c b/src/main.c index d7ae48e..c4cf3da 100644 --- a/src/main.c +++ b/src/main.c @@ -895,6 +895,7 @@ run_game(void) hiscore_register(gPlayer, cLevel); #ifdef STEAM_BUILD steam_register_score((int)hiscore_get_top_gold()); + steam_register_kills((int) gPlayer->stat_data.kills); #endif // STEAM_BUILD } else { diff --git a/src/steam/steamworks_api_wrapper.c b/src/steam/steamworks_api_wrapper.c index 0b1209f..ac597b6 100644 --- a/src/steam/steamworks_api_wrapper.c +++ b/src/steam/steamworks_api_wrapper.c @@ -1,9 +1,14 @@ #include +#include #include "steamworks_api_wrapper.h" #include "steamworks_c_wrapper.h" #include "../util.h" #include "../defines.h" #include "../gui.h" +#include "../timer.h" + +static const char *LB_HIGHSCORE = "Highscore"; +static const char *LB_KILLS = "Most Kills"; static Achievement g_Achievements[] = { _ACH_ID(BAD_DOG, "Bad Dog"), @@ -15,8 +20,12 @@ static Achievement g_Achievements[] = { static Uint8 numAchievements = 5; static bool m_Initiated = false; +static bool m_bStatsReceived = false; static Sint64 m_AppID = 0; -static Sint64 m_hLeaderboard = 0; +static Sint64 m_hHighscoreLeaderboard = 0; +static Sint64 m_hKillsLeaderboard = 0; + +static Timer *requestDataTimer = NULL; static bool steam_request_stats(void) @@ -30,6 +39,7 @@ static void stats_received(void) { debug("Steam stats received"); + m_bStatsReceived = true; } static void @@ -39,32 +49,54 @@ stats_stored(void) } static void -leaderboard_received(Sint64 hLeaderboard) +leaderboard_received(Sint64 hLeaderboard, const char *name) { - m_hLeaderboard = hLeaderboard; + debug("Leaderboard received: %s", name); + if (strcmp(LB_HIGHSCORE, name) == 0) + m_hHighscoreLeaderboard = hLeaderboard; + else if (strcmp(LB_KILLS, name) == 0) + m_hKillsLeaderboard = hLeaderboard; } void steam_init() { - c_SteamAPI_SetCallbacks(stats_received, stats_stored, leaderboard_received); m_AppID = c_SteamAPI_Init(); + c_SteamAPI_SetCallbacks(stats_received, stats_stored, leaderboard_received); m_Initiated = m_AppID != 0; - if (m_Initiated) { - steam_request_stats(); - c_SteamUserStats_FindLeaderboard("Highscore"); - } + requestDataTimer = timer_create(); } void steam_shutdown(void) { c_SteamAPI_Shutdown(); + timer_destroy(requestDataTimer); +} + +void +request_data_queue_run(void) +{ + if (!timer_started(requestDataTimer)) + timer_start(requestDataTimer); + + if (timer_get_ticks(requestDataTimer) > 1000) { + if (!m_bStatsReceived) + steam_request_stats(); + else if (!m_hHighscoreLeaderboard) + c_SteamUserStats_FindLeaderboard(LB_HIGHSCORE); + else if (!m_hKillsLeaderboard) + c_SteamUserStats_FindLeaderboard(LB_KILLS); + + timer_start(requestDataTimer); + } } void steam_run_callbacks(void) { - if (m_Initiated) + if (m_Initiated) { c_SteamAPI_RunCallbacks(); + request_data_queue_run(); + } } void steam_set_achievement(EAchievement eAch) @@ -80,7 +112,14 @@ void steam_set_achievement(EAchievement eAch) void steam_register_score(Sint32 nScore) { - if (!m_hLeaderboard) + if (!m_hHighscoreLeaderboard) return; - c_SteamUserStats_UploadLeaderboardScore(m_hLeaderboard, nScore); + c_SteamUserStats_UploadLeaderboardScore(m_hHighscoreLeaderboard, nScore); } + +void steam_register_kills(Sint32 nKills) +{ + if (!m_hKillsLeaderboard) + return; + c_SteamUserStats_UploadLeaderboardScore(m_hKillsLeaderboard, nKills); +} \ No newline at end of file diff --git a/src/steam/steamworks_api_wrapper.h b/src/steam/steamworks_api_wrapper.h index 42f0ba1..52ec293 100644 --- a/src/steam/steamworks_api_wrapper.h +++ b/src/steam/steamworks_api_wrapper.h @@ -31,4 +31,6 @@ void steam_run_callbacks(void); void steam_set_achievement(EAchievement eAch); -void steam_register_score(Sint32 nScore); \ No newline at end of file +void steam_register_score(Sint32 nScore); + +void steam_register_kills(Sint32 nKills); diff --git a/steamworks_c_wrapper/CMakeLists.txt b/steamworks_c_wrapper/CMakeLists.txt index 4199e59..731c097 100644 --- a/steamworks_c_wrapper/CMakeLists.txt +++ b/steamworks_c_wrapper/CMakeLists.txt @@ -28,6 +28,7 @@ include_directories(${STEAMWORKS_INCLUDE_DIR}) if (WIN32) add_library(steamworks_c_wrapper src/steamworks_c_wrapper + src/CallbackHandler ) else () add_library(steamworks_c_wrapper diff --git a/steamworks_c_wrapper/src/CallbackHandler.cpp b/steamworks_c_wrapper/src/CallbackHandler.cpp new file mode 100644 index 0000000..2750955 --- /dev/null +++ b/steamworks_c_wrapper/src/CallbackHandler.cpp @@ -0,0 +1,44 @@ +#include "CallbackHandler.h" + +CallbackHandler::CallbackHandler(int64 appId) : + m_CallbackUserStatsReceived(this, &CallbackHandler::OnUserStatsReceived), + m_CallbackUserStatsStored(this, &CallbackHandler::OnUserStatsStored), + m_AppId(appId) +{ + // Nothing +} + +void +CallbackHandler::OnUserStatsReceived(UserStatsReceived_t *pCallback) +{ + if (m_AppId != pCallback->m_nGameID) + return; + + m_bStatsCallbackReceived = true; + if (statsReceivedCb && k_EResultOK == pCallback->m_eResult) + statsReceivedCb(); +} + +void +CallbackHandler::OnUserStatsStored(UserStatsStored_t *pCallback) +{ + if (m_AppId != pCallback->m_nGameID) + return; + + if (statsStoredCb && k_EResultOK == pCallback->m_eResult) + statsStoredCb(); +} + +bool CallbackHandler::CallbackReceived() const +{ + return m_bStatsCallbackReceived; +} + +void CallbackHandler::OnFindLeaderboard(LeaderboardFindResult_t * pCallback, bool bIOFailiure) +{ + if (bIOFailiure || !pCallback->m_bLeaderboardFound) + return; + + if (leaderboardReceivedCb) + leaderboardReceivedCb(pCallback->m_hSteamLeaderboard, SteamUserStats()->GetLeaderboardName(pCallback->m_hSteamLeaderboard)); +} \ No newline at end of file diff --git a/steamworks_c_wrapper/src/CallbackHandler.h b/steamworks_c_wrapper/src/CallbackHandler.h new file mode 100644 index 0000000..2abba9b --- /dev/null +++ b/steamworks_c_wrapper/src/CallbackHandler.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +class CallbackHandler +{ +private: + int64 m_AppId = 0; + bool m_bStatsCallbackReceived = false; + +public: + CallbackHandler(int64 appId); + STEAM_CALLBACK(CallbackHandler, OnUserStatsReceived, UserStatsReceived_t, m_CallbackUserStatsReceived); + STEAM_CALLBACK(CallbackHandler, OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored); + CCallResult m_FindLeaderboardCallResult; + + void(*statsReceivedCb)() = nullptr; + void(*statsStoredCb)() = nullptr; + void(*leaderboardReceivedCb)(int64, const char*) = nullptr; + + bool CallbackReceived() const; + + void OnFindLeaderboard(LeaderboardFindResult_t *pCallback, bool bIOFailiure); +}; \ 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 45b13b2..296a157 100644 --- a/steamworks_c_wrapper/src/steamworks_c_wrapper.cpp +++ b/steamworks_c_wrapper/src/steamworks_c_wrapper.cpp @@ -3,23 +3,18 @@ extern "C" { #include "steamworks_c_wrapper.h" } +#include "CallbackHandler.h" static bool m_Initiated = false; static int64 m_AppId = 0; static CallbackHandler *m_CallbackHandler = NULL; -static bool m_RecvCB = false; - -static void(*statsReceivedCb)(void) = NULL; -static void(*statsStoredCb)(void) = NULL; -static void(*leaderBoardReceived)(int64_t) = NULL; - extern "C" int64_t c_SteamAPI_Init() { if (SteamAPI_Init()) { m_AppId = SteamUtils()->GetAppID(); - m_CallbackHandler = new CallbackHandler(); + m_CallbackHandler = new CallbackHandler(m_AppId); m_Initiated = true; return m_AppId; } @@ -42,11 +37,11 @@ c_SteamAPI_RunCallbacks(void) SteamAPI_RunCallbacks(); } -extern "C" void c_SteamAPI_SetCallbacks(void(*recvCB)(void), void(*storCB)(void), void(*recvLB)(int64_t)) +extern "C" void c_SteamAPI_SetCallbacks(void(*recvCB)(void), void(*storCB)(void), void(*recvLB)(int64_t, const char *)) { - statsReceivedCb = recvCB; - statsStoredCb = storCB; - leaderBoardReceived = recvLB; + m_CallbackHandler->statsReceivedCb = recvCB; + m_CallbackHandler->statsStoredCb = storCB; + m_CallbackHandler->leaderboardReceivedCb = recvLB; } extern "C" void @@ -70,7 +65,7 @@ c_SteamUserStats_RequestCurrentStats() extern "C" bool c_SteamUserStats_SetAchievement(const char *pchName) { - if (!m_RecvCB) + if (m_CallbackHandler && !m_CallbackHandler->CallbackReceived()) return false; bool result = SteamUserStats()->SetAchievement(pchName); @@ -97,53 +92,17 @@ c_SteamUserStats_GetAchievementDisplayAttribute(const char *achId, const char *a extern "C" void c_SteamUserStats_FindLeaderboard(const char * name) { - if (!m_Initiated) + if (!m_Initiated || !m_CallbackHandler) 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) +extern "C" void c_SteamUserStats_UploadLeaderboardScore(int64_t hLeaderboard, int32_t 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 -CallbackHandler::OnUserStatsReceived(UserStatsReceived_t *pCallback) -{ - if (m_AppId != pCallback->m_nGameID) - return; - m_RecvCB = true; - if (statsReceivedCb && k_EResultOK == pCallback->m_eResult) - statsReceivedCb(); -} - -void -CallbackHandler::OnUserStatsStored(UserStatsStored_t *pCallback) -{ - if (m_AppId != pCallback->m_nGameID) - return; - - 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 a0035d5..54b2a7c 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), void(*)(int64_t)); +c_SteamAPI_SetCallbacks(void(*recvCB)(void), void(*storCB)(void), void(*recvLB)(int64_t, const char *)); bool c_SteamUserStats_RequestCurrentStats(void); @@ -33,17 +33,4 @@ void c_SteamUserStats_UploadLeaderboardScore(int64_t hLeaderboard, int32_t nScore); void -c_SteamAPI_Shutdown(void); - -#ifdef __cplusplus -class CallbackHandler -{ -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 +c_SteamAPI_Shutdown(void); \ No newline at end of file