技术开发 频道

游戏是这样写成的(二)

  【IT168 技术文档】在第一篇我们弄好了一个 OpenGL ES 框架, 接下来我们可以再进一步为写游戏作准备了, 这时, 我们有一个问题要好好考虑一下: 到底我们想以 Obj-C 开发还是以 C/C++ 开发呢?

  个人来说, 我还是比较偏向 C/C++, 一来比较熟, 二来要是借用别人游戏方面的代码, 也比较容易找到! 所以在本篇, 我会和大家介绍一下怎么混合 C/C++ 和 Obj-C, 并编写一个 C++ 的 sprite class, 方便以后在游戏里应用!

  我们首先要做的第一件事, 是打开第一篇的示范工程,并把档案的点缀名由 .m 改为 .mm, 这样, 我们就可以在代码里, 随意引用 C++ 的 class了.

  好了, 我们现在可以开始弄我们的 Sprite class 了, 让我们把它叫做 CCSprite 吧 (CC 就是代表cocoachina),class 的结构如下:

  1. class CCSprite
  2. {
  3. public:
  4. ? ? CCSprite(GLuint texId, float width, float height, float texWidth, float texHeight);
  5. ? ? ~CCSprite();
  6. ? ?
  7. ? ? void render(float x, float y);
  8. ? ?
  9. private:
  10. ? ? GLuint mTextureId;
  11. ? ? float mImageWidth;
  12. ? ? float mImageHeight;
  13. ? ? float mTextureWidth;
  14. ? ? float mTextureHeight;
  15. ? ?
  16. };

 

  加好了 CCSprite.h 和 CCSprite.cpp, 基本上, 我们可以抄袭 Texture2D, 把它的功能真接搬到 CCSprite! 至于 Texture2D 的加载贴图功能, 我可看不懂它里面那一大堆的代码, 也不知怎么搬到 C++, 我们直接用它好了! OpenGL ES 来说,我们只要拿到 texture id 就可以画图. 于是把试试把 Texture2D.h 放进  CCSprite.cpp:

#include "Texture2D.h"

  编译一下,天! 1995 个错!有没有搞错!看来在 C++里引用 Obj-C的东西是有点问题,还好,反过来在 Obj-C 里引用 C++ 的东西,就没有问题! 所以我们先要弄一些封装的代码,让我们可以在 C++ 里间接的用 Obj-C 的东西. 于是我们有了 Wrapper.h 和 Wrapper.mm, 在 Wrapper.mm 里引用和生成 CCSprite 就可以了!

  1. CCSprite *CCSpriteCreate(const char *filename)
  2. {
  3.    
  4.     NSString *name = [[NSString alloc] initWithUTF8String: filename];
  5.     Texture2D *tex = [[Texture2D alloc] initWithImagePath:name];
  6.    
  7.     CCSprite *sprite = new CCSprite([tex name], tex.contentSize.width, tex.contentSize.height, tex.pixelsWide, tex.pixelsHigh);
  8.        
  9.     [tex release];
  10.  
  11.     return sprite;
  12. }

  这里有一点注意的是,Texture2D 被释放时会同时 texture 释放掉,我们要把有关代码拿走:

  1. - (void) dealloc
  2. {
  3. ? ? //if(_name)
  4. ? ? //glDeleteTextures(1, &_name);
  5. ? ?
  6. ? ? [super dealloc];
  7. }

 

  接下来,我们就可以把画图部份, 抄到我们自己的class 里, 有了这些, 我们就弄好我们的CCSprite class 了:

  1. void CCSprite::render(float x, float y)
  2. {
  3. ? ? GLfloat _maxS = mImageWidth/mTextureWidth;
  4. ? ? GLfloat _maxT = mImageHeight/mTextureHeight;
  5. ? ?
  6. ? ? GLfloat? ? coordinates[] =
  7. ? ? {
  8. ? ? ? ? 0,? ? ? ? ? ? ? ? _maxT,
  9. ? ? ? ? _maxS,? ? ?  _maxT,
  10. ? ? ? ? 0,? ? ? ? ? ? ? ? 0,
  11. ? ? ? ? _maxS,? ? ?  0
  12. ? ? };
  13. ? ?
  14. ? ? GLfloat? ? width = mImageWidth;
  15. ? ? GLfloat height = mImageHeight;
  16. ? ?
  17. ? ? GLfloat? ? vertices[] =
  18. ? ? {
  19. ? ? ? ? -width / 2 + x,? ? ? ? -height / 2 + y,? ? ? ? 0,
  20. ? ? ? ? width / 2 + x,? ? ? ? -height / 2 + y,? ? ? ? 0,
  21. ? ? ? ? -width / 2 + x,? ? ? ? height / 2 + y,? ? ? ? 0,
  22. ? ? ? ? width / 2 + x,? ? ? ? height / 2 + y,? ? ? ? 0
  23. ? ? };
  24. ? ?
  25. ? ? glBindTexture(GL_TEXTURE_2D, mTextureId);
  26. ? ? glVertexPointer(3, GL_FLOAT, 0, vertices);
  27. ? ? glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
  28. ? ? glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  29. }

  我们再改一下 EGALView 里截入贴图和画图的部份,哈,大功告成!

  本文实例代码iDemo_2下载:http://www.rayfile.com/files/ede66642-3bee-11de-8a92-0019d11a795f/

0
相关文章