Category: ‘C++’

C++のString型

2013年11月16日 Posted by PURGE

Java では、普通に文字列を扱う場合は、String型があるのでそのまま宣言すれば良い。
C++ では、良くわからなかったので下記に覚え書き。

    char aName = "太郎";       //←これはダメ。
    char bName[10] = "太郎";  //←コンパイルはOKだがイマイチ
    char *pName = "太郎";     //←コンパイルはOKだが非推奨
    char cName[] = "太郎";  //←推奨

そもそもの理解がないのであるが、アドレスは普通の変数は前に & を付けるが、配列変数と関数は名前だけで取得できるということ。
これはとても重要で、ポインタ変数 *p = &hoge; のようにだけ覚えていると後々混乱する。

実際のプログラムで、& を余り見かけないからである。
ポインタを使って間接的に変数を扱うことを参照と言う。
参照を理解するのはここが重要である。

C++事始めの覚え書き

2013年11月10日 Posted by PURGE

main.cpp

#include <iostream>
#include "Animal.h"

int main(int argc, const char * argv[])
{
    char name[20] = "ポチ";
    Animal animal(name);
    Animal *dog = new Animal(name);
    // insert code here...
    std::cout << "Goody, " << animal.getName() << std::endl;
    std::cout << "Hello, " << dog->getName() << std::endl;
    
    //new したオブジェクトはdeleteしないとDestractorが呼ばれない
    delete dog;
    return 0;
}

Animal.h

#ifndef __CPlusSample__Animal__
#define __CPlusSample__Animal__

#include <iostream>

class Animal{
public:
    Animal(char *name);
    ~Animal();
    char *getName();
private:
    char name[20];
};

#endif /* defined(__CPlusSample__Animal__) */

Animal.cpp

#include "Animal.h"

//コンストラクタ
Animal::Animal(char* _name){
    strcpy(name, _name);
}
//デストラクタ
Animal::~Animal(){
    std::cout << "Destractor!!" << std::endl;
}

char *Animal::getName(){
    return name;
}

Cocos2d-x 背景画像座標設定の覚え書き

2013年10月20日 Posted by PURGE

典型的な関数に関する覚え書き。

    if ( !CCLayer::init() )
    {
        return false;
    }
    //実際の画面に表示されている領域(width/height)
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    //基本的に0,0(x/y)
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    CCSprite* pBackground = CCSprite::create("Background.png");
    pBackground->setPosition(ccp(origin.x + visibleSize.width/2,
                                 origin.y + visibleSize.height/2));
    
    CCLog("origin.x:%f / orgin.y:%f",origin.x, origin.y);
    CCLog("visibleSize.width:%f / visibleSize.height:%f",visibleSize.width, visibleSize.height);
    CCLog("background.width:%f / background.height:%f",pBackground->getContentSize().width, pBackground->getContentSize().height);
    
    this->addChild(pBackground, 0);

実際試すと、座標設定が理解できるようになる。

Cocos2d-x 縦画面の設定

2013年10月17日 Posted by PURGE

RootViewController.mm

// Override to allow orientations other than the default portrait orientation.
// This method is deprecated on ios6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //縦画面対応
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
    //return UIInterfaceOrientationIsLandscape( interfaceOrientation );
}

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
    //縦画面対応
    return UIInterfaceOrientationMaskPortrait;
    //return UIInterfaceOrientationMaskAllButUpsideDown;
#endif
}

- (BOOL) shouldAutorotate {
    return NO;
}

Cocos2d-xの画面遷移方法

2013年10月16日 Posted by PURGE

一番簡単な、画面遷移(トランジション付き)の方法の覚え書き。

bool ScreenSample::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
    CCMenuItemImage* pNextScreen = CCMenuItemImage::create("Next.png",
                                                           "NextSelected.png",
                                                           this, menu_selector(ScreenSample::nextScreenClicked));
	pNextScreen->setPosition(ccp(origin.x + visibleSize.width - pNextScreen->getContentSize().width/2 ,
                                origin.y + 100));
    
    CCMenu* pNext = CCMenu::create(pNextScreen, NULL);
    pNext->setPosition(CCPointZero);
    this->addChild(pNext, 3);
    
    return true;
}

void ScreenSample::nextScreenBtnClicked(CCObject* pSender)
{
    CCLog("Clicked!!!");
    CCScene* nextScene = NextScreen::scene();
    CCTransitionFlipX* transition = CCTransitionFlipX::create(0.5f, nextScene);
    //CCTransitionFade* transition = CCTransitionFade::create(0.5f, nextScene);
    //CCTransitionZoomFlipY* transition = CCTransitionZoomFlipY::create(0.5f, nextScene);

    CCDirector::sharedDirector()->replaceScene(transition);
}

今後、活用する。

cocos2d-x 事始め

2013年7月18日 Posted by PURGE

はじめて知ったこととして、cocos2d-xプロジェクトを新規作成するには、下記コマンドラインから、pythonコマンドで作成するのも良いらしい。

$ cd /Works/Xcode/cocos2d-x/tools/project-creator 
$ ./create_project.py -project control -package com.whoocus.control -language cpp 
proj.ios		: Done!
proj.android		: Done!
proj.win32		: Done!
proj.mac		: Done!
proj.blackberry		: Done!
proj.linux		: Done!
proj.marmalade		: Done!
New project has been created in this path: /Works/Xcode/cocos2d-x/projects/control
Have Fun!

C++事始め

2013年7月13日 Posted by PURGE

Cocos2d-xを勉強することとなり、今更 C++ の基礎勉強。
面倒なところは、ファイル構成がJavaと異なるし、ヘッダファイルとかあるし、インクルード等がよくわからない。

そこで、最低限のプログラム。

今まで理解しきれなかったポインタの使用方法も整理できた。
わかりやすいかも。

main.cpp

#include <iostream>
#include <string>
#include "Animal.h"
using namespace std;

int main(int argc, const char * argv[])
{

    string name = "Whoocus";
    cout << "こんにちは、" << name << "さん。\n";

    Animal dog("Pochi");
    dog.eat();     //クラスからメソッドを呼ぶ
    Animal cat("Tama");
    cat.eat();     //クラスからメソッドを呼ぶ

    //ポインタ
    Animal *pDog = &dog;
    (*pDog).eat(); //ポインタからメソッドを呼ぶ場合は .
    pDog->eat();   //ポインタからメソッドを呼ぶ場合は ->

    Animal *pCat = &cat;
    (*pCat).eat(); //ポインタからメソッドを呼ぶ場合は .
    pCat->eat();   //ポインタからメソッドを呼ぶ場合は ->

    //Newを使った場合
    Animal *pMonkey = new Animal("Etekichi");
    pMonkey->eat();
    delete pMonkey;
    //オブジェクトはポインタでしか受けられないので下記はエラーとなる
    //Animal monkey = new Animal("Etekichi");

    return 0;
}

Animal.h

#ifndef __CmdTest__Animal__
#define __CmdTest__Animal__

#include <iostream>
using namespace std;

class Animal
{
private:
    string name;
public:
    Animal(string s);
    void eat();
    ~Animal();
};

#endif /* defined(__CmdTest__Animal__) */

Animal.cpp

#include "Animal.h"
using namespace std;

Animal::Animal(string s)
{
    name = s;
}
void Animal::eat()
{
    cout << name << " is eating.\n";
}
Animal::~Animal()
{
    cout << name << " is dead.\n";    
}