Search This Blog

Monday, December 28, 2009

C++ 预定义内容

编译 C++程序时 编译器自动定义了一个预处理器名字__cplusplus 注意前面有两个下 划线 因此 我们可以根据它来判断该程序是否是 C++程序 以便有条件地包含一些代码 例如
#ifdef __cplusplus
我们要编译 C++ // extern "C" 到第 7 章再解释
extern "C" #endif
int min( int, int );
在编译标准 C 时 编译器将自动定义名字__STDC__
当然 __cplusplus 与__STDC__ 不会同时被定义
示为 08:17:05 Oct 31 1996
// 不错
另外两个比较有用的预定义名字是 __LINE__和__FILE__ 编译的行数 __FILE__包含正在被编译的文件的名字 可以这样使用它们
if ( element_count == 0 ) cerr << "Error: " << __FILE__
<< " : line " << __LINE__ << "element_count must be non-zero.\n";
另外两个预定义名字分别包含当前被编译文件的编译时间 __TIME__ __DATE__ 时间格式为 hh:mm:ss
因此如果在上午 8 点 17 分编译一个文件 则时间表
如果这一天是 1996 年 10 月 31 日 星期四 则日期表示为
若当前处理的行或文件发生变化 则__LINE__和__FILE__的值将分别被改变 其他四个 预定义名字在编译期间保持不变 它们的值也不能被修改。

Tuesday, December 15, 2009



1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad
2.keyboardAppearance = UIKeyboardAppearanceAlert

- (void)textViewDidBeginEditing:(UITextView *)textView{
NSArray *ws = [[UIApplication sharedApplication] windows];
for(UIView *w in ws){
NSArray *vs = [w subviews];
for(UIView *v in vs){
if([[NSString stringWithUTF8String:object_getClassName(v)]
isEqualToString:@"UIKeyboard"]){
v.backgroundColor = [UIColor redColor];
}
}
}
}

Thursday, December 10, 2009

Display PDF file


webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
webView.backgroundColor = [UIColor whiteColor];
webView.scalesPageToFit = YES;
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
webView.delegate = self;



