Monday, September 23, 2013

GameCenter Marmalade Code in a Class

Here is my gamecenter code that works from within the Marmalade SDK. I hope you programmers can find aspects of this useful.  I basically took the example that Marmalade supplies and then converted the aspects I wanted into a class.

Good luck,
Da Voodoochief

GameCenter.h
#if !defined(_CGAMECENTER_H_)
#define _CGAMECENTER_H_
#include "s3eIOSGameCenter.h"
#include "s3eIOSIAd.h"
extern bool gAuthenticated;
extern char gUserName[S3E_IOSGAMECENTER_STRING_MAX];
extern int gGameCenterState; // useful for debugging
class cGameCenter
{
public:
 int g_NumFriendsFound;
 char** g_FriendIDs;
 s3eIOSGameCenterVoiceChat* g_VoiceChat;
    //-------------------------------------------------------
    cGameCenter()   {
  gAuthenticated = false;
  strcpy (&gUserName[S3E_IOSGAMECENTER_STRING_MAX], "Unknown");
  g_NumFriendsFound = 0;
  g_FriendIDs = NULL;
  g_VoiceChat = NULL;
  gGameCenterState = 0;
 };
 static void LoadAchievementsCallback(s3eIOSGameCenterAchievementList* list);
 static void ReportAchievementCallback(s3eIOSGameCenterError* error);
 static void LoadAchievementInfoCallback(s3eIOSGameCenterAchievementInfoList* list);
 void GetMyAchievementsHandler();
 void GetAchievementsHandler();
 void ReportAchievementHandler();
 void GameCenter_DisplayAchievements();
// static void GetPlayersCallback(s3eIOSGameCenterPlayerInfo* playersInfo);
 static void AuthenticationCallback(s3eIOSGameCenterError* error, void* userData);
 void AuthenticateLocalHandler();
 void GameCenterRegister();
 static void GameCenter_ReportScoreCallback(s3eIOSGameCenterError* error);
 void GameCenter_ReportScore(int _score, int _level);
 void GameCenter_DisplayLeaderboard();
};

#endif

GameCenter.cpp
#include "Game1.h"
//-------------------------------------------------------
int gGameCenterState = 0; // useful for debugging
bool gAuthenticated = false;
char gUserName[S3E_IOSGAMECENTER_STRING_MAX];
//-------------------------------------------------------
// Callback functions to report authentication, matchmaking, data events, etc...
/*void cGameCenter::GetPlayersCallback(s3eIOSGameCenterPlayerInfo* playersInfo)
{
    if (playersInfo->m_Error != S3E_IOSGAMECENTER_ERR_NONE)
    {
//        AppendMessageColour(RED, "Get players info error: %s", ErrorAsString(playersInfo->m_Error));
        return;
    }
    if (!playersInfo->m_PlayerCount)
    {
//        AppendMessage("`x996666Error? ReceivedPlayers callback indicates no players!");
        return;
    }
    for (int i = 0; i < playersInfo->m_PlayerCount; i++)
    {
        s3eIOSGameCenterPlayer* player = playersInfo->m_Players[i];
        // NB: GetString only guarantees the string will stay valid until the next call to GetString
        // so we have to copy it if calling more than once and expecting it to persist
        char alias[S3E_IOSGAMECENTER_STRING_MAX];
        strcpy(alias, s3eIOSGameCenterPlayerGetString(player, S3E_IOSGAMECENTER_PLAYER_ALIAS));
//        printPlayerInfo(alias,
//                        s3eIOSGameCenterPlayerGetString(player, S3E_IOSGAMECENTER_PLAYER_ID),
//                        false, 0,
//                        s3eIOSGameCenterPlayerGetInt(player, S3E_IOSGAMECENTER_PLAYER_IS_FRIEND) == 1);
    }
    s3eIOSGameCenterReleasePlayers(playersInfo->m_Players, playersInfo->m_PlayerCount);
}
*/

