Last active
August 29, 2015 14:21
-
-
Save jonnyijapan/5225f954377a9f768921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Myinternalclass.cpp | |
// MyGame | |
// | |
// Created by Jonny Bergstrom on 20150521. | |
// | |
// | |
#include "Myinternalclass.h" | |
ENCODABLE_OBJECT_DEFINE_CONSTRUCTOR(Myinternalclass); // declare this for CCNSCoder | |
void Myinternalclass::initObject() { | |
//CCLOG("initObject in Myinternalclass"); | |
this->internalinteger1 = 0; | |
} | |
#pragma mark CCNSCoding routine | |
#define CODEKEY_INTERNALINTEGER1 "CODEKEY_INTERNALINTEGER1" | |
void Myinternalclass::encodeWithCoder(CCNSCoder *encoder) { | |
encoder->encodeIntForKey(this->internalinteger1, CODEKEY_INTERNALINTEGER1); | |
} | |
void Myinternalclass::initWithCoder(CCNSCoder *decoder) { | |
this->internalinteger1 = decoder->decodeIntForKey(CODEKEY_INTERNALINTEGER1); | |
} | |
#pragma mark - |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Myinternalclass.h | |
// MyGame | |
// | |
// Created by Jonny Bergstrom on 20150521. | |
// | |
// | |
#ifndef __MyGame__Myinternalclass__ | |
#define __MyGame__Myinternalclass__ | |
#include <stdio.h> | |
#import "CCNSCoder.h" | |
class Myinternalclass : public CCNSEncodableObject { | |
public: | |
ENCODABLE_OBJECT_DECLARE_CONSTRUCTOR(Myinternalclass); | |
int internalinteger1; | |
// CCNSEncodableObject stuff | |
void initWithCoder(CCNSCoder *decoder); | |
void encodeWithCoder(CCNSCoder *coder); | |
protected: | |
virtual void initObject(); | |
}; | |
#endif /* defined(__MyGame__Myinternalclass__) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Helloworldscene::actionTestsave(Ref* pSender, cocos2d::ui::Widget::TouchEventType eEventType) { | |
if (eEventType != ui::Widget::TouchEventType::ENDED) return; | |
// mytestobject should be a member in Helloworldscene looking like: Mytestclass mytestobject; | |
const int BEFORE = this->mytestobject.integer1++; | |
const int INTERNALBEFORE = this->mytestobject.myinternalobject.internalinteger1--; | |
Myinternalclass anotherinternalobject; | |
this->mytestobject.manyobjects.push_back(anotherinternalobject); | |
CCLOG("Increased this->mytestobject->integer1 from %d to %d. Internal integer was %d, now %d. Added another object to the vector whose size now is %lu. Now save.", BEFORE, this->mytestobject.integer1, INTERNALBEFORE, this->mytestobject.myinternalobject.internalinteger1, this->mytestobject.manyobjects.size()); | |
this->mytestobject.save(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Mytestclass.cpp | |
// MyGame | |
// | |
// Created by Jonny Bergstrom on 20150521. | |
// | |
// | |
#include "Mytestclass.h" | |
#define MYSAVENAME "mytestclass.txt" | |
ENCODABLE_OBJECT_DEFINE_CONSTRUCTOR(Mytestclass); // declare this for CCNSCoder | |
void Mytestclass::initObject() { | |
//CCLOG("initObject in Mytestclass"); | |
this->integer1 = 0; | |
this->myinternalobject = Myinternalclass(); | |
this->manyobjects = std::vector<Myinternalclass>(); | |
} | |
void Mytestclass::save() { | |
bool result = false; | |
CCNSCoder *cd1 = new CCNSCoder(); | |
this->resetSavedState(); | |
if (cd1->saveObjectToFile(MYSAVENAME, this)) { | |
result = true; | |
CCLOG("Save ok"); | |
} | |
else { | |
CCLOGERROR("Save failed"); | |
} | |
delete cd1; | |
} | |
bool Mytestclass::restore() { | |
// Try to load | |
bool result; | |
CCNSCoder *cd2 = new CCNSCoder(); | |
if (cd2->readObjectFromFile(MYSAVENAME, this)) { | |
result = true; | |
} | |
else { | |
result = false; | |
CCLOG("Load failed, or did not exist..."); | |
} | |
delete cd2; | |
return result; | |
} | |
#pragma mark CCNSCoding routine | |
#define CODEKEY_INTEGER1 "CODEKEY_INTEGER1" | |
#define CODEKEY_INTERNALOBJECT "CODEKEY_INTERNALOBJECT" | |
#define CODEKEY_MANYOBJECTS "CODEKEY_MANYOBJECTS" | |
void Mytestclass::encodeWithCoder(CCNSCoder *encoder) { | |
encoder->encodeIntForKey(this->integer1, CODEKEY_INTEGER1); | |
encoder->encodeObjectForKey(&this->myinternalobject, CODEKEY_INTERNALOBJECT); | |
std::vector<CCNSEncodableObject *> vect; | |
for (auto o : this->manyobjects) { | |
vect.push_back(&o); | |
} | |
encoder->encodeVectorOfObjectsForKey(&vect, CODEKEY_MANYOBJECTS); | |
} | |
void Mytestclass::initWithCoder(CCNSCoder *decoder) { | |
this->integer1 = decoder->decodeIntForKey(CODEKEY_INTEGER1); | |
CCNSEncodableObject* obj = decoder->decodeObjectForKey(CODEKEY_INTERNALOBJECT); | |
this->myinternalobject = *((Myinternalclass*)obj); | |
std::vector<CCNSEncodableObject *> vect = *decoder->decodeVectorOfObjectsForKey(CODEKEY_MANYOBJECTS); | |
for (auto o : vect) { | |
Myinternalclass* o2 = (Myinternalclass*)o; | |
this->manyobjects.push_back(*o2); | |
} | |
} | |
#pragma mark - | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Mytestclass.h | |
// MyGame | |
// | |
// Created by Jonny Bergstrom on 20150521. | |
// | |
// | |
#ifndef __MyGame__Mytestclass__ | |
#define __MyGame__Mytestclass__ | |
#include <stdio.h> | |
#import "CCNSCoder.h" | |
#import "Myinternalclass.h" | |
class Mytestclass : public CCNSEncodableObject { | |
public: | |
ENCODABLE_OBJECT_DECLARE_CONSTRUCTOR(Mytestclass); | |
int integer1; | |
Myinternalclass myinternalobject; | |
std::vector<Myinternalclass> manyobjects; | |
void save(); | |
bool restore(); | |
// CCNSEncodableObject stuff | |
void initWithCoder(CCNSCoder *decoder); | |
void encodeWithCoder(CCNSCoder *coder); | |
protected: | |
virtual void initObject(); | |
}; | |
#endif /* defined(__MyGame__Mytestclass__) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment