SDL游戏开发教程10(场景管理器)

本节将模仿ogre的ScreenManager编写一个SDL的ScreenManager。效果图如下


    这是利用场景管理器创建的一个扫雷游戏界面,为后面的扫雷游戏做准备。

 

    这里的场景管理器主要有四个类,SDLEntity(实体)、SDLSceneNode(节点)、SDLLayer(层)、SDLSceneManager(场景管理器)。

 

    他们的关系是:一个SDLSceneManager管理多个SDLLayer,一个SDLLayer有多个SDLSceneNode,一个SDLSceneNode上面关联一个SDLEntity。

 

    SDLSceneManager管理多个SDLLayer是为了将不同的层分离开来,并且通过索引号来决定哪个层显示在下面,哪个层显示在上面。这样做有利用分开管理,比如将游戏界面中不变的背景和游戏中经常变化的前景分开,设置背景的索引号小于前景索引号,这样就可以保证前景在上面显示,背景在后面显示,同时也可以通过整体平移背景层,来达到整个游戏场景的向前移动的效果。

Cpp代码   收藏代码
  1. #ifndef SDLSCENEMANAGER_H_  
  2. #define SDLSCENEMANAGER_H_  
  3.   
  4. #include <map>  
  5. #include "SDL/SDLSurface.h"  
  6. class SDLLayer;  
  7. class SDLSceneManager  
  8. {  
  9.     friend class SDL;  
  10. private:  
  11.     SDLSceneManager();  
  12. public:  
  13.     virtual ~SDLSceneManager();  
  14. public:  
  15.     //获取默认层,默认层为最底层  
  16.     SDLLayer* getDefaultLayer();  
  17.   
  18.     //获取索引号对应的层,索引号必须大于0,索引号大的显示在前面  
  19.     SDLLayer* getLayer(int index);  
  20.   
  21.     //移除索引号对应的层  
  22.     void removeLayer(int index);  
  23.   
  24.     //绘制整个场景  
  25.     void    draw(SDLSurfacePtr screen);  
  26. private:  
  27.     std::map<int, SDLLayer*> layers;  
  28. };  
  29.   
  30. #endif /* SDLSCENEMANAGER_H_ */  
 

    SDLLayer负责管理层中的SDLSceneNode,每个层都有一个根节点,根节点位于整个层的左上角,通过根节点可以创建子节点,然后子节点可以创建自己的子节点,以此类推,就可以得到一棵节点树。整棵节点树就构成了当前层要显示内容的布局。

Cpp代码   收藏代码
  1. #ifndef SDLLAYER_H_  
  2. #define SDLLAYER_H_  
  3. #include "SDLSceneNode.h"  
  4. class SDLSceneManager;  
  5. class SDLLayer  
  6. {  
  7.     friend class SDLSceneManager;  
  8. private:  
  9.     SDLLayer(SDLSceneManager * sceneManager, int id);  
  10. public:  
  11.     virtual ~SDLLayer();  
  12. public:  
  13.     //获得当前层的根节点  
  14.     SDLSceneNode*   getRootSceneNode();  
  15.   
  16.     //获取当前层的索引号  
  17.     int             getID();  
  18.   
  19. private:  
  20.     //绘制整个层  
  21.     void            draw(SDLSurfacePtr screen);  
  22. private:  
  23.     SDLSceneNode *      rootNode;  
  24.     int                 id;  
  25.     SDLSceneManager *   sceneManager;  
  26. };  
  27.   
  28. #endif /* SDLLAYER_H_ */  
 

    SDLSceneNode代表SDLLayer中的一个具体位置,他记录有相对与父节点的相对坐标,通过父节点的绝对坐标,可以获取当前节点的绝对坐标,根节点的绝对坐标为(0,0)。一个节点可以有多个子节点,不同的子节点通过名称来区分;一个节点可以关联一个实体(以后可能会扩充到关联多个),当关联了实体后,场景管理器就会在节点的当前位置绘制实体。

