Author: Sun Dongfeng)
In the previous series of articles, I explained iPhone multithreading, iPhone data persistence, and iPhone network communication BSD Socket. Next I will explain how to build a game engine from scratch.
According to official iPhone statistics, the App Store has the most game applications, which is about times the total of other applications. Among the top 20 applications, there are more than 14 game applications.
The iPhone window system is as follows:
Ü UIKit. framwork
2. UIScreen
2. UIWindow
2. UIView
Ü QuartzCore. framework
2. CALayer
2. CAEAGLLayer
This document describes the graphic framework of UIKit. framework. UIScreen provides the basic Screen System, which is defined as follows:
//
// 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
This class provides the bounds of the screen (the drawable area can be drawn), the size and position of the screen, and the interface mainScreen () gets the main screen of the current window.
UIWindow is a subclass of UIView, and UIView is a SuperClass of all screen views, such as UIButton, UILabel, and UIImageView. The UIView class is defined as follows:
...
@ 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;
...
You can use the UIView class to obtain the "parent View" and "subview array" of the current view ", you can also add a subview to the current view, delete the subview, and insert the subview.
Their Inheritance relationships include:
UIResponder-UIView-custom View
|
UIButton, UILabel, UIImageView, etc.
The main task of this article is to create a custom window in full screen display.
Create a "Window-based Application" project, enter the project name "CrazyWindGameEngine", and then Build & Go will display a light gray background screen, which has several problems with the default screen:
First you need to hide the "Status Bar", which by adding "Status bar is initially hidden" in the CrazyWindGameEngine-info.plist and selecting, the Phone screen structure is as follows:
Secondly, this template program reads MainWindow by reading from the "Main nib file base name" attribute of the CrazyWindGameEngine-info.plist file. xib generates a window, so you need to remove the corresponding module of the template and add it to the self-generated window. The following steps are required:
Delete the Main nib file base name attribute and corresponding value in the CrazyWindGameEngine-info.plist.
Delete the MainWindow. xib file in the Resources directory.
Modify the code in main. m as follows:
Int retVal = UIApplicationMain (argc, argv, nil, @ "CrazyWindGameEngineAppDelegate ");
Remove the IBOutlet keyword of the Variable window in CrazyWindGameEngineAppDelegate. h (it can also be retained without affecting it ).
After the preceding steps, the window generated by the template is deleted. Next, you need to add a custom window. The Code is as follows:
# 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];
}
The above code will generate a "red background window". As mentioned earlier, the UIView class provides operations such as "add View", "insert View", and "delete View, UIButton, UILabel, UIImageView, and other classes inherit from UIView. Therefore, the following code tries to add a UIButton, A UILabel, and a UIImageView to the new window, as shown below:
-(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];
// Rotate the 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, 80366f, 30366f)];
[Button setCenter: CGPointMake (160.0f, 208.0f)];
[Button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[Button setTitle: @ "Start" forState: UICon