//---------------------
void cGameCenter::AuthenticationCallback(s3eIOSGameCenterError* error, void* userData)
{
    gGameCenterState++;

 if (*error != S3E_IOSGAMECENTER_ERR_NONE)
    {
//        AppendMessageColour(RED, "Authentication Error: %s", ErrorAsString(*error));
//  debugBGColor = 0x0000ff00; // red
//        updateBGColor();
  gAuthenticated = false;
        return;
    }
    gAuthenticated = true;
    const char* alias = s3eIOSGameCenterGetString(S3E_IOSGAMECENTER_LOCAL_PLAYER_ALIAS);
// debugBGColor = 0x00ff0000; // green
// updateBGColor();
    if (strlen(alias) > 0)
        strcpy(gUserName, alias);
    else
        strcpy(gUserName, "!user alias error!");
//    AppendMessageColour(BLUE, "---------------------------------------------------");
//    printPlayerInfo(g_UserName,
//                    s3eIOSGameCenterGetString(S3E_IOSGAMECENTER_LOCAL_PLAYER_ID),
//                    true, s3eIOSGameCenterGetInt(S3E_IOSGAMECENTER_LOCAL_PLAYER_IS_UNDERAGE));
//    AppendMessageColour(GREEN, "Local player authenticated:");
//    AppendMessageColour(BLUE, "---------------------------------------------------");
//    s3eIOSGameCenterSetInviteHandler(InviteCallback);
}
// Authenticate local player - prerequisite for other funcs to be used
void cGameCenter::AuthenticateLocalHandler()
{
    // Start new attempt
//  AppendMessageColour(BLUE, "%suthenticating local player...", g_Authenticated ? "Re-a" : "A");
// debugBGColor = 0xff000000; // green
// updateBGColor();
 gGameCenterState++;
 if (!gAuthenticated)
        gAuthenticated = s3eIOSGameCenterGetInt(S3E_IOSGAMECENTER_LOCAL_PLAYER_IS_AUTHENTICATED) ? true : false;
    if (gAuthenticated)
    {
//        AppendMessageColour(GREEN, "Authenticated! (player already logged-in)");
//  debugBGColor = 0x00ff0000; // green
//  updateBGColor();
  return;
    }
    s3eIOSGameCenterAuthenticate(AuthenticationCallback, NULL);
}
void cGameCenter::GameCenterRegister()
{
 gGameCenterState = 0;
 if (!s3eIOSGameCenterAvailable())
    {
  debugBGColor = 0x0000ff00; // red
  updateBGColor();
        s3eDebugErrorShow(S3E_MESSAGE_CONTINUE, "GameCenter extension not found");
        s3eDeviceExit(1);
    }
 AuthenticateLocalHandler();
}
//---------------------
// Achievements
void cGameCenter::ReportAchievementCallback(s3eIOSGameCenterError* error)
{
//    if (*error != S3E_IOSGAMECENTER_ERR_NONE)
//        AppendMessageColour(RED, "Achievement Report Failed: %s", ErrorAsString(*error));
//    else
//        AppendMessageColour(GREEN, "Achievement Reported Successfully");
}
void cGameCenter::LoadAchievementsCallback(s3eIOSGameCenterAchievementList* list)
{
    for (int i = 0; i < list->m_AchievementCount; i++)
    {
        s3eIOSGameCenterAchievement* info = &list->m_Achievements[i];
//        AppendMessage(" %s : %d", info->m_Identifier, info->m_PercentComplete);
    }
//    AppendMessage("Got %d achievements", list->m_AchievementCount);
//    AppendMessageColour(BLUE, "---------------------------------------------------");
}
void cGameCenter::LoadAchievementInfoCallback(s3eIOSGameCenterAchievementInfoList* list)
{
    for (int i = 0; i < list->m_AchievementCount; i++)
    {
        s3eIOSGameCenterAchievementInfo* info = &list->m_Achievements[i];
//        AppendMessage("  desc2: %s", info->m_UnachievedDescription);
//        AppendMessage("   desc: %s", info->m_AchievedDescription);
//        AppendMessage(" %s : %d : %s", info->m_Identifier, info->m_MaxPoints,info->m_Title);
    }
//    AppendMessage("Got %d possible achievements", list->m_AchievementCount);
//    AppendMessageColour(BLUE, "---------------------------------------------------");
}
void cGameCenter::GetMyAchievementsHandler()
{
    s3eIOSGameCenterLoadAchievements(LoadAchievementsCallback);
}
void cGameCenter::GetAchievementsHandler()
{
    s3eIOSGameCenterLoadAchievementInfo(LoadAchievementInfoCallback);
}
void cGameCenter::ReportAchievementHandler()
{
    char name[255];
    const char* tmp = s3eOSReadStringUTF8("Enter achievement name:");
    strcpy(name, tmp);
    const char* valueStr = s3eOSReadStringUTF8("Percent Complete:", S3E_OSREADSTRING_FLAG_NUMBER);
    int value = atoi(valueStr);
    s3eIOSGameCenterReportAchievement(name, value, ReportAchievementCallback);
}
void cGameCenter::GameCenter_DisplayAchievements()
{
 if (s3eIOSGameCenterAchievementsShowGUI() == S3E_RESULT_ERROR)
  return; // error'd out
}
//------------------
// Leaderboards
void cGameCenter::GameCenter_ReportScoreCallback(s3eIOSGameCenterError* error)
{
    if (*error != S3E_IOSGAMECENTER_ERR_NONE) {
  // all bad
  if (*error == S3E_IOSGAMECENTER_ERR_COMMUNICATIONS_FAILURE)
   return;
 } else {
  // all good
 }
}
void cGameCenter::GameCenter_ReportScore(int _score, int _level)
{
// const char* scoreStr = s3eOSReadStringUTF8("What Score?", S3E_OSREADSTRING_FLAG_NUMBER);
// int score = atoi(scoreStr);
 char category[20]; //s3eOSReadStringUTF8("Category (SI_LB01)");
 sprintf(category, "SI_LB%02d", _level);
 s3eIOSGameCenterReportScore(_score, category, GameCenter_ReportScoreCallback);
}
void cGameCenter::GameCenter_DisplayLeaderboard()
{
 const char* category = "SI_LB00"; //s3eOSReadStringUTF8("Category (SI_LB01)"); // enter a name of the LB
 if (s3eIOSGameCenterLeaderboardShowGUI(category, S3E_IOSGAMECENTER_PLAYER_SCOPE_ALL_TIME) == S3E_RESULT_ERROR)
  return;
}

No comments:

Post a Comment