2013년 11월 29일 금요일

TTF 폰트 추가방법 - ios


1. 사용하고자 하는 .ttf 파일을 프로젝트의 Resources디렉토리에 추가한다.

2. 프로젝트에서 다음 이미지에서와 같이 폰트를 추가한다.


TTF font

3. 추가한 폰트를 CCLabelTTF에서 사용한다.

2013년 11월 25일 월요일

Cocos2d-x IOS 프로젝트에서 Sqlite3사용하기

1. 프로젝트에 sqlite3 라이브러리 추가

2. 미리 생성된 .sqlite(또는 .db)파일을 IOS프로젝트의 Resources디렉토리에 위치시킨다.
3. 초기 구동시 DB를 복사한다.

복사방법 : 



#pragma mark -
#pragma mark Initialize DB (Resource 있는 sqlite파일을 Document 복사)
void initializeDB()
{
    NSFileManager* fm  = [NSFileManager defaultManager];
    const char* dbPath = filePath("nih_DB.sqlite");
    
    //[fm removeItemAtPath:[NSString stringWithUTF8String:dbPath] error:NULL];
    
    BOOL fileExists    = [fm fileExistsAtPath:[NSString stringWithUTF8String:dbPath]];
    
    if(!fileExists)
    {
        // 참조 데이터베이스 파일 경로
        NSString* origin = [[NSBundle mainBundle]pathForResource:@"nih_DB" ofType:@"sqlite"];
        
        // Copy DB
        BOOL      copyDB = [fm copyItemAtPath:origin toPath:[NSString stringWithUTF8String:dbPath] error:NULL];
        if(!copyDB)
        {
            printf("Copy failed!");
        }
    }
}

#pragma mark -
#pragma mark File Path
const char* filePath(char* _name)
{
    NSString *name      = [NSString stringWithUTF8String:_name];
    NSArray *paths      = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path      = [paths objectAtIndex:0];
    NSString *_filePath = [path stringByAppendingPathComponent:name];
    
    return [_filePath UTF8String];
}

Cocos2d-x Android 프로젝트에서 Sqlite3 사용하기

미리 생성된 DB를 사용하는 방법

1. external/sqlite3/Android.mk파일에 다음과 같은 내용을 추가한다.


LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sqlite3_static
LOCAL_MODULE_FILENAME := libsqlite3
LOCAL_SRC_FILES := sqlite3.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CFLAGS := -fno-fast-math 
include $(BUILD_STATIC_LIBRARY)

2. jni/Android.mk를 다음과 같이 수정한다.
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES += sqlite3_static // 추가한 코드

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,CocosDenshion/android)
$(call import-module,extensions)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)
$(call import-module,external/sqlite3) // 추가한 코드

3. 생성된 .sqlite(또는 .db) 파일을 프로젝트의 Resources 디렉토리에 위치시킨다.
4. 어플을 초기 구동시 DB를 복사한다.


복사방법:
  1.  
  2.  
  3. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  4. #include "DbManager.h"
  5.  
  6. bool isFileExist(const char* fileName);
  7. void copyDatabase(const char* fileName);
  8.  
  9. void DbManager::initializeDB()
  10. {
  11. // 프로젝트에 데이터베이스 파일이 존재하지 않으면 복사
  12. if (isFileExist("Fortune.sqlite") == false)
  13. {
  14. copyDatabase("Fortune.sqlite");
  15. }
  16. }
  17.  
  18. #pragma mark -
  19. #pragma mark 데이터베이스 파일 존재여부 확인
  20. bool isFileExist(const char* fileName)
  21. {
  22. if (!fileName)
  23. {
  24. return false;
  25. }
  26. std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath();
  27. filePath += fileName;
  28. FILE* file = fopen(filePath.c_str(), "r");
  29. CCLog("%s", filePath.c_str());
  30. if (file)
  31. {
  32. fclose(file);
  33. return true;
  34. }
  35. return false;
  36. }
  37.  
  38. #pragma mark -
  39. #pragma mark copy database file
  40. void copyDatabase(const char* fileName)
  41. {
  42. std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
  43. unsigned long len = 0;
  44. unsigned char* data = NULL;
  45. data = CCFileUtils::sharedFileUtils()->getFileData(path.c_str(), "r", &len);
  46. std::string destPath = CCFileUtils::sharedFileUtils()->getWritablePath();
  47. destPath += fileName;
  48. FILE* file = fopen(destPath.c_str(), "w+");
  49. fwrite(data, sizeof(destPath.c_str()), len, file);
  50. fclose(file);
  51. delete[] data;
  52. data = NULL;
  53. }
  54. #endif
  55.