Hatena::Groupigyuque

うし

2009-01-11

アプリケーションの終了

03:29

http://blog.coordinates.jp/2008/11/iphone-app-1.html

キーワード: sharedApplication terminate

ゲームソフトだと「Quit」とかつけたくなるけど、iPhoneの作法的にはいらないか。

2009-01-03

OpenGLESで画面クリアだけするプログラム

17:55

参考: http://d.hatena.ne.jp/nakamura001/20081215/1229318841

main.m

#import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
    [pool release];
    return retVal;
}

AppController.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <OpenGLES/ES1/gl.h>
#import "GLSceneView.h"

@interface AppController : NSObject
{
	UIWindow* window;
	GLSceneView* sceneView;
}

@end

AppController.m

#import "AppController.h"

@implementation AppController

/* override */
- (void)applicationDidFinishLaunching:(UIApplication*)application
{
	CGRect rect = [[UIScreen mainScreen] bounds];
	window = [[UIWindow alloc] initWithFrame:rect];

	sceneView = [[GLSceneView alloc] initWithFrame:rect];
	[window addSubview:sceneView];	
	
	[window makeKeyAndVisible];
}

/* override */
- (void)dealloc
{
	[sceneView release];
	[window release];
	[super dealloc];
}

@end

GLSceneView.h

#import <UIKit/UIKit.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <QuartzCore/QuartzCore.h>

@interface GLSceneView : UIView {
	EAGLContext* context;

	GLuint txnBackBuffer, txnFrameBuffer;
}

- (void)updateView;

@end

GLSceneView.m

#import "GLSceneView.h"

/* private methods */
@interface GLSceneView (private)
- (id)initGLES;
- (BOOL)createFramebuffer;
- (void)destroyFramebuffer;
- (void)renderScene;
@end


@implementation GLSceneView

/* override */
+ (Class) layerClass
{
	return [CAEAGLLayer class];
}

/* override */
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self = [self initGLES];
    }

	if (self)
		[self updateView];
    return self;
}

- (id)initGLES
{
	CAEAGLLayer* eaglLayer = (CAEAGLLayer*)self.layer;

	eaglLayer.opaque = TRUE;
	eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
									[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
									kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
									nil];

	// setup context
	context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
	if(!context || ![EAGLContext setCurrentContext:context] || ![self createFramebuffer])
	{
		[self release];
		return nil;
	}

	return self;
}

- (BOOL)createFramebuffer
{
	glGenFramebuffersOES(1, &txnFrameBuffer);
	glGenRenderbuffersOES(1, &txnBackBuffer);

	glBindFramebufferOES(GL_FRAMEBUFFER_OES, txnFrameBuffer);
	glBindRenderbufferOES(GL_RENDERBUFFER_OES, txnBackBuffer);

	[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
	glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, txnBackBuffer);

	if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
	{
		NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
		return FALSE;
	}
	
	return TRUE;
}

- (void)destroyFramebuffer
{
	glDeleteFramebuffersOES(1, &txnFrameBuffer);
	glDeleteRenderbuffersOES(1, &txnBackBuffer);

	txnFrameBuffer = 0;
	txnBackBuffer = 0;
}

- (void)renderScene
{

	glClearColor(0, 0, 0.5, 1);
	glClear(GL_COLOR_BUFFER_BIT);
}

/* override */
- (void)layoutSubviews
{
	[EAGLContext setCurrentContext:context];
	[self destroyFramebuffer];
	[self createFramebuffer];
}

- (void)updateView
{
	[EAGLContext setCurrentContext:context];
	glBindFramebufferOES(GL_FRAMEBUFFER_OES,   txnFrameBuffer);
	glBindRenderbufferOES(GL_RENDERBUFFER_OES, txnBackBuffer);

	[self renderScene];
	
	[context presentRenderbuffer:GL_RENDERBUFFER_OES];
	
	GLenum err = glGetError();
	if(err) NSLog(@"%x error", err);
}

/* override */
- (void)drawRect:(CGRect)rect
{
	[self updateView];
}

/* override */
- (void)dealloc
{
	if([EAGLContext currentContext] == context)
		[EAGLContext setCurrentContext:nil];
	
	[context release];
	context = nil;	
	
	[super dealloc];
}


@end

layerClass のオーバーライドが一番のポイントか

いきなり終了しないプログラム

11:19

main.m

#import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
    [pool release];
    return retVal;
}

AppController.h

#import <Foundation/Foundation.h>

@interface AppController : NSObject
{

}

@end

AppController.m

#import "AppController.h"

@implementation AppController

@end

UIKit.framework と Foundation.frameworkを追加。実行するとメッセージループに入る。

2009-01-02

iPhoneプログラム(最小)

03:04

int main(int argc, char *argv[]) {
	return 0;
}

ウィザードが吐いた奴消してここから始める