Cpp代码   收藏代码
  1. #ifndef SDLSCENENODE_H_  
  2. #define SDLSCENENODE_H_  
  3. #include "SDL/SDLCore.h"  
  4. #include "SDLEntity.h"  
  5. class SDLLayer;  
  6. class SDLSceneNode  
  7. {  
  8.     friend class SDLLayer;  
  9. private:  
  10.     SDLSceneNode();  
  11. public:  
  12.     SDLSceneNode(SDLSceneNode *parent, std::string name, int x = 0, int y = 0);  
  13. public:  
  14.     virtual ~SDLSceneNode();  
  15. public:  
  16.     //获取节点名称  
  17.     std::string getName();  
  18.   
  19.     //回去节点相对位置  
  20.     SDL_Point getPosition();  
  21.   
  22.     //获取节点绝对位置  
  23.     SDL_Point getAbsolutePosition();  
  24.   
  25.     //关联实体  
  26.     void attachEntity(SDLEntity *entity);  
  27.   
  28.     //取消与实体的关联  
  29.     void detachEntity();  
  30.   
  31.     //创建子节点  
  32.     SDLSceneNode* createChildSceneNode(std::string name, int x = 0, int y = 0);  
  33.   
  34.     //创建一个与自己关联的实体  
  35.     SDLEntity* createAttachedEntity(const std::string name, SDLSurfacePtr surface);  
  36.   
  37.     //获取已经关联的实体  
  38.     SDLEntity* getEntity();  
  39. private:  
  40.   
  41.     //绘制本节点和所有的子节点  
  42.     void    draw(SDLSurfacePtr screen);  
  43. private:  
  44.     SDLEntity *entity;  
  45.     std::map<std::string, SDLSceneNode*> children;  
  46.     std::string name;  
  47.     SDLLayer *layer;  
  48.     SDLSceneNode *parent;  
  49.     SDL_Point   position;  
  50. };  
  51.   
  52. #endif /* SDLSCENENODE_H_ */  

 

    SDLEntity负责管理要显示在屏幕上的图像,一个SDLEntity只能包含一个surface,只能关联到一个节点。这里实体名称的作用主要是为了标示一个实体,便于跟踪调试,以后可能会有其他用途(在ogre里面能在场景管理器中通过实体的名称来获取实体,我们这里不行,因为暂时没有看出这样做有什么大的用途)。

Cpp代码   收藏代码
  1. #ifndef SDLENTITY_H_  
  2. #define SDLENTITY_H_  
  3. #include "SDL/SDLSurface.h"  
  4. #include <string>  
  5. class   SDLSceneNode;  
  6. class SDLEntity  
  7. {  
  8. private:  
  9.     SDLEntity();  
  10. public:  
  11.     SDLEntity(std::string name, SDLSurfacePtr surface, SDLSceneNode *node = NULL);  
  12.     virtual ~SDLEntity();  
  13. public:  
  14.     //获取关联的节点  
  15.     SDLSceneNode * getSceneNode();  
  16.   
  17.     //获取名称  
  18.     std::string getName();  
  19.   
  20.     //获取包含的Surface  
  21.     SDLSurfacePtr getSurface();  
  22.   
  23.     //设置包含的Surface  
  24.     void setSurface(SDLSurfacePtr surface);  
  25.   
  26.     //取消关联节点  
  27.     void detachSceneNode();  
  28.   
  29.     //关联节点  
  30.     void attachSceneNode(SDLSceneNode * node);  
  31. private:  
  32.     std::string name;  
  33.     SDLSurfacePtr surface;  
  34.     SDLSceneNode *  node;  
  35. };  
  36.   
  37. #endif /* SDLENTITY_H_ */  

 

    这里的逻辑比较简单,以后随着应用的深入,我们再来强化他的功能。

    有了场景管理器后,我们不需要在onRender函数中写任何代码了。

    在SDLFrame::open的消息循环中,去掉调用onRender的代码,加入场景绘制的代码,同时在场景绘制前和绘制后各加一个函数,便于在显示之前和之后做一些事情。

Cpp代码   收藏代码
  1. beforeRender();  
  2.   
  3. SDL::sceneManager()->draw(screen);  
  4.   
  5. afterRender();  
 

 

    下面是应用的代码

