Added kills leaderboard
- Restructured the code a bit to avoid c vs c++ conflicts.
This commit is contained in:
parent
26379535be
commit
1972b5b1df
|
@ -895,6 +895,7 @@ run_game(void)
|
||||||
hiscore_register(gPlayer, cLevel);
|
hiscore_register(gPlayer, cLevel);
|
||||||
#ifdef STEAM_BUILD
|
#ifdef STEAM_BUILD
|
||||||
steam_register_score((int)hiscore_get_top_gold());
|
steam_register_score((int)hiscore_get_top_gold());
|
||||||
|
steam_register_kills((int) gPlayer->stat_data.kills);
|
||||||
#endif // STEAM_BUILD
|
#endif // STEAM_BUILD
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
#include "steamworks_api_wrapper.h"
|
#include "steamworks_api_wrapper.h"
|
||||||
#include "steamworks_c_wrapper.h"
|
#include "steamworks_c_wrapper.h"
|
||||||
#include "../util.h"
|
#include "../util.h"
|
||||||
#include "../defines.h"
|
#include "../defines.h"
|
||||||
#include "../gui.h"
|
#include "../gui.h"
|
||||||
|
#include "../timer.h"
|
||||||
|
|
||||||
|
static const char *LB_HIGHSCORE = "Highscore";
|
||||||
|
static const char *LB_KILLS = "Most Kills";
|
||||||
|
|
||||||
static Achievement g_Achievements[] = {
|
static Achievement g_Achievements[] = {
|
||||||
_ACH_ID(BAD_DOG, "Bad Dog"),
|
_ACH_ID(BAD_DOG, "Bad Dog"),
|
||||||
|
@ -15,8 +20,12 @@ static Achievement g_Achievements[] = {
|
||||||
static Uint8 numAchievements = 5;
|
static Uint8 numAchievements = 5;
|
||||||
|
|
||||||
static bool m_Initiated = false;
|
static bool m_Initiated = false;
|
||||||
|
static bool m_bStatsReceived = false;
|
||||||
static Sint64 m_AppID = 0;
|
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
|
static bool
|
||||||
steam_request_stats(void)
|
steam_request_stats(void)
|
||||||
|
@ -30,6 +39,7 @@ static void
|
||||||
stats_received(void)
|
stats_received(void)
|
||||||
{
|
{
|
||||||
debug("Steam stats received");
|
debug("Steam stats received");
|
||||||
|
m_bStatsReceived = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -39,32 +49,54 @@ stats_stored(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
static 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
|
void
|
||||||
steam_init()
|
steam_init()
|
||||||
{
|
{
|
||||||
c_SteamAPI_SetCallbacks(stats_received, stats_stored, leaderboard_received);
|
|
||||||
m_AppID = c_SteamAPI_Init();
|
m_AppID = c_SteamAPI_Init();
|
||||||
|
c_SteamAPI_SetCallbacks(stats_received, stats_stored, leaderboard_received);
|
||||||
m_Initiated = m_AppID != 0;
|
m_Initiated = m_AppID != 0;
|
||||||
if (m_Initiated) {
|
requestDataTimer = timer_create();
|
||||||
steam_request_stats();
|
|
||||||
c_SteamUserStats_FindLeaderboard("Highscore");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void steam_shutdown(void)
|
void steam_shutdown(void)
|
||||||
{
|
{
|
||||||
c_SteamAPI_Shutdown();
|
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)
|
void steam_run_callbacks(void)
|
||||||
{
|
{
|
||||||
if (m_Initiated)
|
if (m_Initiated) {
|
||||||
c_SteamAPI_RunCallbacks();
|
c_SteamAPI_RunCallbacks();
|
||||||
|
request_data_queue_run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void steam_set_achievement(EAchievement eAch)
|
void steam_set_achievement(EAchievement eAch)
|
||||||
|
@ -80,7 +112,14 @@ void steam_set_achievement(EAchievement eAch)
|
||||||
|
|
||||||
void steam_register_score(Sint32 nScore)
|
void steam_register_score(Sint32 nScore)
|
||||||
{
|
{
|
||||||
if (!m_hLeaderboard)
|
if (!m_hHighscoreLeaderboard)
|
||||||
return;
|
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);
|
||||||
|
}
|
|
@ -31,4 +31,6 @@ void steam_run_callbacks(void);
|
||||||
|
|
||||||
void steam_set_achievement(EAchievement eAch);
|
void steam_set_achievement(EAchievement eAch);
|
||||||
|
|
||||||
void steam_register_score(Sint32 nScore);
|
void steam_register_score(Sint32 nScore);
|
||||||
|
|
||||||
|
void steam_register_kills(Sint32 nKills);
|
||||||
|
|
|
@ -28,6 +28,7 @@ include_directories(${STEAMWORKS_INCLUDE_DIR})
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
add_library(steamworks_c_wrapper
|
add_library(steamworks_c_wrapper
|
||||||
src/steamworks_c_wrapper
|
src/steamworks_c_wrapper
|
||||||
|
src/CallbackHandler
|
||||||
)
|
)
|
||||||
else ()
|
else ()
|
||||||
add_library(steamworks_c_wrapper
|
add_library(steamworks_c_wrapper
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <steam_api.h>
|
||||||
|
|
||||||
|
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<CallbackHandler, LeaderboardFindResult_t> m_FindLeaderboardCallResult;
|
||||||
|
|
||||||
|
void(*statsReceivedCb)() = nullptr;
|
||||||
|
void(*statsStoredCb)() = nullptr;
|
||||||
|
void(*leaderboardReceivedCb)(int64, const char*) = nullptr;
|
||||||
|
|
||||||
|
bool CallbackReceived() const;
|
||||||
|
|
||||||
|
void OnFindLeaderboard(LeaderboardFindResult_t *pCallback, bool bIOFailiure);
|
||||||
|
};
|
|
@ -3,23 +3,18 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "steamworks_c_wrapper.h"
|
#include "steamworks_c_wrapper.h"
|
||||||
}
|
}
|
||||||
|
#include "CallbackHandler.h"
|
||||||
|
|
||||||
static bool m_Initiated = false;
|
static bool m_Initiated = false;
|
||||||
static int64 m_AppId = 0;
|
static int64 m_AppId = 0;
|
||||||
static CallbackHandler *m_CallbackHandler = NULL;
|
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
|
extern "C" int64_t
|
||||||
c_SteamAPI_Init()
|
c_SteamAPI_Init()
|
||||||
{
|
{
|
||||||
if (SteamAPI_Init()) {
|
if (SteamAPI_Init()) {
|
||||||
m_AppId = SteamUtils()->GetAppID();
|
m_AppId = SteamUtils()->GetAppID();
|
||||||
m_CallbackHandler = new CallbackHandler();
|
m_CallbackHandler = new CallbackHandler(m_AppId);
|
||||||
m_Initiated = true;
|
m_Initiated = true;
|
||||||
return m_AppId;
|
return m_AppId;
|
||||||
}
|
}
|
||||||
|
@ -42,11 +37,11 @@ c_SteamAPI_RunCallbacks(void)
|
||||||
SteamAPI_RunCallbacks();
|
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;
|
m_CallbackHandler->statsReceivedCb = recvCB;
|
||||||
statsStoredCb = storCB;
|
m_CallbackHandler->statsStoredCb = storCB;
|
||||||
leaderBoardReceived = recvLB;
|
m_CallbackHandler->leaderboardReceivedCb = recvLB;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void
|
extern "C" void
|
||||||
|
@ -70,7 +65,7 @@ c_SteamUserStats_RequestCurrentStats()
|
||||||
extern "C" bool
|
extern "C" bool
|
||||||
c_SteamUserStats_SetAchievement(const char *pchName)
|
c_SteamUserStats_SetAchievement(const char *pchName)
|
||||||
{
|
{
|
||||||
if (!m_RecvCB)
|
if (m_CallbackHandler && !m_CallbackHandler->CallbackReceived())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool result = SteamUserStats()->SetAchievement(pchName);
|
bool result = SteamUserStats()->SetAchievement(pchName);
|
||||||
|
@ -97,53 +92,17 @@ c_SteamUserStats_GetAchievementDisplayAttribute(const char *achId, const char *a
|
||||||
extern "C" void
|
extern "C" void
|
||||||
c_SteamUserStats_FindLeaderboard(const char * name)
|
c_SteamUserStats_FindLeaderboard(const char * name)
|
||||||
{
|
{
|
||||||
if (!m_Initiated)
|
if (!m_Initiated || !m_CallbackHandler)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
SteamAPICall_t hSteamAPICall = SteamUserStats()->FindLeaderboard(name);
|
SteamAPICall_t hSteamAPICall = SteamUserStats()->FindLeaderboard(name);
|
||||||
m_CallbackHandler->m_FindLeaderboardCallResult.Set(hSteamAPICall, m_CallbackHandler, &CallbackHandler::OnFindLeaderboard);
|
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)
|
if (!hLeaderboard || !m_Initiated)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
SteamUserStats()->UploadLeaderboardScore(hLeaderboard, k_ELeaderboardUploadScoreMethodKeepBest, nScore, nullptr, 0);
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ void
|
||||||
c_SteamAPI_RunCallbacks(void);
|
c_SteamAPI_RunCallbacks(void);
|
||||||
|
|
||||||
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
|
bool
|
||||||
c_SteamUserStats_RequestCurrentStats(void);
|
c_SteamUserStats_RequestCurrentStats(void);
|
||||||
|
@ -33,17 +33,4 @@ void
|
||||||
c_SteamUserStats_UploadLeaderboardScore(int64_t hLeaderboard, int32_t nScore);
|
c_SteamUserStats_UploadLeaderboardScore(int64_t hLeaderboard, int32_t nScore);
|
||||||
|
|
||||||
void
|
void
|
||||||
c_SteamAPI_Shutdown(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<CallbackHandler, LeaderboardFindResult_t> m_FindLeaderboardCallResult;
|
|
||||||
|
|
||||||
void OnFindLeaderboard(LeaderboardFindResult_t *pCallback, bool bIOFailiure);
|
|
||||||
};
|
|
||||||
#endif // __cplusplus
|
|
Loading…
Reference in New Issue