欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

工具库:UtilTools

本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接:
http://www.firedragonpzy.com.cn/index.php/archives/3458

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
【加群请写:Software MyZone或者是firedragonpzy】
群论坛:火龙论坛正试运营阶段,欢迎大家多提些建设性意见……

这里主要是处理游戏中音乐音效的处理和CCUserDefault的加密,先提供工具类与大家分享,不明白的可以参考站点文章,之前挨个分析过。贡献出来,大家直接copy用就好了


//

//  UtilTools.h

//  ToyGame

//

//  Created by firedragonpzy on 13-1-4.

//

//

#ifndef ToyGame_UtilTools_h

#define ToyGame_UtilTools_h

#include "cocos2d.h"

#include "SimpleAudioEngine.h"

#include "Rconfig.h"

#include "DataBase64.h"

using namespace CocosDenshion;

using namespace cocos2d;

class UtilTools

{

public:

static bool musicState;

static bool isMusicOpen;

static void playMusicAndEffect( const char* pszFilePath, bool bLoop )

{

setIntegerForKey(XML_MUSIC_STATUS, FDTRUE);

SimpleAudioEngine::sharedEngine()->playBackgroundMusic(pszFilePath, bLoop);

setIntegerForKey(XML_EFFECT_STATUS, FDTRUE);

UtilTools::musicState = true;

}

static void stopMusicAndEffect( const char* pszFilePath )

{

setIntegerForKey(XML_MUSIC_STATUS, FDFALSE);

SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(MUSIC_BG);

setIntegerForKey(XML_EFFECT_STATUS, FDFALSE);

UtilTools::musicState = false;

}

static void pauseMusicAndEffect()

{

setIntegerForKey(XML_MUSIC_STATUS, FDFALSE);

SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();

setIntegerForKey(XML_EFFECT_STATUS, FDFALSE);

SimpleAudioEngine::sharedEngine()->pauseAllEffects();

UtilTools::musicState = false;

}

static void resumeMusicAndEffect( const char* pszFilePath, bool bLoop )

{

if( UtilTools::isMusicOpen )

{

setIntegerForKey(XML_MUSIC_STATUS, FDTRUE);

setIntegerForKey(XML_EFFECT_STATUS, FDTRUE);

SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

SimpleAudioEngine::sharedEngine()->resumeAllEffects();

}else

{

playMusicAndEffect( pszFilePath, bLoop );

UtilTools::isMusicOpen = true;

}

UtilTools::musicState = true;

}

static void playEffect( const char* pszFilePath )

{

if (getIntegerForKey(XML_EFFECT_STATUS)) {

SimpleAudioEngine::sharedEngine()->playEffect( std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pszFilePath)).c_str() );

}

}

static float getFloatForKey(const char* pKey)
{
std::string key = pKey;
key = SaveData(reinterpret_cast<const unsigned char*>(key.c_str()), key.length());
std::string data = CCUserDefault::sharedUserDefault()->getStringForKey(pKey);
data = ParseData(data);
CCString *str = CCString::create(data);
return str->floatValue();
}

static void setFloatForKey(const char* pKey, float value)
{
char buffer[50];
sprintf(buffer,"%f",value);
std::string data = buffer;
std::string key = pKey;
data = SaveData(reinterpret_cast<const unsigned char*>(data.c_str()), data.length());
key = SaveData(reinterpret_cast<const unsigned char*>(key.c_str()),key.length());
CCUserDefault::sharedUserDefault()->setStringForKey(pKey, data);
CCUserDefault::sharedUserDefault()->flush();
}


static int getIntegerForKey(const char* pKey)
{
std::string key = pKey;
key = SaveData(reinterpret_cast<const unsigned char*>(key.c_str()), key.length());
std::string data = CCUserDefault::sharedUserDefault()->getStringForKey(pKey);
data = ParseData(data);
CCString *str = CCString::create(data);
return str->intValue();
}

static void setIntegerForKey(const char* pKey, int value)
{
char buffer[50];
sprintf(buffer,"%d",value);
std::string data = buffer;
std::string key = pKey;
data = SaveData(reinterpret_cast<const unsigned char*>(data.c_str()), data.length());
key = SaveData(reinterpret_cast<const unsigned char*>(key.c_str()),key.length());
CCUserDefault::sharedUserDefault()->setStringForKey(pKey, data);
CCUserDefault::sharedUserDefault()->flush();
}

};

#endif

DataBase64文件,我看后来引擎版本提供了【cocos2dx/support/base64.cpp】,大家可以研究下引擎里面的,贴出我使用的文件:
DataBase64.h

//

//  DataBase64.h

//  CKCastle

//

//  Created by firedragonpzy on 13-1-28.

//

//

#ifndef DATABASE64_H_

#define DATABASE64_H_

#include <string>

std::string SaveData(unsigned char const* , unsigned int len);

std::string ParseData(std::string const& s);

#endif /* DATABASE64_H_ */

DataBase64.cpp


#include "DataBase64.h"

static const std::string dataChars =

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

"abcdefghijklmnopqrstuvwxyz"

"0123456789+/";

static inline bool baseData(unsigned char c) {

return (isalnum(c) || (c == '+') || (c == '/'));

}

std::string SaveData(unsigned char const* bytes_to_encode, unsigned int in_len) {

std::string ret;

int i = 0;

int j = 0;

unsigned char char_array_3[3];

unsigned char char_array_4[4];

while (in_len--) {

char_array_3[i++] = *(bytes_to_encode++);

if (i == 3) {

char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;

char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);

char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);

char_array_4[3] = char_array_3[2] & 0x3f;

for(i = 0; (i <4) ; i++)

ret += dataChars[char_array_4[i]];

i = 0;

}

}

if (i)

{

for(j = i; j < 3; j++)

char_array_3[j] = '\0';

char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;

char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);

char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);

char_array_4[3] = char_array_3[2] & 0x3f;

for (j = 0; (j < i + 1); j++)

ret += dataChars[char_array_4[j]];

while((i++ < 3))

ret += '=';

}

return ret;

}

std::string ParseData(std::string const& encoded_string) {

int in_len = encoded_string.size();

int i = 0;

int j = 0;

int in_ = 0;

unsigned char char_array_4[4], char_array_3[3];

std::string ret;

while (in_len-- && ( encoded_string[in_] != '=') && baseData(encoded_string[in_])) {

char_array_4[i++] = encoded_string[in_]; in_++;

if (i ==4) {

for (i = 0; i <4; i++)

char_array_4[i] = dataChars.find(char_array_4[i]);

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);

char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);

char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

for (i = 0; (i < 3); i++)

ret += char_array_3[i];

i = 0;

}

}

if (i) {

for (j = i; j <4; j++)

char_array_4[j] = 0;

for (j = 0; j <4; j++)

char_array_4[j] = dataChars.find(char_array_4[j]);

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);

char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);

char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

for (j = 0; (j < i - 1); j++) ret += char_array_3[j];

}

return ret;

}