Cpp代码   收藏代码
  1. void Lesson04::onInit()  
  2. {         
  3.   
  4.     SDLLayer * background = SDL::sceneManager()->getDefaultLayer();  
  5.     SDLSceneNode *root = SDL::sceneManager()->getLayer(1)->getRootSceneNode();  
  6.   
  7.     //加载图片  
  8.     SDLSurfacePtr   mine_unknown = SDL::imageManager()->loadImage(MINE_UNKNOWN);  
  9.     SDLSurfacePtr   frame_outer_h = SDL::imageManager()->loadImage(FRAME_OUTER);  
  10.     SDLSurfacePtr   frame_outer_v = SDL::transform()->Rotate90Degrees(frame_outer_h, 1);  
  11.     SDLSurfacePtr   frame_inner_h = SDL::imageManager()->loadImage(FRAME_INNER);  
  12.     SDLSurfacePtr   frame_inner_v = SDL::transform()->Rotate90Degrees(frame_inner_h, 1);  
  13.     SDLSurfacePtr   frame_background_src = SDL::imageManager()->loadImage(FRAME_BACKGROUND);  
  14.   
  15.     SDL_Rect        mine_unknown_rect = mine_unknown->value()->clip_rect;  
  16.     SDL_Rect        frame_outer_rect = frame_outer_h->value()->clip_rect;  
  17.     SDL_Rect        frame_inner_rect = frame_inner_h->value()->clip_rect;  
  18.     SDL_Rect        frame_background_rect = frame_background_src->value()->clip_rect;  
  19.   
  20.   
  21.     //计算扫雷区域的大小  
  22.     SDL_Rect mine_frame;  
  23.     mine_frame.w = MINE_COLS * mine_unknown_rect.w  
  24.                         + (MINE_COLS-1) * frame_inner_rect.h  
  25.                         + frame_outer_rect.h * 2;  
  26.     mine_frame.h = MINE_ROWS * mine_unknown_rect.h  
  27.                         + (MINE_ROWS-1) * frame_inner_rect.h  
  28.                         + frame_outer_rect.h * 2;  
  29.     SDLSurfacePtr frame_background = SDL::transform()->zoom(frame_background_src, mine_frame.w, mine_frame.h);  
  30.     frame_background_rect = frame_background->value()->clip_rect;  
  31.   
  32.     //----------------------------------------------------------------------------------------  
  33.     //创建背景图  
  34.     SDLSurfacePtr imageSrc = SDL::imageManager()->loadImage(BACKGROUND_ROOT);  
  35.     SDLSurfacePtr image = SDL::transform()->zoom(imageSrc, screen->value()->clip_rect.w, screen->value()->clip_rect.h) ;  
  36.     background->getRootSceneNode()->createChildSceneNode("image", 0, 0)->createAttachedEntity("image",image);  
  37.     //----------------------------------------------------------------------------------------  
  38.     //创建雷区背景  
  39.     int x = (screen->value()->clip_rect.w - frame_background_rect.w)/2;  
  40.     int y = (screen->value()->clip_rect.h - frame_background_rect.h) - 20;  
  41.     SDLSceneNode *frame_background_node = root->createChildSceneNode(FRAME_BACKGROUND, x, y);  
  42.     frame_background_node->createAttachedEntity(FRAME_BACKGROUND, frame_background);  
  43.   
  44.     //----------------------------------------------------------------------------------------  
  45.     //创建边框  
  46.     SDLSurfacePtr frame_outer_h_all = SDL::transform()->flat(frame_outer_h  
  47.             , frame_background_rect.w  
  48.             , frame_outer_rect.h);  
  49.     SDLSurfacePtr frame_outer_v_all = SDL::transform()->flat(frame_outer_v  
  50.                 , frame_outer_rect.h  
  51.                 , frame_background_rect.h);  
  52.     //上  
  53.     std::string FREAM_OUTER_TOP = FRAME_OUTER + "TOP";  
  54.     SDLSceneNode * frame_outer_top_node = frame_background_node->createChildSceneNode(FREAM_OUTER_TOP  
  55.                 , 0  
  56.                 , 0);  
  57.     frame_outer_top_node->createAttachedEntity(FREAM_OUTER_TOP, frame_outer_h_all);  
  58.     //下  
  59.     std::string FREAM_OUTER_BOTTOM = FRAME_OUTER + "BOTTOM";  
  60.     SDLSceneNode *  frame_outer_bottom_node = frame_background_node->createChildSceneNode(FREAM_OUTER_BOTTOM  
  61.                 , 0  
  62.                 , frame_background_rect.h - frame_outer_rect.h);  
  63.     frame_outer_bottom_node->createAttachedEntity(FREAM_OUTER_BOTTOM, frame_outer_h_all);  
  64.     //左  
  65.     std::string FREAM_OUTER_LEFT = FRAME_OUTER + "LEFT";  
  66.     SDLSceneNode *  frame_outer_left_node = frame_background_node->createChildSceneNode(FREAM_OUTER_LEFT  
  67.                 , 0  
  68.                 , 0);  
  69.     frame_outer_left_node->createAttachedEntity(FREAM_OUTER_LEFT, frame_outer_v_all);  
  70.     //右  
  71.     std::string FREAM_OUTER_RIGHT = FRAME_OUTER + "RIGHT";  
  72.     SDLSceneNode *frame_outer_right_node = frame_background_node->createChildSceneNode(FREAM_OUTER_RIGHT  
  73.             , frame_background_rect.w - frame_outer_rect.h  
  74.             , 0);  
  75.     frame_outer_right_node->createAttachedEntity(FREAM_OUTER_RIGHT, frame_outer_v_all);  
  76.   
  77.     //-----------------------------------------------------------------------------------------  
  78.     //创建初始雷格  
  79.     for(int row = 0; row < MINE_ROWS; row++)  
  80.     {  
  81.         for(int col = 0; col < MINE_COLS; col++)  
  82.         {  
  83.             std::string name = MINE_UNKNOWN + boost::lexical_cast<std::string>(row*MINE_COLS + col);  
  84.             SDLSceneNode *node = frame_background_node->createChildSceneNode(name  
  85.                     , frame_outer_rect.h + col * (mine_unknown_rect.w + frame_inner_rect.h)  
  86.                     , frame_outer_rect.h + row * (mine_unknown_rect.h + frame_inner_rect.h));  
  87.             node->createAttachedEntity(name, mine_unknown);  
  88.             this->mines[row][col] = node;  
  89.         }  
  90.     }  
  91.   
  92.     //-----------------------------------------------------------------------------------------  
  93.     //创建内部网格  
  94.     SDLSurfacePtr frame_iner_h_all = SDL::transform()->flat(frame_inner_h  
  95.                 , frame_background_rect.w - frame_outer_rect.h*2  
  96.                 , frame_inner_rect.h);  
  97.         SDLSurfacePtr frame_iner_v_all = SDL::transform()->flat(frame_inner_v  
  98.                     , frame_inner_rect.h  
  99.                     , frame_background_rect.h - frame_outer_rect.h*2);  
  100.     for(int col = 1; col < MINE_COLS; col++)  
  101.     {  
  102.         std::string name = FRAME_INNER + "v" + boost::lexical_cast<std::string>(col);  
  103.         SDLSceneNode *node = frame_background_node->createChildSceneNode(name  
  104.                 , frame_outer_rect.h + col * mine_unknown_rect.w + (col-1)*frame_inner_rect.h  
  105.                 , frame_outer_rect.h);  
  106.         node->createAttachedEntity(name, frame_iner_v_all);  
  107.     }  
  108.     for(int row = 1; row < MINE_ROWS; row++)  
  109.     {  
  110.         std::string name = FRAME_INNER + "r" + boost::lexical_cast<std::string>(row);  
  111.         SDLSceneNode *node = frame_background_node->createChildSceneNode(name  
  112.                 , frame_outer_rect.h  
  113.                 , frame_outer_rect.h + row * mine_unknown_rect.h + (row-1)*frame_inner_rect.h);  
  114.         node->createAttachedEntity(name, frame_iner_h_all);  
  115.     }  
  116.     //-----------------------------------------------------------------------------------------  
  117.     //创建上面的显示剩余雷数和所花时间数  
  118.   
  119.     //剩余雷数  
  120.     int xStart = 0, yStart = 0;  
  121.     SDLSurfacePtr mineAmountTitleSf = SDL::imageManager()->loadImageWithoutColor(MINE_AMOUNT, 255, 255, 255);  
  122.     xStart = frame_background_node->getPosition().x;  
  123.     yStart = (frame_background_node->getPosition().y - mineAmountTitleSf->value()->clip_rect.h)/2;  
  124.     SDLSceneNode *mineAmountTitelNode = root->createChildSceneNode(MINE_AMOUNT, xStart, yStart);  
  125.     mineAmountTitelNode->createAttachedEntity(MINE_AMOUNT, mineAmountTitleSf);  
  126.   
  127.     SDLFontPtr font = SDL::fontManager()->OpenFont(FONT, 24);  
  128.     SDLSurfacePtr mineAmountSf = font->RenderUNICODEBlended("99", SDL::assistant()->makeColor(255, 0, 0));  
  129.     xStart = mineAmountTitleSf->value()->clip_rect.w + frame_background_node->getPosition().x + 5;  
  130.     yStart = (frame_background_node->getPosition().y - mineAmountSf->value()->clip_rect.h)/2;  
  131.     this->mineAmountNode = root->createChildSceneNode(MINE_AMOUNT+boost::lexical_cast<std::string>(1), xStart, yStart);  
  132.     mineAmountNode->createAttachedEntity(MINE_AMOUNT+boost::lexical_cast<std::string>(1), mineAmountSf);  
  133.   
  134.     //重启图标  
  135.     SDLSurfacePtr restartSf = SDL::imageManager()->loadImageWithoutColor(RESTART, 255, 255, 255);  
  136.     xStart = (screen->value()->clip_rect.w - restartSf->value()->clip_rect.w)/2;  
  137.     yStart = (frame_background_node->getPosition().y - restartSf->value()->clip_rect.h)/2;  
  138.     SDLSceneNode *restartNode = root->createChildSceneNode(RESTART, xStart, yStart);  
  139.     restartNode->createAttachedEntity(RESTART, restartSf);  
  140.   
  141.     //计时器  
  142.     SDLSurfacePtr timeCounterSf = font->RenderUNICODEBlended("000", SDL::assistant()->makeColor(255, 0, 0));  
  143.     xStart = frame_background_node->getPosition().x + frame_background_rect.w - timeCounterSf->value()->clip_rect.w;  
  144.     yStart = (frame_background_node->getPosition().y - timeCounterSf->value()->clip_rect.h)/2;  
  145.     SDLSceneNode *timeCounterNode = root->createChildSceneNode(CLOCK+boost::lexical_cast<std::string>(1), xStart, yStart);  
  146.     timeCounterNode->createAttachedEntity(CLOCK+boost::lexical_cast<std::string>(1), timeCounterSf);  
  147.   
  148.     SDLSurfacePtr clockSf = SDL::imageManager()->loadImageWithoutColor(CLOCK, 255, 255, 255);  
  149.     xStart = frame_background_node->getPosition().x + frame_background_rect.w  
  150.                 - timeCounterSf->value()->clip_rect.w - clockSf->value()->clip_rect.w - 5;  
  151.     yStart = (frame_background_node->getPosition().y - clockSf->value()->clip_rect.h)/2;  
  152.     this->clockNode = root->createChildSceneNode(CLOCK, xStart, yStart);  
  153.     clockNode->createAttachedEntity(CLOCK, clockSf);  
  154.   
  155.     //插旗  
  156.     SDLSurfacePtr mine_flag = SDL::transform()->zoom(SDL::imageManager()->loadImage(MINE_FLAG), mine_unknown_rect.w, mine_unknown_rect.w);  
  157.     this->mines[4][5]->getEntity()->setSurface(mine_flag);  
  158.     this->mines[8][10]->getEntity()->setSurface(mine_flag);  
  159.     this->mines[10][3]->getEntity()->setSurface(mine_flag);  
  160.     //插问号  
  161.     SDLSurfacePtr mine_doubt_src = SDL::imageManager()->loadImageWithoutColor(MINE_DOUBT, 255, 255, 255);  
  162.     SDLSurfacePtr mine_doubt = SDL::video()->copySurface(mine_unknown);  
  163.     SDL::video()->BlitSurface(mine_doubt_src, NULL, mine_doubt, NULL);  
  164.     this->mines[8][5]->getEntity()->setSurface(mine_doubt);  
  165.     this->mines[4][10]->getEntity()->setSurface(mine_doubt);  
  166.     this->mines[5][3]->getEntity()->setSurface(mine_doubt);  
  167.     //插0,1,2,3,4,5,6,7,8  
  168.     SDLEntity *entity = this->mines[8][3]->getEntity();  
  169.     entity->detachSceneNode();  
  170.     delete entity;  
  171.     SDLSurfacePtr mine_number_1 = SDL::imageManager()->loadImageWithoutColor(MINE_NUMBER_1, 255, 255, 255);  
  172.     this->mines[8][6]->getEntity()->setSurface(mine_number_1);  
  173.     SDLSurfacePtr mine_number_2 = SDL::imageManager()->loadImageWithoutColor(MINE_NUMBER_2, 255, 255, 255);  
  174.     this->mines[9][10]->getEntity()->setSurface(mine_number_2);  
  175.     SDLSurfacePtr mine_number_3 = SDL::imageManager()->loadImageWithoutColor(MINE_NUMBER_3, 255, 255, 255);  
  176.     this->mines[2][3]->getEntity()->setSurface(mine_number_3);  
  177.     SDLSurfacePtr mine_number_4 = SDL::imageManager()->loadImageWithoutColor(MINE_NUMBER_4, 255, 255, 255);  
  178.     this->mines[5][6]->getEntity()->setSurface(mine_number_4);  
  179.     SDLSurfacePtr mine_number_5 = SDL::imageManager()->loadImageWithoutColor(MINE_NUMBER_5, 255, 255, 255);  
  180.     this->mines[12][10]->getEntity()->setSurface(mine_number_5);  
  181.     SDLSurfacePtr mine_number_6 = SDL::imageManager()->loadImageWithoutColor(MINE_NUMBER_6, 255, 255, 255);  
  182.     this->mines[15][3]->getEntity()->setSurface(mine_number_6);  
  183.     SDLSurfacePtr mine_number_7 = SDL::imageManager()->loadImageWithoutColor(MINE_NUMBER_7, 255, 255, 255);  
  184.     this->mines[13][12]->getEntity()->setSurface(mine_number_7);  
  185.     SDLSurfacePtr mine_number_8 = SDL::imageManager()->loadImageWithoutColor(MINE_NUMBER_8, 255, 255, 255);  
  186.     this->mines[11][15]->getEntity()->setSurface(mine_number_8);  
  187. }  
 

 

    附件中是工程的完整代码和图片,大家可以通过跟踪代码来了解这节的内容。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include //用键盘控制精灵移动 int main(int argc, char ** argv) { SDL_Surface * screen; //主表面 SDL_Surface * image; //用来放MM-----的图片信息(像素) SDL_Surface * PlayerImage; //用来测试的图片 SDL_Event event; Uint32 BeginTicks, EndTicks; SDL_Rect PRect, BRect; //PRect对应精灵的移动的小图位置(实现动画),BRect对应精灵在屏幕的位置。 unsigned char PlayerStarts = 0; unsigned char PlayerIndex = 0; bool bAppRun = true; //初始化SDL if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { fprintf(stderr, "SDL_Init %s\n", SDL_GetError()); return -1; } //初始化成功设置退出要调用的函数SDL_Quit atexit(SDL_Quit); //创建一个640X480 16-bit 模式的主表面 16位可以让MM的效果好看一点 screen = SDL_SetVideoMode(230, 230, 16, SDL_SWSURFACE); if (screen == NULL) { fprintf(stderr, "Couldn't set 640x480x8 video mode %s\n", SDL_GetError()); return -1; } //读取MM图片信息,并创建一个表面,并把数据填入该表面中。 image = SDL_LoadBMP("./mm.bmp"); //请在终端里运行该程序 if (image == NULL) { fprintf(stderr, "Couldn't load MM, %s\n", SDL_GetError()); //遗憾你不能显示MM了,不过你可以用图片浏览程序看。 return -1; } //读取player.bmp PlayerImage = SDL_LoadBMP("./player.bmp"); //请在终端里运行该程序 if (image == NULL) { fprintf(stderr, "Couldn't load MM, %s\n", SDL_GetError()); //遗憾你不能显示MM了,不过你可以用图片浏览程序看。 return -1; } //读取第一个像素 Uint8 key = *((Uint8 *)PlayerImage->pixels); //设置色键 SDL_SetColorKey(PlayerImage, SDL_SRCCOLORKEY, key); //有了MM的表面了,就可以显示了。 //将MM的表面画在我们的主表面上,用MM来作为背景 if (SDL_BlitSurface(image, NULL, screen, NULL) < 0) { //解释一下NULL,第一个是按照image的尺寸显示,第二个是默认显示。你也可以指定大小,不过要用到SDL_Rect你可以看一看帮助。 fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError()); //看看提示吧 return -1; } PRect.x = 0; //初始化动画显示的图片。 PRect.y = 0; PRect.w = 32; PRect.h = 48; BRect.x = 0; //初始化精灵的位置。 BRect.y = 0; BRect.w = 32; BRect.h = 48; //贴上测试用的表面 if (SDL_BlitSurface(PlayerImage, &PRect, screen, &BRect) w, image->h); BeginT

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值