2009年03月06日
【iPhone】【プログラム】ゲームループと文字表示
iPhoneのプログラムを最近始めました。
正直、XCode自体の使い方もわからないし、
サンプルの「Hello World」を見てもよくわからない!
というか、そもそも、新規のプロジェクトから作成する方法ってのが
これまたよくわからないwww
いろいろなページを検索しまくって、
なんとなくわかった気になりはじめたわけですがー
ゲーム用のメインループを作成して、
文字列を表示して、文字列のα値を変更するサンプルを作ってみたので
その手順とか晒してみる!
これであってるのかなぁ・・・?
正直、XCode自体の使い方もわからないし、
サンプルの「Hello World」を見てもよくわからない!
というか、そもそも、新規のプロジェクトから作成する方法ってのが
これまたよくわからないwww
いろいろなページを検索しまくって、
なんとなくわかった気になりはじめたわけですがー
ゲーム用のメインループを作成して、
文字列を表示して、文字列のα値を変更するサンプルを作ってみたので
その手順とか晒してみる!
これであってるのかなぁ・・・?
1.XCodeで新規プロジェクト

「Window-Based Application」を選択
2.プロジェクト名を設定

「000GameLoop」としました。
3.作られてすぐの状態

4.Classesを右クリックから

「追加」→「新規ファイル」を選択
5.Cocoa Touch Classes から

「UIView subclass」を選択
6.ファイル名を「Main.mm」にする

そうすると、
「Main.h」
「Main.mm」
という二つのファイルが作成される。
7.それぞれのソースをコーディングする
●_00GameLoopAppDelegate.h
#import <UIKit/UIKit.h> #import "Main.h" @interface _00GameLoopAppDelegate : NSObject●_00GameLoopAppDelegate.m{ UIWindow *window; // メインクラス Main *m_Main; } @property (nonatomic, retain) IBOutlet UIWindow *window; @end
#import "_00GameLoopAppDelegate.h"
@implementation _00GameLoopAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// 初期化
CGRect scrRect = [[UIScreen mainScreen] applicationFrame];
self.window = [[UIWindow alloc] initWithFrame: scrRect];
// メインクラスを初期化して関連付け
CGRect winRect = scrRect;
winRect.origin.y = 0.0;
m_Main = [[Main alloc] initWithFrame: winRect];
[window addSubview:m_Main];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[m_Main release];
[window release];
[super dealloc];
}
@end
●Main.h
#import <UIKit/UIKit.h>
// 秒間のフレームレート
#define FPS (30.0f)
@interface Main : UIView {
// FPS用のタイマー
NSTimer *m_FPSTimer;
// カウンター
int m_count;
// 文字列のα値
int alpha;
int alphaAdd;
}
@end
/* ―――――――――――――――――――――――――――
描画色の設定
―――――――――――――――――――――――――――*/
void setColor( int r, int g, int b, int a ){
[[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a/255.0] set];
}
/* ―――――――――――――――――――――――――――
文字描画処理
―――――――――――――――――――――――――――*/
#define _FONT_SIZE_ (12)
void drawString( char * str , int x, int y ) {
NSString *string = [[NSString alloc] initWithUTF8String:str];
[string drawAtPoint:CGPointMake(x, y) withFont:[UIFont systemFontOfSize:_FONT_SIZE_]];
[string release];
}
●Main.mm
#import "Main.h"
@implementation Main
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// フレームレートを設定
m_FPSTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / FPS ) target:self selector:@selector(update) userInfo:nil repeats:YES];
// カウンター
m_count = 0;
// 文字列のα値
alpha = 0;
alphaAdd = 8;
}
return self;
}
- (void)dealloc {
[m_FPSTimer release];
[super dealloc];
}
/* ―――――――――――――――――――――――――――
描画処理
―――――――――――――――――――――――――――*/
- (void)drawRect:(CGRect)rect {
// タイトル表示
setColor( 255, 255, 255, 255 );
drawString( "000GameLoop " , 5, 5 );
// 文字列を構築
char str[256];
sprintf( str, "カウンター:%03d", m_count);
// 描画色設定
// r(0〜255)
// g(0〜255)
// b(0〜255)
// a(0〜255)
setColor( 255, 255, 255, alpha );
// 文字列描画
// char*, int x, int y
drawString( str , 12, 24 );
}
/* ―――――――――――――――――――――――――――
フレーム処理
―――――――――――――――――――――――――――*/
- (void)update
{
// α値を変更
alpha += alphaAdd;
if( alpha >= 255 ){
alphaAdd=-alphaAdd;
alpha = 255;
}
if( alpha <= 0 ){
alphaAdd=-alphaAdd;
alpha = 0;
}
// カウンター
m_count++;
// 再描画を通知
[self setNeedsDisplay];
}
@end
8.シュミュレーターでの実行結果

とりあえず、ここまで!