NSBundle * mainBundle = [NSBundle mainBundle];
NSString *IPDBfile=[mainBundle pathForResource:@"Angio-Lung cancer" ofType:@"pdf"];
NSData *pdfData =[NSData dataWithContentsOfFile:IPDBfile];
[webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

Wednesday, December 9, 2009

Common Menu Screens

Legal screen.
This can be a short sentence or a screen full of text. It will include legal issues like copyright notices.

Publisher logo screen.
It can be a requirement to show this screen before the developer logo. There may also be a requirement for how long it needs to be displayed before the user can move on.

Developer logo screen.
This is where your company logo is seen.

Console logo screen.
Some consoles require or encourage developers to display the system logo.

Title screen.
The name of the game appears here. It can also include interactive options.

Options.
An option screen allows the user to change many game settings.

Credits.
This is where everyone who worked on the game is listed.

Environment or level select.
This is used in games where the user can choose a level of loca- tion to play.

Player editor.
A player editor will allow the user to change the look and attributes of char- acters in the game.

Information.
This screen can have extra information, such as story, maps, and so on.

Save / Load game.
This screen lists how many games are saved and allows the user to load and save games.


Before you move on to creating art is a good time to make changes to the flow.

■ How many options appear on each screen?
■ Are they logically grouped?
■ How fast can a new player get into a game?
■ How fast can an experienced player get into the game?
■ Which options will the player want to adjust often?
■ Which options will be changed rarely?
■ How does this menu compare with similar games?
■ How many dead-ends are there? (When the user must back up to start the game.)

Sunday, December 6, 2009

音频播放 (转)

iphone OS主要提供以下4种播放音频的方法。
Sestem Sound Services、AVAudioPlayer类、Audio Queue Services、OpenAL
1.Sestem Sound Services
Sestem Sound Services是最底层的也是最简单的声音播放服务,调用AudioServicesPlaySystemSound这个方法就可以播放一些简单的音频文件,使用此方法只适合播放一些很小的提示或警告音,因为他有很多限制:
{
声音长度要小于30秒
In linear PCM或者IMA4(IMA/ADPCM)格式的
打包成.caf,.aif,或者.wav的文件
不能控制播放的进度
调用方法后立即播放声音
没有循环播放和立体声音控制
}
另外,他还可以调用系统的震动功能。还可以通过AudioServices
AddSystemSoundCompletion方法为音频播放添加CallBack函数,有了CallBack函数我们可以解决不少问题,比如可以克服System Sound Services本身不支持循环播放的问题。
一下代码可以实现在一个程序中循环播放背景音乐:

1. static void completionCallback(SystemSoundID mySSID)
2. {
3. AudioServicesPlaySystemSound(mySSID);
4. }
5. -(void)playSound
6. {
7. CFBundleRef mainBundle;
8. SystemSoundID soundFileObject;
9. mainBundle=CFBundleGetMainBundle();
10.

11. CFURLRef soundFileURLRef=CFBundleCopyResourceURL(
12. mainBundle,CFSTR("background"),CFSTR("wav"),NULL);
13. AudioServicesCreateSystemSoundID(
14. soundFileURLRef,&soundFileObject);
15. AudioServicesAddSystemSoundCompletion(soundFileObject,NULL,NULL,completionCallback,(void*)self);
16. AudioServicesPlaySystemSound(soundFileObject);
17. }

复制代码
2.AVAudioPlayer类
AVAudioPlayer是AVFoundation.framework中定义的一个类,所以使用前要在工程中引入AVFoundation.framework。可以把AVAudioPlayer看做是一个高级播放器,它支持广泛的音频格式,主要有:
{
AAC、AMR(AdaptiveMulti-Rate,aformatforspeech)、ALAC(AppleLossless)、iLBC(internetLowBitrateCodec,anotherformatforspeech)、IMA4(IMA/ADPCM)、linearPCM、u-lawanda-law、mp3
}AVAudioPlayer可以播放任意长度的音频文件、支持循环播放、可以同步播放很多个音频文件、控制播放进度以及从音频文件的任意一点开始播放等,要使用AVAudioPlayer的对象播放文件,只需要为其指定一个音频文件设定一个实现了AVAudioPlayerDelegate协议的 delegate对象。下边的例子和上边的一样实现一直循环播放背景音乐:

1. -(void)playBackgroundSoundEffect
2. {
3. NSString *soundFilePath=[[NSBundle mainBundle] pathForResource:@"background",ofType@"wav"];
4. NSURL *fileURL=[[NSURL alloc]initFileURLWithPath:soundFilePath];
5. AVAudioPlayer *newPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
6. [fileURL release];
7. self.player=newPlayer;
8. [newPlayer release];
9. [self.player prepareToplay];
10. [self.player setDelegate:self];
11. self.player.numberOfLoops=-1;
12. [self.player play];
13. }

复制代码
可以看到,只要将AVAudioPlayer的numberOfLoops属性设为负数,音频文件就会一直循环播放直到调用stop方法。
AVAudioPlayer同样支持Callback,这是AVAudioPlayerDelegate的一个可选delegate方法。

1. -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
2. {
3. if(player==self.player&&flag==YES){NSLog(@"Playback finish");}
4. }

复制代码
另外,可以随时控制AVAudioPlayer对象的播放、停止以及暂停,通过判断对象的状态,分别调用play、pause和stop方法即可:

1. -(IBAction)playOrPause:(id)sender{
2. if(self.playing){[self.player pause];}
3. else{[self.player play];}

复制代码
虽然AVAudioPlayer可以播放很多格式,但是我们在实际开发过程中还是最好使用一些没有压缩的格式,比如WAVE文件,这样可以减少系统处理单元的资源占用,以更好的完成程序的其他功能。另外,在使用AVAudioPlayer连续播放mp3这类经过压缩的音频文件时,在连接处可能出现一定的间隔时间。
3.Audio Queue Services
如果以上两种音频播放的解决方案都无法满足你的需求,可以使用Audio Queue Services,使用Audio Queue Services可以实现对声音的控制。例如,可以在声音数据从文件读到缓冲区后对声音进行一定处理再进行播放,从而实现对音频的快慢播放的功能。
4.OpenAL
OpenAL是一套跨平台开源的音频处理接口,与图形处理的OpenGL类似,它为音频播放提供了一套更加优化的方案,它适合开发游戏的音效,用法也与其他平台下相同。