From a19a13e054d44565da7952950f95e831df2690a4 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Mon, 3 Sep 2018 20:21:54 +0200 Subject: [PATCH] The beginning of an idea for a global leaderboard view --- .clang_complete | 2 + .vimrc | 3 +- steamworks_c_wrapper/CMakeLists.txt | 1 + .../src/CSteamLeaderboard.cpp | 83 +++++++++++++++++++ steamworks_c_wrapper/src/CSteamLeaderboard.h | 29 +++++++ steamworks_c_wrapper/src/CallbackHandler.cpp | 2 +- 6 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 steamworks_c_wrapper/src/CSteamLeaderboard.cpp create mode 100644 steamworks_c_wrapper/src/CSteamLeaderboard.h diff --git a/.clang_complete b/.clang_complete index e1282ec..38a1e64 100644 --- a/.clang_complete +++ b/.clang_complete @@ -1,4 +1,6 @@ -DDEBUG +-DSTEAM_BUILD +-include ./steamworks_c_api/sdk/public/steam/steam_api.h -I./steamworks_c_api/src/ -I./src/steam/ -I./build/config.h diff --git a/.vimrc b/.vimrc index 4647378..ebf83fd 100644 --- a/.vimrc +++ b/.vimrc @@ -5,4 +5,5 @@ nnoremap :ter ++close ./_build/breakhack packadd termdebug let g:termdebug_wide = 1 -let g:syntastic_c_include_dirs = [ '_build', '/usr/include/SDL2', 'steamworks_c_wrapper/src', 'steamworks_c_wrapper/sdk/public/steam' ] +let g:syntastic_c_include_dirs = [ '_build', '/usr/include/SDL2', 'steamworks_c_wrapper/src' ] +let g:syntastic_cpp_include_dirs = [ 'steamworks_c_wrapper/sdk/public/steam' ] diff --git a/steamworks_c_wrapper/CMakeLists.txt b/steamworks_c_wrapper/CMakeLists.txt index f948ab6..9e30331 100644 --- a/steamworks_c_wrapper/CMakeLists.txt +++ b/steamworks_c_wrapper/CMakeLists.txt @@ -35,6 +35,7 @@ else () SHARED src/steamworks_c_wrapper src/CallbackHandler + src/CSteamLeaderboard ) endif() diff --git a/steamworks_c_wrapper/src/CSteamLeaderboard.cpp b/steamworks_c_wrapper/src/CSteamLeaderboard.cpp new file mode 100644 index 0000000..80f8a31 --- /dev/null +++ b/steamworks_c_wrapper/src/CSteamLeaderboard.cpp @@ -0,0 +1,83 @@ +#include +#include "CSteamLeaderboard.h" + +CSteamLeaderboard::CSteamLeaderboard() : m_hCurrentLeaderboard(0) +{ + // Nothing yet +} + +void +CSteamLeaderboard::SetCurrent(SteamLeaderboard_t hCurrentLeaderboard) +{ + m_hCurrentLeaderboard = hCurrentLeaderboard; +} + +void +CSteamLeaderboard::FindLeaderboard(const char *pchLeaderboardName ) +{ + + SteamAPICall_t hSteamAPICall = SteamUserStats()->FindLeaderboard(pchLeaderboardName); + m_callResultFindLeaderboard.Set(hSteamAPICall, + this, + &CSteamLeaderboard::OnFindLeaderboard); +} + +bool +CSteamLeaderboard::UploadScore(int score, const int *details, int nDetails) +{ + if (!m_hCurrentLeaderboard) + return false; + + SteamAPICall_t hSteamAPICall = SteamUserStats()->UploadLeaderboardScore(m_hCurrentLeaderboard, + k_ELeaderboardUploadScoreMethodKeepBest, + score, + details, + nDetails); + m_callResultUploadScore.Set(hSteamAPICall, this, &CSteamLeaderboard::OnUploadScore); + return true; +} + +bool +CSteamLeaderboard::DownloadScores() +{ + if (!m_hCurrentLeaderboard) return false; + SteamAPICall_t hSteamAPICall = SteamUserStats()->DownloadLeaderboardEntries( m_hCurrentLeaderboard, + k_ELeaderboardDataRequestGlobalAroundUser, + -4, + 5); + + m_callResultDownloadScores.Set(hSteamAPICall, this, &CSteamLeaderboard::OnDownloadScores); + return true; +} + +void +CSteamLeaderboard::OnFindLeaderboard(LeaderboardFindResult_t *pCallback, bool bIOFailiure) +{ + if (!pCallback->m_bLeaderboardFound || bIOFailiure) { + std::cerr << "Leaderboard could not be found" << std::endl; + return; + } + m_hCurrentLeaderboard = pCallback->m_hSteamLeaderboard; +} + +void +CSteamLeaderboard::OnUploadScore(LeaderboardScoreUploaded_t *pCallback, bool bIOFailiure) +{ + if (!pCallback->m_bSuccess || bIOFailiure) + std::cerr << "Score could not be uploaded" << std::endl; +} + +void +CSteamLeaderboard::OnDownloadScores(LeaderboardScoresDownloaded_t *pCallback, bool bIOFailiure) +{ + if (!bIOFailiure) { + m_nLeaderboardEntries = std::min(pCallback->m_cEntryCount, 10); + for (int index = 0; index < m_nLeaderboardEntries; index++) { + SteamUserStats()->GetDownloadedLeaderboardEntry( pCallback->m_hSteamLeaderboardEntries, + index, + &m_leaderboardEntries[index], + NULL, + 0); + } + } +} diff --git a/steamworks_c_wrapper/src/CSteamLeaderboard.h b/steamworks_c_wrapper/src/CSteamLeaderboard.h new file mode 100644 index 0000000..3766bad --- /dev/null +++ b/steamworks_c_wrapper/src/CSteamLeaderboard.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +class CSteamLeaderboard +{ + private: + SteamLeaderboard_t m_hCurrentLeaderboard; + LeaderboardEntry_t m_leaderboardEntries[10]; + int m_nLeaderboardEntries = 0; + + public: + CSteamLeaderboard(); + + void SetCurrent(SteamLeaderboard_t hCurrentLeaderboard); + + void FindLeaderboard(const char *pchLeaderboardName ); + bool UploadScore(int score, const int *details, int nDetails); + bool DownloadScores(); + + void OnFindLeaderboard(LeaderboardFindResult_t *pCallback, bool bIOFailiure); + CCallResult m_callResultFindLeaderboard; + + void OnUploadScore(LeaderboardScoreUploaded_t *pCallback, bool bIOFailiure); + CCallResult m_callResultUploadScore; + + void OnDownloadScores(LeaderboardScoresDownloaded_t *pCallback, bool bIOFailiure); + CCallResult m_callResultDownloadScores; +}; diff --git a/steamworks_c_wrapper/src/CallbackHandler.cpp b/steamworks_c_wrapper/src/CallbackHandler.cpp index 2750955..7d1ea04 100644 --- a/steamworks_c_wrapper/src/CallbackHandler.cpp +++ b/steamworks_c_wrapper/src/CallbackHandler.cpp @@ -41,4 +41,4 @@ void CallbackHandler::OnFindLeaderboard(LeaderboardFindResult_t * pCallback, boo if (leaderboardReceivedCb) leaderboardReceivedCb(pCallback->m_hSteamLeaderboard, SteamUserStats()->GetLeaderboardName(pCallback->m_hSteamLeaderboard)); -} \ No newline at end of file +}