iPhone遊戲引擎CWGameEngine之一(建立全屏自訂視窗)

來源:互聯網
上載者:User

作者:孫東風 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.