当前位置:编程学习 > wap >>

iPhone游戏引擎CWGameEngine之一(创建全屏自定义窗口)

作者:孙东风 2009-11-14(请尊重作者劳动成果,转载务必注明出处)

 

笔者在前面的系列文章中依次讲解了iPhone多线程、iPhone数据持久化、iPhone网络通讯BSD Socket等内容,接下来笔者会讲解如何从头搭建一个自己的游戏引擎。

 

根据iPhone官方的统计,App Store中游戏类应用是最多的,大概是其它应用总和的1 .5倍,在排行前20的应用中,游戏类应用超过14个。

 

iPhone窗口系统如下:

 

ü       UIKit.framwork

²       UIScreen

²       UIWindow

²       UIView

 

ü       QuartzCore.framework

²       CALayer

²       CAEAGLLayer

 

       本文主要讲解UIKit.framework图形框架,其中UIScreen提供了屏幕的基本系统,定义如下:

 

//

//  UIScreen.h

//  UIKit

//

//  Copyright 2007-2009 Apple Inc. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import <CoreGraphics/CoreGraphics.h>

#import <UIKit/UIKitDefines.h>

 

UIKIT_EXTERN_CLASS @interface UIScreen : NSObject {

  @private

    CGRect _bounds;

}

 

+ (UIScreen *)mainScreen;

 

@property(nonatomic,readonly) CGRect bounds;              // Bounds of entire screen in points

@property(nonatomic,readonly) CGRect applicationFrame;    // Frame of application screen area in points (i.e. entire screen minus status bar if visible)

 

@end

 

这个类提供了屏幕的bounds(即可绘制区域drawable),屏幕的尺寸和位置,而接口mainScreen()获取当前窗口的主屏幕。

 

UIWindow是UIView的子类,UIView是所有屏幕视图的SuperClass,比如UIButton、UILabel、UIImageView等,UIView类中定义如下:

 

@property(nonatomic,readonly) UIView       *superview;

@property(nonatomic,readonly,copy) NSArray *subviews;

@property(nonatomic,readonly) UIWindow     *window;

 

- (void)removeFromSuperview;

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;

- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

 

- (void)addSubview:(UIView *)view;

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

 

- (void)bringSubviewToFront:(UIView *)view;

- (void)sendSubviewToBack:(UIView *)view;

 

- (void)didAddSubview:(UIView *)subview;

- (void)willRemoveSubview:(UIView *)subview;

 

- (void)willMoveToSuperview:(UIView *)newSuperview;

- (void)didMoveToSuperview;

- (void)willMoveToWindow:(UIWindow *)newWindow;

- (void)didMoveToWindow;

 

可见,利用UIView类可以获取当前视图的“父视图”、“子视图数组”,还可以添加一个“子视图”到当前的视图、删除“子视图”、插入“子视图”等操作。

 

它们的继承关系如下图:

 

UIResponder – UIView – 自定义视图

                           |

UIButton、UILabel、UIImageView等

 

本文的主要任务是创建一个全屏显示的自定义窗口。

新建一个“Window-based Application”项目,输入项目名字“CrazyWindGameEngine”,然后Build&Go可以看到一个浅灰色背景的屏幕,这个默认的屏幕存在几个问题:

 

首先需要隐藏“Status Bar”,这个通过在CrazyWindGameEngine-info.plist中添加“Status bar is initially hidden”并勾选,Phone屏幕的构造如下图:

 

\ 

其次,这个模板程序是通过从CrazyWindGameEngine-info.plist文件的“Main nib file base name”属性中读取MainWindow.xib而生成窗口的,所以需要把模板相应的模块去掉并加入自己生成的窗口,这需要以下几个步骤:

Ø       删除CrazyWindGameEngine-info.plist中的Main nib file base name属性和相应的值。

Ø       到Resources目录下删除MainWindow.xib文件。

Ø       到main.m中修改代码如下:

Ø        int retVal = UIApplicationMain(argc, argv, nil, @"CrazyWindGameEngineAppDelegate");

Ø       CrazyWindGameEngineAppDelegate.h中变量window的IBOutlet关键字去掉(也可以保留,不影响)。

 

经过以上的步骤,模板生成的窗口就被删除掉了,接下来需要添家自己定义的窗口,代码如下:

 

#import "CrazyWindGameEngineAppDelegate.h"

@implementation CrazyWindGameEngineAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

// setup the main window

CGRect windowRect = [[UIScreen mainScreen] bounds];

window = [[UIWindow alloc] initWithFrame:windowRect];

[window setBackgroundColor:[UIColor redColor]];

[window makeKeyAndVisible];

}

 

上面的代码会产生一个“红色背景的窗口”,在前面讲过UIView类提供了“添加视图”、“插入视图”、“删除视图”等操作,而UIButton、UILabel、UIImageView等类继承自UIView,所以下面的代码中尝试添加一个UIButton、一个UILabel、一个UIImageView到新产生的窗口中,如下:

 

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

// setup the main window

CGRect windowRect = [[UIScreen mainScreen] bounds];

window = [[UIWindow alloc] initWithFrame:windowRect];

[window setBackgroundColor:[UIColor redColor]];

// Set up the background image

UIImageView *mBgView = [[[UIImageView alloc] initWithImage: [UIImage applicationImageNamed:@"screenshot.png"]] autorelease];

// 旋转UIImageView

float rotateAngle = M_PI/2;

CGAffineTransform transform = CGAffineTransformMakeRotation(rotateAngle);

mBgView.transform = transform;

[window setContentView: mBgView];

[mBgView release];

// add a UILabel

CGRect labelRect = [[UIScreen mainScreen] applicationFrame];

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(labelRect.origin.x+30, labelRect.origin.y+150, labelRect.size.width-60, labelRect.size.height-300)];

label.text = @"CrazyWindGameEngine";

label.backgroundColor = [UIColor blackColor];

label.shadowColor = [UIColor whiteColor];

label.textAlignment = UITextAlignmentCenter;

label.font = [UIFont systemFontOfSize:22.0];

label.textColor = [UIColor grayColor];

[window addSubview:label];

[label release];

// add a UIButton

UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button setFrame:CGRectMake(0.0f, 0.0f, 80.0f, 30.0f)];

[button setCenter:CGPointMake(160.0f,208.0f)];

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

[button setTitle:@"Start" forState:UICon

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,