BlitzMax Steam Stub


Tweet blitzmax steam code-archives
(Posted 2 weeks ago) RonTek

BlitzMax Steamworks wrapper and stub

It has two uses so far, one Blitz3D and one BlitzMax, achievements only on both games. From my understanding both games weren't allowed on steam without having at minimum a rewards system.

You will need dev status with steam and you should get basic idea of capabilities of sdk from the decls file.

Author: skidracer

BlitzMax Steamworks wrapper and stub

Download v4a BlitzMax only
Download v3 Blitz3D and BlitzMax

For Mac integration: You don't need the steamstub dll as you can include steamstub.cpp (below) directly into blitzmax but you do need a post compile step where the steam api library needs to be copied into the .app folder.

steamstub.cpp

#define _CRT_SECURE_NO_WARNINGS

//#include <windows.h>
#include <string>

#include "include/steam_api.h"

#define BBDECL extern "C" //_declspec(dllexport)

SteamLeaderboard_t handle=0;
std::string steamlog;


class CStatsAndAchievements
{
public:
CGameID m_GameID;

CStatsAndAchievements( void *pGameEngine,int gameID );
STEAM_CALLBACK( CStatsAndAchievements, OnUserStatsReceived, UserStatsReceived_t, m_CallbackUserStatsReceived );
STEAM_CALLBACK( CStatsAndAchievements, OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored );
STEAM_CALLBACK( CStatsAndAchievements, OnAchievementStored, UserAchievementStored_t, m_CallbackAchievementStored );
STEAM_CALLBACK( CStatsAndAchievements, OnFindScores, LeaderboardFindResult_t, m_CallbackFindScores );
STEAM_CALLBACK( CStatsAndAchievements, OnDownload, LeaderboardScoresDownloaded_t, m_CallbackDownloaded );
STEAM_CALLBACK( CStatsAndAchievements, OnUpload, LeaderboardScoreUploaded_t, m_CallbackUploaded );
STEAM_CALLBACK( CStatsAndAchievements, OnCount, NumberOfCurrentPlayers_t, m_CallbackCount );
};

CStatsAndAchievements::CStatsAndAchievements( void *pGameEngine ,int gameID ):
m_CallbackUserStatsReceived( this, &CStatsAndAchievements::OnUserStatsReceived ),
m_CallbackUserStatsStored( this, &CStatsAndAchievements::OnUserStatsStored ),
m_CallbackAchievementStored( this, &CStatsAndAchievements::OnAchievementStored ),
m_CallbackFindScores( this, &CStatsAndAchievements::OnFindScores ),
m_CallbackDownloaded( this, &CStatsAndAchievements::OnDownload ),
m_CallbackUploaded( this, &CStatsAndAchievements::OnUpload ),
m_CallbackCount( this, &CStatsAndAchievements::OnCount ),
m_GameID( gameID )
{
//  m_GameID..Set( uint32 unAccountID, EUniverse eUniverse, EAccountType eAccountType )

}


void CStatsAndAchievements::OnDownload( LeaderboardScoresDownloaded_t *download ){

LeaderboardEntry_t entry;
int32 details[256];
char buffer[128];
int n;

_snprintf( buffer, 128, "download=%d!", download->m_cEntryCount );
steamlog.append(buffer);
for ( int index = 0; index < download->m_cEntryCount; index++ ){
SteamUserStats()->GetDownloadedLeaderboardEntry( 
download->m_hSteamLeaderboardEntries, 
index, 
&entry, 
details,
256
);
n=_snprintf( buffer, 128, "%d:%d:%d:", entry.m_nScore,entry.m_nGlobalRank,entry.m_steamIDUser);
steamlog.append(buffer);
for( int i=0;i<entry.m_cDetails;i++){
steamlog.push_back((char)details[i]);
}
steamlog.append("!");
}
}

void CStatsAndAchievements::OnUpload( LeaderboardScoreUploaded_t *up ){
char buffer[128];
if(up->m_bSuccess && up->m_bScoreChanged){
_snprintf( buffer, 128, "upload=%d:%d:%d!", up->m_nScore,up->m_nGlobalRankNew,up->m_nGlobalRankPrevious );
steamlog.append(buffer);
}
}

void CStatsAndAchievements::OnFindScores( LeaderboardFindResult_t *t ){
char buffer[128];
if(t->m_bLeaderboardFound){
handle=t->m_hSteamLeaderboard;
_snprintf( buffer, 128, "found=%s:%d!", 
SteamUserStats()->GetLeaderboardName(t->m_hSteamLeaderboard),
SteamUserStats()->GetLeaderboardEntryCount(t->m_hSteamLeaderboard));
steamlog.append(buffer);
}
}

void CStatsAndAchievements::OnCount(NumberOfCurrentPlayers_t *n){
char buffer[128];
_snprintf( buffer, 128, "playercount=%d!", n->m_cPlayers);
steamlog.append(buffer);
}

void CStatsAndAchievements::OnUserStatsReceived( UserStatsReceived_t *pCallback )
{
// we may get callbacks for other games' stats arriving, ignore them
if ( m_GameID.ToUint64() == pCallback->m_nGameID )
{
if ( k_EResultOK == pCallback->m_eResult )
{
steamlog.append("stats received!");
}
else
{
char buffer[128];
_snprintf( buffer, 128, "stats failed %d!", pCallback->m_eResult );
steamlog.append( buffer );
}
}
}


