void MainLoop() { // Main loop double currentTime = MyTime_GetSystemTime(); float deltaTime = (float)(currentTime - g_LastTime); g_LastTime = currentTime; PollSDLEvents(); g_pGameCore->OnDrawFrameStart( 0 ); g_pGameCore->Tick( deltaTime ); g_pGameCore->OnDrawFrame( 0 ); g_pGameCore->OnDrawFrameDone(); }
int main(int argc, char *argv[]) { SDL_Init( SDL_INIT_VIDEO ); SDL_Surface* screen = SDL_SetVideoMode( 768, 432, 32, SDL_OPENGL ); g_pGameCore = MyNew GameEmptyReplaceMe; g_pGameCore->OnSurfaceCreated(); g_pGameCore->OnSurfaceChanged( 0, 0, 768, 432 );//SCREEN_WIDTH, SCREEN_HEIGHT ); g_pGameCore->OneTimeInit(); g_LastTime = MyTime_GetSystemTime(); emscripten_set_main_loop( MainLoop, 60, false ); }
int LeaderboardStorage::FindDuplicateBlockOrReturnNew(int offset, int boardid) { // check for old block containing this index. for( int i=0; i<NumLeaderboardStorageBlocksCached; i++ ) { if( m_Blocks[i].m_InUse == true ) { if( m_Blocks[i].m_Offset == offset && m_Blocks[i].m_BoardID == boardid ) return i; } } // find an empty block. for( int i=0; i<NumLeaderboardStorageBlocksCached; i++ ) { if( m_Blocks[i].m_InUse == false ) { return i; } } // find the oldest block in our list: double oldesttime = MyTime_GetSystemTime(); int oldestblock = 0; for( int i=0; i<NumLeaderboardStorageBlocksCached; i++ ) { if( m_Blocks[i].m_TimeRetrieved < oldesttime ) { oldesttime = m_Blocks[i].m_TimeRetrieved; oldestblock = i; } } return oldestblock; //// shouldn't hit this, but if it does, just use the first block as a failsafe. //return 0; }
void LeaderboardStorage::Tick(double TimePassed) { if( m_pWebRequestObject ) m_pWebRequestObject->Tick(); m_TimeSinceLastRequestSent += (float)TimePassed; if( m_TimeSinceLastRequestSent > m_MaxTimeToWaitToWebResponse ) { m_pWebRequestObject->Reset(); } if( m_pWebRequestObject->DidSomethingGoWrong() ) return; if( m_RequestedBoardID >= 0 && m_pWebRequestObject->IsBusy() == false && m_pWebRequestObject->GetResult() == 0 ) GetScoreBlock(); // invalidate all blocks older than 3 minutes. if( true ) { for( int i=0; i<NumLeaderboardStorageBlocksCached; i++ ) { if( m_Blocks[i].m_InUse ) { double age = MyTime_GetSystemTime() - m_Blocks[i].m_TimeRetrieved; if( age > 3*60 ) m_Blocks[i].m_InUse = false; } } } // if we received anything from the web, parse it and store it. if( m_pWebRequestObject->IsBusy() == false ) { char* result = m_pWebRequestObject->GetResult(); if( result ) { cJSON* root = cJSON_Parse( result ); if( root ) { cJSON* obj; obj = cJSON_GetObjectItem( root, "NumEntries" ); if( obj ) { g_pGame->m_pLeaderboardStorage->m_TotalPlayers[m_RequestedBoardID] = obj->valueint; } // read in "UserOffset", only returned if we are looking for a user. obj = cJSON_GetObjectItem( root, "UserOffset" ); if( obj ) { m_UserIndices[m_RequestedBoardID] = obj->valueint - 1; } int blockoffset = m_RequestedOffset; obj = cJSON_GetObjectItem( root, "BlockOffset" ); if( obj ) { blockoffset = obj->valueint; } int boardid = m_RequestedBoardID; // find a block of names and scores to store this info into. int blocktouse = FindDuplicateBlockOrReturnNew( blockoffset, boardid ); m_Blocks[blocktouse].m_InUse = true; m_Blocks[blocktouse].m_Offset = blockoffset; m_Blocks[blocktouse].m_BoardID = boardid; m_Blocks[blocktouse].m_TimeRetrieved = MyTime_GetSystemTime(); m_Blocks[blocktouse].m_NumEntriesInBlock = 0; cJSON* NameArray = cJSON_GetObjectItem( root, "UserNames" ); if( NameArray ) { m_Blocks[blocktouse].m_NumEntriesInBlock = cJSON_GetArraySize( NameArray ); for( int i=0; i<m_Blocks[blocktouse].m_NumEntriesInBlock; i++ ) { LeaderboardBlockEntry* pPlayer = &m_Blocks[blocktouse].m_Entries[i]; cJSON* temp; temp = cJSON_GetArrayItem( NameArray, i ); if( temp ) strcpy_s( pPlayer->m_Username, 15+1, temp->valuestring ); // if we find our own name, then store it's index. if( _stricmp( temp->valuestring, g_pGame->m_pProfileManager->m_CurrentProfile->m_Username ) == 0 ) m_UserIndices[m_RequestedBoardID] = blockoffset + i; } // if we got names, we should get Scores. cJSON* ScoreArray = cJSON_GetObjectItem( root, "Scores" ); if( ScoreArray ) { int numitems = cJSON_GetArraySize( ScoreArray ); for( int i=0; i<numitems; i++ ) { LeaderboardBlockEntry* pPlayer = &m_Blocks[blocktouse].m_Entries[i]; cJSON* temp; temp = cJSON_GetArrayItem( ScoreArray, i ); if( temp ) pPlayer->m_Score = temp->valueint; } } } cJSON_Delete( root ); } m_RequestedOffset = -1; m_RequestedBoardID = -1; m_pWebRequestObject->ClearResult(); } } }
void GameServiceManager::Tick(const char* customuseragentchunk) { if( m_pWebRequestObject == 0 ) return; m_pWebRequestObject->Tick( customuseragentchunk ); if( char* result = m_pWebRequestObject->GetResult() ) { if( m_LastMessageTypeSent == GameServiceMessageType_SubmitScore ) { cJSON* root = cJSON_Parse( result ); if( root ) { cJSON* obj = cJSON_GetObjectItem( root, "LeaderboardUpdated" ); if( obj ) { LOGInfo( LOGTag, "MyService - Leaderboard Submit succeeded!\n" ); MarkLeaderboardSubmitComplete( true, m_CopyOfEntryBeingSentToMyServer.m_Protocol, m_CopyOfEntryBeingSentToMyServer.m_ID, m_CopyOfEntryBeingSentToMyServer.m_Score ); } else // failed { LOGInfo( LOGTag, "MyService - Leaderboard Submit failed!\n" ); MarkLeaderboardSubmitComplete( false, m_CopyOfEntryBeingSentToMyServer.m_Protocol, m_CopyOfEntryBeingSentToMyServer.m_ID, m_CopyOfEntryBeingSentToMyServer.m_Score ); } cJSON_Delete( root ); } m_pWebRequestObject->ClearResult(); m_LastMessageTypeSent = GameServiceMessageType_None; } if( m_LastMessageTypeSent == GameServiceMessageType_GetPlayerList ) { cJSON* root = cJSON_Parse( result ); if( root ) { cJSON* ips = cJSON_GetObjectItem( root, "IPExternals" ); if( ips ) { int numips = cJSON_GetArraySize( ips ); m_MatchmakingEntries.InactivateAllObjects(); for( int i=0; i<numips; i++ ) { cJSON* ipaddr = cJSON_GetArrayItem( ips, i ); if( ipaddr ) { GameMatchmakingEntry* pEntry = m_MatchmakingEntries.MakeObjectActive(); if( pEntry ) { int ip[4]; int port; sscanf_s( ipaddr->valuestring, "%d.%d.%d.%d:%d", &ip[0], &ip[1], &ip[2], &ip[3], &port ); pEntry->ip = ip[0] | ip[1]<<8 | ip[2]<<16 | ip[3]<<24; pEntry->port = port; } } } } cJSON_Delete( root ); } m_pWebRequestObject->ClearResult(); m_LastMessageTypeSent = GameServiceMessageType_None; m_SearchingForMatch = false; } } if( m_SearchingForMatch ) { if( m_pWebRequestObject->IsBusy() == false ) { m_pWebRequestObject->RequestWebPage( "/exgas/getplayerlist.php?game=%s", g_pGameCore->GetMatchmakingGameName() ); m_LastMessageTypeSent = GameServiceMessageType_GetPlayerList; } } for( unsigned int i=0; i<m_Entries.m_ActiveObjects.Count(); i++ ) { GameServiceEntry* pEntry = m_Entries.m_ActiveObjects[i]; if( pEntry->m_State == GameServiceSubmitState_New ) { if( pEntry->m_Type == GameServiceType_LeaderboardEntry ) { if( pEntry->m_Protocol == GameServiceProtocol_MyServer && m_pWebRequestObject->IsBusy() == false ) { m_CopyOfEntryBeingSentToMyServer = *pEntry; m_pWebRequestObject->RequestWebPage( "/mygameservice/leaderboard_post.php?user=%s&pass=%s&gameid=%d&boardid=%d&score=%d", pEntry->m_Username, pEntry->m_Password, pEntry->m_GameID, pEntry->m_ID, pEntry->m_Score ); m_LastMessageTypeSent = GameServiceMessageType_SubmitScore; pEntry->m_State = GameServiceSubmitState_Started; pEntry->m_TimeSendLastAttempted = MyTime_GetSystemTime(); } #if MYFW_IOS if( pEntry->m_Protocol == GameServiceProtocol_GameCenter && g_GameCenter_LoggedIn == true ) { GameCenter_SubmitScore( pEntry->m_ID, pEntry->m_IDStr, pEntry->m_Score ); pEntry->m_State = GameServiceSubmitState_Started; pEntry->m_TimeSendLastAttempted = MyTime_GetSystemTime(); } #endif #if USE_SCORELOOP if( pEntry->m_Protocol == GameServiceProtocol_ScoreLoop && m_pScoreLoop->m_LoggedIn == true ) { m_pScoreLoop->SubmitScore( pEntry, pEntry->m_ID, pEntry->m_IDStr, pEntry->m_Score ); pEntry->m_State = GameServiceSubmitState_Started; pEntry->m_TimeSendLastAttempted = MyTime_GetSystemTime(); } #endif } else if( pEntry->m_Type == GameServiceType_Achievement ) { #if MYFW_IOS if( pEntry->m_Protocol == GameServiceProtocol_GameCenter && g_GameCenter_LoggedIn == true ) { GameCenter_SubmitAchievement( pEntry->m_ID, pEntry->m_IDStr, pEntry->m_Percentage ); pEntry->m_State = GameServiceSubmitState_Started; pEntry->m_TimeSendLastAttempted = MyTime_GetSystemTime(); } #endif #if USE_SCORELOOP if( pEntry->m_Protocol == GameServiceProtocol_ScoreLoop && m_pScoreLoop->m_LoggedIn == true ) { m_pScoreLoop->SubmitAchievement( pEntry, pEntry->m_ID, pEntry->m_IDStr, pEntry->m_Percentage ); pEntry->m_State = GameServiceSubmitState_Started; pEntry->m_TimeSendLastAttempted = MyTime_GetSystemTime(); } #endif } } else if( pEntry->m_State == GameServiceSubmitState_Started ) { } else if( pEntry->m_State == GameServiceSubmitState_Failed ) { if( MyTime_GetSystemTime() - pEntry->m_TimeSendLastAttempted > 60*10 ) { pEntry->m_State = GameServiceSubmitState_New; // try again. } } else if( pEntry->m_State == GameServiceSubmitState_Done ) { m_Entries.MakeObjectInactiveByIndex( i ); i--; continue; } } //// if our player is online, but not in a game, then update his status once a minute. //if( m_PlayerStatus == GameServicePlayerStatus_Online_Idle || // m_PlayerStatus == GameServicePlayerStatus_Online_Searching ) //{ // if( m_pWebRequestObject->IsBusy() == false ) // { // //&pass=%s // // pButtonPassword->m_Strings[1], // m_pWebRequestObject->RequestWebPage( "/exgas/updateinfo.php?user=%s&game=%s&ipext=%s&ipint=%s", // pButtonUsername->m_Strings[1], // g_pGame->GetMatchmakingGameName(), // "192", // "192" ); // } //} }