[cocos2d] Menu - 사용법
Objective-C & OpenGL ES 2009. 8. 25. 19:09 |Menu *menu = [Menu menuWithItems:image, nil];
image.position = cpv( -135, -185);
[self addChild: menu z:2];
-(void) readAnimalsFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the animals Array
// animals = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([[self dataFilePath] UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
NSString* sqlStatement = [NSString stringWithFormat:@"select * from s1"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int idx = sqlite3_column_int( compiledStatement, 0 );
int level = sqlite3_column_int( compiledStatement, 1 );
int c1 = sqlite3_column_int( compiledStatement, 2 );
int c2 = sqlite3_column_int( compiledStatement, 3 );
int c3 = sqlite3_column_int( compiledStatement, 4 );
// Create a new animal object with the data from the database
// Animal *animal = [[Animal alloc] initWithName:aName description:aDescription url:aImageUrl];
// Add the animal object to the animals Array
// [animals addObject:animal];
// [animal release];
}
}
else
{
NSString* msg = [[NSString alloc] initWithFormat:@"%s", sqlite3_errmsg( database )];
NSLog( @"Failed to open database with message '%s'.", sqlite3_errmsg( database ) );
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
** 연동 방법에 대해서는 따로 적지를 않았습니다.
-(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:[self dataFilePath]];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kFilename];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:[self dataFilePath] error:nil];
[fileManager release];
}
위의 코드를 수행하게 되면 document에 데이터가 없다면 복사를 수행하라는 의미의 코드인듯 합니다.( 사실 이 부분은 아직 잘 모름 )
그래서 위의 함수를 적용 및 호출을 아래와 같이 해주니까.
제대로 데이터가 불러와 지더군요.
- (id) init
{
if (self = [super init])
{
touchPos = CGPointMake(0, 0);
gameState = kLogo;
logoBack = [[LogoBack alloc] init];
// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];
// Query the database for all animal records and construct the "animals" array
[self readAnimalsFromDatabase];
}
return self;
}
위의 내용에 대해서 실제적인 코드는 여기 압축을 해서 올리지 못하지만,
sqlite3의 내용을 조금 숙지 하셨다면 금방 이해 하시리라 믿습니다. ^^
이거 가지고 조금 삽질을 했네요.
다행이 이 코드를 보면서 다른 면을 보게 되서 더 기쁘네요 ^^
#import <Foundation/Foundation.h>
#import "Texture2D.h"
@interface Texture2DEx2 : Texture2D {
CGPoint tileSize;
CGPoint frameSize;
int curIndex;
int Delay;
int Tick;
}
@property CGPoint tileSize;
@property CGPoint frameSize;
@property int curIndex;
@property int Delay;
@property int Tick;
- (void) TileSize:(CGPoint)ts FrameSize:(CGPoint)fs Delay:(int)delay;
- (void) drawAtPointTest:(CGPoint)point;
@end
#import "Texture2DEx2.h"
@implementation Texture2DEx2
@synthesize tileSize;
@synthesize frameSize;
@synthesize curIndex;
@synthesize Delay;
@synthesize Tick;
- (void) TileSize:(CGPoint)ts FrameSize:(CGPoint)fs Delay:(int)delay
{
self.tileSize = ts;
self.frameSize = fs;
self.Delay = delay;
self.Tick = 0;
}
- (void) drawAtPointTest:(CGPoint)point
{
if( Tick > Delay )
{
Tick = 0;
curIndex++;
if( curIndex >= frameSize.x )
{
curIndex = 0;
}
}
Tick++;
GLfloat leftTc = tileSize.x * curIndex / _width;
GLfloat rightTc = tileSize.x * (curIndex+1) / _width;
GLfloat coordinates[] = { leftTc, _maxT,
rightTc, _maxT,
leftTc, 0,
rightTc, 0 };
// 넓이와 높이는 타일의 사이즈로 교체
GLfloat width = tileSize.x,
height = tileSize.y;
GLfloat vertices[] = { -width / 2 + point.x, -height / 2 + point.y, 0.0,
width / 2 + point.x, -height / 2 + point.y, 0.0,
-width / 2 + point.x, height / 2 + point.y, 0.0,
width / 2 + point.x, height / 2 + point.y, 0.0 };
glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@end
Texture2DEx2 *bluePlaneEx;
@property (nonatomic, retain) Texture2DEx2 *bluePlaneEx;
if (bluePlaneEx == nil) { self.bluePlaneEx = [[Texture2DEx2 alloc] initWithImage: [UIImage imageNamed:@"bluePlane2.png"]]; glBindTexture(GL_TEXTURE_2D, sprite.name); // 타일 사이즈, 이미지 내부 프레임 개수, 딜레이 타임(호출타임) [self.bluePlaneEx TileSize:CGPointMake(63.0, 55.0) FrameSize:CGPointMake(3, 1) Delay:10]; } [bluePlaneEx drawAtPointTest:CGPointMake(Pos.x, Pos.y)];
#import <Foundation/Foundation.h>
#import "Texture2D.h"
@interface Texture2D (SpriteAnimation)
- (void) drawAtPointTest:(CGPoint)point;
@end
#import "Texture2DEx.h"
@implementation Texture2D (SpriteAnimation)
- (void) drawAtPointTest:(CGPoint)point
{
_maxS = 63.0 / 256.0; // test
GLfloat coordinates[] = { 0, _maxT,
_maxS, _maxT,
0, 0,
_maxS, 0 };
GLfloat width = (GLfloat)_width * _maxS,
height = (GLfloat)_height * _maxT;
width = 63.0; // test
GLfloat vertices[] = { -width / 2 + point.x, -height / 2 + point.y, 0.0,
width / 2 + point.x, -height / 2 + point.y, 0.0,
-width / 2 + point.x, height / 2 + point.y, 0.0,
width / 2 + point.x, height / 2 + point.y, 0.0 };
glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@end