void CStatsAndAchievements::OnUserStatsStored( UserStatsStored_t *pCallback )
{
// we may get callbacks for other games' stats arriving, ignore them
if ( m_GameID.ToUint64() == pCallback->m_nGameID )
{
if ( k_EResultOK == pCallback->m_eResult )
{
steamlog.append("stats stored!");
}
else
{
char buffer[128];
_snprintf( buffer, 128, "stats stored failed %d!", pCallback->m_eResult );
steamlog.append( buffer );
}
}
}
void CStatsAndAchievements::OnAchievementStored( UserAchievementStored_t *pCallback )
{
// we may get callbacks for other games' stats arriving, ignore them
if ( m_GameID.ToUint64() == pCallback->m_nGameID )
{
if ( 0 == pCallback->m_nMaxProgress )
{
char buffer[128];
_snprintf( buffer, 128, "Achievement '%s' unlocked!", pCallback->m_rgchAchievementName );
steamlog.append(buffer);
}
else
{
char buffer[128];
_snprintf( buffer, 128, "Achievement '%s' progress %d:%d!", 
pCallback->m_rgchAchievementName, pCallback->m_nCurProgress, pCallback->m_nMaxProgress );
steamlog.append( buffer );
}
}
}





#define MAXPLAYERFETCH 256
#define BUFFERSIZE 4096+MAXPLAYERFETCH*512

char rbuffer[BUFFERSIZE];

BBDECL char *ReadSteam(){
SteamAPI_RunCallbacks();
_snprintf(rbuffer,BUFFERSIZE,"%s",steamlog.c_str());
steamlog="";
return rbuffer;
}

CStatsAndAchievements *stats;

BBDECL void StoreSteamStats(){
SteamUserStats()->StoreStats();
}

// stats 

BBDECL int GetSteamStat(const char *id){
int32 res;
if (!SteamUserStats()->GetStat(id,&res )){
return -1;
}
return res;
}

BBDECL void SetSteamStat(const char *id,int32 value){
SteamUserStats()->SetStat(id,value);
}

// achieves

BBDECL int GetSteamAchievement(char *id){
bool res;
SteamUserStats()->GetAchievement(id,&res );
return res?1:0;
}

BBDECL void SetSteamAchievement(const char *id){
SteamUserStats()->SetAchievement(id);
}

BBDECL void ClearSteamAchievement(const char *id){
SteamUserStats()->ClearAchievement(id);
}

BBDECL int GetSteamAchievementIcon(const char *pchName){
return SteamUserStats()->GetAchievementIcon( pchName );
}
// Get general attributes (display name / text, etc) for an Achievement
BBDECL const char *GetAchievementDisplayAttribute( const char *pchName, const char *pchKey ){
return SteamUserStats()->GetAchievementDisplayAttribute( pchName, pchKey );
}

// Achievement progress - triggers an AchievementProgress callback, that is all.
// Calling this w/ N out of N progress will NOT set the achievement, the game must still do that.
BBDECL int IndicateAchievementProgress( const char *pchName, uint32 nCurProgress, uint32 nMaxProgress ){
return SteamUserStats()->IndicateAchievementProgress( pchName, nCurProgress, nMaxProgress )?1:0;
}

char leadname[256];

BBDECL void FindOrCreateLeaderboard( char *name, ELeaderboardSortMethod sortmethod, ELeaderboardDisplayType displaytype ){
handle=0;
sprintf(leadname,name);
SteamUserStats()->FindOrCreateLeaderboard(leadname,sortmethod,displaytype);
}

BBDECL void FindLeaderboard( char *name ){
handle=0;
sprintf(leadname,name);
SteamUserStats()->FindLeaderboard(leadname);
}

BBDECL void UploadLeaderboardScore( ELeaderboardUploadScoreMethod method, int32 score, char *details ) {
int data[256];
int i;
if(handle==0){
steamlog.append( "no leaderboard open!" );
return;
}

for(i=0;i<255;i++){
if ((data[i]=details[i])==0){
break;
}
}

data[i++]=0;
SteamUserStats()->UploadLeaderboardScore(handle,method,score,data,i);
}

BBDECL void FindNumberOfCurrentPlayers() {
SteamUserStats()->GetNumberOfCurrentPlayers();
}

BBDECL void DownloadLeaderboardEntries( ELeaderboardDataRequest request, int start, int end ) {
if(handle==0){
steamlog.append( "no leaderboard open!" );
return;
}
if(end-start>MAXPLAYERFETCH){
end=start+MAXPLAYERFETCH;
}
SteamUserStats()->DownloadLeaderboardEntries(handle,request,start,end);
}


BBDECL void CloseSteam(){
SteamAPI_Shutdown();
}

BBDECL int OpenSteam(int gameID){
if(!SteamAPI_Init()){
return -1;
}

CSteamID steamid;
bool loggedon;
steamid=SteamUser()->GetSteamID();
loggedon=SteamUser()->BLoggedOn();
int res=1;

//  engine=new CGameEngine( hInstance, nCmdShow, 1024, 768 );

stats=new CStatsAndAchievements(&res,gameID);

SteamUserStats()->RequestCurrentStats();
//  SteamUserStats()->SetStat("Player hatched",50);
//  SteamUserStats()->StoreStats();

return loggedon?1:0;
}

Reply To Topic (minimum 10 characters)

Please log in to reply